github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/stdlibs/unicode/utf8/example_test.gno (about) 1 // Copyright 2013 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 utf8_test 6 7 import ( 8 "fmt" 9 "unicode/utf8" 10 ) 11 12 func ExampleDecodeLastRune() { 13 b := []byte("Hello, 世界") 14 15 for len(b) > 0 { 16 r, size := utf8.DecodeLastRune(b) 17 fmt.Printf("%c %v\n", r, size) 18 19 b = b[:len(b)-size] 20 } 21 // Output: 22 // 界 3 23 // 世 3 24 // 1 25 // , 1 26 // o 1 27 // l 1 28 // l 1 29 // e 1 30 // H 1 31 } 32 33 func ExampleDecodeLastRuneInString() { 34 str := "Hello, 世界" 35 36 for len(str) > 0 { 37 r, size := utf8.DecodeLastRuneInString(str) 38 fmt.Printf("%c %v\n", r, size) 39 40 str = str[:len(str)-size] 41 } 42 // Output: 43 // 界 3 44 // 世 3 45 // 1 46 // , 1 47 // o 1 48 // l 1 49 // l 1 50 // e 1 51 // H 1 52 } 53 54 func ExampleDecodeRune() { 55 b := []byte("Hello, 世界") 56 57 for len(b) > 0 { 58 r, size := utf8.DecodeRune(b) 59 fmt.Printf("%c %v\n", r, size) 60 61 b = b[size:] 62 } 63 // Output: 64 // H 1 65 // e 1 66 // l 1 67 // l 1 68 // o 1 69 // , 1 70 // 1 71 // 世 3 72 // 界 3 73 } 74 75 func ExampleDecodeRuneInString() { 76 str := "Hello, 世界" 77 78 for len(str) > 0 { 79 r, size := utf8.DecodeRuneInString(str) 80 fmt.Printf("%c %v\n", r, size) 81 82 str = str[size:] 83 } 84 // Output: 85 // H 1 86 // e 1 87 // l 1 88 // l 1 89 // o 1 90 // , 1 91 // 1 92 // 世 3 93 // 界 3 94 } 95 96 func ExampleEncodeRune() { 97 r := '世' 98 buf := make([]byte, 3) 99 100 n := utf8.EncodeRune(buf, r) 101 102 fmt.Println(buf) 103 fmt.Println(n) 104 // Output: 105 // [228 184 150] 106 // 3 107 } 108 109 func ExampleEncodeRune_outOfRange() { 110 runes := []rune{ 111 // Less than 0, out of range. 112 -1, 113 // Greater than 0x10FFFF, out of range. 114 0x110000, 115 // The Unicode replacement character. 116 utf8.RuneError, 117 } 118 for i, c := range runes { 119 buf := make([]byte, 3) 120 size := utf8.EncodeRune(buf, c) 121 fmt.Printf("%d: %d %[2]s %d\n", i, buf, size) 122 } 123 // Output: 124 // 0: [239 191 189] � 3 125 // 1: [239 191 189] � 3 126 // 2: [239 191 189] � 3 127 } 128 129 func ExampleFullRune() { 130 buf := []byte{228, 184, 150} // 世 131 fmt.Println(utf8.FullRune(buf)) 132 fmt.Println(utf8.FullRune(buf[:2])) 133 // Output: 134 // true 135 // false 136 } 137 138 func ExampleFullRuneInString() { 139 str := "世" 140 fmt.Println(utf8.FullRuneInString(str)) 141 fmt.Println(utf8.FullRuneInString(str[:2])) 142 // Output: 143 // true 144 // false 145 } 146 147 func ExampleRuneCount() { 148 buf := []byte("Hello, 世界") 149 fmt.Println("bytes =", len(buf)) 150 fmt.Println("runes =", utf8.RuneCount(buf)) 151 // Output: 152 // bytes = 13 153 // runes = 9 154 } 155 156 func ExampleRuneCountInString() { 157 str := "Hello, 世界" 158 fmt.Println("bytes =", len(str)) 159 fmt.Println("runes =", utf8.RuneCountInString(str)) 160 // Output: 161 // bytes = 13 162 // runes = 9 163 } 164 165 func ExampleRuneLen() { 166 fmt.Println(utf8.RuneLen('a')) 167 fmt.Println(utf8.RuneLen('界')) 168 // Output: 169 // 1 170 // 3 171 } 172 173 func ExampleRuneStart() { 174 buf := []byte("a界") 175 fmt.Println(utf8.RuneStart(buf[0])) 176 fmt.Println(utf8.RuneStart(buf[1])) 177 fmt.Println(utf8.RuneStart(buf[2])) 178 // Output: 179 // true 180 // true 181 // false 182 } 183 184 func ExampleValid() { 185 valid := []byte("Hello, 世界") 186 invalid := []byte{0xff, 0xfe, 0xfd} 187 188 fmt.Println(utf8.Valid(valid)) 189 fmt.Println(utf8.Valid(invalid)) 190 // Output: 191 // true 192 // false 193 } 194 195 func ExampleValidRune() { 196 valid := 'a' 197 invalid := rune(0xfffffff) 198 199 fmt.Println(utf8.ValidRune(valid)) 200 fmt.Println(utf8.ValidRune(invalid)) 201 // Output: 202 // true 203 // false 204 } 205 206 func ExampleValidString() { 207 valid := "Hello, 世界" 208 invalid := string([]byte{0xff, 0xfe, 0xfd}) 209 210 fmt.Println(utf8.ValidString(valid)) 211 fmt.Println(utf8.ValidString(invalid)) 212 // Output: 213 // true 214 // false 215 }