Debian Codegraph @

runes/example_test.go

raw
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package runes_test
6
7 import (
8 "fmt"
9 "unicode"
10
11 "golang.org/x/text/runes"
12 "golang.org/x/text/transform"
13 "golang.org/x/text/unicode/norm"
14 "golang.org/x/text/width"
15 )
16
17 func ExampleRemove() {
18 t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
19 s, _, _ := transform.String(t, "résumé")
20 fmt.Println(s)
21
22 // Output:
23 // resume
24 }
25
26 func ExampleMap() {
27 replaceHyphens := runes.Map(func(r rune) rune {
28 if unicode.Is(unicode.Hyphen, r) {
29 return '|'
30 }
31 return r
32 })
33 s, _, _ := transform.String(replaceHyphens, "a-b‐c⸗d﹣e")
34 fmt.Println(s)
35
36 // Output:
37 // a|b|c|d|e
38 }
39
40 func ExampleIn() {
41 // Convert Latin characters to their canonical form, while keeping other
42 // width distinctions.
43 t := runes.If(runes.In(unicode.Latin), width.Fold, nil)
44 s, _, _ := transform.String(t, "アルアノリウ tech / アルアノリウ tech")
45 fmt.Println(s)
46
47 // Output:
48 // アルアノリウ tech / アルアノリウ tech
49 }
50
51 func ExampleIf() {
52 // Widen everything but ASCII.
53 isASCII := func(r rune) bool { return r <= unicode.MaxASCII }
54 t := runes.If(runes.Predicate(isASCII), nil, width.Widen)
55 s, _, _ := transform.String(t, "アルアノリウ tech / 中國 / 5₩")
56 fmt.Println(s)
57
58 // Output:
59 // アルアノリウ tech / 中國 / 5₩
60 }