Source file
test/utf.go
1
2
3
4
5
6
7
8
9 package main
10
11 import "unicode/utf8"
12
13 func main() {
14 var chars [6]rune
15 chars[0] = 'a'
16 chars[1] = 'b'
17 chars[2] = 'c'
18 chars[3] = '\u65e5'
19 chars[4] = '\u672c'
20 chars[5] = '\u8a9e'
21 s := ""
22 for i := 0; i < 6; i++ {
23 s += string(chars[i])
24 }
25 var l = len(s)
26 for w, i, j := 0, 0, 0; i < l; i += w {
27 var r rune
28 r, w = utf8.DecodeRuneInString(s[i:len(s)])
29 if w == 0 {
30 panic("zero width in string")
31 }
32 if r != chars[j] {
33 panic("wrong value from string")
34 }
35 j++
36 }
37
38 const L = 12
39 if L != l {
40 panic("wrong length constructing array")
41 }
42 a := make([]byte, L)
43 a[0] = 'a'
44 a[1] = 'b'
45 a[2] = 'c'
46 a[3] = 0xe6
47 a[4] = 0x97
48 a[5] = 0xa5
49 a[6] = 0xe6
50 a[7] = 0x9c
51 a[8] = 0xac
52 a[9] = 0xe8
53 a[10] = 0xaa
54 a[11] = 0x9e
55 for w, i, j := 0, 0, 0; i < L; i += w {
56 var r rune
57 r, w = utf8.DecodeRune(a[i:L])
58 if w == 0 {
59 panic("zero width in bytes")
60 }
61 if r != chars[j] {
62 panic("wrong value from bytes")
63 }
64 j++
65 }
66 }
67
View as plain text