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

     1  package jzon
     2  
     3  var (
     4  	skipFunctions [charNum]func(it *Iterator, c byte) error
     5  )
     6  
     7  func init() {
     8  	skipFunctions['"'] = skipString
     9  	skipFunctions['n'] = func(it *Iterator, _ byte) error {
    10  		return it.expectBytes("ull")
    11  	}
    12  	skipFunctions['t'] = func(it *Iterator, _ byte) error {
    13  		return it.expectBytes("rue")
    14  	}
    15  	skipFunctions['f'] = func(it *Iterator, _ byte) error {
    16  		return it.expectBytes("alse")
    17  	}
    18  	skipFunctions['['] = skipArrayWithStack
    19  	skipFunctions['{'] = skipObjectWithStack
    20  	for _, c := range []byte("-0123456789") {
    21  		skipFunctions[c] = skipNumber
    22  	}
    23  	errFunc := func(it *Iterator, c byte) error {
    24  		return UnexpectedByteError{got: c}
    25  	}
    26  	for i := 0; i < charNum; i++ {
    27  		if skipFunctions[i] == nil {
    28  			skipFunctions[i] = errFunc
    29  		}
    30  	}
    31  }
    32  
    33  func skipWithStack(it *Iterator, top stackElement, s *stack) (err error) {
    34  	var c byte
    35  	for {
    36  		c, err = it.nextToken()
    37  		if err != nil {
    38  			return err
    39  		}
    40  		it.head++
    41  		if top&1 == 0 {
    42  			// stackElementObjectBegin
    43  			// stackElementObject
    44  			if c == '}' {
    45  				if top = s.pop(); top == stackElementNone {
    46  					return nil
    47  				}
    48  				continue
    49  			}
    50  			if top == stackElementObject {
    51  				if c != ',' {
    52  					return UnexpectedByteError{got: c, exp: ','}
    53  				}
    54  				c, err = it.nextToken()
    55  				if err != nil {
    56  					return err
    57  				}
    58  				it.head++
    59  			}
    60  			if c != '"' {
    61  				return UnexpectedByteError{got: c, exp: '"'}
    62  			}
    63  			if err = it.skipObjectField(); err != nil {
    64  				return err
    65  			}
    66  			if c, err = it.nextToken(); err != nil {
    67  				return err
    68  			}
    69  			it.head++
    70  			switch c {
    71  			case '[':
    72  				s.pushObject()
    73  				top = stackElementArrayBegin
    74  			case '{':
    75  				s.pushObject()
    76  				top = stackElementObjectBegin
    77  			default:
    78  				if err = skipFunctions[c](it, c); err != nil {
    79  					return err
    80  				}
    81  				top = stackElementObject
    82  			}
    83  		} else {
    84  			// stackElementArrayBegin
    85  			// stackElementArray
    86  			if c == ']' {
    87  				if top = s.pop(); top == stackElementNone {
    88  					return nil
    89  				}
    90  				continue
    91  			}
    92  			if top == stackElementArray {
    93  				if c != ',' {
    94  					return UnexpectedByteError{got: c, exp: ','}
    95  				}
    96  				c, err = it.nextToken()
    97  				if err != nil {
    98  					return err
    99  				}
   100  				it.head++
   101  			}
   102  			switch c {
   103  			case '[':
   104  				s.pushArray()
   105  				top = stackElementArrayBegin
   106  			case '{':
   107  				s.pushArray()
   108  				top = stackElementObjectBegin
   109  			default:
   110  				if err = skipFunctions[c](it, c); err != nil {
   111  					return err
   112  				}
   113  				top = stackElementArray
   114  			}
   115  		}
   116  	}
   117  }
   118  
   119  // Skip skips a json object
   120  func (it *Iterator) Skip() error {
   121  	c, err := it.nextToken()
   122  	if err != nil {
   123  		return err
   124  	}
   125  	it.head++
   126  	return skipFunctions[c](it, c)
   127  }