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

     1  package gotiny
     2  
     3  import (
     4  	"time"
     5  	"unsafe"
     6  )
     7  
     8  func (e *Encoder) encBool(v bool) {
     9  	if e.boolBit == 0 {
    10  		e.boolPos = len(e.buf)
    11  		e.buf = append(e.buf, 0)
    12  		e.boolBit = 1
    13  	}
    14  	if v {
    15  		e.buf[e.boolPos] |= e.boolBit
    16  	}
    17  	e.boolBit <<= 1
    18  }
    19  
    20  func (e *Encoder) encUint64(v uint64) {
    21  	switch {
    22  	case v < 1<<7-1:
    23  		e.buf = append(e.buf, byte(v))
    24  	case v < 1<<14-1:
    25  		e.buf = append(e.buf, byte(v)|0x80, byte(v>>7))
    26  	case v < 1<<21-1:
    27  		e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14))
    28  	case v < 1<<28-1:
    29  		e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21))
    30  	case v < 1<<35-1:
    31  		e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21)|0x80, byte(v>>28))
    32  	case v < 1<<42-1:
    33  		e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21)|0x80, byte(v>>28)|0x80, byte(v>>35))
    34  	case v < 1<<49-1:
    35  		e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21)|0x80, byte(v>>28)|0x80, byte(v>>35)|0x80, byte(v>>42))
    36  	case v < 1<<56-1:
    37  		e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21)|0x80, byte(v>>28)|0x80, byte(v>>35)|0x80, byte(v>>42)|0x80, byte(v>>49))
    38  	default:
    39  		e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21)|0x80, byte(v>>28)|0x80, byte(v>>35)|0x80, byte(v>>42)|0x80, byte(v>>49)|0x80, byte(v>>56))
    40  	}
    41  }
    42  
    43  func (e *Encoder) encUint16(v uint16) {
    44  	if v < 1<<7-1 {
    45  		e.buf = append(e.buf, byte(v))
    46  	} else if v < 1<<14-1 {
    47  		e.buf = append(e.buf, byte(v)|0x80, byte(v>>7))
    48  	} else {
    49  		e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14))
    50  	}
    51  }
    52  
    53  func (e *Encoder) encUint32(v uint32) {
    54  	switch {
    55  	case v < 1<<7-1:
    56  		e.buf = append(e.buf, byte(v))
    57  	case v < 1<<14-1:
    58  		e.buf = append(e.buf, byte(v)|0x80, byte(v>>7))
    59  	case v < 1<<21-1:
    60  		e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14))
    61  	case v < 1<<28-1:
    62  		e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21))
    63  	default:
    64  		e.buf = append(e.buf, byte(v)|0x80, byte(v>>7)|0x80, byte(v>>14)|0x80, byte(v>>21)|0x80, byte(v>>28))
    65  	}
    66  }
    67  
    68  func (e *Encoder) encLength(v int)    { e.encUint32(uint32(v)) }
    69  func (e *Encoder) encString(s string) { e.encUint32(uint32(len(s))); e.buf = append(e.buf, s...) }
    70  func (e *Encoder) encIsNotNil(v bool) { e.encBool(v) }
    71  
    72  func encIgnore(*Encoder, unsafe.Pointer)      {}
    73  func encBool(e *Encoder, p unsafe.Pointer)    { e.encBool(*(*bool)(p)) }
    74  func encInt(e *Encoder, p unsafe.Pointer)     { e.encUint64(int64ToUint64(int64(*(*int)(p)))) }
    75  func encInt8(e *Encoder, p unsafe.Pointer)    { e.buf = append(e.buf, *(*uint8)(p)) }
    76  func encInt16(e *Encoder, p unsafe.Pointer)   { e.encUint16(int16ToUint16(*(*int16)(p))) }
    77  func encInt32(e *Encoder, p unsafe.Pointer)   { e.encUint32(int32ToUint32(*(*int32)(p))) }
    78  func encInt64(e *Encoder, p unsafe.Pointer)   { e.encUint64(int64ToUint64(*(*int64)(p))) }
    79  func encUint8(e *Encoder, p unsafe.Pointer)   { e.buf = append(e.buf, *(*uint8)(p)) }
    80  func encUint16(e *Encoder, p unsafe.Pointer)  { e.encUint16(*(*uint16)(p)) }
    81  func encUint32(e *Encoder, p unsafe.Pointer)  { e.encUint32(*(*uint32)(p)) }
    82  func encUint64(e *Encoder, p unsafe.Pointer)  { e.encUint64(uint64(*(*uint64)(p))) }
    83  func encUint(e *Encoder, p unsafe.Pointer)    { e.encUint64(uint64(*(*uint)(p))) }
    84  func encUintptr(e *Encoder, p unsafe.Pointer) { e.encUint64(uint64(*(*uintptr)(p))) }
    85  func encPointer(e *Encoder, p unsafe.Pointer) { e.encUint64(uint64(*(*uintptr)(p))) }
    86  func encFloat32(e *Encoder, p unsafe.Pointer) { e.encUint32(float32ToUint32(p)) }
    87  func encFloat64(e *Encoder, p unsafe.Pointer) { e.encUint64(float64ToUint64(p)) }
    88  func encString(e *Encoder, p unsafe.Pointer) {
    89  	s := *(*string)(p)
    90  	e.encUint32(uint32(len(s)))
    91  	e.buf = append(e.buf, s...)
    92  }
    93  func encTime(e *Encoder, p unsafe.Pointer)      { e.encUint64(uint64((*time.Time)(p).UnixNano())) }
    94  func encComplex64(e *Encoder, p unsafe.Pointer) { e.encUint64(*(*uint64)(p)) }
    95  func encComplex128(e *Encoder, p unsafe.Pointer) {
    96  	e.encUint64(*(*uint64)(p))
    97  	e.encUint64(*(*uint64)(unsafe.Pointer(uintptr(p) + ptr1Size)))
    98  }
    99  
   100  func encBytes(e *Encoder, p unsafe.Pointer) {
   101  	isNotNil := !isNil(p)
   102  	e.encIsNotNil(isNotNil)
   103  	if isNotNil {
   104  		buf := *(*[]byte)(p)
   105  		e.encLength(len(buf))
   106  		e.buf = append(e.buf, buf...)
   107  	}
   108  }