Debian Codegraph @

libgo/go/unicode/example_test.go

raw
1 // Copyright 2015 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 unicode_test
6
7 import (
8 "fmt"
9 "unicode"
10 )
11
12 // Functions starting with "Is" can be used to inspect which table of range a
13 // rune belongs to. Note that runes may fit into more than one range.
14 func Example_is() {
15
16 // constant with mixed type runes
17 const mixed = "\b5Ὂg̀9! ℃ᾭG"
18 for _, c := range mixed {
19 fmt.Printf("For %q:\n", c)
20 if unicode.IsControl(c) {
21 fmt.Println("\tis control rune")
22 }
23 if unicode.IsDigit(c) {
24 fmt.Println("\tis digit rune")
25 }
26 if unicode.IsGraphic(c) {
27 fmt.Println("\tis graphic rune")
28 }
29 if unicode.IsLetter(c) {
30 fmt.Println("\tis letter rune")
31 }
32 if unicode.IsLower(c) {
33 fmt.Println("\tis lower case rune")
34 }
35 if unicode.IsMark(c) {
36 fmt.Println("\tis mark rune")
37 }
38 if unicode.IsNumber(c) {
39 fmt.Println("\tis number rune")
40 }
41 if unicode.IsPrint(c) {
42 fmt.Println("\tis printable rune")
43 }
44 if !unicode.IsPrint(c) {
45 fmt.Println("\tis not printable rune")
46 }
47 if unicode.IsPunct(c) {
48 fmt.Println("\tis punct rune")
49 }
50 if unicode.IsSpace(c) {
51 fmt.Println("\tis space rune")
52 }
53 if unicode.IsSymbol(c) {
54 fmt.Println("\tis symbol rune")
55 }
56 if unicode.IsTitle(c) {
57 fmt.Println("\tis title case rune")
58 }
59 if unicode.IsUpper(c) {
60 fmt.Println("\tis upper case rune")
61 }
62 }
63
64 // Output:
65 // For '\b':
66 // is control rune
67 // is not printable rune
68 // For '5':
69 // is digit rune
70 // is graphic rune
71 // is number rune
72 // is printable rune
73 // For 'Ὂ':
74 // is graphic rune
75 // is letter rune
76 // is printable rune
77 // is upper case rune
78 // For 'g':
79 // is graphic rune
80 // is letter rune
81 // is lower case rune
82 // is printable rune
83 // For '̀':
84 // is graphic rune
85 // is mark rune
86 // is printable rune
87 // For '9':
88 // is digit rune
89 // is graphic rune
90 // is number rune
91 // is printable rune
92 // For '!':
93 // is graphic rune
94 // is printable rune
95 // is punct rune
96 // For ' ':
97 // is graphic rune
98 // is printable rune
99 // is space rune
100 // For '℃':
101 // is graphic rune
102 // is printable rune
103 // is symbol rune
104 // For 'ᾭ':
105 // is graphic rune
106 // is letter rune
107 // is printable rune
108 // is title case rune
109 // For 'G':
110 // is graphic rune
111 // is letter rune
112 // is printable rune
113 // is upper case rune
114 }
115
116 func ExampleSimpleFold() {
117 fmt.Printf("%#U\n", unicode.SimpleFold('A')) // 'a'
118 fmt.Printf("%#U\n", unicode.SimpleFold('a')) // 'A'
119 fmt.Printf("%#U\n", unicode.SimpleFold('K')) // 'k'
120 fmt.Printf("%#U\n", unicode.SimpleFold('k')) // '\u212A' (Kelvin symbol, K)
121 fmt.Printf("%#U\n", unicode.SimpleFold('\u212A')) // 'K'
122 fmt.Printf("%#U\n", unicode.SimpleFold('1')) // '1'
123
124 // Output:
125 // U+0061 'a'
126 // U+0041 'A'
127 // U+006B 'k'
128 // U+212A 'K'
129 // U+004B 'K'
130 // U+0031 '1'
131 }
132
133 func ExampleTo() {
134 const lcG = 'g'
135 fmt.Printf("%#U\n", unicode.To(unicode.UpperCase, lcG))
136 fmt.Printf("%#U\n", unicode.To(unicode.LowerCase, lcG))
137 fmt.Printf("%#U\n", unicode.To(unicode.TitleCase, lcG))
138
139 const ucG = 'G'
140 fmt.Printf("%#U\n", unicode.To(unicode.UpperCase, ucG))
141 fmt.Printf("%#U\n", unicode.To(unicode.LowerCase, ucG))
142 fmt.Printf("%#U\n", unicode.To(unicode.TitleCase, ucG))
143
144 // Output:
145 // U+0047 'G'
146 // U+0067 'g'
147 // U+0047 'G'
148 // U+0047 'G'
149 // U+0067 'g'
150 // U+0047 'G'
151 }
152
153 func ExampleToLower() {
154 const ucG = 'G'
155 fmt.Printf("%#U\n", unicode.ToLower(ucG))
156
157 // Output:
158 // U+0067 'g'
159 }
160 func ExampleToTitle() {
161 const ucG = 'g'
162 fmt.Printf("%#U\n", unicode.ToTitle(ucG))
163
164 // Output:
165 // U+0047 'G'
166 }
167
168 func ExampleToUpper() {
169 const ucG = 'g'
170 fmt.Printf("%#U\n", unicode.ToUpper(ucG))
171
172 // Output:
173 // U+0047 'G'
174 }
175
176 func ExampleSpecialCase() {
177 t := unicode.TurkishCase
178
179 const lci = 'i'
180 fmt.Printf("%#U\n", t.ToLower(lci))
181 fmt.Printf("%#U\n", t.ToTitle(lci))
182 fmt.Printf("%#U\n", t.ToUpper(lci))
183
184 const uci = 'İ'
185 fmt.Printf("%#U\n", t.ToLower(uci))
186 fmt.Printf("%#U\n", t.ToTitle(uci))
187 fmt.Printf("%#U\n", t.ToUpper(uci))
188
189 // Output:
190 // U+0069 'i'
191 // U+0130 'İ'
192 // U+0130 'İ'
193 // U+0069 'i'
194 // U+0130 'İ'
195 // U+0130 'İ'
196 }
197
198 func ExampleIsDigit() {
199 fmt.Printf("%t\n", unicode.IsDigit('৩'))
200 fmt.Printf("%t\n", unicode.IsDigit('A'))
201 // Output:
202 // true
203 // false
204 }
205
206 func ExampleIsNumber() {
207 fmt.Printf("%t\n", unicode.IsNumber('Ⅷ'))
208 fmt.Printf("%t\n", unicode.IsNumber('A'))
209 // Output:
210 // true
211 // false
212 }
213
214 func ExampleIsLetter() {
215 fmt.Printf("%t\n", unicode.IsLetter('A'))
216 fmt.Printf("%t\n", unicode.IsLetter('7'))
217 // Output:
218 // true
219 // false
220 }
221
222 func ExampleIsLower() {
223 fmt.Printf("%t\n", unicode.IsLower('a'))
224 fmt.Printf("%t\n", unicode.IsLower('A'))
225 // Output:
226 // true
227 // false
228 }
229
230 func ExampleIsUpper() {
231 fmt.Printf("%t\n", unicode.IsUpper('A'))
232 fmt.Printf("%t\n", unicode.IsUpper('a'))
233 // Output:
234 // true
235 // false
236 }
237
238 func ExampleIsTitle() {
239 fmt.Printf("%t\n", unicode.IsTitle('Dž'))
240 fmt.Printf("%t\n", unicode.IsTitle('a'))
241 // Output:
242 // true
243 // false
244 }
245
246 func ExampleIsSpace() {
247 fmt.Printf("%t\n", unicode.IsSpace(' '))
248 fmt.Printf("%t\n", unicode.IsSpace('\n'))
249 fmt.Printf("%t\n", unicode.IsSpace('\t'))
250 fmt.Printf("%t\n", unicode.IsSpace('a'))
251 // Output:
252 // true
253 // true
254 // true
255 // false
256 }