在 golang (go1.17 windows/amd64) 中,下面的程式給出了以下結果:
rune1 = U 0130 '?'
rune2 = U 0131 '?'
lower(rune1) = U 0069 'i'
upper(rune2) = U 0049 'I'
strings.EqualFold(?, ?) = false
strings.EqualFold(i, I) = true
我認為這strings.EqualFold會在 Unicode 大小寫折疊下檢查字串的相等性;然而,上面的例子似乎給出了一個反例。顯然,兩個符文都可以(手動)折疊成在大小寫折疊下相等的代碼點。
問:是golang正確的,strings.EqualFold(?, ?)是false?我預計它會屈服true。如果 golang 是正確的,為什么會這樣?或者這是根據某些 Unicode 規范的行為。
我在這里錯過了什么。
程式:
func TestRune2(t *testing.T) {
r1 := rune(0x0130) // U 0130 '?'
r2 := rune(0x0131) // U 0131 '?'
r1u := unicode.ToLower(r1)
r2u := unicode.ToUpper(r2)
t.Logf("\nrune1 = %#U\nrune2 = %#U\nlower(rune1) = %#U\nupper(rune2) = %#U\nstrings.EqualFold(%s, %s) = %v\nstrings.EqualFold(%s, %s) = %v",
r1, r2, r1u, r2u, string(r1), string(r2), strings.EqualFold(string(r1), string(r2)), string(r1u), string(r2u), strings.EqualFold(string(r1u), string(r2u)))
}
uj5u.com熱心網友回復:
是的,這是“正確”的行為。這些字母在大小寫折疊下表現不正常。請參閱:http : //www.unicode.org/Public/UCD/latest/ucd/CaseFolding.txt
U 0131 有全案折疊“F”和特殊的“T”:
T: special case for uppercase I and dotted uppercase I
- For non-Turkic languages, this mapping is normally not used.
- For Turkic languages (tr, az), this mapping can be used instead
of the normal mapping for these characters.
Note that the Turkic mappings do not maintain canonical equivalence
without additional processing.
See the discussions of case mapping in the Unicode Standard for more information.
我認為沒有辦法強制包字串使用 tr 或 az 映射。
uj5u.com熱心網友回復:
來自strings.EqualFold源 -unicode.ToLower并unicode.ToUpper沒有使用。
相反,它使用unicode.SimpleFold來查看特定符文是否“可折疊”,因此可能具有可比性:
// General case. SimpleFold(x) returns the next equivalent rune > x
// or wraps around to smaller values.
r := unicode.SimpleFold(sr)
for r != sr && r < tr {
r = unicode.SimpleFold(r)
}
符文“?”不可折疊。它的小寫代碼點是:
r := rune(0x0130) // U 0130 '?'
lr := unicode.ToLower(r) // U 0069 'i'
fmt.Printf("foldable? %v\n", r != unicode.SimpleFold(r)) // foldable? false
fmt.Printf("foldable? %v\n", lr != unicode.SimpleFold(lr)) // foldable? true
https://play.golang.org/p/105x0I714nS
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/347808.html
