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

ambiguous.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // This file is generated by modules/charset/ambiguous/generate.go DO NOT EDIT
  2. // Copyright 2022 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package charset
  5. import (
  6. "sort"
  7. "strings"
  8. "unicode"
  9. "code.gitea.io/gitea/modules/translation"
  10. )
  11. // AmbiguousTablesForLocale provides the table of ambiguous characters for this locale.
  12. func AmbiguousTablesForLocale(locale translation.Locale) []*AmbiguousTable {
  13. key := locale.Language()
  14. var table *AmbiguousTable
  15. var ok bool
  16. for len(key) > 0 {
  17. if table, ok = AmbiguousCharacters[key]; ok {
  18. break
  19. }
  20. idx := strings.LastIndexAny(key, "-_")
  21. if idx < 0 {
  22. key = ""
  23. } else {
  24. key = key[:idx]
  25. }
  26. }
  27. if table == nil && (locale.Language() == "zh-CN" || locale.Language() == "zh_CN") {
  28. table = AmbiguousCharacters["zh-hans"]
  29. }
  30. if table == nil && strings.HasPrefix(locale.Language(), "zh") {
  31. table = AmbiguousCharacters["zh-hant"]
  32. }
  33. if table == nil {
  34. table = AmbiguousCharacters["_default"]
  35. }
  36. return []*AmbiguousTable{
  37. table,
  38. AmbiguousCharacters["_common"],
  39. }
  40. }
  41. func isAmbiguous(r rune, confusableTo *rune, tables ...*AmbiguousTable) bool {
  42. for _, table := range tables {
  43. if !unicode.Is(table.RangeTable, r) {
  44. continue
  45. }
  46. i := sort.Search(len(table.Confusable), func(i int) bool {
  47. return table.Confusable[i] >= r
  48. })
  49. (*confusableTo) = table.With[i]
  50. return true
  51. }
  52. return false
  53. }