aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/unicode
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@google.com>2016-02-03 21:58:02 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2016-02-03 21:58:02 +0000
commitf98dd1a338867a408f7c72d73fbad7fe7fc93e3a (patch)
tree2f8da9862a9c1fe0df138917f997b03439c02773 /libgo/go/unicode
parentb081ed4efc144da0c45a6484aebfd10e0eb9fda3 (diff)
downloadgcc-f98dd1a338867a408f7c72d73fbad7fe7fc93e3a.zip
gcc-f98dd1a338867a408f7c72d73fbad7fe7fc93e3a.tar.gz
gcc-f98dd1a338867a408f7c72d73fbad7fe7fc93e3a.tar.bz2
libgo: Update to go1.6rc1.
Reviewed-on: https://go-review.googlesource.com/19200 From-SVN: r233110
Diffstat (limited to 'libgo/go/unicode')
-rw-r--r--libgo/go/unicode/example_test.go196
-rw-r--r--libgo/go/unicode/tables.go4
-rw-r--r--libgo/go/unicode/utf8/utf8.go473
-rw-r--r--libgo/go/unicode/utf8/utf8_test.go68
4 files changed, 541 insertions, 200 deletions
diff --git a/libgo/go/unicode/example_test.go b/libgo/go/unicode/example_test.go
new file mode 100644
index 0000000..50c5b18
--- /dev/null
+++ b/libgo/go/unicode/example_test.go
@@ -0,0 +1,196 @@
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package unicode_test
+
+import (
+ "fmt"
+ "unicode"
+)
+
+// Functions starting with "Is" can be used to inspect which table of range a
+// rune belongs to. Note that runes may fit into more than one range.
+func Example_is() {
+
+ // constant with mixed type runes
+ const mixed = "\b5Ὂg̀9! ℃ᾭG"
+ for _, c := range mixed {
+ fmt.Printf("For %q:\n", c)
+ if unicode.IsControl(c) {
+ fmt.Println("\tis control rune")
+ }
+ if unicode.IsDigit(c) {
+ fmt.Println("\tis digit rune")
+ }
+ if unicode.IsGraphic(c) {
+ fmt.Println("\tis graphic rune")
+ }
+ if unicode.IsLetter(c) {
+ fmt.Println("\tis letter rune")
+ }
+ if unicode.IsLower(c) {
+ fmt.Println("\tis lower case rune")
+ }
+ if unicode.IsMark(c) {
+ fmt.Println("\tis mark rune")
+ }
+ if unicode.IsNumber(c) {
+ fmt.Println("\tis number rune")
+ }
+ if unicode.IsPrint(c) {
+ fmt.Println("\tis printable rune")
+ }
+ if !unicode.IsPrint(c) {
+ fmt.Println("\tis not printable rune")
+ }
+ if unicode.IsPunct(c) {
+ fmt.Println("\tis punct rune")
+ }
+ if unicode.IsSpace(c) {
+ fmt.Println("\tis space rune")
+ }
+ if unicode.IsSymbol(c) {
+ fmt.Println("\tis symbol rune")
+ }
+ if unicode.IsTitle(c) {
+ fmt.Println("\tis title case rune")
+ }
+ if unicode.IsUpper(c) {
+ fmt.Println("\tis upper case rune")
+ }
+ }
+
+ // Output:
+ // For '\b':
+ // is control rune
+ // is not printable rune
+ // For '5':
+ // is digit rune
+ // is graphic rune
+ // is number rune
+ // is printable rune
+ // For 'Ὂ':
+ // is graphic rune
+ // is letter rune
+ // is printable rune
+ // is upper case rune
+ // For 'g':
+ // is graphic rune
+ // is letter rune
+ // is lower case rune
+ // is printable rune
+ // For '̀':
+ // is graphic rune
+ // is mark rune
+ // is printable rune
+ // For '9':
+ // is digit rune
+ // is graphic rune
+ // is number rune
+ // is printable rune
+ // For '!':
+ // is graphic rune
+ // is printable rune
+ // is punct rune
+ // For ' ':
+ // is graphic rune
+ // is printable rune
+ // is space rune
+ // For '℃':
+ // is graphic rune
+ // is printable rune
+ // is symbol rune
+ // For 'ᾭ':
+ // is graphic rune
+ // is letter rune
+ // is printable rune
+ // is title case rune
+ // For 'G':
+ // is graphic rune
+ // is letter rune
+ // is printable rune
+ // is upper case rune
+}
+
+func ExampleSimpleFold() {
+ fmt.Printf("%#U\n", unicode.SimpleFold('A')) // 'a'
+ fmt.Printf("%#U\n", unicode.SimpleFold('a')) // 'A'
+ fmt.Printf("%#U\n", unicode.SimpleFold('K')) // 'k'
+ fmt.Printf("%#U\n", unicode.SimpleFold('k')) // '\u212A' (Kelvin symbol, K)
+ fmt.Printf("%#U\n", unicode.SimpleFold('\u212A')) // 'K'
+ fmt.Printf("%#U\n", unicode.SimpleFold('1')) // '1'
+
+ // Output:
+ // U+0061 'a'
+ // U+0041 'A'
+ // U+006B 'k'
+ // U+212A 'K'
+ // U+004B 'K'
+ // U+0031 '1'
+}
+
+func ExampleTo() {
+ const lcG = 'g'
+ fmt.Printf("%#U\n", unicode.To(unicode.UpperCase, lcG))
+ fmt.Printf("%#U\n", unicode.To(unicode.LowerCase, lcG))
+ fmt.Printf("%#U\n", unicode.To(unicode.TitleCase, lcG))
+
+ const ucG = 'G'
+ fmt.Printf("%#U\n", unicode.To(unicode.UpperCase, ucG))
+ fmt.Printf("%#U\n", unicode.To(unicode.LowerCase, ucG))
+ fmt.Printf("%#U\n", unicode.To(unicode.TitleCase, ucG))
+
+ // Output:
+ // U+0047 'G'
+ // U+0067 'g'
+ // U+0047 'G'
+ // U+0047 'G'
+ // U+0067 'g'
+ // U+0047 'G'
+}
+
+func ExampleToLower() {
+ const ucG = 'G'
+ fmt.Printf("%#U\n", unicode.ToLower(ucG))
+
+ // Output:
+ // U+0067 'g'
+}
+func ExampleToTitle() {
+ const ucG = 'g'
+ fmt.Printf("%#U\n", unicode.ToTitle(ucG))
+
+ // Output:
+ // U+0047 'G'
+}
+
+func ExampleToUpper() {
+ const ucG = 'g'
+ fmt.Printf("%#U\n", unicode.ToUpper(ucG))
+
+ // Output:
+ // U+0047 'G'
+}
+
+func ExampleSpecialCase() {
+ t := unicode.TurkishCase
+
+ const lci = 'i'
+ fmt.Printf("%#U\n", t.ToLower(lci))
+ fmt.Printf("%#U\n", t.ToTitle(lci))
+ fmt.Printf("%#U\n", t.ToUpper(lci))
+
+ const uci = 'İ'
+ fmt.Printf("%#U\n", t.ToLower(uci))
+ fmt.Printf("%#U\n", t.ToTitle(uci))
+ fmt.Printf("%#U\n", t.ToUpper(uci))
+
+ // Output:
+ // U+0069 'i'
+ // U+0130 'İ'
+ // U+0130 'İ'
+ // U+0069 'i'
+ // U+0130 'İ'
+ // U+0130 'İ'
+}
diff --git a/libgo/go/unicode/tables.go b/libgo/go/unicode/tables.go
index 370a9d1..8bb4206 100644
--- a/libgo/go/unicode/tables.go
+++ b/libgo/go/unicode/tables.go
@@ -53,7 +53,7 @@ var Categories = map[string]*RangeTable{
var _C = &RangeTable{
R16: []Range16{
- {0x0001, 0x001f, 1},
+ {0x0000, 0x001f, 1},
{0x007f, 0x009f, 1},
{0x00ad, 0x0600, 1363},
{0x0601, 0x0605, 1},
@@ -81,7 +81,7 @@ var _C = &RangeTable{
var _Cc = &RangeTable{
R16: []Range16{
- {0x0001, 0x001f, 1},
+ {0x0000, 0x001f, 1},
{0x007f, 0x009f, 1},
},
LatinOffset: 2,
diff --git a/libgo/go/unicode/utf8/utf8.go b/libgo/go/unicode/utf8/utf8.go
index 9ac3718..bbaf14a 100644
--- a/libgo/go/unicode/utf8/utf8.go
+++ b/libgo/go/unicode/utf8/utf8.go
@@ -40,206 +40,208 @@ const (
rune1Max = 1<<7 - 1
rune2Max = 1<<11 - 1
rune3Max = 1<<16 - 1
-)
-
-func decodeRuneInternal(p []byte) (r rune, size int, short bool) {
- n := len(p)
- if n < 1 {
- return RuneError, 0, true
- }
- c0 := p[0]
-
- // 1-byte, 7-bit sequence?
- if c0 < tx {
- return rune(c0), 1, false
- }
-
- // unexpected continuation byte?
- if c0 < t2 {
- return RuneError, 1, false
- }
-
- // need first continuation byte
- if n < 2 {
- return RuneError, 1, true
- }
- c1 := p[1]
- if c1 < tx || t2 <= c1 {
- return RuneError, 1, false
- }
-
- // 2-byte, 11-bit sequence?
- if c0 < t3 {
- r = rune(c0&mask2)<<6 | rune(c1&maskx)
- if r <= rune1Max {
- return RuneError, 1, false
- }
- return r, 2, false
- }
- // need second continuation byte
- if n < 3 {
- return RuneError, 1, true
- }
- c2 := p[2]
- if c2 < tx || t2 <= c2 {
- return RuneError, 1, false
- }
-
- // 3-byte, 16-bit sequence?
- if c0 < t4 {
- r = rune(c0&mask3)<<12 | rune(c1&maskx)<<6 | rune(c2&maskx)
- if r <= rune2Max {
- return RuneError, 1, false
- }
- if surrogateMin <= r && r <= surrogateMax {
- return RuneError, 1, false
- }
- return r, 3, false
- }
-
- // need third continuation byte
- if n < 4 {
- return RuneError, 1, true
- }
- c3 := p[3]
- if c3 < tx || t2 <= c3 {
- return RuneError, 1, false
- }
-
- // 4-byte, 21-bit sequence?
- if c0 < t5 {
- r = rune(c0&mask4)<<18 | rune(c1&maskx)<<12 | rune(c2&maskx)<<6 | rune(c3&maskx)
- if r <= rune3Max || MaxRune < r {
- return RuneError, 1, false
- }
- return r, 4, false
- }
+ // The default lowest and highest continuation byte.
+ locb = 0x80 // 1000 0000
+ hicb = 0xBF // 1011 1111
+
+ // These names of these constants are chosen to give nice alignment in the
+ // table below. The first nibble is an index into acceptRanges or F for
+ // special one-byte cases. The second nibble is the Rune length or the
+ // Status for the special one-byte case.
+ xx = 0xF1 // invalid: size 1
+ as = 0xF0 // ASCII: size 1
+ s1 = 0x02 // accept 0, size 2
+ s2 = 0x13 // accept 1, size 3
+ s3 = 0x03 // accept 0, size 3
+ s4 = 0x23 // accept 2, size 3
+ s5 = 0x34 // accept 3, size 4
+ s6 = 0x04 // accept 0, size 4
+ s7 = 0x44 // accept 4, size 4
+)
- // error
- return RuneError, 1, false
+// first is information about the first byte in a UTF-8 sequence.
+var first = [256]uint8{
+ // 1 2 3 4 5 6 7 8 9 A B C D E F
+ as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x00-0x0F
+ as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x10-0x1F
+ as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x20-0x2F
+ as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x30-0x3F
+ as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x40-0x4F
+ as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x50-0x5F
+ as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x60-0x6F
+ as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x70-0x7F
+ // 1 2 3 4 5 6 7 8 9 A B C D E F
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x80-0x8F
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x90-0x9F
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xA0-0xAF
+ xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xB0-0xBF
+ xx, xx, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xC0-0xCF
+ s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xD0-0xDF
+ s2, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s4, s3, s3, // 0xE0-0xEF
+ s5, s6, s6, s6, s7, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xF0-0xFF
}
-func decodeRuneInStringInternal(s string) (r rune, size int, short bool) {
- n := len(s)
- if n < 1 {
- return RuneError, 0, true
- }
- c0 := s[0]
-
- // 1-byte, 7-bit sequence?
- if c0 < tx {
- return rune(c0), 1, false
- }
-
- // unexpected continuation byte?
- if c0 < t2 {
- return RuneError, 1, false
- }
-
- // need first continuation byte
- if n < 2 {
- return RuneError, 1, true
- }
- c1 := s[1]
- if c1 < tx || t2 <= c1 {
- return RuneError, 1, false
- }
-
- // 2-byte, 11-bit sequence?
- if c0 < t3 {
- r = rune(c0&mask2)<<6 | rune(c1&maskx)
- if r <= rune1Max {
- return RuneError, 1, false
- }
- return r, 2, false
- }
-
- // need second continuation byte
- if n < 3 {
- return RuneError, 1, true
- }
- c2 := s[2]
- if c2 < tx || t2 <= c2 {
- return RuneError, 1, false
- }
-
- // 3-byte, 16-bit sequence?
- if c0 < t4 {
- r = rune(c0&mask3)<<12 | rune(c1&maskx)<<6 | rune(c2&maskx)
- if r <= rune2Max {
- return RuneError, 1, false
- }
- if surrogateMin <= r && r <= surrogateMax {
- return RuneError, 1, false
- }
- return r, 3, false
- }
-
- // need third continuation byte
- if n < 4 {
- return RuneError, 1, true
- }
- c3 := s[3]
- if c3 < tx || t2 <= c3 {
- return RuneError, 1, false
- }
-
- // 4-byte, 21-bit sequence?
- if c0 < t5 {
- r = rune(c0&mask4)<<18 | rune(c1&maskx)<<12 | rune(c2&maskx)<<6 | rune(c3&maskx)
- if r <= rune3Max || MaxRune < r {
- return RuneError, 1, false
- }
- return r, 4, false
- }
+// acceptRange gives the range of valid values for the second byte in a UTF-8
+// sequence.
+type acceptRange struct {
+ lo uint8 // lowest value for second byte.
+ hi uint8 // highest value for second byte.
+}
- // error
- return RuneError, 1, false
+var acceptRanges = [...]acceptRange{
+ 0: {locb, hicb},
+ 1: {0xA0, hicb},
+ 2: {locb, 0x9F},
+ 3: {0x90, hicb},
+ 4: {locb, 0x8F},
}
// FullRune reports whether the bytes in p begin with a full UTF-8 encoding of a rune.
// An invalid encoding is considered a full Rune since it will convert as a width-1 error rune.
func FullRune(p []byte) bool {
- _, _, short := decodeRuneInternal(p)
- return !short
+ n := len(p)
+ if n == 0 {
+ return false
+ }
+ x := first[p[0]]
+ if n >= int(x&7) {
+ return true // ASCII, invalid or valid.
+ }
+ // Must be short or invalid.
+ accept := acceptRanges[x>>4]
+ if n > 1 {
+ if c := p[1]; c < accept.lo || accept.hi < c {
+ return true
+ } else if n > 2 && (p[2] < locb || hicb < p[2]) {
+ return true
+ }
+ }
+ return false
}
// FullRuneInString is like FullRune but its input is a string.
func FullRuneInString(s string) bool {
- _, _, short := decodeRuneInStringInternal(s)
- return !short
+ n := len(s)
+ if n == 0 {
+ return false
+ }
+ x := first[s[0]]
+ if n >= int(x&7) {
+ return true // ASCII, invalid, or valid.
+ }
+ // Must be short or invalid.
+ accept := acceptRanges[x>>4]
+ if n > 1 {
+ if c := s[1]; c < accept.lo || accept.hi < c {
+ return true
+ } else if n > 2 && (s[2] < locb || hicb < s[2]) {
+ return true
+ }
+ }
+ return false
}
// DecodeRune unpacks the first UTF-8 encoding in p and returns the rune and
// its width in bytes. If p is empty it returns (RuneError, 0). Otherwise, if
// the encoding is invalid, it returns (RuneError, 1). Both are impossible
-// results for correct UTF-8.
+// results for correct, non-empty UTF-8.
//
// An encoding is invalid if it is incorrect UTF-8, encodes a rune that is
// out of range, or is not the shortest possible UTF-8 encoding for the
// value. No other validation is performed.
func DecodeRune(p []byte) (r rune, size int) {
- r, size, _ = decodeRuneInternal(p)
- return
+ n := len(p)
+ if n < 1 {
+ return RuneError, 0
+ }
+ p0 := p[0]
+ x := first[p0]
+ if x >= as {
+ // The following code simulates an additional check for x == xx and
+ // handling the ASCII and invalid cases accordingly. This mask-and-or
+ // approach prevents an additional branch.
+ mask := rune(x) << 31 >> 31 // Create 0x0000 or 0xFFFF.
+ return rune(p[0])&^mask | RuneError&mask, 1
+ }
+ sz := x & 7
+ accept := acceptRanges[x>>4]
+ if n < int(sz) {
+ return RuneError, 1
+ }
+ b1 := p[1]
+ if b1 < accept.lo || accept.hi < b1 {
+ return RuneError, 1
+ }
+ if sz == 2 {
+ return rune(p0&mask2)<<6 | rune(b1&maskx), 2
+ }
+ b2 := p[2]
+ if b2 < locb || hicb < b2 {
+ return RuneError, 1
+ }
+ if sz == 3 {
+ return rune(p0&mask3)<<12 | rune(b1&maskx)<<6 | rune(b2&maskx), 3
+ }
+ b3 := p[3]
+ if b3 < locb || hicb < b3 {
+ return RuneError, 1
+ }
+ return rune(p0&mask4)<<18 | rune(b1&maskx)<<12 | rune(b2&maskx)<<6 | rune(b3&maskx), 4
}
// DecodeRuneInString is like DecodeRune but its input is a string. If s is
// empty it returns (RuneError, 0). Otherwise, if the encoding is invalid, it
-// returns (RuneError, 1). Both are impossible results for correct UTF-8.
+// returns (RuneError, 1). Both are impossible results for correct, non-empty
+// UTF-8.
//
// An encoding is invalid if it is incorrect UTF-8, encodes a rune that is
// out of range, or is not the shortest possible UTF-8 encoding for the
// value. No other validation is performed.
func DecodeRuneInString(s string) (r rune, size int) {
- r, size, _ = decodeRuneInStringInternal(s)
- return
+ n := len(s)
+ if n < 1 {
+ return RuneError, 0
+ }
+ s0 := s[0]
+ x := first[s0]
+ if x >= as {
+ // The following code simulates an additional check for x == xx and
+ // handling the ASCII and invalid cases accordingly. This mask-and-or
+ // approach prevents an additional branch.
+ mask := rune(x) << 31 >> 31 // Create 0x0000 or 0xFFFF.
+ return rune(s[0])&^mask | RuneError&mask, 1
+ }
+ sz := x & 7
+ accept := acceptRanges[x>>4]
+ if n < int(sz) {
+ return RuneError, 1
+ }
+ s1 := s[1]
+ if s1 < accept.lo || accept.hi < s1 {
+ return RuneError, 1
+ }
+ if sz == 2 {
+ return rune(s0&mask2)<<6 | rune(s1&maskx), 2
+ }
+ s2 := s[2]
+ if s2 < locb || hicb < s2 {
+ return RuneError, 1
+ }
+ if sz == 3 {
+ return rune(s0&mask3)<<12 | rune(s1&maskx)<<6 | rune(s2&maskx), 3
+ }
+ s3 := s[3]
+ if s3 < locb || hicb < s3 {
+ return RuneError, 1
+ }
+ return rune(s0&mask4)<<18 | rune(s1&maskx)<<12 | rune(s2&maskx)<<6 | rune(s3&maskx), 4
}
// DecodeLastRune unpacks the last UTF-8 encoding in p and returns the rune and
// its width in bytes. If p is empty it returns (RuneError, 0). Otherwise, if
// the encoding is invalid, it returns (RuneError, 1). Both are impossible
-// results for correct UTF-8.
+// results for correct, non-empty UTF-8.
//
// An encoding is invalid if it is incorrect UTF-8, encodes a rune that is
// out of range, or is not the shortest possible UTF-8 encoding for the
@@ -278,7 +280,8 @@ func DecodeLastRune(p []byte) (r rune, size int) {
// DecodeLastRuneInString is like DecodeLastRune but its input is a string. If
// s is empty it returns (RuneError, 0). Otherwise, if the encoding is invalid,
-// it returns (RuneError, 1). Both are impossible results for correct UTF-8.
+// it returns (RuneError, 1). Both are impossible results for correct,
+// non-empty UTF-8.
//
// An encoding is invalid if it is incorrect UTF-8, encodes a rune that is
// out of range, or is not the shortest possible UTF-8 encoding for the
@@ -367,65 +370,141 @@ func EncodeRune(p []byte, r rune) int {
// RuneCount returns the number of runes in p. Erroneous and short
// encodings are treated as single runes of width 1 byte.
func RuneCount(p []byte) int {
- i := 0
+ np := len(p)
var n int
- for n = 0; i < len(p); n++ {
- if p[i] < RuneSelf {
+ for i := 0; i < np; {
+ n++
+ c := p[i]
+ if c < RuneSelf {
+ // ASCII fast path
i++
- } else {
- _, size := DecodeRune(p[i:])
- i += size
+ continue
+ }
+ x := first[c]
+ if x == xx {
+ i++ // invalid.
+ continue
+ }
+ size := int(x & 7)
+ if i+size > np {
+ i++ // Short or invalid.
+ continue
}
+ accept := acceptRanges[x>>4]
+ if c := p[i+1]; c < accept.lo || accept.hi < c {
+ size = 1
+ } else if size == 2 {
+ } else if c := p[i+2]; c < locb || hicb < c {
+ size = 1
+ } else if size == 3 {
+ } else if c := p[i+3]; c < locb || hicb < c {
+ size = 1
+ }
+ i += size
}
return n
}
// RuneCountInString is like RuneCount but its input is a string.
func RuneCountInString(s string) (n int) {
- for range s {
- n++
+ ns := len(s)
+ for i := 0; i < ns; n++ {
+ c := s[i]
+ if c < RuneSelf {
+ // ASCII fast path
+ i++
+ continue
+ }
+ x := first[c]
+ if x == xx {
+ i++ // invalid.
+ continue
+ }
+ size := int(x & 7)
+ if i+size > ns {
+ i++ // Short or invalid.
+ continue
+ }
+ accept := acceptRanges[x>>4]
+ if c := s[i+1]; c < accept.lo || accept.hi < c {
+ size = 1
+ } else if size == 2 {
+ } else if c := s[i+2]; c < locb || hicb < c {
+ size = 1
+ } else if size == 3 {
+ } else if c := s[i+3]; c < locb || hicb < c {
+ size = 1
+ }
+ i += size
}
- return
+ return n
}
-// RuneStart reports whether the byte could be the first byte of
-// an encoded rune. Second and subsequent bytes always have the top
-// two bits set to 10.
+// RuneStart reports whether the byte could be the first byte of an encoded,
+// possibly invalid 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 {
+ n := len(p)
+ for i := 0; i < n; {
+ pi := p[i]
+ if pi < RuneSelf {
i++
- } else {
- _, size := DecodeRune(p[i:])
- if size == 1 {
- // All valid runes of size 1 (those
- // below RuneSelf) were handled above.
- // This must be a RuneError.
- return false
- }
- i += size
+ continue
+ }
+ x := first[pi]
+ if x == xx {
+ return false // Illegal starter byte.
}
+ size := int(x & 7)
+ if i+size > n {
+ return false // Short or invalid.
+ }
+ accept := acceptRanges[x>>4]
+ if c := p[i+1]; c < accept.lo || accept.hi < c {
+ return false
+ } else if size == 2 {
+ } else if c := p[i+2]; c < locb || hicb < c {
+ return false
+ } else if size == 3 {
+ } else if c := p[i+3]; c < locb || hicb < c {
+ 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
- }
+ n := len(s)
+ for i := 0; i < n; {
+ si := s[i]
+ if si < RuneSelf {
+ i++
+ continue
+ }
+ x := first[si]
+ if x == xx {
+ return false // Illegal starter byte.
+ }
+ size := int(x & 7)
+ if i+size > n {
+ return false // Short or invalid.
+ }
+ accept := acceptRanges[x>>4]
+ if c := s[i+1]; c < accept.lo || accept.hi < c {
+ return false
+ } else if size == 2 {
+ } else if c := s[i+2]; c < locb || hicb < c {
+ return false
+ } else if size == 3 {
+ } else if c := s[i+3]; c < locb || hicb < c {
+ return false
}
+ i += size
}
return true
}
diff --git a/libgo/go/unicode/utf8/utf8_test.go b/libgo/go/unicode/utf8/utf8_test.go
index 758d7a0..51571b6 100644
--- a/libgo/go/unicode/utf8/utf8_test.go
+++ b/libgo/go/unicode/utf8/utf8_test.go
@@ -100,6 +100,15 @@ func TestFullRune(t *testing.T) {
t.Errorf("FullRune(%q) = true, want false", s1)
}
}
+ for _, s := range []string{"\xc0", "\xc1"} {
+ b := []byte(s)
+ if !FullRune(b) {
+ t.Errorf("FullRune(%q) = false, want true", s)
+ }
+ if !FullRuneInString(s) {
+ t.Errorf("FullRuneInString(%q) = false, want true", s)
+ }
+ }
}
func TestEncodeRune(t *testing.T) {
@@ -300,6 +309,8 @@ var runecounttests = []RuneCountTest{
{"☺☻☹", 3},
{"1,2,3,4", 7},
{"\xe2\x00", 2},
+ {"\xe2\x80", 2},
+ {"a\xe2\x80", 3},
}
func TestRuneCount(t *testing.T) {
@@ -352,6 +363,7 @@ var validTests = []ValidTest{
{"ЖЖ", true},
{"брэд-ЛГТМ", true},
{"☺☻☹", true},
+ {"aa\xe2", false},
{string([]byte{66, 250}), false},
{string([]byte{66, 250, 67}), false},
{"a\uFFFDb", true},
@@ -404,17 +416,57 @@ func TestValidRune(t *testing.T) {
}
func BenchmarkRuneCountTenASCIIChars(b *testing.B) {
+ s := []byte("0123456789")
for i := 0; i < b.N; i++ {
- RuneCountInString("0123456789")
+ RuneCount(s)
}
}
func BenchmarkRuneCountTenJapaneseChars(b *testing.B) {
+ s := []byte("日本語日本語日本語日")
+ for i := 0; i < b.N; i++ {
+ RuneCount(s)
+ }
+}
+
+func BenchmarkRuneCountInStringTenASCIIChars(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ RuneCountInString("0123456789")
+ }
+}
+
+func BenchmarkRuneCountInStringTenJapaneseChars(b *testing.B) {
for i := 0; i < b.N; i++ {
RuneCountInString("日本語日本語日本語日")
}
}
+func BenchmarkValidTenASCIIChars(b *testing.B) {
+ s := []byte("0123456789")
+ for i := 0; i < b.N; i++ {
+ Valid(s)
+ }
+}
+
+func BenchmarkValidTenJapaneseChars(b *testing.B) {
+ s := []byte("日本語日本語日本語日")
+ for i := 0; i < b.N; i++ {
+ Valid(s)
+ }
+}
+
+func BenchmarkValidStringTenASCIIChars(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ ValidString("0123456789")
+ }
+}
+
+func BenchmarkValidStringTenJapaneseChars(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ ValidString("日本語日本語日本語日")
+ }
+}
+
func BenchmarkEncodeASCIIRune(b *testing.B) {
buf := make([]byte, UTFMax)
for i := 0; i < b.N; i++ {
@@ -442,3 +494,17 @@ func BenchmarkDecodeJapaneseRune(b *testing.B) {
DecodeRune(nihon)
}
}
+
+func BenchmarkFullASCIIRune(b *testing.B) {
+ a := []byte{'a'}
+ for i := 0; i < b.N; i++ {
+ FullRune(a)
+ }
+}
+
+func BenchmarkFullJapaneseRune(b *testing.B) {
+ nihon := []byte("本")
+ for i := 0; i < b.N; i++ {
+ FullRune(nihon)
+ }
+}