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
c68ae9d4
Commit
c68ae9d4
authored
Sep 26, 2011
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bytes: add EqualFold
R=golang-dev, r, r CC=golang-dev
https://golang.org/cl/5123047
parent
4bdf1fc0
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
1 deletion
+85
-1
bytes.go
src/pkg/bytes/bytes.go
+55
-0
bytes_test.go
src/pkg/bytes/bytes_test.go
+28
-0
strings.go
src/pkg/strings/strings.go
+2
-1
No files found.
src/pkg/bytes/bytes.go
View file @
c68ae9d4
...
...
@@ -608,3 +608,58 @@ func Replace(s, old, new []byte, n int) []byte {
w
+=
copy
(
t
[
w
:
],
s
[
start
:
])
return
t
[
0
:
w
]
}
// EqualFold reports whether s and t, interpreted as UTF-8 strings,
// are equal under Unicode case-folding.
func
EqualFold
(
s
,
t
[]
byte
)
bool
{
for
len
(
s
)
!=
0
&&
len
(
t
)
!=
0
{
// Extract first rune from each.
var
sr
,
tr
int
if
s
[
0
]
<
utf8
.
RuneSelf
{
sr
,
s
=
int
(
s
[
0
]),
s
[
1
:
]
}
else
{
r
,
size
:=
utf8
.
DecodeRune
(
s
)
sr
,
s
=
r
,
s
[
size
:
]
}
if
t
[
0
]
<
utf8
.
RuneSelf
{
tr
,
t
=
int
(
t
[
0
]),
t
[
1
:
]
}
else
{
r
,
size
:=
utf8
.
DecodeRune
(
t
)
tr
,
t
=
r
,
t
[
size
:
]
}
// If they match, keep going; if not, return false.
// Easy case.
if
tr
==
sr
{
continue
}
// Make sr < tr to simplify what follows.
if
tr
<
sr
{
tr
,
sr
=
sr
,
tr
}
// Fast check for ASCII.
if
tr
<
utf8
.
RuneSelf
&&
'A'
<=
sr
&&
sr
<=
'Z'
{
// ASCII, and sr is upper case. tr must be lower case.
if
tr
==
sr
+
'a'
-
'A'
{
continue
}
return
false
}
// General case. SimpleFold(x) returns the next equivalent rune > x
// or wraps around to smaller values.
r
:=
unicode
.
SimpleFold
(
sr
)
for
r
!=
sr
&&
r
<
tr
{
r
=
unicode
.
SimpleFold
(
r
)
}
if
r
==
tr
{
continue
}
return
false
}
// One string is empty. Are both?
return
len
(
s
)
==
len
(
t
)
}
src/pkg/bytes/bytes_test.go
View file @
c68ae9d4
...
...
@@ -862,3 +862,31 @@ func TestTitle(t *testing.T) {
}
}
}
var
EqualFoldTests
=
[]
struct
{
s
,
t
string
out
bool
}{
{
"abc"
,
"abc"
,
true
},
{
"ABcd"
,
"ABcd"
,
true
},
{
"123abc"
,
"123ABC"
,
true
},
{
"αβδ"
,
"ΑΒΔ"
,
true
},
{
"abc"
,
"xyz"
,
false
},
{
"abc"
,
"XYZ"
,
false
},
{
"abcdefghijk"
,
"abcdefghijX"
,
false
},
{
"abcdefghijk"
,
"abcdefghij
\u212A
"
,
true
},
{
"abcdefghijK"
,
"abcdefghij
\u212A
"
,
true
},
{
"abcdefghijkz"
,
"abcdefghij
\u212A
y"
,
false
},
{
"abcdefghijKz"
,
"abcdefghij
\u212A
y"
,
false
},
}
func
TestEqualFold
(
t
*
testing
.
T
)
{
for
_
,
tt
:=
range
EqualFoldTests
{
if
out
:=
EqualFold
([]
byte
(
tt
.
s
),
[]
byte
(
tt
.
t
));
out
!=
tt
.
out
{
t
.
Errorf
(
"EqualFold(%#q, %#q) = %v, want %v"
,
tt
.
s
,
tt
.
t
,
out
,
tt
.
out
)
}
if
out
:=
EqualFold
([]
byte
(
tt
.
t
),
[]
byte
(
tt
.
s
));
out
!=
tt
.
out
{
t
.
Errorf
(
"EqualFold(%#q, %#q) = %v, want %v"
,
tt
.
t
,
tt
.
s
,
out
,
tt
.
out
)
}
}
}
src/pkg/strings/strings.go
View file @
c68ae9d4
...
...
@@ -584,7 +584,8 @@ func Replace(s, old, new string, n int) string {
return
string
(
t
[
0
:
w
])
}
// EqualFold returns true if s and t are equal under Unicode case-folding.
// EqualFold reports whether s and t, interpreted as UTF-8 strings,
// are equal under Unicode case-folding.
func
EqualFold
(
s
,
t
string
)
bool
{
for
s
!=
""
&&
t
!=
""
{
// Extract first rune from each 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