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
|
|
|
)
|
|
|
|
|
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.
|
|
|
|
func CountryCode(s string) string {
|
|
|
|
if s == "zz" {
|
|
|
|
return "zz"
|
|
|
|
}
|
|
|
|
|
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])
|
|
|
|
|
|
|
|
if code, ok := Countries[search]; ok {
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
search := strings.ToLower(w)
|
2020-06-12 18:01:22 +02:00
|
|
|
|
|
|
|
if code, ok := Countries[search]; ok {
|
2020-05-28 15:12:18 +02:00
|
|
|
return code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "zz"
|
|
|
|
}
|