github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/syllab/encode.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package syllab 4 5 import "../convert" 6 7 /* 8 ************************************************************************************************** 9 *****************************************Fixed Size ARRAY***************************************** 10 ************************************************************************************************** 11 */ 12 13 // SetArray encode fixed sized byte array to the payload buffer. 14 func SetArray(p []byte, stackIndex uint32, a []byte) { 15 copy(p[stackIndex:], a[:]) 16 } 17 18 // SetByte encode BYTE to the payload buffer. 19 func SetByte(p []byte, stackIndex uint32, b byte) { 20 p[stackIndex] = b 21 } 22 23 // SetInt8 encode INT8 to the payload buffer. 24 func SetInt8(p []byte, stackIndex uint32, n int8) { 25 p[stackIndex] = byte(n) 26 } 27 28 // SetUInt8 encode UINT8 to the payload buffer. 29 func SetUInt8(p []byte, stackIndex uint32, n uint8) { 30 p[stackIndex] = byte(n) 31 } 32 33 // SetBool encode BOOL to the payload buffer. 34 func SetBool(p []byte, stackIndex uint32, b bool) { 35 if b { 36 p[stackIndex] = 1 37 } // else { 38 // p[stackIndex] = 0 39 // } 40 } 41 42 // SetInt16 encode INT16 to the payload buffer. 43 func SetInt16(p []byte, stackIndex uint32, n int16) { 44 p[stackIndex] = byte(n) 45 p[stackIndex+1] = byte(n >> 8) 46 } 47 48 // SetUInt16 encode UINT16 to the payload buffer. 49 func SetUInt16(p []byte, stackIndex uint32, n uint16) { 50 p[stackIndex] = byte(n) 51 p[stackIndex+1] = byte(n >> 8) 52 } 53 54 // SetInt32 encode INT32 to the payload buffer. 55 func SetInt32(p []byte, stackIndex uint32, n int32) { 56 p[stackIndex] = byte(n) 57 p[stackIndex+1] = byte(n >> 8) 58 p[stackIndex+2] = byte(n >> 16) 59 p[stackIndex+3] = byte(n >> 24) 60 } 61 62 // SetUInt32 encode UINT32 to the payload buffer. 63 func SetUInt32(p []byte, stackIndex uint32, n uint32) { 64 p[stackIndex] = byte(n) 65 p[stackIndex+1] = byte(n >> 8) 66 p[stackIndex+2] = byte(n >> 16) 67 p[stackIndex+3] = byte(n >> 24) 68 } 69 70 // SetFloat32 encode FLOAT32 to the payload buffer. 71 func SetFloat32(p []byte, stackIndex uint32, n float32) { 72 SetUInt32(p, stackIndex, uint32(n)) 73 } 74 75 // SetInt64 encode INT64 to the payload buffer. 76 func SetInt64(p []byte, stackIndex uint32, n int64) { 77 p[stackIndex] = byte(n) 78 p[stackIndex+1] = byte(n >> 8) 79 p[stackIndex+2] = byte(n >> 16) 80 p[stackIndex+3] = byte(n >> 24) 81 p[stackIndex+4] = byte(n >> 32) 82 p[stackIndex+5] = byte(n >> 40) 83 p[stackIndex+6] = byte(n >> 48) 84 p[stackIndex+7] = byte(n >> 56) 85 } 86 87 // SetUInt64 encode UINT64 to the payload buffer. 88 func SetUInt64(p []byte, stackIndex uint32, n uint64) { 89 p[stackIndex] = byte(n) 90 p[stackIndex+1] = byte(n >> 8) 91 p[stackIndex+2] = byte(n >> 16) 92 p[stackIndex+3] = byte(n >> 24) 93 p[stackIndex+4] = byte(n >> 32) 94 p[stackIndex+5] = byte(n >> 40) 95 p[stackIndex+6] = byte(n >> 48) 96 p[stackIndex+7] = byte(n >> 56) 97 } 98 99 // SetFloat64 encode FLOAT64 to the payload buffer. 100 func SetFloat64(p []byte, stackIndex uint32, n float64) { 101 SetUInt64(p, stackIndex, uint64(n)) 102 // TODO::: below code instead up func call not allow go compiler to inline this func! WHY??? 103 // var un = uint64(n) 104 // p[stackIndex] = byte(un) 105 // p[stackIndex+1] = byte(un >> 8) 106 // p[stackIndex+2] = byte(un >> 16) 107 // p[stackIndex+3] = byte(un >> 24) 108 // p[stackIndex+4] = byte(un >> 32) 109 // p[stackIndex+5] = byte(un >> 40) 110 // p[stackIndex+6] = byte(un >> 48) 111 // p[stackIndex+7] = byte(un >> 56) 112 } 113 114 // SetComplex64 encode COMPLEX64 to the payload buffer. 115 func SetComplex64(p []byte, stackIndex uint32, n complex64) { 116 SetFloat32(p, stackIndex, real(n)) 117 SetFloat32(p, stackIndex+3, imag(n)) 118 } 119 120 // SetComplex128 encode COMPLEX128 to the payload buffer. 121 func SetComplex128(p []byte, stackIndex uint32, n complex128) { 122 SetFloat64(p, stackIndex, real(n)) 123 SetFloat64(p, stackIndex+7, imag(n)) 124 } 125 126 /* 127 ************************************************************************************************** 128 **************************************Dynamically size ARRAY************************************** 129 ************************************************************************************************** 130 */ 131 132 // SetString encode string to the payload buffer! 133 func SetString(p []byte, s string, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 134 var ln = uint32(len(s)) 135 // SetUInt32(p, stackIndex, heapAddr) 136 p[stackIndex] = byte(heapAddr) 137 p[stackIndex+1] = byte(heapAddr >> 8) 138 p[stackIndex+2] = byte(heapAddr >> 16) 139 p[stackIndex+3] = byte(heapAddr >> 24) 140 // SetUInt32(p, stackIndex+4, ln) 141 p[stackIndex+4] = byte(ln) 142 p[stackIndex+5] = byte(ln >> 8) 143 p[stackIndex+6] = byte(ln >> 16) 144 p[stackIndex+7] = byte(ln >> 24) 145 copy(p[heapAddr:], s) 146 return heapAddr + ln 147 } 148 149 // SetByteArray encode byte array || uint8 array to the payload buffer! 150 func SetByteArray(p []byte, s []byte, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 151 var ln = uint32(len(s)) 152 SetUInt32(p, stackIndex, heapAddr) 153 SetUInt32(p, stackIndex+4, ln) 154 copy(p[heapAddr:], s) 155 return heapAddr + ln 156 } 157 158 // SetInt8Array encode int8 array to the payload buffer! 159 func SetInt8Array(p []byte, s []int8, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 160 var ln = uint32(len(s)) 161 SetUInt32(p, stackIndex, heapAddr) 162 SetUInt32(p, stackIndex+4, ln) 163 copy(p[heapAddr:], convert.UnsafeInt8SliceToByteSlice(s)) 164 return heapAddr + ln 165 } 166 167 // SetBoolArray encode bool array to the payload buffer! 168 func SetBoolArray(p []byte, s []bool, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 169 var ln = uint32(len(s)) 170 SetUInt32(p, stackIndex, heapAddr) 171 SetUInt32(p, stackIndex+4, ln) 172 copy(p[heapAddr:], convert.UnsafeBoolSliceToByteSlice(s)) 173 return heapAddr + ln 174 } 175 176 // SetInt16Array encode int16 array to the payload buffer! 177 func SetInt16Array(p []byte, s []int16, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 178 var ln = uint32(len(s)) 179 SetUInt32(p, stackIndex, heapAddr) 180 SetUInt32(p, stackIndex+4, ln) 181 copy(p[heapAddr:], convert.UnsafeInt16SliceToByteSlice(s)) 182 return heapAddr + (ln * 2) 183 } 184 185 // SetUInt16Array encode uint16 array to the payload buffer! 186 func SetUInt16Array(p []byte, s []uint16, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 187 var ln = uint32(len(s)) 188 SetUInt32(p, stackIndex, heapAddr) 189 SetUInt32(p, stackIndex+4, ln) 190 copy(p[heapAddr:], convert.UnsafeUInt16SliceToByteSlice(s)) 191 return heapAddr + (ln * 2) 192 } 193 194 // SetInt32Array encode int32 array to the payload buffer! 195 func SetInt32Array(p []byte, s []int32, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 196 var ln = uint32(len(s)) 197 SetUInt32(p, stackIndex, heapAddr) 198 SetUInt32(p, stackIndex+4, ln) 199 copy(p[heapAddr:], convert.UnsafeInt32SliceToByteSlice(s)) 200 return heapAddr + (ln * 4) 201 } 202 203 // SetUInt32Array encode uint32 array to the payload buffer! 204 func SetUInt32Array(p []byte, s []uint32, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 205 var ln = uint32(len(s)) 206 SetUInt32(p, stackIndex, heapAddr) 207 SetUInt32(p, stackIndex+4, ln) 208 copy(p[heapAddr:], convert.UnsafeUInt32SliceToByteSlice(s)) 209 return heapAddr + (ln * 4) 210 } 211 212 // SetInt64Array encode int64 array to the payload buffer! 213 func SetInt64Array(p []byte, s []int64, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 214 var ln = uint32(len(s)) 215 SetUInt32(p, stackIndex, heapAddr) 216 SetUInt32(p, stackIndex+4, ln) 217 copy(p[heapAddr:], convert.UnsafeInt64SliceToByteSlice(s)) 218 return heapAddr + (ln * 8) 219 } 220 221 // SetUInt64Array encode uint64 array to the payload buffer! 222 func SetUInt64Array(p []byte, s []uint64, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 223 var ln = uint32(len(s)) 224 SetUInt32(p, stackIndex, heapAddr) 225 SetUInt32(p, stackIndex+4, ln) 226 copy(p[heapAddr:], convert.UnsafeUInt64SliceToByteSlice(s)) 227 return heapAddr + (ln * 8) 228 } 229 230 // SetFloat32Array encode float32 array to the payload buffer! 231 func SetFloat32Array(p []byte, s []float32, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 232 var ln = uint32(len(s)) 233 SetUInt32(p, stackIndex, heapAddr) 234 SetUInt32(p, stackIndex+4, ln) 235 copy(p[heapAddr:], convert.UnsafeFloat32SliceToByteSlice(s)) 236 return heapAddr + (ln * 4) 237 } 238 239 // SetFloat64Array encode float64 array to the payload buffer! 240 func SetFloat64Array(p []byte, s []float64, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 241 var ln = uint32(len(s)) 242 SetUInt32(p, stackIndex, heapAddr) 243 SetUInt32(p, stackIndex+4, ln) 244 copy(p[heapAddr:], convert.UnsafeFloat64SliceToByteSlice(s)) 245 return heapAddr + (ln * 8) 246 } 247 248 // SetComplex64Array encode complex64 array to the payload buffer! 249 func SetComplex64Array(p []byte, s []complex64, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 250 var ln = uint32(len(s)) 251 SetUInt32(p, stackIndex, heapAddr) 252 SetUInt32(p, stackIndex+4, ln) 253 copy(p[heapAddr:], convert.UnsafeComplex64SliceToByteSlice(s)) 254 return heapAddr + (ln * 8) 255 } 256 257 // SetComplex128Array encode complex128 array to the payload buffer! 258 func SetComplex128Array(p []byte, s []complex128, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 259 var ln = uint32(len(s)) 260 SetUInt32(p, stackIndex, heapAddr) 261 SetUInt32(p, stackIndex+4, ln) 262 copy(p[heapAddr:], convert.UnsafeComplex128SliceToByteSlice(s)) 263 return heapAddr + (ln * 16) 264 } 265 266 /* 267 ************************************************************************************************** 268 *******************Dynamically size ARRAY inside other Dynamically size Array******************* 269 ************************************************************************************************** 270 */ 271 272 // SetStringArray encode string array to the payload buffer! 273 func SetStringArray(p []byte, s []string, stackIndex uint32, heapAddr uint32) (nextHeapAddr uint32) { 274 var ln = uint32(len(s)) 275 SetUInt32(p, stackIndex, heapAddr) 276 SetUInt32(p, stackIndex+4, ln) 277 nextHeapAddr = heapAddr + (ln * 8) 278 var eln uint32 279 for i := 0; i < int(ln); i++ { 280 eln = uint32(len(s[i])) 281 SetUInt32(p, heapAddr, nextHeapAddr) 282 SetUInt32(p, heapAddr+4, eln) 283 copy(p[nextHeapAddr:], s[i]) 284 heapAddr += 8 285 nextHeapAddr += eln 286 } 287 return 288 }