2020-05-20 15:10:24 +02:00
|
|
|
package txt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2020-05-22 16:29:12 +02:00
|
|
|
"strings"
|
2020-05-20 15:10:24 +02:00
|
|
|
)
|
|
|
|
|
2021-02-07 17:42:52 +01:00
|
|
|
var UnknownCountryCode = "zz"
|
2020-06-13 07:40:35 +02:00
|
|
|
var CountryWordsRegexp = regexp.MustCompile("[\\p{L}]{2,}")
|
2020-07-11 16:46:29 +02:00
|
|
|
|
2020-05-20 15:10:24 +02:00
|
|
|
// Int returns a string as int or 0 if it can not be converted.
|
|
|
|
func Int(s string) int {
|
|
|
|
if s == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := strconv.ParseInt(s, 10, 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(result)
|
|
|
|
}
|
2020-05-22 16:29:12 +02:00
|
|
|
|
|
|
|
// IsUInt returns true if a string only contains an unsigned integer.
|
|
|
|
func IsUInt(s string) bool {
|
|
|
|
if s == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range s {
|
|
|
|
if r < 48 || r > 57 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2020-05-28 15:12:18 +02:00
|
|
|
|
|
|
|
// CountryCode tries to find a matching country code for a given string e.g. from a file oder directory name.
|
2021-02-07 16:55:15 +01:00
|
|
|
func CountryCode(s string) (code string) {
|
2021-02-07 17:42:52 +01:00
|
|
|
code = UnknownCountryCode
|
2021-02-07 16:55:15 +01:00
|
|
|
|
2021-02-07 17:42:52 +01:00
|
|
|
if s == "" || s == UnknownCountryCode {
|
2021-02-07 16:55:15 +01:00
|
|
|
return code
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 07:40:35 +02:00
|
|
|
words := CountryWordsRegexp.FindAllString(s, -1)
|
2020-05-28 15:12:18 +02:00
|
|
|
|
2020-06-13 07:40:35 +02:00
|
|
|
for i, w := range words {
|
|
|
|
if i < len(words)-1 {
|
|
|
|
search := strings.ToLower(w + " " + words[i+1])
|
|
|
|
|
2021-02-07 16:55:15 +01:00
|
|
|
if match, ok := Countries[search]; ok {
|
|
|
|
return match
|
2020-06-13 07:40:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
search := strings.ToLower(w)
|
2020-06-12 18:01:22 +02:00
|
|
|
|
2021-02-07 16:55:15 +01:00
|
|
|
if match, ok := Countries[search]; ok {
|
|
|
|
code = match
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-07 16:55:15 +01:00
|
|
|
return code
|
2020-05-28 15:12:18 +02:00
|
|
|
}
|