Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
1eba218e
Commit
1eba218e
authored
Dec 03, 2009
by
Peter Froehlich
Committed by
Russ Cox
Dec 03, 2009
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Runes: turn string into []int
Split: fixed typo in documentation R=rsc, r, r1
https://golang.org/cl/157170
parent
c0efa07c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
116 additions
and
1 deletion
+116
-1
bytes.go
src/pkg/bytes/bytes.go
+13
-0
bytes_test.go
src/pkg/bytes/bytes_test.go
+46
-0
strings.go
src/pkg/strings/strings.go
+12
-1
strings_test.go
src/pkg/strings/strings_test.go
+45
-0
No files found.
src/pkg/bytes/bytes.go
View file @
1eba218e
...
...
@@ -333,3 +333,16 @@ func AddByte(s []byte, t byte) []byte {
s
[
lens
]
=
t
;
return
s
;
}
// Runes returns a slice of runes (Unicode code points) equivalent to s.
func
Runes
(
s
[]
byte
)
[]
int
{
t
:=
make
([]
int
,
utf8
.
RuneCount
(
s
));
i
:=
0
;
for
len
(
s
)
>
0
{
r
,
l
:=
utf8
.
DecodeRune
(
s
);
t
[
i
]
=
r
;
i
++
;
s
=
s
[
l
:
];
}
return
t
;
}
src/pkg/bytes/bytes_test.go
View file @
1eba218e
...
...
@@ -400,3 +400,49 @@ 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
\x80
c"
,
[]
int
{
97
,
98
,
0xFFFD
,
99
},
true
},
RunesTest
{
"ab
\xc0
c"
,
[]
int
{
97
,
98
,
0xFFFD
,
99
},
true
},
}
func
TestRunes
(
t
*
testing
.
T
)
{
for
_
,
tt
:=
range
RunesTests
{
tin
:=
strings
.
Bytes
(
tt
.
in
);
a
:=
Runes
(
tin
);
if
!
runesEqual
(
a
,
tt
.
out
)
{
t
.
Errorf
(
"Runes(%q) = %v; want %v"
,
tin
,
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"
,
tin
,
s
,
tin
)
}
}
}
}
src/pkg/strings/strings.go
View file @
1eba218e
...
...
@@ -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 S
plits s into at most n substrings; the last substring will be the unsplit remainder.
// If n > 0,
Split s
plits 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
;
}
src/pkg/strings/strings_test.go
View file @
1eba218e
...
...
@@ -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
\x80
c"
,
[]
int
{
97
,
98
,
0xFFFD
,
99
},
true
},
RunesTest
{
"ab
\xc0
c"
,
[]
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
)
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment