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
2b95cfba
Commit
2b95cfba
authored
Oct 07, 2011
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
utf8: add Valid and ValidString
R=r, rsc, alex.brainman CC=golang-dev
https://golang.org/cl/5234041
parent
f198bbc8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
0 deletions
+66
-0
utf8.go
src/pkg/utf8/utf8.go
+37
-0
utf8_test.go
src/pkg/utf8/utf8_test.go
+29
-0
No files found.
src/pkg/utf8/utf8.go
View file @
2b95cfba
...
...
@@ -354,3 +354,40 @@ func RuneCountInString(s string) (n int) {
// an encoded rune. Second and subsequent bytes always have the top
// two bits set to 10.
func
RuneStart
(
b
byte
)
bool
{
return
b
&
0xC0
!=
0x80
}
// Valid reports whether p consists entirely of valid UTF-8-encoded runes.
func
Valid
(
p
[]
byte
)
bool
{
i
:=
0
for
i
<
len
(
p
)
{
if
p
[
i
]
<
RuneSelf
{
i
++
}
else
{
_
,
size
:=
DecodeRune
(
p
[
i
:
])
if
size
==
1
{
// All valid runes of size of 1 (those
// below RuneSelf) were handled above.
// This must be a RuneError.
return
false
}
i
+=
size
}
}
return
true
}
// ValidString reports whether s consists entirely of valid UTF-8-encoded runes.
func
ValidString
(
s
string
)
bool
{
for
i
,
r
:=
range
s
{
if
r
==
RuneError
{
// The RuneError value can be an error
// sentinel value (if it's size 1) or the same
// value encoded properly. Decode it to see if
// it's the 1 byte sentinel value.
_
,
size
:=
DecodeRuneInString
(
s
[
i
:
])
if
size
==
1
{
return
false
}
}
}
return
true
}
src/pkg/utf8/utf8_test.go
View file @
2b95cfba
...
...
@@ -274,6 +274,35 @@ func TestRuneCount(t *testing.T) {
}
}
type
ValidTest
struct
{
in
string
out
bool
}
var
validTests
=
[]
ValidTest
{
{
""
,
true
},
{
"a"
,
true
},
{
"abc"
,
true
},
{
"Ж"
,
true
},
{
"ЖЖ"
,
true
},
{
"брэд-ЛГТМ"
,
true
},
{
"☺☻☹"
,
true
},
{
string
([]
byte
{
66
,
250
}),
false
},
{
string
([]
byte
{
66
,
250
,
67
}),
false
},
{
"a
\uFFFD
b"
,
true
},
}
func
TestValid
(
t
*
testing
.
T
)
{
for
i
,
tt
:=
range
validTests
{
if
Valid
([]
byte
(
tt
.
in
))
!=
tt
.
out
{
t
.
Errorf
(
"%d. Valid(%q) = %v; want %v"
,
i
,
tt
.
in
,
!
tt
.
out
,
tt
.
out
)
}
if
ValidString
(
tt
.
in
)
!=
tt
.
out
{
t
.
Errorf
(
"%d. ValidString(%q) = %v; want %v"
,
i
,
tt
.
in
,
!
tt
.
out
,
tt
.
out
)
}
}
}
func
BenchmarkRuneCountTenASCIIChars
(
b
*
testing
.
B
)
{
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
RuneCountInString
(
"0123456789"
)
...
...
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