aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/strings
diff options
context:
space:
mode:
authorPeter Froehlich <peter.hans.froehlich@gmail.com>2009-12-02 20:47:38 -0800
committerRuss Cox <rsc@golang.org>2009-12-02 20:47:38 -0800
commit1eba218e4448a94d4319e73148d5c478a1e6685f (patch)
tree1487846fea3c3bc79149f8d492b5e343743228f3 /src/pkg/strings
parentc0efa07c65ab85dee65d1de4adc225a12756ab63 (diff)
downloadgo-1eba218e4448a94d4319e73148d5c478a1e6685f.tar.xz
Runes: turn string into []int
Split: fixed typo in documentation R=rsc, r, r1 https://golang.org/cl/157170
Diffstat (limited to 'src/pkg/strings')
-rw-r--r--src/pkg/strings/strings.go13
-rw-r--r--src/pkg/strings/strings_test.go45
2 files changed, 57 insertions, 1 deletions
diff --git a/src/pkg/strings/strings.go b/src/pkg/strings/strings.go
index 013af680a2..7be98e6c10 100644
--- a/src/pkg/strings/strings.go
+++ b/src/pkg/strings/strings.go
@@ -124,7 +124,7 @@ func genSplit(s, sep string, sepSave, n int) []string {
// Split splits the string s around each instance of sep, returning an array of substrings of s.
// If sep is empty, Split splits s after each UTF-8 sequence.
-// If n > 0, split Splits s into at most n substrings; the last substring will be the unsplit remainder.
+// If n > 0, Split splits s into at most n substrings; the last substring will be the unsplit remainder.
func Split(s, sep string, n int) []string { return genSplit(s, sep, 0, n) }
// SplitAfter splits the string s after each instance of sep, returning an array of substrings of s.
@@ -272,3 +272,14 @@ func Bytes(s string) []byte {
}
return b;
}
+
+// Runes returns a slice of runes (Unicode code points) equivalent to the string s.
+func Runes(s string) []int {
+ t := make([]int, utf8.RuneCountInString(s));
+ i := 0;
+ for _, r := range s {
+ t[i] = r;
+ i++;
+ }
+ return t;
+}
diff --git a/src/pkg/strings/strings_test.go b/src/pkg/strings/strings_test.go
index 1171db224e..ce77c5c2f2 100644
--- a/src/pkg/strings/strings_test.go
+++ b/src/pkg/strings/strings_test.go
@@ -370,3 +370,48 @@ func TestRepeat(t *testing.T) {
}
}
}
+
+func runesEqual(a, b []int) bool {
+ if len(a) != len(b) {
+ return false
+ }
+ for i, r := range a {
+ if r != b[i] {
+ return false
+ }
+ }
+ return true;
+}
+
+type RunesTest struct {
+ in string;
+ out []int;
+ lossy bool;
+}
+
+var RunesTests = []RunesTest{
+ RunesTest{"", []int{}, false},
+ RunesTest{" ", []int{32}, false},
+ RunesTest{"ABC", []int{65, 66, 67}, false},
+ RunesTest{"abc", []int{97, 98, 99}, false},
+ RunesTest{"\u65e5\u672c\u8a9e", []int{26085, 26412, 35486}, false},
+ RunesTest{"ab\x80c", []int{97, 98, 0xFFFD, 99}, true},
+ RunesTest{"ab\xc0c", []int{97, 98, 0xFFFD, 99}, true},
+}
+
+func TestRunes(t *testing.T) {
+ for _, tt := range RunesTests {
+ a := Runes(tt.in);
+ if !runesEqual(a, tt.out) {
+ t.Errorf("Runes(%q) = %v; want %v", tt.in, a, tt.out);
+ continue;
+ }
+ if !tt.lossy {
+ // can only test reassembly if we didn't lose information
+ s := string(a);
+ if s != tt.in {
+ t.Errorf("string(Runes(%q)) = %x; want %x", tt.in, s, tt.in)
+ }
+ }
+ }
+}