2017-11-17 12:10:16 +00:00
|
|
|
// Copyright (C) 2017 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2020-09-11 07:16:10 +00:00
|
|
|
"strings"
|
2017-11-17 12:10:16 +00:00
|
|
|
"unicode"
|
2020-09-11 07:16:10 +00:00
|
|
|
"unicode/utf8"
|
2017-11-17 12:10:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func UnicodeLowercase(s string) string {
|
2020-09-11 07:16:10 +00:00
|
|
|
i := firstCaseChange(s)
|
|
|
|
if i == -1 {
|
|
|
|
return s
|
2017-11-17 12:10:16 +00:00
|
|
|
}
|
2020-09-11 07:16:10 +00:00
|
|
|
|
|
|
|
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])
|
|
|
|
|
|
|
|
for _, r := range s[i:] {
|
|
|
|
rs.WriteRune(unicode.ToLower(unicode.ToUpper(r)))
|
|
|
|
}
|
|
|
|
return rs.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Byte index of the first rune r s.t. lower(upper(r)) != r.
|
|
|
|
func firstCaseChange(s string) int {
|
|
|
|
for i, r := range s {
|
|
|
|
if r <= unicode.MaxASCII && (r < 'A' || r > 'Z') {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if unicode.ToLower(unicode.ToUpper(r)) != r {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
2017-11-17 12:10:16 +00:00
|
|
|
}
|