github.com/goccy/go-json@v0.10.3-0.20240509105655-5e2ae3f23c1d/internal/encoder/decode_rune.go (about)

     1  package encoder
     2  
     3  import "unicode/utf8"
     4  
     5  const (
     6  	// The default lowest and highest continuation byte.
     7  	locb = 128 //0b10000000
     8  	hicb = 191 //0b10111111
     9  
    10  	// These names of these constants are chosen to give nice alignment in the
    11  	// table below. The first nibble is an index into acceptRanges or F for
    12  	// special one-byte cases. The second nibble is the Rune length or the
    13  	// Status for the special one-byte case.
    14  	xx = 0xF1 // invalid: size 1
    15  	as = 0xF0 // ASCII: size 1
    16  	s1 = 0x02 // accept 0, size 2
    17  	s2 = 0x13 // accept 1, size 3
    18  	s3 = 0x03 // accept 0, size 3
    19  	s4 = 0x23 // accept 2, size 3
    20  	s5 = 0x34 // accept 3, size 4
    21  	s6 = 0x04 // accept 0, size 4
    22  	s7 = 0x44 // accept 4, size 4
    23  )
    24  
    25  // first is information about the first byte in a UTF-8 sequence.
    26  var first = [256]uint8{
    27  	//   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
    28  	as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x00-0x0F
    29  	as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x10-0x1F
    30  	as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x20-0x2F
    31  	as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x30-0x3F
    32  	as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x40-0x4F
    33  	as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x50-0x5F
    34  	as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x60-0x6F
    35  	as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x70-0x7F
    36  	//   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
    37  	xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x80-0x8F
    38  	xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x90-0x9F
    39  	xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xA0-0xAF
    40  	xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xB0-0xBF
    41  	xx, xx, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xC0-0xCF
    42  	s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xD0-0xDF
    43  	s2, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s4, s3, s3, // 0xE0-0xEF
    44  	s5, s6, s6, s6, s7, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xF0-0xFF
    45  }
    46  
    47  const (
    48  	lineSep      = byte(168) //'\u2028'
    49  	paragraphSep = byte(169) //'\u2029'
    50  )
    51  
    52  type decodeRuneState int
    53  
    54  const (
    55  	validUTF8State decodeRuneState = iota
    56  	runeErrorState
    57  	lineSepState
    58  	paragraphSepState
    59  )
    60  
    61  func decodeRuneInString(s string) (decodeRuneState, int) {
    62  	n := len(s)
    63  	s0 := s[0]
    64  	x := first[s0]
    65  	if x >= as {
    66  		// The following code simulates an additional check for x == xx and
    67  		// handling the ASCII and invalid cases accordingly. This mask-and-or
    68  		// approach prevents an additional branch.
    69  		mask := rune(x) << 31 >> 31 // Create 0x0000 or 0xFFFF.
    70  		if rune(s[0])&^mask|utf8.RuneError&mask == utf8.RuneError {
    71  			return runeErrorState, 1
    72  		}
    73  		return validUTF8State, 1
    74  	}
    75  	sz := int(x & 7)
    76  	if n < sz {
    77  		return runeErrorState, 1
    78  	}
    79  	s1 := s[1]
    80  	switch x >> 4 {
    81  	case 0:
    82  		if s1 < locb || hicb < s1 {
    83  			return runeErrorState, 1
    84  		}
    85  	case 1:
    86  		if s1 < 0xA0 || hicb < s1 {
    87  			return runeErrorState, 1
    88  		}
    89  	case 2:
    90  		if s1 < locb || 0x9F < s1 {
    91  			return runeErrorState, 1
    92  		}
    93  	case 3:
    94  		if s1 < 0x90 || hicb < s1 {
    95  			return runeErrorState, 1
    96  		}
    97  	case 4:
    98  		if s1 < locb || 0x8F < s1 {
    99  			return runeErrorState, 1
   100  		}
   101  	}
   102  	if sz <= 2 {
   103  		return validUTF8State, 2
   104  	}
   105  	s2 := s[2]
   106  	if s2 < locb || hicb < s2 {
   107  		return runeErrorState, 1
   108  	}
   109  	if sz <= 3 {
   110  		// separator character prefixes: [2]byte{226, 128}
   111  		if s0 == 226 && s1 == 128 {
   112  			switch s2 {
   113  			case lineSep:
   114  				return lineSepState, 3
   115  			case paragraphSep:
   116  				return paragraphSepState, 3
   117  			}
   118  		}
   119  		return validUTF8State, 3
   120  	}
   121  	s3 := s[3]
   122  	if s3 < locb || hicb < s3 {
   123  		return runeErrorState, 1
   124  	}
   125  	return validUTF8State, 4
   126  }