summaryrefslogtreecommitdiff
path: root/src/lib/strings.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2008-12-04 21:00:34 -0800
committerRuss Cox <rsc@golang.org>2008-12-04 21:00:34 -0800
commit966045873e54da1fe6f38d92f5b7850b8913c97b (patch)
tree40924d98cce298fa84801b75f01186399c0ef609 /src/lib/strings.go
parent1fe7cdb6b5aef2617326746a4f01c8cd6476464f (diff)
downloadgolang-966045873e54da1fe6f38d92f5b7850b8913c97b.tar.gz
strings.utflen -> utf8.RuneCount, RuneCountInString
R=r DELTA=94 (52 added, 33 deleted, 9 changed) OCL=20547 CL=20552
Diffstat (limited to 'src/lib/strings.go')
-rw-r--r--src/lib/strings.go29
1 files changed, 8 insertions, 21 deletions
diff --git a/src/lib/strings.go b/src/lib/strings.go
index 433e50064..c171214db 100644
--- a/src/lib/strings.go
+++ b/src/lib/strings.go
@@ -4,30 +4,17 @@
package strings
-// Count UTF-8 sequences in s.
-// Assumes s is well-formed.
-export func utflen(s string) int {
- n := 0;
- for i := 0; i < len(s); i++ {
- if s[i]&0xC0 != 0x80 {
- n++
- }
- }
- return n
-}
+import "utf8"
// Split string into array of UTF-8 sequences (still strings)
export func explode(s string) *[]string {
- a := new([]string, utflen(s));
+ a := new([]string, utf8.RuneCountInString(s, 0, len(s)));
j := 0;
+ var size, rune int;
for i := 0; i < len(a); i++ {
- ej := j;
- ej++;
- for ej < len(s) && (s[ej]&0xC0) == 0x80 {
- ej++
- }
- a[i] = s[j:ej];
- j = ej
+ rune, size = utf8.DecodeRuneInString(s, j);
+ a[i] = string(rune);
+ j += size;
}
return a
}
@@ -35,7 +22,7 @@ export func explode(s string) *[]string {
// Count non-overlapping instances of sep in s.
export func count(s, sep string) int {
if sep == "" {
- return utflen(s)+1
+ return utf8.RuneCountInString(s, 0, len(s))+1
}
c := sep[0];
n := 0;
@@ -83,7 +70,7 @@ export func split(s, sep string) *[]string {
a[na] = s[start:len(s)];
return a
}
-
+
// Join list of strings with separators between them.
export func join(a *[]string, sep string) string {
if len(a) == 0 {