diff options
author | Rob Pike <r@golang.org> | 2009-04-12 17:01:17 -0700 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2009-04-12 17:01:17 -0700 |
commit | 2256f3ca6967cfec702161d025e5b0ea38fa157a (patch) | |
tree | d32d2a8598ce38c4d0b60ae2b367a3a520d063ac /src/runtime/string.c | |
parent | 44b201710f3f44e1a19b66d428575f43086e2796 (diff) | |
download | golang-2256f3ca6967cfec702161d025e5b0ea38fa157a.tar.gz |
fix string range to have full unicode range (up to 10FFFF).
add test for string range.
test has minor failure: after loop the index == len(s); should be len(s)-1
in this case. according to spec, vars are left at position at last
iteration.
R=ken,rsc
DELTA=259 (161 added, 96 deleted, 2 changed)
OCL=27343
CL=27343
Diffstat (limited to 'src/runtime/string.c')
-rw-r--r-- | src/runtime/string.c | 100 |
1 files changed, 2 insertions, 98 deletions
diff --git a/src/runtime/string.c b/src/runtime/string.c index 4d89e9600..b9b361861 100644 --- a/src/runtime/string.c +++ b/src/runtime/string.c @@ -189,11 +189,9 @@ sys·arraystring(Array b, String s) FLUSH(&s); } -static int32 chartorune(int32 *rune, byte *str); enum { Runeself = 0x80, - Runeerror = 0xfffd, }; // func stringiter(string, int) (retk int); @@ -213,13 +211,7 @@ sys·stringiter(String s, int32 k, int32 retk) if(l >= Runeself) { // multi-char rune - n = chartorune(&l, s.str+k); - if(k+n > s.len) { - // special case of multi-char rune - // that ran off end of string - l = Runeerror; - n = 1; - } + n = charntorune(&l, s.str+k, s.len-k); } retk = k+n; @@ -246,13 +238,7 @@ sys·stringiter2(String s, int32 k, int32 retk, int32 retv) if(l >= Runeself) { // multi-char rune - n = chartorune(&l, s.str+k); - if(k+n > s.len) { - // special case of multi-char rune - // that ran off end of string - l = Runeerror; - n = 1; - } + n = charntorune(&l, s.str+k, s.len-k); } retk = k+n; @@ -262,85 +248,3 @@ out: FLUSH(&retk); FLUSH(&retv); } - -// -// copied from plan9 library -// - -enum -{ - Bit1 = 7, - Bitx = 6, - Bit2 = 5, - Bit3 = 4, - Bit4 = 3, - - T1 = ((1<<(Bit1+1))-1) ^ 0xFF, /* 0000 0000 */ - Tx = ((1<<(Bitx+1))-1) ^ 0xFF, /* 1000 0000 */ - T2 = ((1<<(Bit2+1))-1) ^ 0xFF, /* 1100 0000 */ - T3 = ((1<<(Bit3+1))-1) ^ 0xFF, /* 1110 0000 */ - T4 = ((1<<(Bit4+1))-1) ^ 0xFF, /* 1111 0000 */ - - Rune1 = (1<<(Bit1+0*Bitx))-1, /* 0000 0000 0111 1111 */ - Rune2 = (1<<(Bit2+1*Bitx))-1, /* 0000 0111 1111 1111 */ - Rune3 = (1<<(Bit3+2*Bitx))-1, /* 1111 1111 1111 1111 */ - - Maskx = (1<<Bitx)-1, /* 0011 1111 */ - Testx = Maskx ^ 0xFF, /* 1100 0000 */ -}; - -static int32 -chartorune(int32 *rune, byte *str) -{ - int32 c, c1, c2; - int32 l; - - /* - * one character sequence - * 00000-0007F => T1 - */ - c = str[0]; - if(c < Tx) { - *rune = c; - return 1; - } - - /* - * two character sequence - * 0080-07FF => T2 Tx - */ - c1 = str[1] ^ Tx; - if(c1 & Testx) - goto bad; - if(c < T3) { - if(c < T2) - goto bad; - l = ((c << Bitx) | c1) & Rune2; - if(l <= Rune1) - goto bad; - *rune = l; - return 2; - } - - /* - * three character sequence - * 0800-FFFF => T3 Tx Tx - */ - c2 = str[2] ^ Tx; - if(c2 & Testx) - goto bad; - if(c < T4) { - l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3; - if(l <= Rune2) - goto bad; - *rune = l; - return 3; - } - - /* - * bad decoding - */ -bad: - *rune = Runeerror; - return 1; -} |