2012-02-16 21:51:04 -07:00
|
|
|
// run
|
2009-04-12 18:01:17 -06: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-23 17:48:19 -07:00
|
|
|
// Test range over strings.
|
|
|
|
|
2009-04-12 18:01:17 -06:00
|
|
|
package main
|
|
|
|
|
2009-08-17 14:30:22 -06:00
|
|
|
import (
|
2010-09-03 18:36:13 -06:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2011-11-08 16:43:02 -07:00
|
|
|
"unicode/utf8"
|
2009-04-12 18:01:17 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2010-09-03 18:36:13 -06: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-25 23:20:02 -06:00
|
|
|
expect := []rune{0, 0123, 0, 0xFFFD, 0xFFFD, 0x123, 0xbabe, 0xbabe, 0x10FFFF, 'x'}
|
2010-09-03 18:36:13 -06: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-25 23:20:02 -06:00
|
|
|
var i int
|
|
|
|
var c rune
|
2010-09-03 18:36:13 -06:00
|
|
|
ok := true
|
|
|
|
cnum := 0
|
2009-04-12 18:01:17 -06: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-25 23:20:02 -06:00
|
|
|
r, size := utf8.DecodeRuneInString(s[i:len(s)]) // check it another way
|
2009-04-12 18:01:17 -06:00
|
|
|
if i != offset {
|
2010-09-03 18:36:13 -06:00
|
|
|
fmt.Printf("unexpected offset %d not %d\n", i, offset)
|
|
|
|
ok = false
|
2009-04-12 18:01:17 -06: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-25 23:20:02 -06:00
|
|
|
if r != expect[cnum] {
|
|
|
|
fmt.Printf("unexpected rune %d from DecodeRuneInString: %x not %x\n", i, r, expect[cnum])
|
2010-09-03 18:36:13 -06:00
|
|
|
ok = false
|
2009-04-12 18:01:17 -06: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-25 23:20:02 -06:00
|
|
|
fmt.Printf("unexpected rune %d from range: %x not %x\n", i, r, expect[cnum])
|
2010-09-03 18:36:13 -06:00
|
|
|
ok = false
|
2009-04-12 18:01:17 -06:00
|
|
|
}
|
2010-09-03 18:36:13 -06:00
|
|
|
offset += size
|
|
|
|
cnum++
|
2009-04-12 18:01:17 -06:00
|
|
|
}
|
|
|
|
if i != len(s)-1 {
|
2010-09-03 18:36:13 -06:00
|
|
|
fmt.Println("after loop i is", i, "not", len(s)-1)
|
|
|
|
ok = false
|
2009-04-12 18:01:17 -06:00
|
|
|
}
|
2009-04-13 06:31:44 -06:00
|
|
|
|
2010-09-03 18:36:13 -06:00
|
|
|
i = 12345
|
|
|
|
c = 23456
|
2009-04-13 06:31:44 -06:00
|
|
|
for i, c = range "" {
|
|
|
|
}
|
|
|
|
if i != 12345 {
|
2010-09-03 18:36:13 -06:00
|
|
|
fmt.Println("range empty string assigned to index:", i)
|
|
|
|
ok = false
|
2009-04-13 06:31:44 -06:00
|
|
|
}
|
|
|
|
if c != 23456 {
|
2010-09-03 18:36:13 -06:00
|
|
|
fmt.Println("range empty string assigned to value:", c)
|
|
|
|
ok = false
|
2009-04-13 06:31:44 -06:00
|
|
|
}
|
|
|
|
|
2012-08-08 15:01:23 -06: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-12 18:01:17 -06:00
|
|
|
if !ok {
|
2010-09-03 18:36:13 -06:00
|
|
|
fmt.Println("BUG: stringrange")
|
2009-05-08 16:21:41 -06:00
|
|
|
os.Exit(1)
|
2009-04-12 18:01:17 -06:00
|
|
|
}
|
|
|
|
}
|