SimpleFold Cycles Case Equivalents

Walk every case variant of a rune with unicode.SimpleFold, which returns the next equivalent rune and wraps back to the original at cycle end.

GoData Integrity

Code

gosnippet
40 lines
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', 'ß'))
}

What the code does

What another module actually calls is the function main — everything else is wiring.

The loop for i := 0; i < 3; i++ is the only repeated construct.

Watch out for

  • The dependency list is empty — the language runtime already provides everything.

Applying it

The primary signature, spelled out, is func main()

Look for the after, fold(A)=, three folds return to prefixes as the file executes; they connect every printed line to the step behind it.

Statements that matter most here

next := unicode.SimpleFold(start)

Interactive Tools for This Topic

FAQ

What does SimpleFold Cycles Case Equivalents print when run?
Running the file directly is enough — expect labeled output lines such as `after`, `fold(A)=`, `three folds return to`.