github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/unicode/example_test.go (about) 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 "github.com/shogo82148/std/fmt" 9 "github.com/shogo82148/std/unicode" 10 ) 11 12 // 「Is」で始まる関数は、runeがどの範囲のテーブルに属しているかを調べるために使用できます。ただし、runesは複数の範囲に当てはまる場合があることに注意してください。 13 func Example_is() { 14 15 // 異なる型のルーンを持つ定数 16 const mixed = "\b5Ὂg̀9! ℃ᾭG" 17 for _, c := range mixed { 18 fmt.Printf("For %q:\n", c) 19 if unicode.IsControl(c) { 20 fmt.Println("\tis control rune") 21 } 22 if unicode.IsDigit(c) { 23 fmt.Println("\tis digit rune") 24 } 25 if unicode.IsGraphic(c) { 26 fmt.Println("\tis graphic rune") 27 } 28 if unicode.IsLetter(c) { 29 fmt.Println("\tis letter rune") 30 } 31 if unicode.IsLower(c) { 32 fmt.Println("\tis lower case rune") 33 } 34 if unicode.IsMark(c) { 35 fmt.Println("\tis mark rune") 36 } 37 if unicode.IsNumber(c) { 38 fmt.Println("\tis number rune") 39 } 40 if unicode.IsPrint(c) { 41 fmt.Println("\tis printable rune") 42 } 43 if !unicode.IsPrint(c) { 44 fmt.Println("\tis not printable rune") 45 } 46 if unicode.IsPunct(c) { 47 fmt.Println("\tis punct rune") 48 } 49 if unicode.IsSpace(c) { 50 fmt.Println("\tis space rune") 51 } 52 if unicode.IsSymbol(c) { 53 fmt.Println("\tis symbol rune") 54 } 55 if unicode.IsTitle(c) { 56 fmt.Println("\tis title case rune") 57 } 58 if unicode.IsUpper(c) { 59 fmt.Println("\tis upper case rune") 60 } 61 } 62 63 // Output: 64 // For '\b': 65 // is control rune 66 // is not printable rune 67 // For '5': 68 // is digit rune 69 // is graphic rune 70 // is number rune 71 // is printable rune 72 // For 'Ὂ': 73 // is graphic rune 74 // is letter rune 75 // is printable rune 76 // is upper case rune 77 // For 'g': 78 // is graphic rune 79 // is letter rune 80 // is lower case rune 81 // is printable rune 82 // For '̀': 83 // is graphic rune 84 // is mark rune 85 // is printable rune 86 // For '9': 87 // is digit rune 88 // is graphic rune 89 // is number rune 90 // is printable rune 91 // For '!': 92 // is graphic rune 93 // is printable rune 94 // is punct rune 95 // For ' ': 96 // is graphic rune 97 // is printable rune 98 // is space rune 99 // For '℃': 100 // is graphic rune 101 // is printable rune 102 // is symbol rune 103 // For 'ᾭ': 104 // is graphic rune 105 // is letter rune 106 // is printable rune 107 // is title case rune 108 // For 'G': 109 // is graphic rune 110 // is letter rune 111 // is printable rune 112 // is upper case rune 113 } 114 115 func ExampleSimpleFold() { 116 fmt.Printf("%#U\n", unicode.SimpleFold('A')) // 'a' 117 fmt.Printf("%#U\n", unicode.SimpleFold('a')) // 'A' 118 fmt.Printf("%#U\n", unicode.SimpleFold('K')) // 'k' 119 fmt.Printf("%#U\n", unicode.SimpleFold('k')) // '\u212A' (ケルビン記号、K) 120 fmt.Printf("%#U\n", unicode.SimpleFold('\u212A')) // 'K' 121 fmt.Printf("%#U\n", unicode.SimpleFold('1')) // '1' 122 123 // Output: 124 // U+0061 'a' 125 // U+0041 'A' 126 // U+006B 'k' 127 // U+212A 'K' 128 // U+004B 'K' 129 // U+0031 '1' 130 } 131 132 func ExampleTo() { 133 const lcG = 'g' 134 fmt.Printf("%#U\n", unicode.To(unicode.UpperCase, lcG)) 135 fmt.Printf("%#U\n", unicode.To(unicode.LowerCase, lcG)) 136 fmt.Printf("%#U\n", unicode.To(unicode.TitleCase, lcG)) 137 138 const ucG = 'G' 139 fmt.Printf("%#U\n", unicode.To(unicode.UpperCase, ucG)) 140 fmt.Printf("%#U\n", unicode.To(unicode.LowerCase, ucG)) 141 fmt.Printf("%#U\n", unicode.To(unicode.TitleCase, ucG)) 142 143 // Output: 144 // U+0047 'G' 145 // U+0067 'g' 146 // U+0047 'G' 147 // U+0047 'G' 148 // U+0067 'g' 149 // U+0047 'G' 150 } 151 152 func ExampleToLower() { 153 const ucG = 'G' 154 fmt.Printf("%#U\n", unicode.ToLower(ucG)) 155 156 // Output: 157 // U+0067 'g' 158 } 159 func ExampleToTitle() { 160 const ucG = 'g' 161 fmt.Printf("%#U\n", unicode.ToTitle(ucG)) 162 163 // Output: 164 // U+0047 'G' 165 } 166 167 func ExampleToUpper() { 168 const ucG = 'g' 169 fmt.Printf("%#U\n", unicode.ToUpper(ucG)) 170 171 // Output: 172 // U+0047 'G' 173 } 174 175 func ExampleSpecialCase() { 176 t := unicode.TurkishCase 177 178 const lci = 'i' 179 fmt.Printf("%#U\n", t.ToLower(lci)) 180 fmt.Printf("%#U\n", t.ToTitle(lci)) 181 fmt.Printf("%#U\n", t.ToUpper(lci)) 182 183 const uci = 'İ' 184 fmt.Printf("%#U\n", t.ToLower(uci)) 185 fmt.Printf("%#U\n", t.ToTitle(uci)) 186 fmt.Printf("%#U\n", t.ToUpper(uci)) 187 188 // Output: 189 // U+0069 'i' 190 // U+0130 'İ' 191 // U+0130 'İ' 192 // U+0069 'i' 193 // U+0130 'İ' 194 // U+0130 'İ' 195 } 196 197 func ExampleIsDigit() { 198 fmt.Printf("%t\n", unicode.IsDigit('৩')) 199 fmt.Printf("%t\n", unicode.IsDigit('A')) 200 // Output: 201 // true 202 // false 203 } 204 205 func ExampleIsNumber() { 206 fmt.Printf("%t\n", unicode.IsNumber('Ⅷ')) 207 fmt.Printf("%t\n", unicode.IsNumber('A')) 208 // Output: 209 // true 210 // false 211 } 212 213 func ExampleIsLetter() { 214 fmt.Printf("%t\n", unicode.IsLetter('A')) 215 fmt.Printf("%t\n", unicode.IsLetter('7')) 216 // Output: 217 // true 218 // false 219 } 220 221 func ExampleIsLower() { 222 fmt.Printf("%t\n", unicode.IsLower('a')) 223 fmt.Printf("%t\n", unicode.IsLower('A')) 224 // Output: 225 // true 226 // false 227 } 228 229 func ExampleIsUpper() { 230 fmt.Printf("%t\n", unicode.IsUpper('A')) 231 fmt.Printf("%t\n", unicode.IsUpper('a')) 232 // Output: 233 // true 234 // false 235 } 236 237 func ExampleIsTitle() { 238 fmt.Printf("%t\n", unicode.IsTitle('Dž')) 239 fmt.Printf("%t\n", unicode.IsTitle('a')) 240 // Output: 241 // true 242 // false 243 } 244 245 func ExampleIsSpace() { 246 fmt.Printf("%t\n", unicode.IsSpace(' ')) 247 fmt.Printf("%t\n", unicode.IsSpace('\n')) 248 fmt.Printf("%t\n", unicode.IsSpace('\t')) 249 fmt.Printf("%t\n", unicode.IsSpace('a')) 250 // Output: 251 // true 252 // true 253 // true 254 // false 255 }