2020-01-07 17:36:49 +01:00
|
|
|
package txt
|
2019-05-16 08:41:16 +02:00
|
|
|
|
|
|
|
import (
|
2019-12-08 15:05:35 +01:00
|
|
|
"regexp"
|
2019-06-13 20:26:01 +02:00
|
|
|
"strings"
|
2019-05-16 08:41:16 +02:00
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
2019-12-13 03:07:26 +01:00
|
|
|
var ContainsNumberRegexp = regexp.MustCompile("\\d+")
|
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
// ContainsNumber returns true if string contains a number.
|
2019-12-13 03:07:26 +01:00
|
|
|
func ContainsNumber(s string) bool {
|
|
|
|
return ContainsNumberRegexp.MatchString(s)
|
|
|
|
}
|
|
|
|
|
2019-06-13 20:26:01 +02:00
|
|
|
// isSeparator reports whether the rune could mark a word boundary.
|
|
|
|
func isSeparator(r rune) bool {
|
|
|
|
// ASCII alphanumerics and underscore are not separators
|
|
|
|
if r <= 0x7F {
|
|
|
|
switch {
|
|
|
|
case '0' <= r && r <= '9':
|
|
|
|
return false
|
|
|
|
case 'a' <= r && r <= 'z':
|
|
|
|
return false
|
|
|
|
case 'A' <= r && r <= 'Z':
|
|
|
|
return false
|
|
|
|
case r == '_', r == '\'':
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Letters and digits are not separators
|
|
|
|
if unicode.IsLetter(r) || unicode.IsDigit(r) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Otherwise, all we can do for now is treat spaces as separators.
|
|
|
|
return unicode.IsSpace(r)
|
|
|
|
}
|
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
// UcFirst returns the string with the first character converted to uppercase.
|
2019-05-16 08:41:16 +02:00
|
|
|
func UcFirst(str string) string {
|
|
|
|
for i, v := range str {
|
|
|
|
return string(unicode.ToUpper(v)) + str[i+1:]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2019-06-13 20:26:01 +02:00
|
|
|
|
2020-01-29 15:28:20 +01:00
|
|
|
// Title returns the string with the first characters of each word converted to uppercase.
|
2019-06-13 20:26:01 +02:00
|
|
|
func Title(s string) string {
|
|
|
|
prev := ' '
|
|
|
|
return strings.Map(
|
|
|
|
func(r rune) rune {
|
|
|
|
if isSeparator(prev) {
|
|
|
|
prev = r
|
|
|
|
return unicode.ToTitle(r)
|
|
|
|
}
|
|
|
|
prev = r
|
|
|
|
return r
|
|
|
|
},
|
|
|
|
s)
|
|
|
|
}
|
2020-01-31 15:29:06 +01:00
|
|
|
|
|
|
|
// Bool casts a string to bool.
|
|
|
|
func Bool(s string) bool {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
|
|
|
if s == "" || s == "0" || s == "false" || s == "no" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|