mirror of
https://github.com/golang/go
synced 2024-11-23 04:40:09 -07:00
unicode: add "In" function to test membership of a rune
The existing function, IsOneOf, is hard to use. Since the slice comes before the rune, in parallelism with the other Is functions, the slice is clumsy to build. This CL adds a nicer-signatured In function of equivalent functionality (its implementation is identical) that's much easier to use. Compare: unicode.IsOneOf([]*unicode.RangeTable{unicode.Letter, unicode.Number}, r) unicode.In(r, unicode.Letter, unicode.Number) R=golang-dev, adg CC=golang-dev https://golang.org/cl/11672044
This commit is contained in:
parent
a0a45bbb71
commit
6a801539c5
@ -39,7 +39,7 @@ func IsGraphic(r rune) bool {
|
|||||||
if uint32(r) <= MaxLatin1 {
|
if uint32(r) <= MaxLatin1 {
|
||||||
return properties[uint8(r)]&pg != 0
|
return properties[uint8(r)]&pg != 0
|
||||||
}
|
}
|
||||||
return IsOneOf(GraphicRanges, r)
|
return In(r, GraphicRanges...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsPrint reports whether the rune is defined as printable by Go. Such
|
// IsPrint reports whether the rune is defined as printable by Go. Such
|
||||||
@ -51,12 +51,23 @@ func IsPrint(r rune) bool {
|
|||||||
if uint32(r) <= MaxLatin1 {
|
if uint32(r) <= MaxLatin1 {
|
||||||
return properties[uint8(r)]&pp != 0
|
return properties[uint8(r)]&pp != 0
|
||||||
}
|
}
|
||||||
return IsOneOf(PrintRanges, r)
|
return In(r, PrintRanges...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsOneOf reports whether the rune is a member of one of the ranges.
|
// IsOneOf reports whether the rune is a member of one of the ranges.
|
||||||
func IsOneOf(set []*RangeTable, r rune) bool {
|
// The function "In" provides a nicer signature and should be used in preference to IsOneOf.
|
||||||
for _, inside := range set {
|
func IsOneOf(ranges []*RangeTable, r rune) bool {
|
||||||
|
for _, inside := range ranges {
|
||||||
|
if Is(inside, r) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// In reports whether the rune is a member of one of the ranges.
|
||||||
|
func In(r rune, ranges ...*RangeTable) bool {
|
||||||
|
for _, inside := range ranges {
|
||||||
if Is(inside, r) {
|
if Is(inside, r) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,7 @@ func TestNumberLatin1(t *testing.T) {
|
|||||||
func TestIsPrintLatin1(t *testing.T) {
|
func TestIsPrintLatin1(t *testing.T) {
|
||||||
for i := rune(0); i <= MaxLatin1; i++ {
|
for i := rune(0); i <= MaxLatin1; i++ {
|
||||||
got := IsPrint(i)
|
got := IsPrint(i)
|
||||||
want := IsOneOf(PrintRanges, i)
|
want := In(i, PrintRanges...)
|
||||||
if i == ' ' {
|
if i == ' ' {
|
||||||
want = true
|
want = true
|
||||||
}
|
}
|
||||||
@ -84,7 +84,7 @@ func TestIsPrintLatin1(t *testing.T) {
|
|||||||
func TestIsGraphicLatin1(t *testing.T) {
|
func TestIsGraphicLatin1(t *testing.T) {
|
||||||
for i := rune(0); i <= MaxLatin1; i++ {
|
for i := rune(0); i <= MaxLatin1; i++ {
|
||||||
got := IsGraphic(i)
|
got := IsGraphic(i)
|
||||||
want := IsOneOf(GraphicRanges, i)
|
want := In(i, GraphicRanges...)
|
||||||
if got != want {
|
if got != want {
|
||||||
t.Errorf("%U incorrect: got %t; want %t", i, got, want)
|
t.Errorf("%U incorrect: got %t; want %t", i, got, want)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user