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.
12 func ExampleDecodeLastRune() {
13 b
:= []byte("Hello, 世界")
16 r
, size
:= utf8
.DecodeLastRune(b
)
17 fmt
.Printf("%c %v\n", r
, size
)
33 func ExampleDecodeLastRuneInString() {
37 r
, size
:= utf8
.DecodeLastRuneInString(str
)
38 fmt
.Printf("%c %v\n", r
, size
)
40 str
= str
[:len(str
)-size
]
55 func ExampleDecodeRune() {
56 b
:= []byte("Hello, 世界")
59 r
, size
:= utf8
.DecodeRune(b
)
60 fmt
.Printf("%c %v\n", r
, size
)
76 func ExampleDecodeRuneInString() {
80 r
, size
:= utf8
.DecodeRuneInString(str
)
81 fmt
.Printf("%c %v\n", r
, size
)
97 func ExampleEncodeRune() {
99 buf
:= make([]byte, 3)
101 n
:= utf8
.EncodeRune(buf
, r
)
110 func ExampleFullRune() {
111 buf
:= []byte{228, 184, 150} // 世
112 fmt
.Println(utf8
.FullRune(buf
))
113 fmt
.Println(utf8
.FullRune(buf
[:2]))
119 func ExampleFullRuneInString() {
121 fmt
.Println(utf8
.FullRuneInString(str
))
122 fmt
.Println(utf8
.FullRuneInString(str
[:2]))
128 func ExampleRuneCount() {
129 buf
:= []byte("Hello, 世界")
130 fmt
.Println("bytes =", len(buf
))
131 fmt
.Println("runes =", utf8
.RuneCount(buf
))
137 func ExampleRuneCountInString() {
139 fmt
.Println("bytes =", len(str
))
140 fmt
.Println("runes =", utf8
.RuneCountInString(str
))
146 func ExampleRuneLen() {
147 fmt
.Println(utf8
.RuneLen('a'))
148 fmt
.Println(utf8
.RuneLen('界'))
154 func ExampleRuneStart() {
156 fmt
.Println(utf8
.RuneStart(buf
[0]))
157 fmt
.Println(utf8
.RuneStart(buf
[1]))
158 fmt
.Println(utf8
.RuneStart(buf
[2]))
165 func ExampleValid() {
166 valid
:= []byte("Hello, 世界")
167 invalid
:= []byte{0xff, 0xfe, 0xfd}
169 fmt
.Println(utf8
.Valid(valid
))
170 fmt
.Println(utf8
.Valid(invalid
))
176 func ExampleValidRune() {
178 invalid
:= rune(0xfffffff)
180 fmt
.Println(utf8
.ValidRune(valid
))
181 fmt
.Println(utf8
.ValidRune(invalid
))
187 func ExampleValidString() {
189 invalid
:= string([]byte{0xff, 0xfe, 0xfd})
191 fmt
.Println(utf8
.ValidString(valid
))
192 fmt
.Println(utf8
.ValidString(invalid
))