Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
da1ce837
Commit
da1ce837
authored
Sep 17, 2012
by
Sanjay Menakuru
Committed by
Rob Pike
Sep 17, 2012
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
unicode/utf8: add Examples
R=golang-dev, r CC=adg, golang-dev
https://golang.org/cl/6493124
parent
129b2bb1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
192 additions
and
0 deletions
+192
-0
example_test.go
src/pkg/unicode/utf8/example_test.go
+192
-0
No files found.
src/pkg/unicode/utf8/example_test.go
0 → 100644
View file @
da1ce837
package
utf8_test
import
(
"fmt"
"unicode/utf8"
)
func
ExampleDecodeLastRune
()
{
b
:=
[]
byte
(
"Hello, 世界"
)
for
len
(
b
)
>
0
{
r
,
size
:=
utf8
.
DecodeLastRune
(
b
)
fmt
.
Printf
(
"%c %v
\n
"
,
r
,
size
)
b
=
b
[
:
len
(
b
)
-
size
]
}
// Output:
// 界 3
// 世 3
// 1
// , 1
// o 1
// l 1
// l 1
// e 1
// H 1
}
func
ExampleDecodeLastRuneInString
()
{
str
:=
"Hello, 世界"
for
len
(
str
)
>
0
{
r
,
size
:=
utf8
.
DecodeLastRuneInString
(
str
)
fmt
.
Printf
(
"%c %v
\n
"
,
r
,
size
)
str
=
str
[
:
len
(
str
)
-
size
]
}
// Output:
// 界 3
// 世 3
// 1
// , 1
// o 1
// l 1
// l 1
// e 1
// H 1
}
func
ExampleDecodeRune
()
{
b
:=
[]
byte
(
"Hello, 世界"
)
for
len
(
b
)
>
0
{
r
,
size
:=
utf8
.
DecodeRune
(
b
)
fmt
.
Printf
(
"%c %v
\n
"
,
r
,
size
)
b
=
b
[
size
:
]
}
// Output:
// H 1
// e 1
// l 1
// l 1
// o 1
// , 1
// 1
// 世 3
// 界 3
}
func
ExampleDecodeRuneInString
()
{
str
:=
"Hello, 世界"
for
len
(
str
)
>
0
{
r
,
size
:=
utf8
.
DecodeRuneInString
(
str
)
fmt
.
Printf
(
"%c %v
\n
"
,
r
,
size
)
str
=
str
[
size
:
]
}
// Output:
// H 1
// e 1
// l 1
// l 1
// o 1
// , 1
// 1
// 世 3
// 界 3
}
func
ExampleEncodeRune
()
{
r
:=
'世'
buf
:=
make
([]
byte
,
3
)
n
:=
utf8
.
EncodeRune
(
buf
,
r
)
fmt
.
Println
(
buf
)
fmt
.
Println
(
n
)
// Output:
// [228 184 150]
// 3
}
func
ExampleFullRune
()
{
buf
:=
[]
byte
{
228
,
184
,
150
}
// 世
fmt
.
Println
(
utf8
.
FullRune
(
buf
))
fmt
.
Println
(
utf8
.
FullRune
(
buf
[
:
2
]))
// Output:
// true
// false
}
func
ExampleFullRuneInString
()
{
str
:=
"世"
fmt
.
Println
(
utf8
.
FullRuneInString
(
str
))
fmt
.
Println
(
utf8
.
FullRuneInString
(
str
[
:
2
]))
// Output:
// true
// false
}
func
ExampleRuneCount
()
{
buf
:=
[]
byte
(
"Hello, 世界"
)
fmt
.
Println
(
"bytes ="
,
len
(
buf
))
fmt
.
Println
(
"runes ="
,
utf8
.
RuneCount
(
buf
))
// Output:
// bytes = 13
// runes = 9
}
func
ExampleRuneCountInString
()
{
str
:=
"Hello, 世界"
fmt
.
Println
(
"bytes ="
,
len
(
str
))
fmt
.
Println
(
"runes ="
,
utf8
.
RuneCountInString
(
str
))
// Output:
// bytes = 13
// runes = 9
}
func
ExampleRuneLen
()
{
fmt
.
Println
(
utf8
.
RuneLen
(
'a'
))
fmt
.
Println
(
utf8
.
RuneLen
(
'界'
))
// Output:
// 1
// 3
}
func
ExampleRuneStart
()
{
buf
:=
[]
byte
(
"a界"
)
fmt
.
Println
(
utf8
.
RuneStart
(
buf
[
0
]))
fmt
.
Println
(
utf8
.
RuneStart
(
buf
[
1
]))
fmt
.
Println
(
utf8
.
RuneStart
(
buf
[
2
]))
// Output:
// true
// true
// false
}
func
ExampleValid
()
{
valid
:=
[]
byte
(
"Hello, 世界"
)
invalid
:=
[]
byte
{
0xff
,
0xfe
,
0xfd
}
fmt
.
Println
(
utf8
.
Valid
(
valid
))
fmt
.
Println
(
utf8
.
Valid
(
invalid
))
// Output:
// true
// false
}
func
ExampleValidRune
()
{
valid
:=
'a'
invalid
:=
rune
(
0xfffffff
)
fmt
.
Println
(
utf8
.
ValidRune
(
valid
))
fmt
.
Println
(
utf8
.
ValidRune
(
invalid
))
// Output:
// true
// false
}
func
ExampleValidString
()
{
valid
:=
"Hello, 世界"
invalid
:=
string
([]
byte
{
0xff
,
0xfe
,
0xfd
})
fmt
.
Println
(
utf8
.
ValidString
(
valid
))
fmt
.
Println
(
utf8
.
ValidString
(
invalid
))
// Output:
// true
// false
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment