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
7f501c06
Commit
7f501c06
authored
Dec 16, 2009
by
Andrey Mirtchovski
Committed by
Russ Cox
Dec 16, 2009
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bytes, strings: add new function Fields
R=rsc, r, phf CC=golang-dev
https://golang.org/cl/170046
parent
d5bcf7bf
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
132 additions
and
0 deletions
+132
-0
bytes.go
src/pkg/bytes/bytes.go
+38
-0
bytes_test.go
src/pkg/bytes/bytes_test.go
+30
-0
strings.go
src/pkg/strings/strings.go
+34
-0
strings_test.go
src/pkg/strings/strings_test.go
+30
-0
No files found.
src/pkg/bytes/bytes.go
View file @
7f501c06
...
...
@@ -163,6 +163,44 @@ func SplitAfter(s, sep []byte, n int) [][]byte {
return
genSplit
(
s
,
sep
,
len
(
sep
),
n
)
}
// Fields splits the array s around each instance of one or more consecutive white space
// characters, returning a slice of subarrays of s or an empty list if s contains only white space.
func
Fields
(
s
[]
byte
)
[][]
byte
{
n
:=
0
inField
:=
false
for
i
:=
0
;
i
<
len
(
s
);
{
rune
,
size
:=
utf8
.
DecodeRune
(
s
[
i
:
])
wasInField
:=
inField
inField
=
!
unicode
.
IsSpace
(
rune
)
if
inField
&&
!
wasInField
{
n
++
}
i
+=
size
}
a
:=
make
([][]
byte
,
n
)
na
:=
0
fieldStart
:=
-
1
for
i
:=
0
;
i
<=
len
(
s
)
&&
na
<
n
;
{
rune
,
size
:=
utf8
.
DecodeRune
(
s
[
i
:
])
if
fieldStart
<
0
&&
size
>
0
&&
!
unicode
.
IsSpace
(
rune
)
{
fieldStart
=
i
i
+=
size
continue
}
if
fieldStart
>=
0
&&
(
size
==
0
||
unicode
.
IsSpace
(
rune
))
{
a
[
na
]
=
s
[
fieldStart
:
i
]
na
++
fieldStart
=
-
1
}
if
size
==
0
{
break
}
i
+=
size
}
return
a
[
0
:
na
]
}
// Join concatenates the elements of a to create a single byte array. The separator
// sep is placed between elements in the resulting array.
func
Join
(
a
[][]
byte
,
sep
[]
byte
)
[]
byte
{
...
...
src/pkg/bytes/bytes_test.go
View file @
7f501c06
...
...
@@ -254,6 +254,36 @@ func TestSplitAfter(t *testing.T) {
}
}
type
FieldsTest
struct
{
s
string
a
[]
string
}
var
fieldstests
=
[]
FieldsTest
{
FieldsTest
{
""
,
[]
string
{}},
FieldsTest
{
" "
,
[]
string
{}},
FieldsTest
{
"
\t
"
,
[]
string
{}},
FieldsTest
{
" abc "
,
[]
string
{
"abc"
}},
FieldsTest
{
"1 2 3 4"
,
[]
string
{
"1"
,
"2"
,
"3"
,
"4"
}},
FieldsTest
{
"1 2 3 4"
,
[]
string
{
"1"
,
"2"
,
"3"
,
"4"
}},
FieldsTest
{
"1
\t\t
2
\t\t
3
\t
4"
,
[]
string
{
"1"
,
"2"
,
"3"
,
"4"
}},
FieldsTest
{
"1
\u2000
2
\u2001
3
\u2002
4"
,
[]
string
{
"1"
,
"2"
,
"3"
,
"4"
}},
FieldsTest
{
"
\u2000\u2001\u2002
"
,
[]
string
{}},
FieldsTest
{
"
\n
™
\t
™
\n
"
,
[]
string
{
"™"
,
"™"
}},
FieldsTest
{
faces
,
[]
string
{
faces
}},
}
func
TestFields
(
t
*
testing
.
T
)
{
for
_
,
tt
:=
range
fieldstests
{
a
:=
Fields
(
strings
.
Bytes
(
tt
.
s
))
result
:=
arrayOfString
(
a
)
if
!
eq
(
result
,
tt
.
a
)
{
t
.
Errorf
(
"Fields(%q) = %v; want %v"
,
tt
.
s
,
a
,
tt
.
a
)
continue
}
}
}
// Test case for any function which accepts and returns a byte array.
// For ease of creation, we write the byte arrays as strings.
type
StringTest
struct
{
...
...
src/pkg/strings/strings.go
View file @
7f501c06
...
...
@@ -134,6 +134,40 @@ func SplitAfter(s, sep string, n int) []string {
return
genSplit
(
s
,
sep
,
len
(
sep
),
n
)
}
// Fields splits the string s around each instance of one or more consecutive white space
// characters, returning an array of substrings of s or an empty list if s contains only white space.
func
Fields
(
s
string
)
[]
string
{
n
:=
0
inField
:=
false
for
_
,
rune
:=
range
s
{
wasInField
:=
inField
inField
=
!
unicode
.
IsSpace
(
rune
)
if
inField
&&
!
wasInField
{
n
++
}
}
a
:=
make
([]
string
,
n
)
na
:=
0
fieldStart
:=
-
1
for
i
,
rune
:=
range
s
{
if
unicode
.
IsSpace
(
rune
)
{
if
fieldStart
>=
0
{
a
[
na
]
=
s
[
fieldStart
:
i
]
na
++
fieldStart
=
-
1
}
}
else
if
fieldStart
==
-
1
{
fieldStart
=
i
}
}
if
fieldStart
!=
-
1
{
a
[
na
]
=
s
[
fieldStart
:
]
na
++
}
return
a
[
0
:
na
]
}
// Join concatenates the elements of a to create a single string. The separator string
// sep is placed between elements in the resulting string.
func
Join
(
a
[]
string
,
sep
string
)
string
{
...
...
src/pkg/strings/strings_test.go
View file @
7f501c06
...
...
@@ -180,6 +180,36 @@ func TestSplitAfter(t *testing.T) {
}
}
type
FieldsTest
struct
{
s
string
a
[]
string
}
var
fieldstests
=
[]
FieldsTest
{
FieldsTest
{
""
,
[]
string
{}},
FieldsTest
{
" "
,
[]
string
{}},
FieldsTest
{
"
\t
"
,
[]
string
{}},
FieldsTest
{
" abc "
,
[]
string
{
"abc"
}},
FieldsTest
{
"1 2 3 4"
,
[]
string
{
"1"
,
"2"
,
"3"
,
"4"
}},
FieldsTest
{
"1 2 3 4"
,
[]
string
{
"1"
,
"2"
,
"3"
,
"4"
}},
FieldsTest
{
"1
\t\t
2
\t\t
3
\t
4"
,
[]
string
{
"1"
,
"2"
,
"3"
,
"4"
}},
FieldsTest
{
"1
\u2000
2
\u2001
3
\u2002
4"
,
[]
string
{
"1"
,
"2"
,
"3"
,
"4"
}},
FieldsTest
{
"
\u2000\u2001\u2002
"
,
[]
string
{}},
FieldsTest
{
"
\n
™
\t
™
\n
"
,
[]
string
{
"™"
,
"™"
}},
FieldsTest
{
faces
,
[]
string
{
faces
}},
}
func
TestFields
(
t
*
testing
.
T
)
{
for
_
,
tt
:=
range
fieldstests
{
a
:=
Fields
(
tt
.
s
)
if
!
eq
(
a
,
tt
.
a
)
{
t
.
Errorf
(
"Fields(%q) = %v; want %v"
,
tt
.
s
,
a
,
tt
.
a
)
continue
}
}
}
// Test case for any function which accepts and returns a single string.
type
StringTest
struct
{
in
,
out
string
...
...
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