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

     1  package jzon
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  // int8 decoder
     8  type int8Decoder struct {
     9  }
    10  
    11  func (*int8Decoder) Decode(ptr unsafe.Pointer, it *Iterator, opts *DecOpts) error {
    12  	c, err := it.nextToken()
    13  	if err != nil {
    14  		return err
    15  	}
    16  	if c == 'n' {
    17  		it.head++
    18  		return it.expectBytes("ull")
    19  	}
    20  	quoted := (opts != nil) && (opts.Quoted || opts.MapKey)
    21  	if quoted {
    22  		if c != '"' {
    23  			return UnexpectedByteError{got: c, exp: '"'}
    24  		}
    25  		it.head++
    26  		c, err = it.nextByte()
    27  		if err != nil {
    28  			return err
    29  		}
    30  	}
    31  	it.head++
    32  	i, err := it.readInt8(c)
    33  	if err != nil {
    34  		return err
    35  	}
    36  	if quoted {
    37  		c, err = it.nextByte()
    38  		if err != nil {
    39  			return err
    40  		}
    41  		if c != '"' {
    42  			return UnexpectedByteError{got: c, exp: '"'}
    43  		}
    44  		it.head++
    45  	}
    46  	*(*int8)(ptr) = i
    47  	return nil
    48  }
    49  
    50  // int16 decoder
    51  type int16Decoder struct {
    52  }
    53  
    54  func (*int16Decoder) Decode(ptr unsafe.Pointer, it *Iterator, opts *DecOpts) error {
    55  	c, err := it.nextToken()
    56  	if err != nil {
    57  		return err
    58  	}
    59  	if c == 'n' {
    60  		it.head++
    61  		return it.expectBytes("ull")
    62  	}
    63  	quoted := (opts != nil) && (opts.Quoted || opts.MapKey)
    64  	if quoted {
    65  		if c != '"' {
    66  			return UnexpectedByteError{got: c, exp: '"'}
    67  		}
    68  		it.head++
    69  		c, err = it.nextByte()
    70  		if err != nil {
    71  			return err
    72  		}
    73  	}
    74  	it.head++
    75  	i, err := it.readInt16(c)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	if quoted {
    80  		c, err = it.nextByte()
    81  		if err != nil {
    82  			return err
    83  		}
    84  		if c != '"' {
    85  			return UnexpectedByteError{got: c, exp: '"'}
    86  		}
    87  		it.head++
    88  	}
    89  	*(*int16)(ptr) = i
    90  	return nil
    91  }
    92  
    93  // int32 decoder
    94  type int32Decoder struct {
    95  }
    96  
    97  func (*int32Decoder) Decode(ptr unsafe.Pointer, it *Iterator, opts *DecOpts) error {
    98  	c, err := it.nextToken()
    99  	if err != nil {
   100  		return err
   101  	}
   102  	if c == 'n' {
   103  		it.head++
   104  		return it.expectBytes("ull")
   105  	}
   106  	quoted := (opts != nil) && (opts.Quoted || opts.MapKey)
   107  	if quoted {
   108  		if c != '"' {
   109  			return UnexpectedByteError{got: c, exp: '"'}
   110  		}
   111  		it.head++
   112  		c, err = it.nextByte()
   113  		if err != nil {
   114  			return err
   115  		}
   116  	}
   117  	it.head++
   118  	i, err := it.readInt32(c)
   119  	if err != nil {
   120  		return err
   121  	}
   122  	if quoted {
   123  		c, err = it.nextByte()
   124  		if err != nil {
   125  			return err
   126  		}
   127  		if c != '"' {
   128  			return UnexpectedByteError{got: c, exp: '"'}
   129  		}
   130  		it.head++
   131  	}
   132  	*(*int32)(ptr) = i
   133  	return nil
   134  }
   135  
   136  // int64 decoder
   137  type int64Decoder struct {
   138  }
   139  
   140  func (*int64Decoder) Decode(ptr unsafe.Pointer, it *Iterator, opts *DecOpts) error {
   141  	c, err := it.nextToken()
   142  	if err != nil {
   143  		return err
   144  	}
   145  	if c == 'n' {
   146  		it.head++
   147  		return it.expectBytes("ull")
   148  	}
   149  	quoted := (opts != nil) && (opts.Quoted || opts.MapKey)
   150  	if quoted {
   151  		if c != '"' {
   152  			return UnexpectedByteError{got: c, exp: '"'}
   153  		}
   154  		it.head++
   155  		c, err = it.nextByte()
   156  		if err != nil {
   157  			return err
   158  		}
   159  	}
   160  	it.head++
   161  	i, err := it.readInt64(c)
   162  	if err != nil {
   163  		return err
   164  	}
   165  	if quoted {
   166  		c, err = it.nextByte()
   167  		if err != nil {
   168  			return err
   169  		}
   170  		if c != '"' {
   171  			return UnexpectedByteError{got: c, exp: '"'}
   172  		}
   173  		it.head++
   174  	}
   175  	*(*int64)(ptr) = i
   176  	return nil
   177  }