2
* The authors of this software are Rob Pike and Ken Thompson.
3
* Copyright (c) 2002 by Lucent Technologies.
4
* Portions Copyright 2009 The Go Authors. All rights reserved.
5
* Permission to use, copy, modify, and distribute this software for any
6
* purpose without fee is hereby granted, provided that this entire notice
7
* is included in all copies of any software which is or includes a copy
8
* or modification of this software and in all copies of the supporting
9
* documentation for such software.
10
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
11
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
12
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
13
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
17
* This code is copied, with slight editing due to type differences,
18
* from a subset of ../lib9/utf/rune.c
32
T1 = ((1<<(Bit1+1))-1) ^ 0xFF, /* 0000 0000 */
33
Tx = ((1<<(Bitx+1))-1) ^ 0xFF, /* 1000 0000 */
34
T2 = ((1<<(Bit2+1))-1) ^ 0xFF, /* 1100 0000 */
35
T3 = ((1<<(Bit3+1))-1) ^ 0xFF, /* 1110 0000 */
36
T4 = ((1<<(Bit4+1))-1) ^ 0xFF, /* 1111 0000 */
37
T5 = ((1<<(Bit5+1))-1) ^ 0xFF, /* 1111 1000 */
39
Rune1 = (1<<(Bit1+0*Bitx))-1, /* 0000 0000 0111 1111 */
40
Rune2 = (1<<(Bit2+1*Bitx))-1, /* 0000 0111 1111 1111 */
41
Rune3 = (1<<(Bit3+2*Bitx))-1, /* 1111 1111 1111 1111 */
42
Rune4 = (1<<(Bit4+3*Bitx))-1, /* 0001 1111 1111 1111 1111 1111 */
44
Maskx = (1<<Bitx)-1, /* 0011 1111 */
45
Testx = Maskx ^ 0xFF, /* 1100 0000 */
50
SurrogateMin = 0xD800,
51
SurrogateMax = 0xDFFF,
55
Runemax = 0x10FFFF, /* maximum rune value */
59
* Modified by Wei-Hwa Huang, Google Inc., on 2004-09-24
60
* This is a slower but "safe" version of the old chartorune
61
* that works on strings that are not necessarily null-terminated.
63
* If you know for sure that your string is null-terminated,
64
* chartorune will be a bit faster.
66
* It is guaranteed not to attempt to access "length"
67
* past the incoming pointer. This is to avoid
68
* possible access violations. If the string appears to be
69
* well-formed but incomplete (i.e., to get the whole Rune
70
* we'd need to read past str+length) then we'll set the Rune
71
* to Bad and return 0.
73
* Note that if we have decoding problems for other
74
* reasons, we return 1 instead of 0.
77
runtime·charntorune(int32 *rune, uint8 *str, int32 length)
79
int32 c, c1, c2, c3, l;
81
/* When we're not allowed to read anything */
87
* one character sequence (7-bit value)
96
// If we can't read more than one character we must stop
102
* two character sequence (11-bit value)
105
c1 = *(uint8*)(str+1) ^ Tx;
111
l = ((c << Bitx) | c1) & Rune2;
118
// If we can't read more than two characters we must stop
124
* three character sequence (16-bit value)
125
* 0800-FFFF => T3 Tx Tx
127
c2 = *(uint8*)(str+2) ^ Tx;
131
l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
134
if (SurrogateMin <= l && l <= SurrogateMax)
144
* four character sequence (21-bit value)
145
* 10000-1FFFFF => T4 Tx Tx Tx
147
c3 = *(uint8*)(str+3) ^ Tx;
151
l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4;
152
if (l <= Rune3 || l > Runemax)
158
// Support for 5-byte or longer UTF-8 would go here, but
159
// since we don't have that, we'll just fall through to bad.
168
// was return 0, but return 1 is more convenient for the runtime.
175
runtime·runetochar(byte *str, int32 rune) /* note: in original, arg2 was pointer */
177
/* Runes are signed, so convert to unsigned for range check. */
181
* one character sequence
182
* 00000-0007F => 00-7F
191
* two character sequence
195
str[0] = T2 | (c >> 1*Bitx);
196
str[1] = Tx | (c & Maskx);
201
* If the Rune is out of range or a surrogate half, convert it to the error rune.
202
* Do this test here because the error rune encodes to three bytes.
203
* Doing it earlier would duplicate work, since an out of range
204
* Rune wouldn't have fit in one or two bytes.
208
if (SurrogateMin <= c && c <= SurrogateMax)
212
* three character sequence
213
* 0800-FFFF => T3 Tx Tx
216
str[0] = T3 | (c >> 2*Bitx);
217
str[1] = Tx | ((c >> 1*Bitx) & Maskx);
218
str[2] = Tx | (c & Maskx);
223
* four character sequence (21-bit value)
224
* 10000-1FFFFF => T4 Tx Tx Tx
226
str[0] = T4 | (c >> 3*Bitx);
227
str[1] = Tx | ((c >> 2*Bitx) & Maskx);
228
str[2] = Tx | ((c >> 1*Bitx) & Maskx);
229
str[3] = Tx | (c & Maskx);