folding.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. )
  12. func UnicodeLowercase(s string) string {
  13. i := firstCaseChange(s)
  14. if i == -1 {
  15. return s
  16. }
  17. var rs strings.Builder
  18. // WriteRune always reserves utf8.UTFMax bytes for non-ASCII runes,
  19. // even if it doesn't need all that space. Overallocate now to prevent
  20. // it from ever triggering a reallocation.
  21. rs.Grow(utf8.UTFMax - 1 + len(s))
  22. rs.WriteString(s[:i])
  23. for _, r := range s[i:] {
  24. rs.WriteRune(unicode.ToLower(unicode.ToUpper(r)))
  25. }
  26. return rs.String()
  27. }
  28. // Byte index of the first rune r s.t. lower(upper(r)) != r.
  29. func firstCaseChange(s string) int {
  30. for i, r := range s {
  31. if r <= unicode.MaxASCII && (r < 'A' || r > 'Z') {
  32. continue
  33. }
  34. if unicode.ToLower(unicode.ToUpper(r)) != r {
  35. return i
  36. }
  37. }
  38. return -1
  39. }