实现说明
SimpleFold 遍历大小写等价环以函数 main为骨架。
for i := 0; i < 3; i++ 承担全部的逐项处理。
使用要点
- 每一步打印都带着自己的标签:
after、fold(A)=、three folds return to。
常见问题
运行「SimpleFold 遍历大小写等价环」会打印什么?
不用做任何设置,直接把文件跑完——输出里的 after、fold(A)=、three folds return to 标签标明各步骤。
核心语句
next := unicode.SimpleFold(start)
代码示例
package main
import (
"fmt"
"unicode"
)
func main() {
// SimpleFold:给一个 rune,返回下一个大小写等价的 rune;环闭合一圈后回到起点
start := 'a'
next := unicode.SimpleFold(start)
fmt.Printf("after %c comes %c\n", start, next)
// 大写与希腊字母同样有等价环:Sigma 的环是 σ -> Σ -> ς -> σ
fmt.Printf("fold(A)=%c fold(Σ)=%c fold(ς)=%c\n",
unicode.SimpleFold('A'), unicode.SimpleFold('Σ'), unicode.SimpleFold('ς'))
// 走完整圈验证回到起点
r := 'a'
for i := 0; i < 3; i++ {
r = unicode.SimpleFold(r)
}
fmt.Printf("three folds return to %c\n", r)
// 没有大小写等价的字符折回自身:数字、汉字
fmt.Printf("fold('5')=%c fold('中')=%c\n", unicode.SimpleFold('5'), unicode.SimpleFold('中'))
// 等价环可用于自定义大小写折叠比较:逐环探测另一字符是否在环内
equalFold := func(a, b rune) bool {
for r := a; ; {
if r == b {
return true
}
if r = unicode.SimpleFold(r); r == a {
return false
}
}
}
fmt.Println("'STRASSE' member 'ß' folds to 's':", equalFold('s', 'ß'))
}