You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/nstr/runes.go

79 lines
1.8 KiB
Go

package nstr
import "unicode"
// RuneIsLower checks if a character is lower case ('a' to 'z')
func RuneIsLower(c rune) bool {
return 'a' <= c && c <= 'z'
}
// RuneToLower converts a character 'A' to 'Z' to its lower case
func RuneToLower(r rune) rune {
if r >= 'A' && r <= 'Z' {
return r + 32
}
return r
}
// RuneToLowerAll converts a character 'A' to 'Z' to its lower case
func RuneToLowerAll(rs []rune) []rune {
for i := range rs {
rs[i] = RuneToLower(rs[i])
}
return rs
}
// RuneIsUpper checks if a character is upper case ('A' to 'Z')
func RuneIsUpper(c rune) bool {
return 'A' <= c && c <= 'Z'
}
// RuneToUpper converts a character 'a' to 'z' to its upper case
func RuneToUpper(r rune) rune {
if r >= 'a' && r <= 'z' {
return r - 32
}
return r
}
// RuneToUpperAll converts a character 'a' to 'z' to its upper case
func RuneToUpperAll(rs []rune) []rune {
for i := range rs {
rs[i] = RuneToUpper(rs[i])
}
return rs
}
// RuneIsDigit checks if a character is digit ('0' to '9')
func RuneIsDigit(r rune) bool {
return r >= '0' && r <= '9'
}
// RuneIsLetter checks r is a letter but not CJK character.
func RuneIsLetter(r rune) bool {
if !unicode.IsLetter(r) {
return false
}
switch {
// cjk char: /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/
// hiragana and katakana (Japanese only)
case r >= '\u3034' && r < '\u30ff':
return false
// CJK unified ideographs extension A (Chinese, Japanese, and Korean)
case r >= '\u3400' && r < '\u4dbf':
return false
// CJK unified ideographs (Chinese, Japanese, and Korean)
case r >= '\u4e00' && r < '\u9fff':
return false
// CJK compatibility ideographs (Chinese, Japanese, and Korean)
case r >= '\uf900' && r < '\ufaff':
return false
// half-width katakana (Japanese only)
case r >= '\uff66' && r < '\uff9f':
return false
}
return true
}