From 8039ca76a5705ae5052b20cee64110c32545c4fc Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Thu, 24 Mar 2011 23:46:17 +0000 Subject: Update to current version of Go library. From-SVN: r171427 --- libgo/go/strings/strings.go | 16 ++++++++++--- libgo/go/strings/strings_test.go | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) (limited to 'libgo/go/strings') diff --git a/libgo/go/strings/strings.go b/libgo/go/strings/strings.go index 98a0d57..5f009e5 100644 --- a/libgo/go/strings/strings.go +++ b/libgo/go/strings/strings.go @@ -119,9 +119,19 @@ func LastIndex(s, sep string) int { // IndexRune returns the index of the first instance of the Unicode code point // rune, or -1 if rune is not present in s. func IndexRune(s string, rune int) int { - for i, c := range s { - if c == rune { - return i + switch { + case rune < 0x80: + b := byte(rune) + for i := 0; i < len(s); i++ { + if s[i] == b { + return i + } + } + default: + for i, c := range s { + if c == rune { + return i + } } } return -1 diff --git a/libgo/go/strings/strings_test.go b/libgo/go/strings/strings_test.go index 734fdd3..41e3987 100644 --- a/libgo/go/strings/strings_test.go +++ b/libgo/go/strings/strings_test.go @@ -6,6 +6,7 @@ package strings_test import ( "os" + "strconv" . "strings" "testing" "unicode" @@ -116,6 +117,57 @@ func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", l func TestIndexAny(t *testing.T) { runIndexTests(t, IndexAny, "IndexAny", indexAnyTests) } func TestLastIndexAny(t *testing.T) { runIndexTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests) } +type IndexRuneTest struct { + s string + rune int + out int +} + +var indexRuneTests = []IndexRuneTest{ + {"a A x", 'A', 2}, + {"some_text=some_value", '=', 9}, + {"☺a", 'a', 3}, + {"a☻☺b", '☺', 4}, +} + +func TestIndexRune(t *testing.T) { + for _, test := range indexRuneTests { + if actual := IndexRune(test.s, test.rune); actual != test.out { + t.Errorf("IndexRune(%q,%d)= %v; want %v", test.s, test.rune, actual, test.out) + } + } +} + +const benchmarkString = "some_text=some☺value" + +func BenchmarkIndexRune(b *testing.B) { + if got := IndexRune(benchmarkString, '☺'); got != 14 { + panic("wrong index: got=" + strconv.Itoa(got)) + } + for i := 0; i < b.N; i++ { + IndexRune(benchmarkString, '☺') + } +} + +func BenchmarkIndexRuneFastPath(b *testing.B) { + if got := IndexRune(benchmarkString, 'v'); got != 17 { + panic("wrong index: got=" + strconv.Itoa(got)) + } + for i := 0; i < b.N; i++ { + IndexRune(benchmarkString, 'v') + } +} + +func BenchmarkIndex(b *testing.B) { + if got := Index(benchmarkString, "v"); got != 17 { + panic("wrong index: got=" + strconv.Itoa(got)) + } + for i := 0; i < b.N; i++ { + Index(benchmarkString, "v") + } +} + + type ExplodeTest struct { s string n int -- cgit v1.1