Commit 7a92287a authored by Rob Pike's avatar Rob Pike

unicode: for consistency with MaxRune, s/Latin1Max/MaxLatin1/ and

s/ASCIIMax/MaxASCII/

R=golang-dev, r, gri
CC=golang-dev
https://golang.org/cl/4539109
parent 8d64e73f
...@@ -6,7 +6,7 @@ package unicode ...@@ -6,7 +6,7 @@ package unicode
// IsDigit reports whether the rune is a decimal digit. // IsDigit reports whether the rune is a decimal digit.
func IsDigit(rune int) bool { func IsDigit(rune int) bool {
if rune < Latin1Max { if rune <= MaxLatin1 {
return '0' <= rune && rune <= '9' return '0' <= rune && rune <= '9'
} }
return Is(Digit, rune) return Is(Digit, rune)
......
...@@ -118,7 +118,7 @@ func TestDigit(t *testing.T) { ...@@ -118,7 +118,7 @@ func TestDigit(t *testing.T) {
// Test that the special case in IsDigit agrees with the table // Test that the special case in IsDigit agrees with the table
func TestDigitOptimization(t *testing.T) { func TestDigitOptimization(t *testing.T) {
for i := 0; i < Latin1Max; i++ { for i := 0; i <= MaxLatin1; i++ {
if Is(Digit, i) != IsDigit(i) { if Is(Digit, i) != IsDigit(i) {
t.Errorf("IsDigit(U+%04X) disagrees with Is(Digit)", i) t.Errorf("IsDigit(U+%04X) disagrees with Is(Digit)", i)
} }
......
...@@ -34,7 +34,7 @@ var PrintRanges = []*RangeTable{ ...@@ -34,7 +34,7 @@ var PrintRanges = []*RangeTable{
func IsGraphic(rune int) bool { func IsGraphic(rune int) bool {
// We cast to uint32 to avoid the extra test for negative, // We cast to uint32 to avoid the extra test for negative,
// and in the index we cast to uint8 to avoid the range check. // and in the index we cast to uint8 to avoid the range check.
if uint32(rune) < Latin1Max { if uint32(rune) <= MaxLatin1 {
return properties[uint8(rune)]&pg != 0 return properties[uint8(rune)]&pg != 0
} }
return IsOneOf(GraphicRanges, rune) return IsOneOf(GraphicRanges, rune)
...@@ -46,7 +46,7 @@ func IsGraphic(rune int) bool { ...@@ -46,7 +46,7 @@ func IsGraphic(rune int) bool {
// character. This categorization is the same as IsGraphic except that the // character. This categorization is the same as IsGraphic except that the
// only spacing character is ASCII space, U+0020. // only spacing character is ASCII space, U+0020.
func IsPrint(rune int) bool { func IsPrint(rune int) bool {
if uint32(rune) < Latin1Max { if uint32(rune) <= MaxLatin1 {
return properties[uint8(rune)]&pp != 0 return properties[uint8(rune)]&pp != 0
} }
return IsOneOf(PrintRanges, rune) return IsOneOf(PrintRanges, rune)
...@@ -67,7 +67,7 @@ func IsOneOf(set []*RangeTable, rune int) bool { ...@@ -67,7 +67,7 @@ func IsOneOf(set []*RangeTable, rune int) bool {
// The C (Other) Unicode category includes more code points // The C (Other) Unicode category includes more code points
// such as surrogates; use Is(C, rune) to test for them. // such as surrogates; use Is(C, rune) to test for them.
func IsControl(rune int) bool { func IsControl(rune int) bool {
if uint32(rune) < Latin1Max { if uint32(rune) <= MaxLatin1 {
return properties[uint8(rune)]&pC != 0 return properties[uint8(rune)]&pC != 0
} }
// All control characters are < Latin1Max. // All control characters are < Latin1Max.
...@@ -76,7 +76,7 @@ func IsControl(rune int) bool { ...@@ -76,7 +76,7 @@ func IsControl(rune int) bool {
// IsLetter reports whether the rune is a letter (category L). // IsLetter reports whether the rune is a letter (category L).
func IsLetter(rune int) bool { func IsLetter(rune int) bool {
if uint32(rune) < Latin1Max { if uint32(rune) <= MaxLatin1 {
return properties[uint8(rune)]&(pLu|pLl) != 0 return properties[uint8(rune)]&(pLu|pLl) != 0
} }
return Is(Letter, rune) return Is(Letter, rune)
...@@ -90,7 +90,7 @@ func IsMark(rune int) bool { ...@@ -90,7 +90,7 @@ func IsMark(rune int) bool {
// IsNumber reports whether the rune is a number (category N). // IsNumber reports whether the rune is a number (category N).
func IsNumber(rune int) bool { func IsNumber(rune int) bool {
if uint32(rune) < Latin1Max { if uint32(rune) <= MaxLatin1 {
return properties[uint8(rune)]&pN != 0 return properties[uint8(rune)]&pN != 0
} }
return Is(Number, rune) return Is(Number, rune)
...@@ -99,7 +99,7 @@ func IsNumber(rune int) bool { ...@@ -99,7 +99,7 @@ func IsNumber(rune int) bool {
// IsPunct reports whether the rune is a Unicode punctuation character // IsPunct reports whether the rune is a Unicode punctuation character
// (category P). // (category P).
func IsPunct(rune int) bool { func IsPunct(rune int) bool {
if uint32(rune) < Latin1Max { if uint32(rune) <= MaxLatin1 {
return properties[uint8(rune)]&pP != 0 return properties[uint8(rune)]&pP != 0
} }
return Is(Punct, rune) return Is(Punct, rune)
...@@ -113,7 +113,7 @@ func IsPunct(rune int) bool { ...@@ -113,7 +113,7 @@ func IsPunct(rune int) bool {
// Z and property Pattern_White_Space. // Z and property Pattern_White_Space.
func IsSpace(rune int) bool { func IsSpace(rune int) bool {
// This property isn't the same as Z; special-case it. // This property isn't the same as Z; special-case it.
if uint32(rune) < Latin1Max { if uint32(rune) <= MaxLatin1 {
switch rune { switch rune {
case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0: case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0:
return true return true
...@@ -125,7 +125,7 @@ func IsSpace(rune int) bool { ...@@ -125,7 +125,7 @@ func IsSpace(rune int) bool {
// IsSymbol reports whether the rune is a symbolic character. // IsSymbol reports whether the rune is a symbolic character.
func IsSymbol(rune int) bool { func IsSymbol(rune int) bool {
if uint32(rune) < Latin1Max { if uint32(rune) <= MaxLatin1 {
return properties[uint8(rune)]&pS != 0 return properties[uint8(rune)]&pS != 0
} }
return Is(Symbol, rune) return Is(Symbol, rune)
......
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
// in the Latin-1 range through the property table. // in the Latin-1 range through the property table.
func TestIsControlLatin1(t *testing.T) { func TestIsControlLatin1(t *testing.T) {
for i := 0; i < Latin1Max; i++ { for i := 0; i <= MaxLatin1; i++ {
got := IsControl(i) got := IsControl(i)
want := false want := false
switch { switch {
...@@ -29,7 +29,7 @@ func TestIsControlLatin1(t *testing.T) { ...@@ -29,7 +29,7 @@ func TestIsControlLatin1(t *testing.T) {
} }
func TestIsLetterLatin1(t *testing.T) { func TestIsLetterLatin1(t *testing.T) {
for i := 0; i < Latin1Max; i++ { for i := 0; i <= MaxLatin1; i++ {
got := IsLetter(i) got := IsLetter(i)
want := Is(Letter, i) want := Is(Letter, i)
if got != want { if got != want {
...@@ -39,7 +39,7 @@ func TestIsLetterLatin1(t *testing.T) { ...@@ -39,7 +39,7 @@ func TestIsLetterLatin1(t *testing.T) {
} }
func TestIsUpperLatin1(t *testing.T) { func TestIsUpperLatin1(t *testing.T) {
for i := 0; i < Latin1Max; i++ { for i := 0; i <= MaxLatin1; i++ {
got := IsUpper(i) got := IsUpper(i)
want := Is(Upper, i) want := Is(Upper, i)
if got != want { if got != want {
...@@ -49,7 +49,7 @@ func TestIsUpperLatin1(t *testing.T) { ...@@ -49,7 +49,7 @@ func TestIsUpperLatin1(t *testing.T) {
} }
func TestIsLowerLatin1(t *testing.T) { func TestIsLowerLatin1(t *testing.T) {
for i := 0; i < Latin1Max; i++ { for i := 0; i <= MaxLatin1; i++ {
got := IsLower(i) got := IsLower(i)
want := Is(Lower, i) want := Is(Lower, i)
if got != want { if got != want {
...@@ -59,7 +59,7 @@ func TestIsLowerLatin1(t *testing.T) { ...@@ -59,7 +59,7 @@ func TestIsLowerLatin1(t *testing.T) {
} }
func TestNumberLatin1(t *testing.T) { func TestNumberLatin1(t *testing.T) {
for i := 0; i < Latin1Max; i++ { for i := 0; i <= MaxLatin1; i++ {
got := IsNumber(i) got := IsNumber(i)
want := Is(Number, i) want := Is(Number, i)
if got != want { if got != want {
...@@ -69,7 +69,7 @@ func TestNumberLatin1(t *testing.T) { ...@@ -69,7 +69,7 @@ func TestNumberLatin1(t *testing.T) {
} }
func TestIsPrintLatin1(t *testing.T) { func TestIsPrintLatin1(t *testing.T) {
for i := 0; i < Latin1Max; i++ { for i := 0; i <= MaxLatin1; i++ {
got := IsPrint(i) got := IsPrint(i)
want := IsOneOf(PrintRanges, i) want := IsOneOf(PrintRanges, i)
if i == ' ' { if i == ' ' {
...@@ -82,7 +82,7 @@ func TestIsPrintLatin1(t *testing.T) { ...@@ -82,7 +82,7 @@ func TestIsPrintLatin1(t *testing.T) {
} }
func TestIsGraphicLatin1(t *testing.T) { func TestIsGraphicLatin1(t *testing.T) {
for i := 0; i < Latin1Max; i++ { for i := 0; i <= MaxLatin1; i++ {
got := IsGraphic(i) got := IsGraphic(i)
want := IsOneOf(GraphicRanges, i) want := IsOneOf(GraphicRanges, i)
if got != want { if got != want {
...@@ -92,7 +92,7 @@ func TestIsGraphicLatin1(t *testing.T) { ...@@ -92,7 +92,7 @@ func TestIsGraphicLatin1(t *testing.T) {
} }
func TestIsPunctLatin1(t *testing.T) { func TestIsPunctLatin1(t *testing.T) {
for i := 0; i < Latin1Max; i++ { for i := 0; i <= MaxLatin1; i++ {
got := IsPunct(i) got := IsPunct(i)
want := Is(Punct, i) want := Is(Punct, i)
if got != want { if got != want {
...@@ -102,7 +102,7 @@ func TestIsPunctLatin1(t *testing.T) { ...@@ -102,7 +102,7 @@ func TestIsPunctLatin1(t *testing.T) {
} }
func TestIsSpaceLatin1(t *testing.T) { func TestIsSpaceLatin1(t *testing.T) {
for i := 0; i < Latin1Max; i++ { for i := 0; i <= MaxLatin1; i++ {
got := IsSpace(i) got := IsSpace(i)
want := Is(White_Space, i) want := Is(White_Space, i)
if got != want { if got != want {
...@@ -112,7 +112,7 @@ func TestIsSpaceLatin1(t *testing.T) { ...@@ -112,7 +112,7 @@ func TestIsSpaceLatin1(t *testing.T) {
} }
func TestIsSymbolLatin1(t *testing.T) { func TestIsSymbolLatin1(t *testing.T) {
for i := 0; i < Latin1Max; i++ { for i := 0; i <= MaxLatin1; i++ {
got := IsSymbol(i) got := IsSymbol(i)
want := Is(Symbol, i) want := Is(Symbol, i)
if got != want { if got != want {
......
...@@ -9,8 +9,8 @@ package unicode ...@@ -9,8 +9,8 @@ package unicode
const ( const (
MaxRune = 0x10FFFF // Maximum valid Unicode code point. MaxRune = 0x10FFFF // Maximum valid Unicode code point.
ReplacementChar = 0xFFFD // Represents invalid code points. ReplacementChar = 0xFFFD // Represents invalid code points.
ASCIIMax = 0x80 // (1 beyond) maximum ASCII value. MaxASCII = 0x7F // maximum ASCII value.
Latin1Max = 0x100 // (1 beyond) maximum Latin-1 value. MaxLatin1 = 0xFF // maximum Latin-1 value.
) )
// RangeTable defines a set of Unicode code points by listing the ranges of // RangeTable defines a set of Unicode code points by listing the ranges of
...@@ -123,7 +123,7 @@ func is32(ranges []Range32, rune uint32) bool { ...@@ -123,7 +123,7 @@ func is32(ranges []Range32, rune uint32) bool {
// Is tests whether rune is in the specified table of ranges. // Is tests whether rune is in the specified table of ranges.
func Is(rangeTab *RangeTable, rune int) bool { func Is(rangeTab *RangeTable, rune int) bool {
// common case: rune is ASCII or Latin-1. // common case: rune is ASCII or Latin-1.
if uint32(rune) < Latin1Max { if uint32(rune) <= MaxLatin1 {
// Only need to check R16, since R32 is always >= 1<<16. // Only need to check R16, since R32 is always >= 1<<16.
r16 := uint16(rune) r16 := uint16(rune)
for _, r := range rangeTab.R16 { for _, r := range rangeTab.R16 {
...@@ -151,7 +151,7 @@ func Is(rangeTab *RangeTable, rune int) bool { ...@@ -151,7 +151,7 @@ func Is(rangeTab *RangeTable, rune int) bool {
// IsUpper reports whether the rune is an upper case letter. // IsUpper reports whether the rune is an upper case letter.
func IsUpper(rune int) bool { func IsUpper(rune int) bool {
// See comment in IsGraphic. // See comment in IsGraphic.
if uint32(rune) < Latin1Max { if uint32(rune) <= MaxLatin1 {
return properties[uint8(rune)]&pLu != 0 return properties[uint8(rune)]&pLu != 0
} }
return Is(Upper, rune) return Is(Upper, rune)
...@@ -160,7 +160,7 @@ func IsUpper(rune int) bool { ...@@ -160,7 +160,7 @@ func IsUpper(rune int) bool {
// IsLower reports whether the rune is a lower case letter. // IsLower reports whether the rune is a lower case letter.
func IsLower(rune int) bool { func IsLower(rune int) bool {
// See comment in IsGraphic. // See comment in IsGraphic.
if uint32(rune) < Latin1Max { if uint32(rune) <= MaxLatin1 {
return properties[uint8(rune)]&pLl != 0 return properties[uint8(rune)]&pLl != 0
} }
return Is(Lower, rune) return Is(Lower, rune)
...@@ -168,7 +168,7 @@ func IsLower(rune int) bool { ...@@ -168,7 +168,7 @@ func IsLower(rune int) bool {
// IsTitle reports whether the rune is a title case letter. // IsTitle reports whether the rune is a title case letter.
func IsTitle(rune int) bool { func IsTitle(rune int) bool {
if rune < Latin1Max { if rune <= MaxLatin1 {
return false return false
} }
return Is(Title, rune) return Is(Title, rune)
...@@ -218,7 +218,7 @@ func To(_case int, rune int) int { ...@@ -218,7 +218,7 @@ func To(_case int, rune int) int {
// ToUpper maps the rune to upper case. // ToUpper maps the rune to upper case.
func ToUpper(rune int) int { func ToUpper(rune int) int {
if rune < ASCIIMax { if rune <= MaxASCII {
if 'a' <= rune && rune <= 'z' { if 'a' <= rune && rune <= 'z' {
rune -= 'a' - 'A' rune -= 'a' - 'A'
} }
...@@ -229,7 +229,7 @@ func ToUpper(rune int) int { ...@@ -229,7 +229,7 @@ func ToUpper(rune int) int {
// ToLower maps the rune to lower case. // ToLower maps the rune to lower case.
func ToLower(rune int) int { func ToLower(rune int) int {
if rune < ASCIIMax { if rune <= MaxASCII {
if 'A' <= rune && rune <= 'Z' { if 'A' <= rune && rune <= 'Z' {
rune += 'a' - 'A' rune += 'a' - 'A'
} }
...@@ -240,7 +240,7 @@ func ToLower(rune int) int { ...@@ -240,7 +240,7 @@ func ToLower(rune int) int {
// ToTitle maps the rune to title case. // ToTitle maps the rune to title case.
func ToTitle(rune int) int { func ToTitle(rune int) int {
if rune < ASCIIMax { if rune <= MaxASCII {
if 'a' <= rune && rune <= 'z' { // title case is upper case for ASCII if 'a' <= rune && rune <= 'z' { // title case is upper case for ASCII
rune -= 'a' - 'A' rune -= 'a' - 'A'
} }
......
...@@ -323,7 +323,7 @@ func TestIsSpace(t *testing.T) { ...@@ -323,7 +323,7 @@ func TestIsSpace(t *testing.T) {
// Check that the optimizations for IsLetter etc. agree with the tables. // Check that the optimizations for IsLetter etc. agree with the tables.
// We only need to check the Latin-1 range. // We only need to check the Latin-1 range.
func TestLetterOptimizations(t *testing.T) { func TestLetterOptimizations(t *testing.T) {
for i := 0; i < Latin1Max; i++ { for i := 0; i <= MaxLatin1; i++ {
if Is(Letter, i) != IsLetter(i) { if Is(Letter, i) != IsLetter(i) {
t.Errorf("IsLetter(U+%04X) disagrees with Is(Letter)", i) t.Errorf("IsLetter(U+%04X) disagrees with Is(Letter)", i)
} }
......
...@@ -919,8 +919,8 @@ func printLatinProperties() { ...@@ -919,8 +919,8 @@ func printLatinProperties() {
if *test { if *test {
return return
} }
fmt.Println("var properties = [Latin1Max]uint8{") fmt.Println("var properties = [MaxLatin1+1]uint8{")
for code := 0; code < unicode.Latin1Max; code++ { for code := 0; code <= unicode.MaxLatin1; code++ {
var property string var property string
switch chars[code].category { switch chars[code].category {
case "Cc", "": // NUL has no category. case "Cc", "": // NUL has no category.
......
...@@ -5411,7 +5411,7 @@ var _CaseRanges = []CaseRange{ ...@@ -5411,7 +5411,7 @@ var _CaseRanges = []CaseRange{
{0x10400, 0x10427, d{0, 40, 0}}, {0x10400, 0x10427, d{0, 40, 0}},
{0x10428, 0x1044F, d{-40, 0, -40}}, {0x10428, 0x1044F, d{-40, 0, -40}},
} }
var properties = [Latin1Max]uint8{ var properties = [MaxLatin1 + 1]uint8{
0x00: pC, // '\x00' 0x00: pC, // '\x00'
0x01: pC, // '\x01' 0x01: pC, // '\x01'
0x02: pC, // '\x02' 0x02: pC, // '\x02'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment