2012-02-17 04:51:04 +00:00
|
|
|
// run
|
2009-04-13 00:01:17 +00:00
|
|
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-24 00:48:19 +00:00
|
|
|
// Test range over strings.
|
|
|
|
|
2009-04-13 00:01:17 +00:00
|
|
|
package main
|
|
|
|
|
2009-08-17 20:30:22 +00:00
|
|
|
import (
|
2010-09-04 00:36:13 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2011-11-08 23:43:02 +00:00
|
|
|
"unicode/utf8"
|
2009-04-13 00:01:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2010-09-04 00:36:13 +00:00
|
|
|
s := "\000\123\x00\xca\xFE\u0123\ubabe\U0000babe\U0010FFFFx"
|
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
2011-10-26 05:20:02 +00:00
|
|
|
expect := []rune{0, 0123, 0, 0xFFFD, 0xFFFD, 0x123, 0xbabe, 0xbabe, 0x10FFFF, 'x'}
|
2010-09-04 00:36:13 +00:00
|
|
|
offset := 0
|
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
2011-10-26 05:20:02 +00:00
|
|
|
var i int
|
|
|
|
var c rune
|
2010-09-04 00:36:13 +00:00
|
|
|
ok := true
|
|
|
|
cnum := 0
|
2009-04-13 00:01:17 +00:00
|
|
|
for i, c = range s {
|
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
2011-10-26 05:20:02 +00:00
|
|
|
r, size := utf8.DecodeRuneInString(s[i:len(s)]) // check it another way
|
2009-04-13 00:01:17 +00:00
|
|
|
if i != offset {
|
2010-09-04 00:36:13 +00:00
|
|
|
fmt.Printf("unexpected offset %d not %d\n", i, offset)
|
|
|
|
ok = false
|
2009-04-13 00:01:17 +00: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
2011-10-26 05:20:02 +00:00
|
|
|
if r != expect[cnum] {
|
|
|
|
fmt.Printf("unexpected rune %d from DecodeRuneInString: %x not %x\n", i, r, expect[cnum])
|
2010-09-04 00:36:13 +00:00
|
|
|
ok = false
|
2009-04-13 00:01:17 +00:00
|
|
|
}
|
|
|
|
if c != expect[cnum] {
|
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
2011-10-26 05:20:02 +00:00
|
|
|
fmt.Printf("unexpected rune %d from range: %x not %x\n", i, r, expect[cnum])
|
2010-09-04 00:36:13 +00:00
|
|
|
ok = false
|
2009-04-13 00:01:17 +00:00
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
offset += size
|
|
|
|
cnum++
|
2009-04-13 00:01:17 +00:00
|
|
|
}
|
|
|
|
if i != len(s)-1 {
|
2010-09-04 00:36:13 +00:00
|
|
|
fmt.Println("after loop i is", i, "not", len(s)-1)
|
|
|
|
ok = false
|
2009-04-13 00:01:17 +00:00
|
|
|
}
|
2009-04-13 12:31:44 +00:00
|
|
|
|
2010-09-04 00:36:13 +00:00
|
|
|
i = 12345
|
|
|
|
c = 23456
|
2009-04-13 12:31:44 +00:00
|
|
|
for i, c = range "" {
|
|
|
|
}
|
|
|
|
if i != 12345 {
|
2010-09-04 00:36:13 +00:00
|
|
|
fmt.Println("range empty string assigned to index:", i)
|
|
|
|
ok = false
|
2009-04-13 12:31:44 +00:00
|
|
|
}
|
|
|
|
if c != 23456 {
|
2010-09-04 00:36:13 +00:00
|
|
|
fmt.Println("range empty string assigned to value:", c)
|
|
|
|
ok = false
|
2009-04-13 12:31:44 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 21:01:23 +00:00
|
|
|
for _, c := range "a\xed\xa0\x80a" {
|
|
|
|
if c != 'a' && c != utf8.RuneError {
|
|
|
|
fmt.Printf("surrogate UTF-8 does not error: %U\n", c)
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-13 00:01:17 +00:00
|
|
|
if !ok {
|
2010-09-04 00:36:13 +00:00
|
|
|
fmt.Println("BUG: stringrange")
|
2009-05-08 22:21:41 +00:00
|
|
|
os.Exit(1)
|
2009-04-13 00:01:17 +00:00
|
|
|
}
|
|
|
|
}
|