github.com/niubaoshu/gotiny@v0.0.4-0.20211018120156-10d393f19ad0/decbase.go (about)

     1  package gotiny
     2  
     3  import (
     4  	"time"
     5  	"unsafe"
     6  )
     7  
     8  func (d *Decoder) decBool() (b bool) {
     9  	if d.boolBit == 0 {
    10  		d.boolBit = 1
    11  		d.boolPos = d.buf[d.index]
    12  		d.index++
    13  	}
    14  	b = d.boolPos&d.boolBit != 0
    15  	d.boolBit <<= 1
    16  	return
    17  }
    18  
    19  func (d *Decoder) decUint64() uint64 {
    20  	buf, i := d.buf, d.index
    21  	x := uint64(buf[i])
    22  	if x < 0x80 {
    23  		d.index++
    24  		return x
    25  	}
    26  	x1 := buf[i+1]
    27  	x += uint64(x1) << 7
    28  	if x1 < 0x80 {
    29  		d.index += 2
    30  		return x - 1<<7
    31  	}
    32  	x2 := buf[i+2]
    33  	x += uint64(x2) << 14
    34  	if x2 < 0x80 {
    35  		d.index += 3
    36  		return x - (1<<7 + 1<<14)
    37  	}
    38  	x3 := buf[i+3]
    39  	x += uint64(x3) << 21
    40  	if x3 < 0x80 {
    41  		d.index += 4
    42  		return x - (1<<7 + 1<<14 + 1<<21)
    43  	}
    44  	x4 := buf[i+4]
    45  	x += uint64(x4) << 28
    46  	if x4 < 0x80 {
    47  		d.index += 5
    48  		return x - (1<<7 + 1<<14 + 1<<21 + 1<<28)
    49  	}
    50  	x5 := buf[i+5]
    51  	x += uint64(x5) << 35
    52  	if x5 < 0x80 {
    53  		d.index += 6
    54  		return x - (1<<7 + 1<<14 + 1<<21 + 1<<28 + 1<<35)
    55  	}
    56  	x6 := buf[i+6]
    57  	x += uint64(x6) << 42
    58  	if x6 < 0x80 {
    59  		d.index += 7
    60  		return x - (1<<7 + 1<<14 + 1<<21 + 1<<28 + 1<<35 + 1<<42)
    61  	}
    62  	x7 := buf[i+7]
    63  	x += uint64(x7) << 49
    64  	if x7 < 0x80 {
    65  		d.index += 8
    66  		return x - (1<<7 + 1<<14 + 1<<21 + 1<<28 + 1<<35 + 1<<42 + 1<<49)
    67  	}
    68  	d.index += 9
    69  	return x + uint64(buf[i+8])<<56 - (1<<7 + 1<<14 + 1<<21 + 1<<28 + 1<<35 + 1<<42 + 1<<49 + 1<<56)
    70  }
    71  
    72  func (d *Decoder) decUint16() uint16 {
    73  	buf, i := d.buf, d.index
    74  	x := uint16(buf[i])
    75  	if x < 0x80 {
    76  		d.index++
    77  		return x
    78  	}
    79  	x1 := buf[i+1]
    80  	x += uint16(x1) << 7
    81  	if x1 < 0x80 {
    82  		d.index += 2
    83  		return x - 1<<7
    84  	}
    85  	d.index += 3
    86  	return x + uint16(buf[i+2])<<14 - (1<<7 + 1<<14)
    87  }
    88  
    89  func (d *Decoder) decUint32() uint32 {
    90  	buf, i := d.buf, d.index
    91  	x := uint32(buf[i])
    92  	if x < 0x80 {
    93  		d.index++
    94  		return x
    95  	}
    96  	x1 := buf[i+1]
    97  	x += uint32(x1) << 7
    98  	if x1 < 0x80 {
    99  		d.index += 2
   100  		return x - 1<<7
   101  	}
   102  	x2 := buf[i+2]
   103  	x += uint32(x2) << 14
   104  	if x2 < 0x80 {
   105  		d.index += 3
   106  		return x - (1<<7 + 1<<14)
   107  	}
   108  	x3 := buf[i+3]
   109  	x += uint32(x3) << 21
   110  	if x3 < 0x80 {
   111  		d.index += 4
   112  		return x - (1<<7 + 1<<14 + 1<<21)
   113  	}
   114  	x4 := buf[i+4]
   115  	x += uint32(x4) << 28
   116  	d.index += 5
   117  	return x - (1<<7 + 1<<14 + 1<<21 + 1<<28)
   118  }
   119  
   120  func (d *Decoder) decLength() int    { return int(d.decUint32()) }
   121  func (d *Decoder) decIsNotNil() bool { return d.decBool() }
   122  
   123  func decIgnore(*Decoder, unsafe.Pointer)        {}
   124  func decBool(d *Decoder, p unsafe.Pointer)      { *(*bool)(p) = d.decBool() }
   125  func decInt(d *Decoder, p unsafe.Pointer)       { *(*int)(p) = int(uint64ToInt64(d.decUint64())) }
   126  func decInt8(d *Decoder, p unsafe.Pointer)      { *(*int8)(p) = int8(d.buf[d.index]); d.index++ }
   127  func decInt16(d *Decoder, p unsafe.Pointer)     { *(*int16)(p) = uint16ToInt16(d.decUint16()) }
   128  func decInt32(d *Decoder, p unsafe.Pointer)     { *(*int32)(p) = uint32ToInt32(d.decUint32()) }
   129  func decInt64(d *Decoder, p unsafe.Pointer)     { *(*int64)(p) = uint64ToInt64(d.decUint64()) }
   130  func decUint(d *Decoder, p unsafe.Pointer)      { *(*uint)(p) = uint(d.decUint64()) }
   131  func decUint8(d *Decoder, p unsafe.Pointer)     { *(*uint8)(p) = d.buf[d.index]; d.index++ }
   132  func decUint16(d *Decoder, p unsafe.Pointer)    { *(*uint16)(p) = d.decUint16() }
   133  func decUint32(d *Decoder, p unsafe.Pointer)    { *(*uint32)(p) = d.decUint32() }
   134  func decUint64(d *Decoder, p unsafe.Pointer)    { *(*uint64)(p) = d.decUint64() }
   135  func decUintptr(d *Decoder, p unsafe.Pointer)   { *(*uintptr)(p) = uintptr(d.decUint64()) }
   136  func decPointer(d *Decoder, p unsafe.Pointer)   { *(*uintptr)(p) = uintptr(d.decUint64()) }
   137  func decFloat32(d *Decoder, p unsafe.Pointer)   { *(*float32)(p) = uint32ToFloat32(d.decUint32()) }
   138  func decFloat64(d *Decoder, p unsafe.Pointer)   { *(*float64)(p) = uint64ToFloat64(d.decUint64()) }
   139  func decTime(d *Decoder, p unsafe.Pointer)      { *(*time.Time)(p) = time.Unix(0, int64(d.decUint64())) }
   140  func decComplex64(d *Decoder, p unsafe.Pointer) { *(*uint64)(p) = d.decUint64() }
   141  func decComplex128(d *Decoder, p unsafe.Pointer) {
   142  	*(*uint64)(p) = d.decUint64()
   143  	*(*uint64)(unsafe.Pointer(uintptr(p) + ptr1Size)) = d.decUint64()
   144  }
   145  
   146  func decString(d *Decoder, p unsafe.Pointer) {
   147  	l, val := int(d.decUint32()), (*string)(p)
   148  	*val = string(d.buf[d.index : d.index+l])
   149  	d.index += l
   150  }
   151  
   152  func decBytes(d *Decoder, p unsafe.Pointer) {
   153  	bytes := (*[]byte)(p)
   154  	if d.decIsNotNil() {
   155  		l := int(d.decUint32())
   156  		*bytes = d.buf[d.index : d.index+l]
   157  		d.index += l
   158  	} else if !isNil(p) {
   159  		*bytes = nil
   160  	}
   161  }