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
4a3cb1ad
Commit
4a3cb1ad
authored
May 19, 2010
by
Michael Hoisie
Committed by
Russ Cox
May 19, 2010
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bytes: add Trim, TrimLeft, TrimRight, and generic functions
R=rsc, r CC=golang-dev
https://golang.org/cl/946045
parent
d2aa7411
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
134 additions
and
20 deletions
+134
-20
bytes.go
src/pkg/bytes/bytes.go
+65
-20
bytes_test.go
src/pkg/bytes/bytes_test.go
+69
-0
No files found.
src/pkg/bytes/bytes.go
View file @
4a3cb1ad
...
...
@@ -330,40 +330,85 @@ func ToLower(s []byte) []byte { return Map(unicode.ToLower, s) }
// ToTitle returns a copy of the byte array s with all Unicode letters mapped to their title case.
func
ToTitle
(
s
[]
byte
)
[]
byte
{
return
Map
(
unicode
.
ToTitle
,
s
)
}
// Trim returns a slice of the string s, with all leading and trailing white space
// removed, as defined by Unicode. The slice is is interpreted as UTF-8 encoded
// Unicode code points.
func
TrimSpace
(
s
[]
byte
)
[]
byte
{
start
,
end
:=
0
,
len
(
s
)
for
start
<
end
{
wid
:=
1
// TrimLeftFunc returns a subslice of s by slicing off all leading UTF-8 encoded
// Unicode code points c that satisfy f(c).
func
TrimLeftFunc
(
s
[]
byte
,
f
func
(
r
int
)
bool
)
[]
byte
{
var
start
,
wid
int
for
start
=
0
;
start
<
len
(
s
);
start
+=
wid
{
wid
=
1
rune
:=
int
(
s
[
start
])
if
rune
>=
utf8
.
RuneSelf
{
rune
,
wid
=
utf8
.
DecodeRune
(
s
[
start
:
end
])
rune
,
wid
=
utf8
.
DecodeRune
(
s
[
start
:
])
}
if
!
unicode
.
IsSpace
(
rune
)
{
if
!
f
(
rune
)
{
break
}
start
+=
wid
}
for
start
<
end
{
wid
:=
1
rune
:=
int
(
s
[
end
-
1
])
return
s
[
start
:
]
}
// TrimRightFunc returns a subslice of s by slicing off all trailing UTF-8
// encoded Unicode code points c that satisfy f(c).
func
TrimRightFunc
(
s
[]
byte
,
f
func
(
r
int
)
bool
)
[]
byte
{
var
end
,
wid
int
for
end
=
len
(
s
);
end
>
0
;
end
-=
wid
{
wid
=
1
rune
:=
int
(
s
[
end
-
wid
])
if
rune
>=
utf8
.
RuneSelf
{
// Back up
carefully looking
for beginning of rune. Mustn't pass start.
for
wid
=
2
;
start
<=
end
-
wid
&&
!
utf8
.
RuneStart
(
s
[
end
-
wid
]);
wid
++
{
// Back up
& look
for beginning of rune. Mustn't pass start.
for
wid
=
2
;
end
-
wid
>=
0
&&
!
utf8
.
RuneStart
(
s
[
end
-
wid
]);
wid
++
{
}
if
start
>
end
-
wid
{
// invalid UTF-8 sequence; stop processing
return
s
[
start
:
end
]
if
end
-
wid
<
0
{
// invalid UTF-8 sequence; stop processing
break
}
rune
,
wid
=
utf8
.
DecodeRune
(
s
[
end
-
wid
:
end
])
}
if
!
unicode
.
IsSpace
(
rune
)
{
if
!
f
(
rune
)
{
break
}
end
-=
wid
}
return
s
[
start
:
end
]
return
s
[
0
:
end
]
}
// TrimFunc returns a subslice of s by slicing off all leading and trailing
// UTF-8 encoded Unicode code points c that satisfy f(c).
func
TrimFunc
(
s
[]
byte
,
f
func
(
r
int
)
bool
)
[]
byte
{
return
TrimRightFunc
(
TrimLeftFunc
(
s
,
f
),
f
)
}
func
makeCutsetFunc
(
cutset
string
)
func
(
rune
int
)
bool
{
return
func
(
rune
int
)
bool
{
for
_
,
c
:=
range
cutset
{
if
c
==
rune
{
return
true
}
}
return
false
}
}
// Trim returns a subslice of s by slicing off all leading and
// trailing UTF-8 encoded Unicode code points contained in cutset.
func
Trim
(
s
[]
byte
,
cutset
string
)
[]
byte
{
return
TrimFunc
(
s
,
makeCutsetFunc
(
cutset
))
}
// TrimLeft returns a subslice of s by slicing off all leading
// UTF-8 encoded Unicode code points contained in cutset.
func
TrimLeft
(
s
[]
byte
,
cutset
string
)
[]
byte
{
return
TrimLeftFunc
(
s
,
makeCutsetFunc
(
cutset
))
}
// TrimRight returns a subslice of s by slicing off all trailing
// UTF-8 encoded Unicode code points that are contained in cutset.
func
TrimRight
(
s
[]
byte
,
cutset
string
)
[]
byte
{
return
TrimRightFunc
(
s
,
makeCutsetFunc
(
cutset
))
}
// TrimSpace returns a subslice of s by slicing off all leading and
// trailing white space, as as defined by Unicode.
func
TrimSpace
(
s
[]
byte
)
[]
byte
{
return
TrimFunc
(
s
,
unicode
.
IsSpace
)
}
// How big to make a byte array when growing.
...
...
src/pkg/bytes/bytes_test.go
View file @
4a3cb1ad
...
...
@@ -576,3 +576,72 @@ func TestRunes(t *testing.T) {
}
}
}
type
TrimTest
struct
{
f
func
([]
byte
,
string
)
[]
byte
in
,
cutset
,
out
string
}
var
trimTests
=
[]
TrimTest
{
TrimTest
{
Trim
,
"abba"
,
"a"
,
"bb"
},
TrimTest
{
Trim
,
"abba"
,
"ab"
,
""
},
TrimTest
{
TrimLeft
,
"abba"
,
"ab"
,
""
},
TrimTest
{
TrimRight
,
"abba"
,
"ab"
,
""
},
TrimTest
{
TrimLeft
,
"abba"
,
"a"
,
"bba"
},
TrimTest
{
TrimRight
,
"abba"
,
"a"
,
"abb"
},
TrimTest
{
Trim
,
"<tag>"
,
"<>"
,
"tag"
},
TrimTest
{
Trim
,
"* listitem"
,
" *"
,
"listitem"
},
TrimTest
{
Trim
,
`"quote"`
,
`"`
,
"quote"
},
TrimTest
{
Trim
,
"
\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F
"
,
"
\u2C6F
"
,
"
\u0250\u0250
"
},
//empty string tests
TrimTest
{
Trim
,
"abba"
,
""
,
"abba"
},
TrimTest
{
Trim
,
""
,
"123"
,
""
},
TrimTest
{
Trim
,
""
,
""
,
""
},
TrimTest
{
TrimLeft
,
"abba"
,
""
,
"abba"
},
TrimTest
{
TrimLeft
,
""
,
"123"
,
""
},
TrimTest
{
TrimLeft
,
""
,
""
,
""
},
TrimTest
{
TrimRight
,
"abba"
,
""
,
"abba"
},
TrimTest
{
TrimRight
,
""
,
"123"
,
""
},
TrimTest
{
TrimRight
,
""
,
""
,
""
},
}
func
TestTrim
(
t
*
testing
.
T
)
{
for
_
,
tc
:=
range
trimTests
{
actual
:=
string
(
tc
.
f
([]
byte
(
tc
.
in
),
tc
.
cutset
))
var
name
string
switch
tc
.
f
{
case
Trim
:
name
=
"Trim"
case
TrimLeft
:
name
=
"TrimLeft"
case
TrimRight
:
name
=
"TrimRight"
default
:
t
.
Error
(
"Undefined trim function"
)
}
if
actual
!=
tc
.
out
{
t
.
Errorf
(
"%s(%q, %q) = %q; want %q"
,
name
,
tc
.
in
,
tc
.
cutset
,
actual
,
tc
.
out
)
}
}
}
type
TrimFuncTest
struct
{
f
func
(
r
int
)
bool
name
,
in
,
out
string
}
var
trimFuncTests
=
[]
TrimFuncTest
{
TrimFuncTest
{
unicode
.
IsSpace
,
"IsSpace"
,
space
+
" hello "
+
space
,
"hello"
},
TrimFuncTest
{
unicode
.
IsDigit
,
"IsDigit"
,
"
\u0e50\u0e52
12hello34
\u0e50\u0e51
"
,
"hello"
},
TrimFuncTest
{
unicode
.
IsUpper
,
"IsUpper"
,
"
\u2C6F\u2C6F\u2C6F\u2C6F
ABCDhelloEF
\u2C6F\u2C6F
GH
\u2C6F\u2C6F
"
,
"hello"
},
}
func
TestTrimFunc
(
t
*
testing
.
T
)
{
for
_
,
tc
:=
range
trimFuncTests
{
actual
:=
string
(
TrimFunc
([]
byte
(
tc
.
in
),
tc
.
f
))
if
actual
!=
tc
.
out
{
t
.
Errorf
(
"TrimFunc(%q, %q) = %q; want %q"
,
tc
.
in
,
tc
.
name
,
actual
,
tc
.
out
)
}
}
}
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