blob: 7b3e7ac742570cab11d311d5dbc958673e43ab6c [file] [log] [blame]
ChaiShushan64379b82013-12-17 06:52:32 -08001// 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
Sanjay Menakuruda1ce832012-09-17 11:06:42 -07005package utf8_test
6
7import (
8 "fmt"
9 "unicode/utf8"
10)
11
12func 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
33func 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
55func 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
76func 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
97func 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
110func ExampleFullRune() {
111 buf := []byte{228, 184, 150} // 世
112 fmt.Println(utf8.FullRune(buf))
113 fmt.Println(utf8.FullRune(buf[:2]))
114 // Output:
115 // true
116 // false
117}
118
119func ExampleFullRuneInString() {
120 str := "世"
121 fmt.Println(utf8.FullRuneInString(str))
122 fmt.Println(utf8.FullRuneInString(str[:2]))
123 // Output:
124 // true
125 // false
126}
127
128func ExampleRuneCount() {
129 buf := []byte("Hello, 世界")
130 fmt.Println("bytes =", len(buf))
131 fmt.Println("runes =", utf8.RuneCount(buf))
132 // Output:
133 // bytes = 13
134 // runes = 9
135}
136
137func ExampleRuneCountInString() {
138 str := "Hello, 世界"
139 fmt.Println("bytes =", len(str))
140 fmt.Println("runes =", utf8.RuneCountInString(str))
141 // Output:
142 // bytes = 13
143 // runes = 9
144}
145
146func ExampleRuneLen() {
147 fmt.Println(utf8.RuneLen('a'))
148 fmt.Println(utf8.RuneLen('界'))
149 // Output:
150 // 1
151 // 3
152}
153
154func ExampleRuneStart() {
155 buf := []byte("a界")
156 fmt.Println(utf8.RuneStart(buf[0]))
157 fmt.Println(utf8.RuneStart(buf[1]))
158 fmt.Println(utf8.RuneStart(buf[2]))
159 // Output:
160 // true
161 // true
162 // false
163}
164
165func ExampleValid() {
166 valid := []byte("Hello, 世界")
167 invalid := []byte{0xff, 0xfe, 0xfd}
168
169 fmt.Println(utf8.Valid(valid))
170 fmt.Println(utf8.Valid(invalid))
171 // Output:
172 // true
173 // false
174}
175
176func ExampleValidRune() {
177 valid := 'a'
178 invalid := rune(0xfffffff)
179
180 fmt.Println(utf8.ValidRune(valid))
181 fmt.Println(utf8.ValidRune(invalid))
182 // Output:
183 // true
184 // false
185}
186
187func ExampleValidString() {
188 valid := "Hello, 世界"
189 invalid := string([]byte{0xff, 0xfe, 0xfd})
190
191 fmt.Println(utf8.ValidString(valid))
192 fmt.Println(utf8.ValidString(invalid))
193 // Output:
194 // true
195 // false
196}