| 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 |
|
| 55 |
func ExampleDecodeRune() { |
| 56 |
b := []byte("Hello, 世界") |
| 57 |
|
| 58 |
for len(b) > 0 { |
| 59 |
r, size := utf8.DecodeRune(b) |
| 60 |
fmt.Printf("%c %v\n", r, size) |
| 61 |
|
| 62 |
b = b[size:] |
| 63 |
} |
| 64 |
// Output: |
| 65 |
// H 1 |
| 66 |
// e 1 |
| 67 |
// l 1 |
| 68 |
// l 1 |
| 69 |
// o 1 |
| 70 |
// , 1 |
| 71 |
// 1 |
| 72 |
// 世 3 |
| 73 |
// 界 3 |
| 74 |
} |
| 75 |
|
| 76 |
func ExampleDecodeRuneInString() { |
| 77 |
str := "Hello, 世界" |
| 78 |
|
| 79 |
for len(str) > 0 { |
| 80 |
r, size := utf8.DecodeRuneInString(str) |
| 81 |
fmt.Printf("%c %v\n", r, size) |
| 82 |
|
| 83 |
str = str[size:] |
| 84 |
} |
| 85 |
// Output: |
| 86 |
// H 1 |
| 87 |
// e 1 |
| 88 |
// l 1 |
| 89 |
// l 1 |
| 90 |
// o 1 |
| 91 |
// , 1 |
| 92 |
// 1 |
| 93 |
// 世 3 |
| 94 |
// 界 3 |
| 95 |
} |
| 96 |
|
| 97 |
func ExampleEncodeRune() { |
| 98 |
r := '世' |
| 99 |
buf := make([]byte, 3) |
| 100 |
|
| 101 |
n := utf8.EncodeRune(buf, r) |
| 102 |
|
| 103 |
fmt.Println(buf) |
| 104 |
fmt.Println(n) |
| 105 |
// Output: |
| 106 |
// [228 184 150] |
| 107 |
// 3 |
| 108 |
} |
| 109 |
|
| 110 |
func ExampleEncodeRune_outOfRange() { |
| 111 |
runes := []rune{ |
| 112 |
// Less than 0, out of range. |
| 113 |
-1, |
| 114 |
// Greater than 0x10FFFF, out of range. |
| 115 |
0x110000, |
| 116 |
// The Unicode replacement character. |
| 117 |
utf8.RuneError, |
| 118 |
} |
| 119 |
for i, c := range runes { |
| 120 |
buf := make([]byte, 3) |
| 121 |
size := utf8.EncodeRune(buf, c) |
| 122 |
fmt.Printf("%d: %d %[2]s %d\n", i, buf, size) |
| 123 |
} |
| 124 |
// Output: |
| 125 |
// 0: [239 191 189] � 3 |
| 126 |
// 1: [239 191 189] � 3 |
| 127 |
// 2: [239 191 189] � 3 |
| 128 |
} |
| 129 |
|
| 130 |
func ExampleFullRune() { |
| 131 |
buf := []byte{228, 184, 150} // 世 |
| 132 |
fmt.Println(utf8.FullRune(buf)) |
| 133 |
fmt.Println(utf8.FullRune(buf[:2])) |
| 134 |
// Output: |
| 135 |
// true |
| 136 |
// false |
| 137 |
} |
| 138 |
|
| 139 |
func ExampleFullRuneInString() { |
| 140 |
str := "世" |
| 141 |
fmt.Println(utf8.FullRuneInString(str)) |
| 142 |
fmt.Println(utf8.FullRuneInString(str[:2])) |
| 143 |
// Output: |
| 144 |
// true |
| 145 |
// false |
| 146 |
} |
| 147 |
|
| 148 |
func ExampleRuneCount() { |
| 149 |
buf := []byte("Hello, 世界") |
| 150 |
fmt.Println("bytes =", len(buf)) |
| 151 |
fmt.Println("runes =", utf8.RuneCount(buf)) |
| 152 |
// Output: |
| 153 |
// bytes = 13 |
| 154 |
// runes = 9 |
| 155 |
} |
| 156 |
|
| 157 |
func ExampleRuneCountInString() { |
| 158 |
str := "Hello, 世界" |
| 159 |
fmt.Println("bytes =", len(str)) |
| 160 |
fmt.Println("runes =", utf8.RuneCountInString(str)) |
| 161 |
// Output: |
| 162 |
// bytes = 13 |
| 163 |
// runes = 9 |
| 164 |
} |
| 165 |
|
| 166 |
func ExampleRuneLen() { |
| 167 |
fmt.Println(utf8.RuneLen('a')) |
| 168 |
fmt.Println(utf8.RuneLen('界')) |
| 169 |
// Output: |
| 170 |
// 1 |
| 171 |
// 3 |
| 172 |
} |
| 173 |
|
| 174 |
func ExampleRuneStart() { |
| 175 |
buf := []byte("a界") |
| 176 |
fmt.Println(utf8.RuneStart(buf[0])) |
| 177 |
fmt.Println(utf8.RuneStart(buf[1])) |
| 178 |
fmt.Println(utf8.RuneStart(buf[2])) |
| 179 |
// Output: |
| 180 |
// true |
| 181 |
// true |
| 182 |
// false |
| 183 |
} |
| 184 |
|
| 185 |
func ExampleValid() { |
| 186 |
valid := []byte("Hello, 世界") |
| 187 |
invalid := []byte{0xff, 0xfe, 0xfd} |
| 188 |
|
| 189 |
fmt.Println(utf8.Valid(valid)) |
| 190 |
fmt.Println(utf8.Valid(invalid)) |
| 191 |
// Output: |
| 192 |
// true |
| 193 |
// false |
| 194 |
} |
| 195 |
|
| 196 |
func ExampleValidRune() { |
| 197 |
valid := 'a' |
| 198 |
invalid := rune(0xfffffff) |
| 199 |
|
| 200 |
fmt.Println(utf8.ValidRune(valid)) |
| 201 |
fmt.Println(utf8.ValidRune(invalid)) |
| 202 |
// Output: |
| 203 |
// true |
| 204 |
// false |
| 205 |
} |
| 206 |
|
| 207 |
func ExampleValidString() { |
| 208 |
valid := "Hello, 世界" |
| 209 |
invalid := string([]byte{0xff, 0xfe, 0xfd}) |
| 210 |
|
| 211 |
fmt.Println(utf8.ValidString(valid)) |
| 212 |
fmt.Println(utf8.ValidString(invalid)) |
| 213 |
// Output: |
| 214 |
// true |
| 215 |
// false |
| 216 |
} |
| 217 |
|
| 218 |
func ExampleAppendRune() { |
| 219 |
buf1 := utf8.AppendRune(nil, 0x10000) |
| 220 |
buf2 := utf8.AppendRune([]byte("init"), 0x10000) |
| 221 |
fmt.Println(string(buf1)) |
| 222 |
fmt.Println(string(buf2)) |
| 223 |
// Output: |
| 224 |
// 𐀀 |
| 225 |
// init𐀀 |
| 226 |
} |