github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/unicode/utf8/utf8.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package utf8 implements functions and constants to support text encoded in 6 // UTF-8. It includes functions to translate between runes and UTF-8 byte sequences. 7 package utf8 8 9 // The conditions RuneError==unicode.ReplacementChar and 10 // MaxRune==unicode.MaxRune are verified in the tests. 11 // Defining them locally avoids this package depending on package unicode. 12 13 // Numbers fundamental to the encoding. 14 const ( 15 RuneError = '\uFFFD' // the "error" Rune or "Unicode replacement character" 16 RuneSelf = 0x80 // characters below Runeself are represented as themselves in a single byte. 17 MaxRune = '\U0010FFFF' // Maximum valid Unicode code point. 18 UTFMax = 4 // maximum number of bytes of a UTF-8 encoded Unicode character. 19 ) 20 21 // Code points in the surrogate range are not valid for UTF-8. 22 const ( 23 surrogateMin = 0xD800 24 surrogateMax = 0xDFFF 25 ) 26 27 const ( 28 t1 = 0x00 // 0000 0000 29 tx = 0x80 // 1000 0000 30 t2 = 0xC0 // 1100 0000 31 t3 = 0xE0 // 1110 0000 32 t4 = 0xF0 // 1111 0000 33 t5 = 0xF8 // 1111 1000 34 35 maskx = 0x3F // 0011 1111 36 mask2 = 0x1F // 0001 1111 37 mask3 = 0x0F // 0000 1111 38 mask4 = 0x07 // 0000 0111 39 40 rune1Max = 1<<7 - 1 41 rune2Max = 1<<11 - 1 42 rune3Max = 1<<16 - 1 43 44 // The default lowest and highest continuation byte. 45 locb = 0x80 // 1000 0000 46 hicb = 0xBF // 1011 1111 47 48 // These names of these constants are chosen to give nice alignment in the 49 // table below. The first nibble is an index into acceptRanges or F for 50 // special one-byte cases. The second nibble is the Rune length or the 51 // Status for the special one-byte case. 52 xx = 0xF1 // invalid: size 1 53 as = 0xF0 // ASCII: size 1 54 s1 = 0x02 // accept 0, size 2 55 s2 = 0x13 // accept 1, size 3 56 s3 = 0x03 // accept 0, size 3 57 s4 = 0x23 // accept 2, size 3 58 s5 = 0x34 // accept 3, size 4 59 s6 = 0x04 // accept 0, size 4 60 s7 = 0x44 // accept 4, size 4 61 ) 62 63 // first is information about the first byte in a UTF-8 sequence. 64 var first = [256]uint8{ 65 // 1 2 3 4 5 6 7 8 9 A B C D E F 66 as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x00-0x0F 67 as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x10-0x1F 68 as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x20-0x2F 69 as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x30-0x3F 70 as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x40-0x4F 71 as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x50-0x5F 72 as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x60-0x6F 73 as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x70-0x7F 74 // 1 2 3 4 5 6 7 8 9 A B C D E F 75 xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x80-0x8F 76 xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x90-0x9F 77 xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xA0-0xAF 78 xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xB0-0xBF 79 xx, xx, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xC0-0xCF 80 s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xD0-0xDF 81 s2, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s4, s3, s3, // 0xE0-0xEF 82 s5, s6, s6, s6, s7, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xF0-0xFF 83 } 84 85 // acceptRange gives the range of valid values for the second byte in a UTF-8 86 // sequence. 87 type acceptRange struct { 88 lo uint8 // lowest value for second byte. 89 hi uint8 // highest value for second byte. 90 } 91 92 var acceptRanges = [...]acceptRange{ 93 0: {locb, hicb}, 94 1: {0xA0, hicb}, 95 2: {locb, 0x9F}, 96 3: {0x90, hicb}, 97 4: {locb, 0x8F}, 98 } 99 100 // FullRune reports whether the bytes in p begin with a full UTF-8 encoding of a rune. 101 // An invalid encoding is considered a full Rune since it will convert as a width-1 error rune. 102 func FullRune(p []byte) bool { 103 n := len(p) 104 if n == 0 { 105 return false 106 } 107 x := first[p[0]] 108 if n >= int(x&7) { 109 return true // ASCII, invalid or valid. 110 } 111 // Must be short or invalid. 112 accept := acceptRanges[x>>4] 113 if n > 1 { 114 if c := p[1]; c < accept.lo || accept.hi < c { 115 return true 116 } else if n > 2 && (p[2] < locb || hicb < p[2]) { 117 return true 118 } 119 } 120 return false 121 } 122 123 // FullRuneInString is like FullRune but its input is a string. 124 func FullRuneInString(s string) bool { 125 n := len(s) 126 if n == 0 { 127 return false 128 } 129 x := first[s[0]] 130 if n >= int(x&7) { 131 return true // ASCII, invalid, or valid. 132 } 133 // Must be short or invalid. 134 accept := acceptRanges[x>>4] 135 if n > 1 { 136 if c := s[1]; c < accept.lo || accept.hi < c { 137 return true 138 } else if n > 2 && (s[2] < locb || hicb < s[2]) { 139 return true 140 } 141 } 142 return false 143 } 144 145 // DecodeRune unpacks the first UTF-8 encoding in p and returns the rune and 146 // its width in bytes. If p is empty it returns (RuneError, 0). Otherwise, if 147 // the encoding is invalid, it returns (RuneError, 1). Both are impossible 148 // results for correct, non-empty UTF-8. 149 // 150 // An encoding is invalid if it is incorrect UTF-8, encodes a rune that is 151 // out of range, or is not the shortest possible UTF-8 encoding for the 152 // value. No other validation is performed. 153 func DecodeRune(p []byte) (r rune, size int) { 154 n := len(p) 155 if n < 1 { 156 return RuneError, 0 157 } 158 p0 := p[0] 159 x := first[p0] 160 if x >= as { 161 // The following code simulates an additional check for x == xx and 162 // handling the ASCII and invalid cases accordingly. This mask-and-or 163 // approach prevents an additional branch. 164 mask := rune(x) << 31 >> 31 // Create 0x0000 or 0xFFFF. 165 return rune(p[0])&^mask | RuneError&mask, 1 166 } 167 sz := x & 7 168 accept := acceptRanges[x>>4] 169 if n < int(sz) { 170 return RuneError, 1 171 } 172 b1 := p[1] 173 if b1 < accept.lo || accept.hi < b1 { 174 return RuneError, 1 175 } 176 if sz == 2 { 177 return rune(p0&mask2)<<6 | rune(b1&maskx), 2 178 } 179 b2 := p[2] 180 if b2 < locb || hicb < b2 { 181 return RuneError, 1 182 } 183 if sz == 3 { 184 return rune(p0&mask3)<<12 | rune(b1&maskx)<<6 | rune(b2&maskx), 3 185 } 186 b3 := p[3] 187 if b3 < locb || hicb < b3 { 188 return RuneError, 1 189 } 190 return rune(p0&mask4)<<18 | rune(b1&maskx)<<12 | rune(b2&maskx)<<6 | rune(b3&maskx), 4 191 } 192 193 // DecodeRuneInString is like DecodeRune but its input is a string. If s is 194 // empty it returns (RuneError, 0). Otherwise, if the encoding is invalid, it 195 // returns (RuneError, 1). Both are impossible results for correct, non-empty 196 // UTF-8. 197 // 198 // An encoding is invalid if it is incorrect UTF-8, encodes a rune that is 199 // out of range, or is not the shortest possible UTF-8 encoding for the 200 // value. No other validation is performed. 201 func DecodeRuneInString(s string) (r rune, size int) { 202 n := len(s) 203 if n < 1 { 204 return RuneError, 0 205 } 206 s0 := s[0] 207 x := first[s0] 208 if x >= as { 209 // The following code simulates an additional check for x == xx and 210 // handling the ASCII and invalid cases accordingly. This mask-and-or 211 // approach prevents an additional branch. 212 mask := rune(x) << 31 >> 31 // Create 0x0000 or 0xFFFF. 213 return rune(s[0])&^mask | RuneError&mask, 1 214 } 215 sz := x & 7 216 accept := acceptRanges[x>>4] 217 if n < int(sz) { 218 return RuneError, 1 219 } 220 s1 := s[1] 221 if s1 < accept.lo || accept.hi < s1 { 222 return RuneError, 1 223 } 224 if sz == 2 { 225 return rune(s0&mask2)<<6 | rune(s1&maskx), 2 226 } 227 s2 := s[2] 228 if s2 < locb || hicb < s2 { 229 return RuneError, 1 230 } 231 if sz == 3 { 232 return rune(s0&mask3)<<12 | rune(s1&maskx)<<6 | rune(s2&maskx), 3 233 } 234 s3 := s[3] 235 if s3 < locb || hicb < s3 { 236 return RuneError, 1 237 } 238 return rune(s0&mask4)<<18 | rune(s1&maskx)<<12 | rune(s2&maskx)<<6 | rune(s3&maskx), 4 239 } 240 241 // DecodeLastRune unpacks the last UTF-8 encoding in p and returns the rune and 242 // its width in bytes. If p is empty it returns (RuneError, 0). Otherwise, if 243 // the encoding is invalid, it returns (RuneError, 1). Both are impossible 244 // results for correct, non-empty UTF-8. 245 // 246 // An encoding is invalid if it is incorrect UTF-8, encodes a rune that is 247 // out of range, or is not the shortest possible UTF-8 encoding for the 248 // value. No other validation is performed. 249 func DecodeLastRune(p []byte) (r rune, size int) { 250 end := len(p) 251 if end == 0 { 252 return RuneError, 0 253 } 254 start := end - 1 255 r = rune(p[start]) 256 if r < RuneSelf { 257 return r, 1 258 } 259 // guard against O(n^2) behavior when traversing 260 // backwards through strings with long sequences of 261 // invalid UTF-8. 262 lim := end - UTFMax 263 if lim < 0 { 264 lim = 0 265 } 266 for start--; start >= lim; start-- { 267 if RuneStart(p[start]) { 268 break 269 } 270 } 271 if start < 0 { 272 start = 0 273 } 274 r, size = DecodeRune(p[start:end]) 275 if start+size != end { 276 return RuneError, 1 277 } 278 return r, size 279 } 280 281 // DecodeLastRuneInString is like DecodeLastRune but its input is a string. If 282 // s is empty it returns (RuneError, 0). Otherwise, if the encoding is invalid, 283 // it returns (RuneError, 1). Both are impossible results for correct, 284 // non-empty UTF-8. 285 // 286 // An encoding is invalid if it is incorrect UTF-8, encodes a rune that is 287 // out of range, or is not the shortest possible UTF-8 encoding for the 288 // value. No other validation is performed. 289 func DecodeLastRuneInString(s string) (r rune, size int) { 290 end := len(s) 291 if end == 0 { 292 return RuneError, 0 293 } 294 start := end - 1 295 r = rune(s[start]) 296 if r < RuneSelf { 297 return r, 1 298 } 299 // guard against O(n^2) behavior when traversing 300 // backwards through strings with long sequences of 301 // invalid UTF-8. 302 lim := end - UTFMax 303 if lim < 0 { 304 lim = 0 305 } 306 for start--; start >= lim; start-- { 307 if RuneStart(s[start]) { 308 break 309 } 310 } 311 if start < 0 { 312 start = 0 313 } 314 r, size = DecodeRuneInString(s[start:end]) 315 if start+size != end { 316 return RuneError, 1 317 } 318 return r, size 319 } 320 321 // RuneLen returns the number of bytes required to encode the rune. 322 // It returns -1 if the rune is not a valid value to encode in UTF-8. 323 func RuneLen(r rune) int { 324 switch { 325 case r < 0: 326 return -1 327 case r <= rune1Max: 328 return 1 329 case r <= rune2Max: 330 return 2 331 case surrogateMin <= r && r <= surrogateMax: 332 return -1 333 case r <= rune3Max: 334 return 3 335 case r <= MaxRune: 336 return 4 337 } 338 return -1 339 } 340 341 // EncodeRune writes into p (which must be large enough) the UTF-8 encoding of the rune. 342 // It returns the number of bytes written. 343 func EncodeRune(p []byte, r rune) int { 344 // Negative values are erroneous. Making it unsigned addresses the problem. 345 switch i := uint32(r); { 346 case i <= rune1Max: 347 p[0] = byte(r) 348 return 1 349 case i <= rune2Max: 350 p[0] = t2 | byte(r>>6) 351 p[1] = tx | byte(r)&maskx 352 return 2 353 case i > MaxRune, surrogateMin <= i && i <= surrogateMax: 354 r = RuneError 355 fallthrough 356 case i <= rune3Max: 357 p[0] = t3 | byte(r>>12) 358 p[1] = tx | byte(r>>6)&maskx 359 p[2] = tx | byte(r)&maskx 360 return 3 361 default: 362 p[0] = t4 | byte(r>>18) 363 p[1] = tx | byte(r>>12)&maskx 364 p[2] = tx | byte(r>>6)&maskx 365 p[3] = tx | byte(r)&maskx 366 return 4 367 } 368 } 369 370 // RuneCount returns the number of runes in p. Erroneous and short 371 // encodings are treated as single runes of width 1 byte. 372 func RuneCount(p []byte) int { 373 np := len(p) 374 var n int 375 for i := 0; i < np; { 376 n++ 377 c := p[i] 378 if c < RuneSelf { 379 // ASCII fast path 380 i++ 381 continue 382 } 383 x := first[c] 384 if x == xx { 385 i++ // invalid. 386 continue 387 } 388 size := int(x & 7) 389 if i+size > np { 390 i++ // Short or invalid. 391 continue 392 } 393 accept := acceptRanges[x>>4] 394 if c := p[i+1]; c < accept.lo || accept.hi < c { 395 size = 1 396 } else if size == 2 { 397 } else if c := p[i+2]; c < locb || hicb < c { 398 size = 1 399 } else if size == 3 { 400 } else if c := p[i+3]; c < locb || hicb < c { 401 size = 1 402 } 403 i += size 404 } 405 return n 406 } 407 408 // RuneCountInString is like RuneCount but its input is a string. 409 func RuneCountInString(s string) (n int) { 410 ns := len(s) 411 for i := 0; i < ns; n++ { 412 c := s[i] 413 if c < RuneSelf { 414 // ASCII fast path 415 i++ 416 continue 417 } 418 x := first[c] 419 if x == xx { 420 i++ // invalid. 421 continue 422 } 423 size := int(x & 7) 424 if i+size > ns { 425 i++ // Short or invalid. 426 continue 427 } 428 accept := acceptRanges[x>>4] 429 if c := s[i+1]; c < accept.lo || accept.hi < c { 430 size = 1 431 } else if size == 2 { 432 } else if c := s[i+2]; c < locb || hicb < c { 433 size = 1 434 } else if size == 3 { 435 } else if c := s[i+3]; c < locb || hicb < c { 436 size = 1 437 } 438 i += size 439 } 440 return n 441 } 442 443 // RuneStart reports whether the byte could be the first byte of an encoded, 444 // possibly invalid rune. Second and subsequent bytes always have the top two 445 // bits set to 10. 446 func RuneStart(b byte) bool { return b&0xC0 != 0x80 } 447 448 // Valid reports whether p consists entirely of valid UTF-8-encoded runes. 449 func Valid(p []byte) bool { 450 n := len(p) 451 for i := 0; i < n; { 452 pi := p[i] 453 if pi < RuneSelf { 454 i++ 455 continue 456 } 457 x := first[pi] 458 if x == xx { 459 return false // Illegal starter byte. 460 } 461 size := int(x & 7) 462 if i+size > n { 463 return false // Short or invalid. 464 } 465 accept := acceptRanges[x>>4] 466 if c := p[i+1]; c < accept.lo || accept.hi < c { 467 return false 468 } else if size == 2 { 469 } else if c := p[i+2]; c < locb || hicb < c { 470 return false 471 } else if size == 3 { 472 } else if c := p[i+3]; c < locb || hicb < c { 473 return false 474 } 475 i += size 476 } 477 return true 478 } 479 480 // ValidString reports whether s consists entirely of valid UTF-8-encoded runes. 481 func ValidString(s string) bool { 482 n := len(s) 483 for i := 0; i < n; { 484 si := s[i] 485 if si < RuneSelf { 486 i++ 487 continue 488 } 489 x := first[si] 490 if x == xx { 491 return false // Illegal starter byte. 492 } 493 size := int(x & 7) 494 if i+size > n { 495 return false // Short or invalid. 496 } 497 accept := acceptRanges[x>>4] 498 if c := s[i+1]; c < accept.lo || accept.hi < c { 499 return false 500 } else if size == 2 { 501 } else if c := s[i+2]; c < locb || hicb < c { 502 return false 503 } else if size == 3 { 504 } else if c := s[i+3]; c < locb || hicb < c { 505 return false 506 } 507 i += size 508 } 509 return true 510 } 511 512 // ValidRune reports whether r can be legally encoded as UTF-8. 513 // Code points that are out of range or a surrogate half are illegal. 514 func ValidRune(r rune) bool { 515 switch { 516 case r < 0: 517 return false 518 case surrogateMin <= r && r <= surrogateMax: 519 return false 520 case r > MaxRune: 521 return false 522 } 523 return true 524 }