diff options
author | bt90 <btom1990@googlemail.com> | 2024-01-23 15:50:47 +0000 |
---|---|---|
committer | bt90 <btom1990@googlemail.com> | 2024-01-23 15:50:47 +0000 |
commit | d358a61eee887dffad517ff5a7debb371e2108d5 (patch) | |
tree | b360e42a9296cf59fe659d2832b9d6cd85b70b88 | |
parent | 18ca1f6ebb7f0c97031d85c0f35d5c66a56c0d96 (diff) |
Use strings.Map
-rw-r--r-- | lib/fs/folding.go | 50 |
1 files changed, 10 insertions, 40 deletions
diff --git a/lib/fs/folding.go b/lib/fs/folding.go index de77cc09c..05fe9051a 100644 --- a/lib/fs/folding.go +++ b/lib/fs/folding.go @@ -9,7 +9,6 @@ package fs import ( "strings" "unicode" - "unicode/utf8" "golang.org/x/text/unicode/norm" ) @@ -17,51 +16,22 @@ import ( // UnicodeLowercaseNormalized returns the Unicode lower case variant of s, // having also normalized it to normalization form C. func UnicodeLowercaseNormalized(s string) string { - i, isASCII := firstCaseChange(s) - if isASCII { - if i == -1 { - return s - } + if isASCII(s) { return strings.ToLower(s) } - if i == -1 { - return norm.NFC.String(s) - } - var rs strings.Builder - // WriteRune always reserves utf8.UTFMax bytes for non-ASCII runes, - // even if it doesn't need all that space. Overallocate now to prevent - // it from ever triggering a reallocation. - rs.Grow(utf8.UTFMax - 1 + len(s)) - rs.WriteString(s[:i]) + return norm.NFC.String(strings.Map(toLower, s)) +} - for _, r := range s[i:] { - r = unicode.ToLower(unicode.ToUpper(r)) - if r < utf8.RuneSelf { - rs.WriteByte(byte(r)) - } else { - rs.WriteRune(r) - } - } - return norm.NFC.String(rs.String()) +func toLower(r rune) rune { + return unicode.ToLower(unicode.ToUpper(r)) } -// Byte index of the first rune r s.t. lower(upper(r)) != r. -// Boolean indicating if the whole string consists of ASCII characters. -func firstCaseChange(s string) (int, bool) { - index := -1 - isASCII := true - for i, r := range s { - if r <= unicode.MaxASCII { - if index == -1 && 'A' <= r && r <= 'Z' { - index = i - } - } else { - if index == -1 && unicode.ToLower(unicode.ToUpper(r)) != r { - index = i - } - isASCII = false +func isASCII(s string) bool { + for i := 0; i < len(s); i++ { + if s[i] > unicode.MaxASCII { + return false } } - return index, isASCII + return true } |