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
2f80d328
Commit
2f80d328
authored
Sep 24, 2010
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
utf8.String: Slice(i,j)
R=rsc CC=golang-dev
https://golang.org/cl/2225048
parent
b1f44a12
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
1 deletion
+76
-1
string.go
src/pkg/utf8/string.go
+37
-1
string_test.go
src/pkg/utf8/string_test.go
+39
-0
No files found.
src/pkg/utf8/string.go
View file @
2f80d328
...
...
@@ -11,6 +11,8 @@ package utf8
// O(N) in the length of the string, but the overhead is less than always
// scanning from the beginning.
// If the string is ASCII, random access is O(1).
// Unlike the built-in string type, String has internal mutable state and
// is not thread-safe.
type
String
struct
{
str
string
numRunes
int
...
...
@@ -55,6 +57,39 @@ func (s *String) IsASCII() bool {
return
s
.
width
==
0
}
// Slice returns the string sliced at rune positions [i:j].
func
(
s
*
String
)
Slice
(
i
,
j
int
)
string
{
// ASCII is easy. Let the compiler catch the indexing error if there is one.
if
j
<
s
.
nonASCII
{
return
s
.
str
[
i
:
j
]
}
if
i
<
0
||
j
>
s
.
numRunes
||
i
>
j
{
panic
(
sliceOutOfRange
)
}
if
i
==
j
{
return
""
}
// For non-ASCII, after At(i), bytePos is always the position of the indexed character.
var
low
,
high
int
switch
{
case
i
<
s
.
nonASCII
:
low
=
i
case
i
==
s
.
numRunes
:
low
=
len
(
s
.
str
)
default
:
s
.
At
(
i
)
low
=
s
.
bytePos
}
switch
{
case
j
==
s
.
numRunes
:
high
=
len
(
s
.
str
)
default
:
s
.
At
(
j
)
high
=
s
.
bytePos
}
return
s
.
str
[
low
:
high
]
}
// At returns the rune with index i in the String. The sequence of runes is the same
// as iterating over the contents with a "for range" clause.
func
(
s
*
String
)
At
(
i
int
)
int
{
...
...
@@ -163,4 +198,5 @@ func (err error) String() string {
func
(
err
error
)
RunTimeError
()
{
}
var
outOfRange
=
error
(
"utf8.String: index out of Range"
)
var
outOfRange
=
error
(
"utf8.String: index out of range"
)
var
sliceOutOfRange
=
error
(
"utf8.String: slice index out of range"
)
src/pkg/utf8/string_test.go
View file @
2f80d328
...
...
@@ -68,3 +68,42 @@ func TestRandomAccess(t *testing.T) {
}
}
}
func
TestRandomSliceAccess
(
t
*
testing
.
T
)
{
for
_
,
s
:=
range
testStrings
{
if
len
(
s
)
==
0
||
s
[
0
]
==
'\x80'
{
// the bad-UTF-8 string fools this simple test
continue
}
runes
:=
[]
int
(
s
)
str
:=
NewString
(
s
)
if
str
.
RuneCount
()
!=
len
(
runes
)
{
t
.
Error
(
"%s: expected %d runes; got %d"
,
s
,
len
(
runes
),
str
.
RuneCount
())
break
}
for
k
:=
0
;
k
<
randCount
;
k
++
{
i
:=
rand
.
Intn
(
len
(
runes
))
j
:=
rand
.
Intn
(
len
(
runes
)
+
1
)
if
i
>
j
{
// include empty strings
continue
}
expect
:=
string
(
runes
[
i
:
j
])
got
:=
str
.
Slice
(
i
,
j
)
if
got
!=
expect
{
t
.
Errorf
(
"%s[%d:%d]: expected %q got %q"
,
s
,
i
,
j
,
expect
,
got
)
}
}
}
}
func
TestLimitSliceAccess
(
t
*
testing
.
T
)
{
for
_
,
s
:=
range
testStrings
{
str
:=
NewString
(
s
)
if
str
.
Slice
(
0
,
0
)
!=
""
{
t
.
Error
(
"failure with empty slice at beginning"
)
}
nr
:=
RuneCountInString
(
s
)
if
str
.
Slice
(
nr
,
nr
)
!=
""
{
t
.
Error
(
"failure with empty slice at end"
)
}
}
}
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