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
de228c0e
Commit
de228c0e
authored
Jul 21, 2010
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bytes: add Title
R=rsc CC=golang-dev
https://golang.org/cl/1872042
parent
a0d1c926
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
0 deletions
+68
-0
bytes.go
src/pkg/bytes/bytes.go
+46
-0
bytes_test.go
src/pkg/bytes/bytes_test.go
+22
-0
No files found.
src/pkg/bytes/bytes.go
View file @
de228c0e
...
@@ -332,6 +332,52 @@ func ToLower(s []byte) []byte { return Map(unicode.ToLower, s) }
...
@@ -332,6 +332,52 @@ 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.
// 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
)
}
func
ToTitle
(
s
[]
byte
)
[]
byte
{
return
Map
(
unicode
.
ToTitle
,
s
)
}
// isSeparator reports whether the rune could mark a word boundary.
// TODO: update when package unicode captures more of the properties.
func
isSeparator
(
rune
int
)
bool
{
// ASCII alphanumerics and underscore are not separators
if
rune
<=
0x7F
{
switch
{
case
'0'
<=
rune
&&
rune
<=
'9'
:
return
false
case
'a'
<=
rune
&&
rune
<=
'z'
:
return
false
case
'A'
<=
rune
&&
rune
<=
'Z'
:
return
false
case
rune
==
'_'
:
return
false
}
return
true
}
// Letters and digits are not separators
if
unicode
.
IsLetter
(
rune
)
||
unicode
.
IsDigit
(
rune
)
{
return
false
}
// Otherwise, all we can do for now is treat spaces as separators.
return
unicode
.
IsSpace
(
rune
)
}
// BUG(r): The rule Title uses for word boundaries does not handle Unicode punctuation properly.
// Title returns a copy of s with all Unicode letters that begin words
// mapped to their title case.
func
Title
(
s
[]
byte
)
[]
byte
{
// Use a closure here to remember state.
// Hackish but effective. Depends on Map scanning in order and calling
// the closure once per rune.
prev
:=
' '
return
Map
(
func
(
r
int
)
int
{
if
isSeparator
(
prev
)
{
prev
=
r
return
unicode
.
ToTitle
(
r
)
}
prev
=
r
return
r
},
s
)
}
// TrimLeftFunc returns a subslice of s by slicing off all leading UTF-8 encoded
// TrimLeftFunc returns a subslice of s by slicing off all leading UTF-8 encoded
// Unicode code points c that satisfy f(c).
// Unicode code points c that satisfy f(c).
func
TrimLeftFunc
(
s
[]
byte
,
f
func
(
r
int
)
bool
)
[]
byte
{
func
TrimLeftFunc
(
s
[]
byte
,
f
func
(
r
int
)
bool
)
[]
byte
{
...
...
src/pkg/bytes/bytes_test.go
View file @
de228c0e
...
@@ -685,3 +685,25 @@ func TestReplace(t *testing.T) {
...
@@ -685,3 +685,25 @@ func TestReplace(t *testing.T) {
}
}
}
}
}
}
type
TitleTest
struct
{
in
,
out
string
}
var
TitleTests
=
[]
TitleTest
{
TitleTest
{
""
,
""
},
TitleTest
{
"a"
,
"A"
},
TitleTest
{
" aaa aaa aaa "
,
" Aaa Aaa Aaa "
},
TitleTest
{
" Aaa Aaa Aaa "
,
" Aaa Aaa Aaa "
},
TitleTest
{
"123a456"
,
"123a456"
},
TitleTest
{
"double-blind"
,
"Double-Blind"
},
TitleTest
{
"ÿøû"
,
"Ÿøû"
},
}
func
TestTitle
(
t
*
testing
.
T
)
{
for
_
,
tt
:=
range
TitleTests
{
if
s
:=
string
(
Title
([]
byte
(
tt
.
in
)));
s
!=
tt
.
out
{
t
.
Errorf
(
"Title(%q) = %q, want %q"
,
tt
.
in
,
s
,
tt
.
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