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
299cd38f
Commit
299cd38f
authored
Mar 29, 2010
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
strings.FIelds: slight simplification.
R=rsc CC=golang-dev
https://golang.org/cl/833042
parent
bd1690ce
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
4 deletions
+5
-4
strings.go
src/pkg/strings/strings.go
+5
-4
No files found.
src/pkg/strings/strings.go
View file @
299cd38f
...
@@ -161,6 +161,7 @@ func SplitAfter(s, sep string, n int) []string {
...
@@ -161,6 +161,7 @@ func SplitAfter(s, sep string, n int) []string {
// Fields splits the string s around each instance of one or more consecutive white space
// 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.
// characters, returning an array of substrings of s or an empty list if s contains only white space.
func
Fields
(
s
string
)
[]
string
{
func
Fields
(
s
string
)
[]
string
{
// First count the fields.
n
:=
0
n
:=
0
inField
:=
false
inField
:=
false
for
_
,
rune
:=
range
s
{
for
_
,
rune
:=
range
s
{
...
@@ -171,9 +172,10 @@ func Fields(s string) []string {
...
@@ -171,9 +172,10 @@ func Fields(s string) []string {
}
}
}
}
// Now create them.
a
:=
make
([]
string
,
n
)
a
:=
make
([]
string
,
n
)
na
:=
0
na
:=
0
fieldStart
:=
-
1
fieldStart
:=
-
1
// Set to -1 when looking for start of field.
for
i
,
rune
:=
range
s
{
for
i
,
rune
:=
range
s
{
if
unicode
.
IsSpace
(
rune
)
{
if
unicode
.
IsSpace
(
rune
)
{
if
fieldStart
>=
0
{
if
fieldStart
>=
0
{
...
@@ -185,11 +187,10 @@ func Fields(s string) []string {
...
@@ -185,11 +187,10 @@ func Fields(s string) []string {
fieldStart
=
i
fieldStart
=
i
}
}
}
}
if
fieldStart
!=
-
1
{
if
fieldStart
!=
-
1
{
// Last field might end at EOF.
a
[
na
]
=
s
[
fieldStart
:
]
a
[
na
]
=
s
[
fieldStart
:
]
na
++
}
}
return
a
[
0
:
na
]
return
a
}
}
// Join concatenates the elements of a to create a single string. The separator string
// Join concatenates the elements of a to create a single string. The separator 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