github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_skip_str.go (about)

     1  package jzon
     2  
     3  func (it *Iterator) skipU4() error {
     4  	remain := 4
     5  	for {
     6  		i := it.head
     7  		for ; i < it.tail; i++ {
     8  			c := it.buffer[i]
     9  			u4v := hexValue[c]
    10  			if u4v == invalidHex {
    11  				return InvalidUnicodeCharError{c: c}
    12  			}
    13  			if remain == 1 {
    14  				it.head = i + 1
    15  				return nil
    16  			}
    17  			remain--
    18  		}
    19  		it.head = i
    20  		if err := it.readMore(); err != nil {
    21  			return err
    22  		}
    23  	}
    24  }
    25  
    26  func (it *Iterator) skipEscapedChar() error {
    27  	c, err := it.nextByte()
    28  	if err != nil {
    29  		return err
    30  	}
    31  	escaped := escapeMap[c]
    32  	if escaped != noEscape {
    33  		it.head++
    34  		return nil
    35  	}
    36  	if c != 'u' {
    37  		return InvalidEscapeCharError{c: c}
    38  	}
    39  	it.head++
    40  	return it.skipU4()
    41  }
    42  
    43  // internal, call only after a '"' is consumed
    44  func skipString(it *Iterator, _ byte) error {
    45  	for {
    46  		i := it.head
    47  		for i < it.tail {
    48  			c := it.buffer[i]
    49  			if c == '"' {
    50  				it.head = i + 1
    51  				return nil
    52  			} else if c == '\\' {
    53  				it.head = i + 1
    54  				err := it.skipEscapedChar()
    55  				if err != nil {
    56  					return err
    57  				}
    58  				i = it.head
    59  			} else if c < ' ' { // json.org
    60  				return InvalidStringCharError{c: c}
    61  			} else {
    62  				i++
    63  			}
    64  		}
    65  		// i == it.tail
    66  		it.head = i
    67  		if err := it.readMore(); err != nil {
    68  			return err
    69  		}
    70  	}
    71  }
    72  
    73  // SkipString skips a string
    74  func (it *Iterator) SkipString() error {
    75  	c, err := it.nextToken()
    76  	if err != nil {
    77  		return err
    78  	}
    79  	if c != '"' {
    80  		return UnexpectedByteError{got: c, exp: '"'}
    81  	}
    82  	it.head++
    83  	return skipString(it, c)
    84  }