folding.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (C) 2017 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package fs
  7. import (
  8. "strings"
  9. "unicode"
  10. "unicode/utf8"
  11. "golang.org/x/text/unicode/norm"
  12. )
  13. // UnicodeLowercaseNormalized returns the Unicode lower case variant of s,
  14. // having also normalized it to normalization form C.
  15. func UnicodeLowercaseNormalized(s string) string {
  16. i := firstCaseChange(s)
  17. if i == -1 {
  18. return s
  19. }
  20. var rs strings.Builder
  21. // WriteRune always reserves utf8.UTFMax bytes for non-ASCII runes,
  22. // even if it doesn't need all that space. Overallocate now to prevent
  23. // it from ever triggering a reallocation.
  24. rs.Grow(utf8.UTFMax - 1 + len(s))
  25. rs.WriteString(s[:i])
  26. for _, r := range s[i:] {
  27. rs.WriteRune(unicode.ToLower(unicode.ToUpper(r)))
  28. }
  29. return norm.NFC.String(rs.String())
  30. }
  31. // Byte index of the first rune r s.t. lower(upper(r)) != r.
  32. func firstCaseChange(s string) int {
  33. for i, r := range s {
  34. if r <= unicode.MaxASCII && (r < 'A' || r > 'Z') {
  35. continue
  36. }
  37. if unicode.ToLower(unicode.ToUpper(r)) != r {
  38. return i
  39. }
  40. }
  41. return -1
  42. }