code.gitea.io/gitea@v1.19.3/modules/charset/ambiguous.go (about)

     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  
     5  package charset
     6  
     7  import (
     8  	"sort"
     9  	"strings"
    10  	"unicode"
    11  
    12  	"code.gitea.io/gitea/modules/translation"
    13  )
    14  
    15  // AmbiguousTablesForLocale provides the table of ambiguous characters for this locale.
    16  func AmbiguousTablesForLocale(locale translation.Locale) []*AmbiguousTable {
    17  	key := locale.Language()
    18  	var table *AmbiguousTable
    19  	var ok bool
    20  	for len(key) > 0 {
    21  		if table, ok = AmbiguousCharacters[key]; ok {
    22  			break
    23  		}
    24  		idx := strings.LastIndexAny(key, "-_")
    25  		if idx < 0 {
    26  			key = ""
    27  		} else {
    28  			key = key[:idx]
    29  		}
    30  	}
    31  	if table == nil && (locale.Language() == "zh-CN" || locale.Language() == "zh_CN") {
    32  		table = AmbiguousCharacters["zh-hans"]
    33  	}
    34  	if table == nil && strings.HasPrefix(locale.Language(), "zh") {
    35  		table = AmbiguousCharacters["zh-hant"]
    36  	}
    37  	if table == nil {
    38  		table = AmbiguousCharacters["_default"]
    39  	}
    40  
    41  	return []*AmbiguousTable{
    42  		table,
    43  		AmbiguousCharacters["_common"],
    44  	}
    45  }
    46  
    47  func isAmbiguous(r rune, confusableTo *rune, tables ...*AmbiguousTable) bool {
    48  	for _, table := range tables {
    49  		if !unicode.Is(table.RangeTable, r) {
    50  			continue
    51  		}
    52  		i := sort.Search(len(table.Confusable), func(i int) bool {
    53  			return table.Confusable[i] >= r
    54  		})
    55  		(*confusableTo) = table.With[i]
    56  		return true
    57  	}
    58  	return false
    59  }