utf8internal.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2015 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. // Package utf8internal contains low-level utf8-related constants, tables, etc.
  5. // that are used internally by the text package.
  6. package utf8internal
  7. // The default lowest and highest continuation byte.
  8. const (
  9. LoCB = 0x80 // 1000 0000
  10. HiCB = 0xBF // 1011 1111
  11. )
  12. // Constants related to getting information of first bytes of UTF-8 sequences.
  13. const (
  14. // ASCII identifies a UTF-8 byte as ASCII.
  15. ASCII = as
  16. // FirstInvalid indicates a byte is invalid as a first byte of a UTF-8
  17. // sequence.
  18. FirstInvalid = xx
  19. // SizeMask is a mask for the size bits. Use use x&SizeMask to get the size.
  20. SizeMask = 7
  21. // AcceptShift is the right-shift count for the first byte info byte to get
  22. // the index into the AcceptRanges table. See AcceptRanges.
  23. AcceptShift = 4
  24. // The names of these constants are chosen to give nice alignment in the
  25. // table below. The first nibble is an index into acceptRanges or F for
  26. // special one-byte cases. The second nibble is the Rune length or the
  27. // Status for the special one-byte case.
  28. xx = 0xF1 // invalid: size 1
  29. as = 0xF0 // ASCII: size 1
  30. s1 = 0x02 // accept 0, size 2
  31. s2 = 0x13 // accept 1, size 3
  32. s3 = 0x03 // accept 0, size 3
  33. s4 = 0x23 // accept 2, size 3
  34. s5 = 0x34 // accept 3, size 4
  35. s6 = 0x04 // accept 0, size 4
  36. s7 = 0x44 // accept 4, size 4
  37. )
  38. // First is information about the first byte in a UTF-8 sequence.
  39. var First = [256]uint8{
  40. // 1 2 3 4 5 6 7 8 9 A B C D E F
  41. as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x00-0x0F
  42. as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x10-0x1F
  43. as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x20-0x2F
  44. as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x30-0x3F
  45. as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x40-0x4F
  46. as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x50-0x5F
  47. as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x60-0x6F
  48. as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x70-0x7F
  49. // 1 2 3 4 5 6 7 8 9 A B C D E F
  50. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x80-0x8F
  51. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x90-0x9F
  52. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xA0-0xAF
  53. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xB0-0xBF
  54. xx, xx, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xC0-0xCF
  55. s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xD0-0xDF
  56. s2, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s4, s3, s3, // 0xE0-0xEF
  57. s5, s6, s6, s6, s7, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xF0-0xFF
  58. }
  59. // AcceptRange gives the range of valid values for the second byte in a UTF-8
  60. // sequence for any value for First that is not ASCII or FirstInvalid.
  61. type AcceptRange struct {
  62. Lo uint8 // lowest value for second byte.
  63. Hi uint8 // highest value for second byte.
  64. }
  65. // AcceptRanges is a slice of AcceptRange values. For a given byte sequence b
  66. //
  67. // AcceptRanges[First[b[0]]>>AcceptShift]
  68. //
  69. // will give the value of AcceptRange for the multi-byte UTF-8 sequence starting
  70. // at b[0].
  71. var AcceptRanges = [...]AcceptRange{
  72. 0: {LoCB, HiCB},
  73. 1: {0xA0, HiCB},
  74. 2: {LoCB, 0x9F},
  75. 3: {0x90, HiCB},
  76. 4: {LoCB, 0x8F},
  77. }