diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2011-12-02 19:34:41 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2011-12-02 19:34:41 +0000 |
commit | 506cf9aaead4f5519f5549a918d285365b44e989 (patch) | |
tree | fe0344f264049738dca876a6dd2f69e96621ca17 /libgo/go/utf8 | |
parent | bfa9b58039ceacb1bae803fbbfb049b93540f2a7 (diff) | |
download | gcc-506cf9aaead4f5519f5549a918d285365b44e989.zip gcc-506cf9aaead4f5519f5549a918d285365b44e989.tar.gz gcc-506cf9aaead4f5519f5549a918d285365b44e989.tar.bz2 |
libgo: Update to weekly.2011-11-01.
From-SVN: r181938
Diffstat (limited to 'libgo/go/utf8')
-rw-r--r-- | libgo/go/utf8/string.go | 28 | ||||
-rw-r--r-- | libgo/go/utf8/string_test.go | 8 | ||||
-rw-r--r-- | libgo/go/utf8/utf8.go | 98 | ||||
-rw-r--r-- | libgo/go/utf8/utf8_test.go | 88 |
4 files changed, 110 insertions, 112 deletions
diff --git a/libgo/go/utf8/string.go b/libgo/go/utf8/string.go index 83b56b9..b333479 100644 --- a/libgo/go/utf8/string.go +++ b/libgo/go/utf8/string.go @@ -101,10 +101,10 @@ func (s *String) Slice(i, j int) string { // At returns the rune with index i in the String. The sequence of runes is the same // as iterating over the contents with a "for range" clause. -func (s *String) At(i int) int { +func (s *String) At(i int) rune { // ASCII is easy. Let the compiler catch the indexing error if there is one. if i < s.nonASCII { - return int(s.str[i]) + return rune(s.str[i]) } // Now we do need to know the index is valid. @@ -112,35 +112,35 @@ func (s *String) At(i int) int { panic(outOfRange) } - var rune int + var r rune // Five easy common cases: within 1 spot of bytePos/runePos, or the beginning, or the end. // With these cases, all scans from beginning or end work in O(1) time per rune. switch { case i == s.runePos-1: // backing up one rune - rune, s.width = DecodeLastRuneInString(s.str[0:s.bytePos]) + r, s.width = DecodeLastRuneInString(s.str[0:s.bytePos]) s.runePos = i s.bytePos -= s.width - return rune + return r case i == s.runePos+1: // moving ahead one rune s.runePos = i s.bytePos += s.width fallthrough case i == s.runePos: - rune, s.width = DecodeRuneInString(s.str[s.bytePos:]) - return rune + r, s.width = DecodeRuneInString(s.str[s.bytePos:]) + return r case i == 0: // start of string - rune, s.width = DecodeRuneInString(s.str) + r, s.width = DecodeRuneInString(s.str) s.runePos = 0 s.bytePos = 0 - return rune + return r case i == s.numRunes-1: // last rune in string - rune, s.width = DecodeLastRuneInString(s.str) + r, s.width = DecodeLastRuneInString(s.str) s.runePos = i s.bytePos = len(s.str) - s.width - return rune + return r } // We need to do a linear scan. There are three places to start from: @@ -173,7 +173,7 @@ func (s *String) At(i int) int { if forward { // TODO: Is it much faster to use a range loop for this scan? for { - rune, s.width = DecodeRuneInString(s.str[s.bytePos:]) + r, s.width = DecodeRuneInString(s.str[s.bytePos:]) if s.runePos == i { break } @@ -182,7 +182,7 @@ func (s *String) At(i int) int { } } else { for { - rune, s.width = DecodeLastRuneInString(s.str[0:s.bytePos]) + r, s.width = DecodeLastRuneInString(s.str[0:s.bytePos]) s.runePos-- s.bytePos -= s.width if s.runePos == i { @@ -190,7 +190,7 @@ func (s *String) At(i int) int { } } } - return rune + return r } // We want the panic in At(i) to satisfy os.Error, because that's what diff --git a/libgo/go/utf8/string_test.go b/libgo/go/utf8/string_test.go index f376b62..920d2a0 100644 --- a/libgo/go/utf8/string_test.go +++ b/libgo/go/utf8/string_test.go @@ -12,7 +12,7 @@ import ( func TestScanForwards(t *testing.T) { for _, s := range testStrings { - runes := []int(s) + runes := []rune(s) str := NewString(s) if str.RuneCount() != len(runes) { t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount()) @@ -29,7 +29,7 @@ func TestScanForwards(t *testing.T) { func TestScanBackwards(t *testing.T) { for _, s := range testStrings { - runes := []int(s) + runes := []rune(s) str := NewString(s) if str.RuneCount() != len(runes) { t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount()) @@ -57,7 +57,7 @@ func TestRandomAccess(t *testing.T) { if len(s) == 0 { continue } - runes := []int(s) + runes := []rune(s) str := NewString(s) if str.RuneCount() != len(runes) { t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount()) @@ -79,7 +79,7 @@ func TestRandomSliceAccess(t *testing.T) { if len(s) == 0 || s[0] == '\x80' { // the bad-UTF-8 string fools this simple test continue } - runes := []int(s) + runes := []rune(s) str := NewString(s) if str.RuneCount() != len(runes) { t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount()) diff --git a/libgo/go/utf8/utf8.go b/libgo/go/utf8/utf8.go index 3cd919d..a5f9983 100644 --- a/libgo/go/utf8/utf8.go +++ b/libgo/go/utf8/utf8.go @@ -34,7 +34,7 @@ const ( rune4Max = 1<<21 - 1 ) -func decodeRuneInternal(p []byte) (rune, size int, short bool) { +func decodeRuneInternal(p []byte) (r rune, size int, short bool) { n := len(p) if n < 1 { return RuneError, 0, true @@ -43,7 +43,7 @@ func decodeRuneInternal(p []byte) (rune, size int, short bool) { // 1-byte, 7-bit sequence? if c0 < tx { - return int(c0), 1, false + return rune(c0), 1, false } // unexpected continuation byte? @@ -62,11 +62,11 @@ func decodeRuneInternal(p []byte) (rune, size int, short bool) { // 2-byte, 11-bit sequence? if c0 < t3 { - rune = int(c0&mask2)<<6 | int(c1&maskx) - if rune <= rune1Max { + r = rune(c0&mask2)<<6 | rune(c1&maskx) + if r <= rune1Max { return RuneError, 1, false } - return rune, 2, false + return r, 2, false } // need second continuation byte @@ -80,11 +80,11 @@ func decodeRuneInternal(p []byte) (rune, size int, short bool) { // 3-byte, 16-bit sequence? if c0 < t4 { - rune = int(c0&mask3)<<12 | int(c1&maskx)<<6 | int(c2&maskx) - if rune <= rune2Max { + r = rune(c0&mask3)<<12 | rune(c1&maskx)<<6 | rune(c2&maskx) + if r <= rune2Max { return RuneError, 1, false } - return rune, 3, false + return r, 3, false } // need third continuation byte @@ -98,18 +98,18 @@ func decodeRuneInternal(p []byte) (rune, size int, short bool) { // 4-byte, 21-bit sequence? if c0 < t5 { - rune = int(c0&mask4)<<18 | int(c1&maskx)<<12 | int(c2&maskx)<<6 | int(c3&maskx) - if rune <= rune3Max { + r = rune(c0&mask4)<<18 | rune(c1&maskx)<<12 | rune(c2&maskx)<<6 | rune(c3&maskx) + if r <= rune3Max { return RuneError, 1, false } - return rune, 4, false + return r, 4, false } // error return RuneError, 1, false } -func decodeRuneInStringInternal(s string) (rune, size int, short bool) { +func decodeRuneInStringInternal(s string) (r rune, size int, short bool) { n := len(s) if n < 1 { return RuneError, 0, true @@ -118,7 +118,7 @@ func decodeRuneInStringInternal(s string) (rune, size int, short bool) { // 1-byte, 7-bit sequence? if c0 < tx { - return int(c0), 1, false + return rune(c0), 1, false } // unexpected continuation byte? @@ -137,11 +137,11 @@ func decodeRuneInStringInternal(s string) (rune, size int, short bool) { // 2-byte, 11-bit sequence? if c0 < t3 { - rune = int(c0&mask2)<<6 | int(c1&maskx) - if rune <= rune1Max { + r = rune(c0&mask2)<<6 | rune(c1&maskx) + if r <= rune1Max { return RuneError, 1, false } - return rune, 2, false + return r, 2, false } // need second continuation byte @@ -155,11 +155,11 @@ func decodeRuneInStringInternal(s string) (rune, size int, short bool) { // 3-byte, 16-bit sequence? if c0 < t4 { - rune = int(c0&mask3)<<12 | int(c1&maskx)<<6 | int(c2&maskx) - if rune <= rune2Max { + r = rune(c0&mask3)<<12 | rune(c1&maskx)<<6 | rune(c2&maskx) + if r <= rune2Max { return RuneError, 1, false } - return rune, 3, false + return r, 3, false } // need third continuation byte @@ -173,11 +173,11 @@ func decodeRuneInStringInternal(s string) (rune, size int, short bool) { // 4-byte, 21-bit sequence? if c0 < t5 { - rune = int(c0&mask4)<<18 | int(c1&maskx)<<12 | int(c2&maskx)<<6 | int(c3&maskx) - if rune <= rune3Max { + r = rune(c0&mask4)<<18 | rune(c1&maskx)<<12 | rune(c2&maskx)<<6 | rune(c3&maskx) + if r <= rune3Max { return RuneError, 1, false } - return rune, 4, false + return r, 4, false } // error @@ -198,28 +198,28 @@ func FullRuneInString(s string) bool { } // DecodeRune unpacks the first UTF-8 encoding in p and returns the rune and its width in bytes. -func DecodeRune(p []byte) (rune, size int) { - rune, size, _ = decodeRuneInternal(p) +func DecodeRune(p []byte) (r rune, size int) { + r, size, _ = decodeRuneInternal(p) return } // DecodeRuneInString is like DecodeRune but its input is a string. -func DecodeRuneInString(s string) (rune, size int) { - rune, size, _ = decodeRuneInStringInternal(s) +func DecodeRuneInString(s string) (r rune, size int) { + r, size, _ = decodeRuneInStringInternal(s) return } // DecodeLastRune unpacks the last UTF-8 encoding in p // and returns the rune and its width in bytes. -func DecodeLastRune(p []byte) (rune, size int) { +func DecodeLastRune(p []byte) (r rune, size int) { end := len(p) if end == 0 { return RuneError, 0 } start := end - 1 - rune = int(p[start]) - if rune < RuneSelf { - return rune, 1 + r = rune(p[start]) + if r < RuneSelf { + return r, 1 } // guard against O(n^2) behavior when traversing // backwards through strings with long sequences of @@ -236,23 +236,23 @@ func DecodeLastRune(p []byte) (rune, size int) { if start < 0 { start = 0 } - rune, size = DecodeRune(p[start:end]) + r, size = DecodeRune(p[start:end]) if start+size != end { return RuneError, 1 } - return rune, size + return r, size } // DecodeLastRuneInString is like DecodeLastRune but its input is a string. -func DecodeLastRuneInString(s string) (rune, size int) { +func DecodeLastRuneInString(s string) (r rune, size int) { end := len(s) if end == 0 { return RuneError, 0 } start := end - 1 - rune = int(s[start]) - if rune < RuneSelf { - return rune, 1 + r = rune(s[start]) + if r < RuneSelf { + return r, 1 } // guard against O(n^2) behavior when traversing // backwards through strings with long sequences of @@ -269,23 +269,23 @@ func DecodeLastRuneInString(s string) (rune, size int) { if start < 0 { start = 0 } - rune, size = DecodeRuneInString(s[start:end]) + r, size = DecodeRuneInString(s[start:end]) if start+size != end { return RuneError, 1 } - return rune, size + return r, size } // RuneLen returns the number of bytes required to encode the rune. -func RuneLen(rune int) int { +func RuneLen(r rune) int { switch { - case rune <= rune1Max: + case r <= rune1Max: return 1 - case rune <= rune2Max: + case r <= rune2Max: return 2 - case rune <= rune3Max: + case r <= rune3Max: return 3 - case rune <= rune4Max: + case r <= rune4Max: return 4 } return -1 @@ -293,26 +293,24 @@ func RuneLen(rune int) int { // EncodeRune writes into p (which must be large enough) the UTF-8 encoding of the rune. // It returns the number of bytes written. -func EncodeRune(p []byte, rune int) int { +func EncodeRune(p []byte, r rune) int { // Negative values are erroneous. Making it unsigned addresses the problem. - r := uint(rune) - - if r <= rune1Max { + if uint32(r) <= rune1Max { p[0] = byte(r) return 1 } - if r <= rune2Max { + if uint32(r) <= rune2Max { p[0] = t2 | byte(r>>6) p[1] = tx | byte(r)&maskx return 2 } - if r > unicode.MaxRune { + if uint32(r) > unicode.MaxRune { r = RuneError } - if r <= rune3Max { + if uint32(r) <= rune3Max { p[0] = t3 | byte(r>>12) p[1] = tx | byte(r>>6)&maskx p[2] = tx | byte(r)&maskx diff --git a/libgo/go/utf8/utf8_test.go b/libgo/go/utf8/utf8_test.go index 6cbbebc..857bcf6 100644 --- a/libgo/go/utf8/utf8_test.go +++ b/libgo/go/utf8/utf8_test.go @@ -11,8 +11,8 @@ import ( ) type Utf8Map struct { - rune int - str string + r rune + str string } var utf8map = []Utf8Map{ @@ -58,11 +58,11 @@ func TestFullRune(t *testing.T) { m := utf8map[i] b := []byte(m.str) if !FullRune(b) { - t.Errorf("FullRune(%q) (%U) = false, want true", b, m.rune) + t.Errorf("FullRune(%q) (%U) = false, want true", b, m.r) } s := m.str if !FullRuneInString(s) { - t.Errorf("FullRuneInString(%q) (%U) = false, want true", s, m.rune) + t.Errorf("FullRuneInString(%q) (%U) = false, want true", s, m.r) } b1 := b[0 : len(b)-1] if FullRune(b1) { @@ -80,10 +80,10 @@ func TestEncodeRune(t *testing.T) { m := utf8map[i] b := []byte(m.str) var buf [10]byte - n := EncodeRune(buf[0:], m.rune) + n := EncodeRune(buf[0:], m.r) b1 := buf[0:n] if !bytes.Equal(b, b1) { - t.Errorf("EncodeRune(%#04x) = %q want %q", m.rune, b1, b) + t.Errorf("EncodeRune(%#04x) = %q want %q", m.r, b1, b) } } } @@ -92,25 +92,25 @@ func TestDecodeRune(t *testing.T) { for i := 0; i < len(utf8map); i++ { m := utf8map[i] b := []byte(m.str) - rune, size := DecodeRune(b) - if rune != m.rune || size != len(b) { - t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, rune, size, m.rune, len(b)) + r, size := DecodeRune(b) + if r != m.r || size != len(b) { + t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, r, size, m.r, len(b)) } s := m.str - rune, size = DecodeRuneInString(s) - if rune != m.rune || size != len(b) { - t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", s, rune, size, m.rune, len(b)) + r, size = DecodeRuneInString(s) + if r != m.r || size != len(b) { + t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", s, r, size, m.r, len(b)) } // there's an extra byte that bytes left behind - make sure trailing byte works - rune, size = DecodeRune(b[0:cap(b)]) - if rune != m.rune || size != len(b) { - t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, rune, size, m.rune, len(b)) + r, size = DecodeRune(b[0:cap(b)]) + if r != m.r || size != len(b) { + t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, r, size, m.r, len(b)) } s = m.str + "\x00" - rune, size = DecodeRuneInString(s) - if rune != m.rune || size != len(b) { - t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, rune, size, m.rune, len(b)) + r, size = DecodeRuneInString(s) + if r != m.r || size != len(b) { + t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, r, size, m.r, len(b)) } // make sure missing bytes fail @@ -118,14 +118,14 @@ func TestDecodeRune(t *testing.T) { if wantsize >= len(b) { wantsize = 0 } - rune, size = DecodeRune(b[0 : len(b)-1]) - if rune != RuneError || size != wantsize { - t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b[0:len(b)-1], rune, size, RuneError, wantsize) + r, size = DecodeRune(b[0 : len(b)-1]) + if r != RuneError || size != wantsize { + t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b[0:len(b)-1], r, size, RuneError, wantsize) } s = m.str[0 : len(m.str)-1] - rune, size = DecodeRuneInString(s) - if rune != RuneError || size != wantsize { - t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, rune, size, RuneError, wantsize) + r, size = DecodeRuneInString(s) + if r != RuneError || size != wantsize { + t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, r, size, RuneError, wantsize) } // make sure bad sequences fail @@ -134,14 +134,14 @@ func TestDecodeRune(t *testing.T) { } else { b[len(b)-1] = 0x7F } - rune, size = DecodeRune(b) - if rune != RuneError || size != 1 { - t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, rune, size, RuneError, 1) + r, size = DecodeRune(b) + if r != RuneError || size != 1 { + t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, r, size, RuneError, 1) } s = string(b) - rune, size = DecodeRune(b) - if rune != RuneError || size != 1 { - t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, rune, size, RuneError, 1) + r, size = DecodeRune(b) + if r != RuneError || size != 1 { + t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, r, size, RuneError, 1) } } @@ -164,7 +164,7 @@ func TestSequencing(t *testing.T) { // it's good to verify func TestIntConversion(t *testing.T) { for _, ts := range testStrings { - runes := []int(ts) + runes := []rune(ts) if RuneCountInString(ts) != len(runes) { t.Errorf("%q: expected %d runes; got %d", ts, len(runes), RuneCountInString(ts)) break @@ -182,7 +182,7 @@ func TestIntConversion(t *testing.T) { func testSequence(t *testing.T, s string) { type info struct { index int - rune int + r rune } index := make([]info, len(s)) b := []byte(s) @@ -195,14 +195,14 @@ func testSequence(t *testing.T, s string) { } index[j] = info{i, r} j++ - rune1, size1 := DecodeRune(b[i:]) - if r != rune1 { - t.Errorf("DecodeRune(%q) = %#04x, want %#04x", s[i:], rune1, r) + r1, size1 := DecodeRune(b[i:]) + if r != r1 { + t.Errorf("DecodeRune(%q) = %#04x, want %#04x", s[i:], r1, r) return } - rune2, size2 := DecodeRuneInString(s[i:]) - if r != rune2 { - t.Errorf("DecodeRuneInString(%q) = %#04x, want %#04x", s[i:], rune2, r) + r2, size2 := DecodeRuneInString(s[i:]) + if r != r2 { + t.Errorf("DecodeRuneInString(%q) = %#04x, want %#04x", s[i:], r2, r) return } if size1 != size2 { @@ -213,18 +213,18 @@ func testSequence(t *testing.T, s string) { } j-- for si = len(s); si > 0; { - rune1, size1 := DecodeLastRune(b[0:si]) - rune2, size2 := DecodeLastRuneInString(s[0:si]) + r1, size1 := DecodeLastRune(b[0:si]) + r2, size2 := DecodeLastRuneInString(s[0:si]) if size1 != size2 { t.Errorf("DecodeLastRune/DecodeLastRuneInString(%q, %d) size mismatch %d/%d", s, si, size1, size2) return } - if rune1 != index[j].rune { - t.Errorf("DecodeLastRune(%q, %d) = %#04x, want %#04x", s, si, rune1, index[j].rune) + if r1 != index[j].r { + t.Errorf("DecodeLastRune(%q, %d) = %#04x, want %#04x", s, si, r1, index[j].r) return } - if rune2 != index[j].rune { - t.Errorf("DecodeLastRuneInString(%q, %d) = %#04x, want %#04x", s, si, rune2, index[j].rune) + if r2 != index[j].r { + t.Errorf("DecodeLastRuneInString(%q, %d) = %#04x, want %#04x", s, si, r2, index[j].r) return } si -= size1 |