1
0
mirror of https://github.com/golang/go synced 2024-11-24 22:00:09 -07:00

cgo, goyacc, go/build, html, http, path, path/filepath, testing/quick, test: use rune

Nothing terribly interesting here.

R=golang-dev, bradfitz, gri, r
CC=golang-dev
https://golang.org/cl/5300043
This commit is contained in:
Russ Cox 2011-10-25 22:20:02 -07:00
parent 6ed3fa6553
commit db33959797
20 changed files with 114 additions and 107 deletions

View File

@ -71,7 +71,7 @@ func (p *Package) ParseFlags(f *File, srcfile string) {
NextLine: NextLine:
for _, line := range linesIn { for _, line := range linesIn {
l := strings.TrimSpace(line) l := strings.TrimSpace(line)
if len(l) < 5 || l[:4] != "#cgo" || !unicode.IsSpace(int(l[4])) { if len(l) < 5 || l[:4] != "#cgo" || !unicode.IsSpace(rune(l[4])) {
linesOut = append(linesOut, line) linesOut = append(linesOut, line)
continue continue
} }
@ -193,28 +193,28 @@ func pkgConfig(packages []string) (cflags, ldflags []string, err os.Error) {
// //
func splitQuoted(s string) (r []string, err os.Error) { func splitQuoted(s string) (r []string, err os.Error) {
var args []string var args []string
arg := make([]int, len(s)) arg := make([]rune, len(s))
escaped := false escaped := false
quoted := false quoted := false
quote := 0 quote := rune(0)
i := 0 i := 0
for _, rune := range s { for _, r := range s {
switch { switch {
case escaped: case escaped:
escaped = false escaped = false
case rune == '\\': case r == '\\':
escaped = true escaped = true
continue continue
case quote != 0: case quote != 0:
if rune == quote { if r == quote {
quote = 0 quote = 0
continue continue
} }
case rune == '"' || rune == '\'': case r == '"' || r == '\'':
quoted = true quoted = true
quote = rune quote = r
continue continue
case unicode.IsSpace(rune): case unicode.IsSpace(r):
if quoted || i > 0 { if quoted || i > 0 {
quoted = false quoted = false
args = append(args, string(arg[:i])) args = append(args, string(arg[:i]))
@ -222,7 +222,7 @@ func splitQuoted(s string) (r []string, err os.Error) {
} }
continue continue
} }
arg[i] = rune arg[i] = r
i++ i++
} }
if quoted || i > 0 { if quoted || i > 0 {

View File

@ -102,7 +102,7 @@ func creat(name string) *os.File {
return f return f
} }
func slashToUnderscore(c int) int { func slashToUnderscore(c rune) rune {
if c == '/' || c == '\\' || c == ':' { if c == '/' || c == '\\' || c == ':' {
c = '_' c = '_'
} }

View File

@ -813,7 +813,8 @@ func defin(nt int, s string) int {
var peekline = 0 var peekline = 0
func gettok() int { func gettok() int {
var i, match, c int var i int
var match, c rune
tokname = "" tokname = ""
for { for {
@ -919,25 +920,25 @@ func gettok() int {
getword(c) getword(c)
// find a reserved word // find a reserved word
for c = 0; c < len(resrv); c++ { for i := range resrv {
if tokname == resrv[c].name { if tokname == resrv[i].name {
if tokflag { if tokflag {
fmt.Printf(">>> %%%v %v %v\n", tokname, fmt.Printf(">>> %%%v %v %v\n", tokname,
resrv[c].value-PRIVATE, lineno) resrv[i].value-PRIVATE, lineno)
} }
return resrv[c].value return resrv[i].value
} }
} }
errorf("invalid escape, or illegal reserved word: %v", tokname) errorf("invalid escape, or illegal reserved word: %v", tokname)
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
numbval = c - '0' numbval = int(c - '0')
for { for {
c = getrune(finput) c = getrune(finput)
if !isdigit(c) { if !isdigit(c) {
break break
} }
numbval = numbval*10 + c - '0' numbval = numbval*10 + int(c-'0')
} }
ungetrune(finput, c) ungetrune(finput, c)
if tokflag { if tokflag {
@ -953,7 +954,7 @@ func gettok() int {
if tokflag { if tokflag {
fmt.Printf(">>> OPERATOR %v %v\n", string(c), lineno) fmt.Printf(">>> OPERATOR %v %v\n", string(c), lineno)
} }
return c return int(c)
} }
// look ahead to distinguish IDENTIFIER from IDENTCOLON // look ahead to distinguish IDENTIFIER from IDENTCOLON
@ -982,7 +983,7 @@ func gettok() int {
return IDENTIFIER return IDENTIFIER
} }
func getword(c int) { func getword(c rune) {
tokname = "" tokname = ""
for isword(c) || isdigit(c) || c == '_' || c == '.' || c == '$' { for isword(c) || isdigit(c) || c == '_' || c == '.' || c == '$' {
tokname += string(c) tokname += string(c)
@ -1107,7 +1108,7 @@ func cpycode() {
// skipcom is called after reading a '/' // skipcom is called after reading a '/'
// //
func skipcom() int { func skipcom() int {
var c int var c rune
c = getrune(finput) c = getrune(finput)
if c == '/' { if c == '/' {
@ -1221,7 +1222,7 @@ loop:
j := 0 j := 0
if isdigit(c) { if isdigit(c) {
for isdigit(c) { for isdigit(c) {
j = j*10 + c - '0' j = j*10 + int(c-'0')
c = getrune(finput) c = getrune(finput)
} }
ungetrune(finput, c) ungetrune(finput, c)
@ -2837,10 +2838,10 @@ func others() {
fmt.Fprintf(ftable, "%d,\n}\n", 0) fmt.Fprintf(ftable, "%d,\n}\n", 0)
// copy parser text // copy parser text
c = getrune(finput) ch := getrune(finput)
for c != EOF { for ch != EOF {
ftable.WriteRune(c) ftable.WriteRune(ch)
c = getrune(finput) ch = getrune(finput)
} }
// copy yaccpar // copy yaccpar
@ -2976,11 +2977,11 @@ func prlook(p Lkset) {
// //
// utility routines // utility routines
// //
var peekrune int var peekrune rune
func isdigit(c int) bool { return c >= '0' && c <= '9' } func isdigit(c rune) bool { return c >= '0' && c <= '9' }
func isword(c int) bool { func isword(c rune) bool {
return c >= 0xa0 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') return c >= 0xa0 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
} }
@ -3010,8 +3011,8 @@ func putrune(f *bufio.Writer, c int) {
} }
} }
func getrune(f *bufio.Reader) int { func getrune(f *bufio.Reader) rune {
var r int var r rune
if peekrune != 0 { if peekrune != 0 {
if peekrune == EOF { if peekrune == EOF {
@ -3033,7 +3034,7 @@ func getrune(f *bufio.Reader) int {
return c return c
} }
func ungetrune(f *bufio.Reader, c int) { func ungetrune(f *bufio.Reader, c rune) {
if f != finput { if f != finput {
panic("ungetc - not finput") panic("ungetc - not finput")
} }

View File

@ -461,10 +461,10 @@ func safeName(s string) bool {
// //
func splitQuoted(s string) (r []string, err os.Error) { func splitQuoted(s string) (r []string, err os.Error) {
var args []string var args []string
arg := make([]int, len(s)) arg := make([]rune, len(s))
escaped := false escaped := false
quoted := false quoted := false
quote := 0 quote := rune(0)
i := 0 i := 0
for _, rune := range s { for _, rune := range s {
switch { switch {

View File

@ -13,7 +13,7 @@ const longestEntityWithoutSemicolon = 6
// //
// Note that the HTML5 list is larger than the HTML4 list at // Note that the HTML5 list is larger than the HTML4 list at
// http://www.w3.org/TR/html4/sgml/entities.html // http://www.w3.org/TR/html4/sgml/entities.html
var entity = map[string]int{ var entity = map[string]rune{
"AElig;": '\U000000C6', "AElig;": '\U000000C6',
"AMP;": '\U00000026', "AMP;": '\U00000026',
"Aacute;": '\U000000C1', "Aacute;": '\U000000C1',
@ -2155,7 +2155,7 @@ var entity = map[string]int{
} }
// HTML entities that are two unicode codepoints. // HTML entities that are two unicode codepoints.
var entity2 = map[string][2]int{ var entity2 = map[string][2]rune{
// TODO(nigeltao): Handle replacements that are wider than their names. // TODO(nigeltao): Handle replacements that are wider than their names.
// "nLt;": {'\u226A', '\u20D2'}, // "nLt;": {'\u226A', '\u20D2'},
// "nGt;": {'\u226B', '\u20D2'}, // "nGt;": {'\u226B', '\u20D2'},

View File

@ -14,7 +14,7 @@ import (
// These replacements permit compatibility with old numeric entities that // These replacements permit compatibility with old numeric entities that
// assumed Windows-1252 encoding. // assumed Windows-1252 encoding.
// http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#consume-a-character-reference // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#consume-a-character-reference
var replacementTable = [...]int{ var replacementTable = [...]rune{
'\u20AC', // First entry is what 0x80 should be replaced with. '\u20AC', // First entry is what 0x80 should be replaced with.
'\u0081', '\u0081',
'\u201A', '\u201A',
@ -79,23 +79,23 @@ func unescapeEntity(b []byte, dst, src int, attribute bool) (dst1, src1 int) {
i++ i++
} }
x := 0 x := rune(0)
for i < len(s) { for i < len(s) {
c = s[i] c = s[i]
i++ i++
if hex { if hex {
if '0' <= c && c <= '9' { if '0' <= c && c <= '9' {
x = 16*x + int(c) - '0' x = 16*x + rune(c) - '0'
continue continue
} else if 'a' <= c && c <= 'f' { } else if 'a' <= c && c <= 'f' {
x = 16*x + int(c) - 'a' + 10 x = 16*x + rune(c) - 'a' + 10
continue continue
} else if 'A' <= c && c <= 'F' { } else if 'A' <= c && c <= 'F' {
x = 16*x + int(c) - 'A' + 10 x = 16*x + rune(c) - 'A' + 10
continue continue
} }
} else if '0' <= c && c <= '9' { } else if '0' <= c && c <= '9' {
x = 10*x + int(c) - '0' x = 10*x + rune(c) - '0'
continue continue
} }
if c != ';' { if c != ';' {

View File

@ -333,18 +333,18 @@ func (h *Handler) handleInternalRedirect(rw http.ResponseWriter, req *http.Reque
h.PathLocationHandler.ServeHTTP(rw, newReq) h.PathLocationHandler.ServeHTTP(rw, newReq)
} }
func upperCaseAndUnderscore(rune int) int { func upperCaseAndUnderscore(r rune) rune {
switch { switch {
case rune >= 'a' && rune <= 'z': case r >= 'a' && r <= 'z':
return rune - ('a' - 'A') return r - ('a' - 'A')
case rune == '-': case r == '-':
return '_' return '_'
case rune == '=': case r == '=':
// Maybe not part of the CGI 'spec' but would mess up // Maybe not part of the CGI 'spec' but would mess up
// the environment in any case, as Go represents the // the environment in any case, as Go represents the
// environment as a slice of "key=value" strings. // environment as a slice of "key=value" strings.
return '_' return '_'
} }
// TODO: other transformations in spec or practice? // TODO: other transformations in spec or practice?
return rune return r
} }

View File

@ -136,7 +136,7 @@ func matchChunk(chunk, s string) (rest string, ok bool, err os.Error) {
chunk = chunk[1:] chunk = chunk[1:]
break break
} }
var lo, hi int var lo, hi rune
if lo, chunk, err = getEsc(chunk); err != nil { if lo, chunk, err = getEsc(chunk); err != nil {
return return
} }
@ -183,7 +183,7 @@ func matchChunk(chunk, s string) (rest string, ok bool, err os.Error) {
} }
// getEsc gets a possibly-escaped character from chunk, for a character class. // getEsc gets a possibly-escaped character from chunk, for a character class.
func getEsc(chunk string) (r int, nchunk string, err os.Error) { func getEsc(chunk string) (r rune, nchunk string, err os.Error) {
if len(chunk) == 0 || chunk[0] == '-' || chunk[0] == ']' { if len(chunk) == 0 || chunk[0] == '-' || chunk[0] == ']' {
err = ErrBadPattern err = ErrBadPattern
return return

View File

@ -136,7 +136,7 @@ func matchChunk(chunk, s string) (rest string, ok bool, err os.Error) {
chunk = chunk[1:] chunk = chunk[1:]
break break
} }
var lo, hi int var lo, hi rune
if lo, chunk, err = getEsc(chunk); err != nil { if lo, chunk, err = getEsc(chunk); err != nil {
return return
} }
@ -183,7 +183,7 @@ func matchChunk(chunk, s string) (rest string, ok bool, err os.Error) {
} }
// getEsc gets a possibly-escaped character from chunk, for a character class. // getEsc gets a possibly-escaped character from chunk, for a character class.
func getEsc(chunk string) (r int, nchunk string, err os.Error) { func getEsc(chunk string) (r rune, nchunk string, err os.Error) {
if len(chunk) == 0 || chunk[0] == '-' || chunk[0] == ']' { if len(chunk) == 0 || chunk[0] == '-' || chunk[0] == ']' {
err = ErrBadPattern err = ErrBadPattern
return return

View File

@ -100,7 +100,7 @@ func makeCmdLine(args []string) string {
// Last bytes are two UCS-2 NULs, or four NUL bytes. // Last bytes are two UCS-2 NULs, or four NUL bytes.
func createEnvBlock(envv []string) *uint16 { func createEnvBlock(envv []string) *uint16 {
if len(envv) == 0 { if len(envv) == 0 {
return &utf16.Encode([]int("\x00\x00"))[0] return &utf16.Encode([]rune("\x00\x00"))[0]
} }
length := 0 length := 0
for _, s := range envv { for _, s := range envv {
@ -118,7 +118,7 @@ func createEnvBlock(envv []string) *uint16 {
} }
copy(b[i:i+1], []byte{0}) copy(b[i:i+1], []byte{0})
return &utf16.Encode([]int(string(b)))[0] return &utf16.Encode([]rune(string(b)))[0]
} }
func CloseOnExec(fd Handle) { func CloseOnExec(fd Handle) {

View File

@ -56,7 +56,7 @@ func main() {
// StringToUTF16 returns the UTF-16 encoding of the UTF-8 string s, // StringToUTF16 returns the UTF-16 encoding of the UTF-8 string s,
// with a terminating NUL added. // with a terminating NUL added.
func StringToUTF16(s string) []uint16 { return utf16.Encode([]int(s + "\x00")) } func StringToUTF16(s string) []uint16 { return utf16.Encode([]rune(s + "\x00")) }
// UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s, // UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s,
// with a terminating NUL removed. // with a terminating NUL removed.

View File

@ -123,9 +123,9 @@ func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) {
return s, true return s, true
case reflect.String: case reflect.String:
numChars := rand.Intn(complexSize) numChars := rand.Intn(complexSize)
codePoints := make([]int, numChars) codePoints := make([]rune, numChars)
for i := 0; i < numChars; i++ { for i := 0; i < numChars; i++ {
codePoints[i] = rand.Intn(0x10ffff) codePoints[i] = rune(rand.Intn(0x10ffff))
} }
return reflect.ValueOf(string(codePoints)), true return reflect.ValueOf(string(codePoints)), true
case reflect.Struct: case reflect.Struct:

View File

@ -36,7 +36,7 @@ var good3 int = 1e9
var good4 float64 = 1e20 var good4 float64 = 1e20
// explicit conversion of string is okay // explicit conversion of string is okay
var _ = []int("abc") var _ = []rune("abc")
var _ = []byte("abc") var _ = []byte("abc")
// implicit is not // implicit is not
@ -47,20 +47,20 @@ var _ []byte = "abc" // ERROR "cannot use|incompatible|invalid"
type Tstring string type Tstring string
var ss Tstring = "abc" var ss Tstring = "abc"
var _ = []int(ss) var _ = []rune(ss)
var _ = []byte(ss) var _ = []byte(ss)
// implicit is still not // implicit is still not
var _ []int = ss // ERROR "cannot use|incompatible|invalid" var _ []rune = ss // ERROR "cannot use|incompatible|invalid"
var _ []byte = ss // ERROR "cannot use|incompatible|invalid" var _ []byte = ss // ERROR "cannot use|incompatible|invalid"
// named slice is not // named slice is not
type Tint []int type Trune []rune
type Tbyte []byte type Tbyte []byte
var _ = Tint("abc") // ERROR "convert|incompatible|invalid" var _ = Trune("abc") // ERROR "convert|incompatible|invalid"
var _ = Tbyte("abc") // ERROR "convert|incompatible|invalid" var _ = Tbyte("abc") // ERROR "convert|incompatible|invalid"
// implicit is still not // implicit is still not
var _ Tint = "abc" // ERROR "cannot use|incompatible|invalid" var _ Trune = "abc" // ERROR "cannot use|incompatible|invalid"
var _ Tbyte = "abc" // ERROR "cannot use|incompatible|invalid" var _ Tbyte = "abc" // ERROR "cannot use|incompatible|invalid"

View File

@ -7,18 +7,18 @@
package main package main
func main() { func main() {
nchar := 0; nchar := 0
a := []int { '日', '本', '語', 0xFFFD }; a := []rune{'日', '本', '語', 0xFFFD}
for _, char := range "日本語\xc0" { for _, char := range "日本語\xc0" {
if nchar >= len(a) { if nchar >= len(a) {
println("BUG"); println("BUG")
break; break
} }
if char != a[nchar] { if char != a[nchar] {
println("expected", a[nchar], "got", char); println("expected", a[nchar], "got", char)
println("BUG"); println("BUG")
break; break
} }
nchar++; nchar++
} }
} }

View File

@ -95,7 +95,7 @@ func main() {
} }
/* create string with int array */ /* create string with int array */
var z2 [3]int var z2 [3]rune
z2[0] = 'a' z2[0] = 'a'
z2[1] = '\u1234' z2[1] = '\u1234'
z2[2] = 'c' z2[2] = 'c'

View File

@ -172,7 +172,7 @@ func makestring() string {
} }
func teststring() { func teststring() {
s := 0 var s rune
nmake = 0 nmake = 0
for _, v := range makestring() { for _, v := range makestring() {
s += v s += v
@ -208,7 +208,7 @@ func teststring1() {
func makemap() map[int]int { func makemap() map[int]int {
nmake++ nmake++
return map[int]int{0:'a', 1:'b', 2:'c', 3:'d', 4:'☺'} return map[int]int{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: '☺'}
} }
func testmap() { func testmap() {

View File

@ -14,7 +14,7 @@ const N = 11 + 1 // length of a board row (+1 for newline)
// The board must be surrounded by 2 illegal fields in each direction // The board must be surrounded by 2 illegal fields in each direction
// so that move() doesn't need to check the board boundaries. Periods // so that move() doesn't need to check the board boundaries. Periods
// represent illegal fields, ● are pegs, and ○ are holes. // represent illegal fields, ● are pegs, and ○ are holes.
var board = []int( var board = []rune(
`........... `...........
........... ...........
........ ........
@ -28,7 +28,6 @@ var board = []int(
........... ...........
`) `)
// center is the position of the center hole if there is a single one; // center is the position of the center hole if there is a single one;
// otherwise it is -1. // otherwise it is -1.
var center int var center int
@ -46,7 +45,6 @@ func init() {
} }
} }
var moves int // number of times move is called var moves int // number of times move is called
// move tests if there is a peg at position pos that can jump over another peg // move tests if there is a peg at position pos that can jump over another peg
@ -63,7 +61,6 @@ func move(pos, dir int) bool {
return false return false
} }
// unmove reverts a previously executed valid move. // unmove reverts a previously executed valid move.
func unmove(pos, dir int) { func unmove(pos, dir int) {
board[pos] = '●' board[pos] = '●'
@ -71,7 +68,6 @@ func unmove(pos, dir int) {
board[pos+2*dir] = '○' board[pos+2*dir] = '○'
} }
// solve tries to find a sequence of moves such that there is only one peg left // solve tries to find a sequence of moves such that there is only one peg left
// at the end; if center is >= 0, that last peg must be in the center position. // at the end; if center is >= 0, that last peg must be in the center position.
// If a solution is found, solve prints the board after each move in a backward // If a solution is found, solve prints the board after each move in a backward
@ -110,7 +106,6 @@ func solve() bool {
return false return false
} }
func main() { func main() {
if !solve() { if !solve() {
println("no solution found") println("no solution found")

View File

@ -35,14 +35,14 @@ func assert(a, b, c string) {
} }
const ( const (
gx1 = "aä本☺" gx1 = "aä本☺"
gx2 = "aä\xFF\xFF本☺" gx2 = "aä\xFF\xFF本☺"
gx2fix = "aä\uFFFD\uFFFD本☺" gx2fix = "aä\uFFFD\uFFFD本☺"
) )
var ( var (
gr1 = []int(gx1) gr1 = []rune(gx1)
gr2 = []int(gx2) gr2 = []rune(gx2)
gb1 = []byte(gx1) gb1 = []byte(gx1)
gb2 = []byte(gx2) gb2 = []byte(gx2)
) )
@ -93,26 +93,26 @@ func main() {
// test large runes. perhaps not the most logical place for this test. // test large runes. perhaps not the most logical place for this test.
var r int32 var r int32
r = 0x10ffff; // largest rune value r = 0x10ffff // largest rune value
s = string(r) s = string(r)
assert(s, "\xf4\x8f\xbf\xbf", "largest rune") assert(s, "\xf4\x8f\xbf\xbf", "largest rune")
r = 0x10ffff + 1 r = 0x10ffff + 1
s = string(r) s = string(r)
assert(s, "\xef\xbf\xbd", "too-large rune") assert(s, "\xef\xbf\xbd", "too-large rune")
assert(string(gr1), gx1, "global ->[]int") assert(string(gr1), gx1, "global ->[]rune")
assert(string(gr2), gx2fix, "global invalid ->[]int") assert(string(gr2), gx2fix, "global invalid ->[]rune")
assert(string(gb1), gx1, "->[]byte") assert(string(gb1), gx1, "->[]byte")
assert(string(gb2), gx2, "global invalid ->[]byte") assert(string(gb2), gx2, "global invalid ->[]byte")
var ( var (
r1 = []int(gx1) r1 = []rune(gx1)
r2 = []int(gx2) r2 = []rune(gx2)
b1 = []byte(gx1) b1 = []byte(gx1)
b2 = []byte(gx2) b2 = []byte(gx2)
) )
assert(string(r1), gx1, "->[]int") assert(string(r1), gx1, "->[]rune")
assert(string(r2), gx2fix, "invalid ->[]int") assert(string(r2), gx2fix, "invalid ->[]rune")
assert(string(b1), gx1, "->[]byte") assert(string(b1), gx1, "->[]byte")
assert(string(b2), gx2, "invalid ->[]byte") assert(string(b2), gx2, "invalid ->[]byte")

View File

@ -14,23 +14,24 @@ import (
func main() { func main() {
s := "\000\123\x00\xca\xFE\u0123\ubabe\U0000babe\U0010FFFFx" s := "\000\123\x00\xca\xFE\u0123\ubabe\U0000babe\U0010FFFFx"
expect := []int{ 0, 0123, 0, 0xFFFD, 0xFFFD, 0x123, 0xbabe, 0xbabe, 0x10FFFF, 'x' } expect := []rune{0, 0123, 0, 0xFFFD, 0xFFFD, 0x123, 0xbabe, 0xbabe, 0x10FFFF, 'x'}
offset := 0 offset := 0
var i, c int var i int
var c rune
ok := true ok := true
cnum := 0 cnum := 0
for i, c = range s { for i, c = range s {
rune, size := utf8.DecodeRuneInString(s[i:len(s)]) // check it another way r, size := utf8.DecodeRuneInString(s[i:len(s)]) // check it another way
if i != offset { if i != offset {
fmt.Printf("unexpected offset %d not %d\n", i, offset) fmt.Printf("unexpected offset %d not %d\n", i, offset)
ok = false ok = false
} }
if rune != expect[cnum] { if r != expect[cnum] {
fmt.Printf("unexpected rune %d from DecodeRuneInString: %x not %x\n", i, rune, expect[cnum]) fmt.Printf("unexpected rune %d from DecodeRuneInString: %x not %x\n", i, r, expect[cnum])
ok = false ok = false
} }
if c != expect[cnum] { if c != expect[cnum] {
fmt.Printf("unexpected rune %d from range: %x not %x\n", i, rune, expect[cnum]) fmt.Printf("unexpected rune %d from range: %x not %x\n", i, r, expect[cnum])
ok = false ok = false
} }
offset += size offset += size

View File

@ -9,7 +9,7 @@ package main
import "utf8" import "utf8"
func main() { func main() {
var chars [6] int var chars [6]rune
chars[0] = 'a' chars[0] = 'a'
chars[1] = 'b' chars[1] = 'b'
chars[2] = 'c' chars[2] = 'c'
@ -21,16 +21,22 @@ func main() {
s += string(chars[i]) s += string(chars[i])
} }
var l = len(s) var l = len(s)
for w, i, j := 0,0,0; i < l; i += w { for w, i, j := 0, 0, 0; i < l; i += w {
var r int var r rune
r, w = utf8.DecodeRuneInString(s[i:len(s)]) r, w = utf8.DecodeRuneInString(s[i:len(s)])
if w == 0 { panic("zero width in string") } if w == 0 {
if r != chars[j] { panic("wrong value from string") } panic("zero width in string")
}
if r != chars[j] {
panic("wrong value from string")
}
j++ j++
} }
// encoded as bytes: 'a' 'b' 'c' e6 97 a5 e6 9c ac e8 aa 9e // encoded as bytes: 'a' 'b' 'c' e6 97 a5 e6 9c ac e8 aa 9e
const L = 12 const L = 12
if L != l { panic("wrong length constructing array") } if L != l {
panic("wrong length constructing array")
}
a := make([]byte, L) a := make([]byte, L)
a[0] = 'a' a[0] = 'a'
a[1] = 'b' a[1] = 'b'
@ -44,11 +50,15 @@ func main() {
a[9] = 0xe8 a[9] = 0xe8
a[10] = 0xaa a[10] = 0xaa
a[11] = 0x9e a[11] = 0x9e
for w, i, j := 0,0,0; i < L; i += w { for w, i, j := 0, 0, 0; i < L; i += w {
var r int var r rune
r, w = utf8.DecodeRune(a[i:L]) r, w = utf8.DecodeRune(a[i:L])
if w == 0 { panic("zero width in bytes") } if w == 0 {
if r != chars[j] { panic("wrong value from bytes") } panic("zero width in bytes")
}
if r != chars[j] {
panic("wrong value from bytes")
}
j++ j++
} }
} }