github.com/karrick/go@v0.0.0-20170817181416-d5b0ec858b37/test/utf.go (about) 1 // run 2 3 // Copyright 2009 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // Test UTF-8 in strings and character constants. 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 // encoded as bytes: 'a' 'b' 'c' e6 97 a5 e6 9c ac e8 aa 9e 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 }