github.com/patricebensoussan/go/codec@v1.2.99/helper_not_unsafe.go (about) 1 // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved. 2 // Use of this source code is governed by a MIT license found in the LICENSE file. 3 4 //go:build !go1.9 || safe || codec.safe || appengine 5 // +build !go1.9 safe codec.safe appengine 6 7 package codec 8 9 import ( 10 // "hash/adler32" 11 "math" 12 "reflect" 13 "sync/atomic" 14 "time" 15 ) 16 17 // This file has safe variants of some helper functions. 18 // MARKER: See helper_unsafe.go for the usage documentation. 19 20 const safeMode = true 21 22 const transientSizeMax = 0 23 const transientValueHasStringSlice = true 24 25 func stringView(v []byte) string { 26 return string(v) 27 } 28 29 func bytesView(v string) []byte { 30 return []byte(v) 31 } 32 33 func byteSliceSameData(v1 []byte, v2 []byte) bool { 34 return cap(v1) != 0 && cap(v2) != 0 && &(v1[:1][0]) == &(v2[:1][0]) 35 } 36 37 func okBytes3(b []byte) (v [4]byte) { 38 copy(v[1:], b) 39 return 40 } 41 42 func okBytes4(b []byte) (v [4]byte) { 43 copy(v[:], b) 44 return 45 } 46 47 func okBytes8(b []byte) (v [8]byte) { 48 copy(v[:], b) 49 return 50 } 51 52 func isNil(v interface{}) (rv reflect.Value, isnil bool) { 53 rv = reflect.ValueOf(v) 54 if isnilBitset.isset(byte(rv.Kind())) { 55 isnil = rv.IsNil() 56 } 57 return 58 } 59 60 func eq4i(i0, i1 interface{}) bool { 61 return i0 == i1 62 } 63 64 func rv4iptr(i interface{}) reflect.Value { return reflect.ValueOf(i) } 65 func rv4istr(i interface{}) reflect.Value { return reflect.ValueOf(i) } 66 67 // func rv4i(i interface{}) reflect.Value { return reflect.ValueOf(i) } 68 // func rv4iK(i interface{}, kind byte, isref bool) reflect.Value { return reflect.ValueOf(i) } 69 70 func rv2i(rv reflect.Value) interface{} { 71 return rv.Interface() 72 } 73 74 func rvAddr(rv reflect.Value, ptrType reflect.Type) reflect.Value { 75 return rv.Addr() 76 } 77 78 func rvIsNil(rv reflect.Value) bool { 79 return rv.IsNil() 80 } 81 82 func rvSetSliceLen(rv reflect.Value, length int) { 83 rv.SetLen(length) 84 } 85 86 func rvZeroAddrK(t reflect.Type, k reflect.Kind) reflect.Value { 87 return reflect.New(t).Elem() 88 } 89 90 func rvZeroK(t reflect.Type, k reflect.Kind) reflect.Value { 91 return reflect.Zero(t) 92 } 93 94 func rvConvert(v reflect.Value, t reflect.Type) (rv reflect.Value) { 95 // Note that reflect.Value.Convert(...) will make a copy if it is addressable. 96 // Since we decode into the passed value, we must try to convert the addressable value.. 97 if v.CanAddr() { 98 return v.Addr().Convert(reflect.PtrTo(t)).Elem() 99 } 100 return v.Convert(t) 101 } 102 103 func rt2id(rt reflect.Type) uintptr { 104 return reflect.ValueOf(rt).Pointer() 105 } 106 107 func i2rtid(i interface{}) uintptr { 108 return reflect.ValueOf(reflect.TypeOf(i)).Pointer() 109 } 110 111 // -------------------------- 112 113 func isEmptyValue(v reflect.Value, tinfos *TypeInfos, recursive bool) bool { 114 switch v.Kind() { 115 case reflect.Invalid: 116 return true 117 case reflect.Array, reflect.String: 118 return v.Len() == 0 119 case reflect.Map, reflect.Slice, reflect.Chan: 120 return v.IsNil() || v.Len() == 0 121 case reflect.Bool: 122 return !v.Bool() 123 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 124 return v.Int() == 0 125 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 126 return v.Uint() == 0 127 case reflect.Complex64, reflect.Complex128: 128 c := v.Complex() 129 return math.Float64bits(real(c)) == 0 && math.Float64bits(imag(c)) == 0 130 case reflect.Float32, reflect.Float64: 131 return v.Float() == 0 132 case reflect.Func, reflect.UnsafePointer: 133 return v.IsNil() 134 case reflect.Interface, reflect.Ptr: 135 isnil := v.IsNil() 136 if recursive && !isnil { 137 return isEmptyValue(v.Elem(), tinfos, recursive) 138 } 139 return isnil 140 case reflect.Struct: 141 return isEmptyStruct(v, tinfos, recursive) 142 } 143 return false 144 } 145 146 // isEmptyStruct is only called from isEmptyValue, and checks if a struct is empty: 147 // - does it implement IsZero() bool 148 // - is it comparable, and can i compare directly using == 149 // - if checkStruct, then walk through the encodable fields 150 // and check if they are empty or not. 151 func isEmptyStruct(v reflect.Value, tinfos *TypeInfos, recursive bool) bool { 152 // v is a struct kind - no need to check again. 153 // We only check isZero on a struct kind, to reduce the amount of times 154 // that we lookup the rtid and typeInfo for each type as we walk the tree. 155 156 vt := rvType(v) 157 rtid := rt2id(vt) 158 if tinfos == nil { 159 tinfos = defTypeInfos 160 } 161 ti := tinfos.get(rtid, vt) 162 if ti.rtid == timeTypId { 163 return rv2i(v).(time.Time).IsZero() 164 } 165 if ti.flagIsZeroer { 166 return rv2i(v).(isZeroer).IsZero() 167 } 168 if ti.flagIsZeroerPtr && v.CanAddr() { 169 return rv2i(v.Addr()).(isZeroer).IsZero() 170 } 171 if ti.flagIsCodecEmptyer { 172 return rv2i(v).(isCodecEmptyer).IsCodecEmpty() 173 } 174 if ti.flagIsCodecEmptyerPtr && v.CanAddr() { 175 return rv2i(v.Addr()).(isCodecEmptyer).IsCodecEmpty() 176 } 177 if ti.flagComparable { 178 return rv2i(v) == rv2i(rvZeroK(vt, reflect.Struct)) 179 } 180 if !recursive { 181 return false 182 } 183 // We only care about what we can encode/decode, 184 // so that is what we use to check omitEmpty. 185 for _, si := range ti.sfi.source() { 186 sfv := si.path.field(v) 187 if sfv.IsValid() && !isEmptyValue(sfv, tinfos, recursive) { 188 return false 189 } 190 } 191 return true 192 } 193 194 // -------------------------- 195 196 type perTypeElem struct { 197 t reflect.Type 198 rtid uintptr 199 zero reflect.Value 200 addr [2]reflect.Value 201 } 202 203 func (x *perTypeElem) get(index uint8) (v reflect.Value) { 204 v = x.addr[index%2] 205 if v.IsValid() { 206 v.Set(x.zero) 207 } else { 208 v = reflect.New(x.t).Elem() 209 x.addr[index%2] = v 210 } 211 return 212 } 213 214 type perType struct { 215 v []perTypeElem 216 } 217 218 type decPerType struct { 219 perType 220 } 221 222 type encPerType struct { 223 perType 224 } 225 226 func (x *perType) elem(t reflect.Type) *perTypeElem { 227 rtid := rt2id(t) 228 var h, i uint 229 var j = uint(len(x.v)) 230 LOOP: 231 if i < j { 232 h = (i + j) >> 1 // avoid overflow when computing h // h = i + (j-i)/2 233 if x.v[h].rtid < rtid { 234 i = h + 1 235 } else { 236 j = h 237 } 238 goto LOOP 239 } 240 if i < uint(len(x.v)) { 241 if x.v[i].rtid != rtid { 242 x.v = append(x.v, perTypeElem{}) 243 copy(x.v[i+1:], x.v[i:]) 244 x.v[i] = perTypeElem{t: t, rtid: rtid, zero: reflect.Zero(t)} 245 } 246 } else { 247 x.v = append(x.v, perTypeElem{t: t, rtid: rtid, zero: reflect.Zero(t)}) 248 } 249 return &x.v[i] 250 } 251 252 func (x *perType) TransientAddrK(t reflect.Type, k reflect.Kind) (rv reflect.Value) { 253 return x.elem(t).get(0) 254 } 255 256 func (x *perType) TransientAddr2K(t reflect.Type, k reflect.Kind) (rv reflect.Value) { 257 return x.elem(t).get(1) 258 } 259 260 func (x *perType) AddressableRO(v reflect.Value) (rv reflect.Value) { 261 rv = x.elem(v.Type()).get(0) 262 rvSetDirect(rv, v) 263 return 264 } 265 266 // -------------------------- 267 type structFieldInfos struct { 268 c []*structFieldInfo 269 s []*structFieldInfo 270 } 271 272 func (x *structFieldInfos) load(source, sorted []*structFieldInfo) { 273 x.c = source 274 x.s = sorted 275 } 276 277 func (x *structFieldInfos) sorted() (v []*structFieldInfo) { return x.s } 278 func (x *structFieldInfos) source() (v []*structFieldInfo) { return x.c } 279 280 type atomicClsErr struct { 281 v atomic.Value 282 } 283 284 func (x *atomicClsErr) load() (e clsErr) { 285 if i := x.v.Load(); i != nil { 286 e = i.(clsErr) 287 } 288 return 289 } 290 291 func (x *atomicClsErr) store(p clsErr) { 292 x.v.Store(p) 293 } 294 295 // -------------------------- 296 type atomicTypeInfoSlice struct { 297 v atomic.Value 298 } 299 300 func (x *atomicTypeInfoSlice) load() (e []rtid2ti) { 301 if i := x.v.Load(); i != nil { 302 e = i.([]rtid2ti) 303 } 304 return 305 } 306 307 func (x *atomicTypeInfoSlice) store(p []rtid2ti) { 308 x.v.Store(p) 309 } 310 311 // -------------------------- 312 type atomicRtidFnSlice struct { 313 v atomic.Value 314 } 315 316 func (x *atomicRtidFnSlice) load() (e []codecRtidFn) { 317 if i := x.v.Load(); i != nil { 318 e = i.([]codecRtidFn) 319 } 320 return 321 } 322 323 func (x *atomicRtidFnSlice) store(p []codecRtidFn) { 324 x.v.Store(p) 325 } 326 327 // -------------------------- 328 func (n *fauxUnion) ru() reflect.Value { 329 return reflect.ValueOf(&n.u).Elem() 330 } 331 func (n *fauxUnion) ri() reflect.Value { 332 return reflect.ValueOf(&n.i).Elem() 333 } 334 func (n *fauxUnion) rf() reflect.Value { 335 return reflect.ValueOf(&n.f).Elem() 336 } 337 func (n *fauxUnion) rl() reflect.Value { 338 return reflect.ValueOf(&n.l).Elem() 339 } 340 func (n *fauxUnion) rs() reflect.Value { 341 return reflect.ValueOf(&n.s).Elem() 342 } 343 func (n *fauxUnion) rt() reflect.Value { 344 return reflect.ValueOf(&n.t).Elem() 345 } 346 func (n *fauxUnion) rb() reflect.Value { 347 return reflect.ValueOf(&n.b).Elem() 348 } 349 350 // -------------------------- 351 func rvSetBytes(rv reflect.Value, v []byte) { 352 rv.SetBytes(v) 353 } 354 355 func rvSetString(rv reflect.Value, v string) { 356 rv.SetString(v) 357 } 358 359 func rvSetBool(rv reflect.Value, v bool) { 360 rv.SetBool(v) 361 } 362 363 func rvSetTime(rv reflect.Value, v time.Time) { 364 rv.Set(reflect.ValueOf(v)) 365 } 366 367 func rvSetFloat32(rv reflect.Value, v float32) { 368 rv.SetFloat(float64(v)) 369 } 370 371 func rvSetFloat64(rv reflect.Value, v float64) { 372 rv.SetFloat(v) 373 } 374 375 func rvSetComplex64(rv reflect.Value, v complex64) { 376 rv.SetComplex(complex128(v)) 377 } 378 379 func rvSetComplex128(rv reflect.Value, v complex128) { 380 rv.SetComplex(v) 381 } 382 383 func rvSetInt(rv reflect.Value, v int) { 384 rv.SetInt(int64(v)) 385 } 386 387 func rvSetInt8(rv reflect.Value, v int8) { 388 rv.SetInt(int64(v)) 389 } 390 391 func rvSetInt16(rv reflect.Value, v int16) { 392 rv.SetInt(int64(v)) 393 } 394 395 func rvSetInt32(rv reflect.Value, v int32) { 396 rv.SetInt(int64(v)) 397 } 398 399 func rvSetInt64(rv reflect.Value, v int64) { 400 rv.SetInt(v) 401 } 402 403 func rvSetUint(rv reflect.Value, v uint) { 404 rv.SetUint(uint64(v)) 405 } 406 407 func rvSetUintptr(rv reflect.Value, v uintptr) { 408 rv.SetUint(uint64(v)) 409 } 410 411 func rvSetUint8(rv reflect.Value, v uint8) { 412 rv.SetUint(uint64(v)) 413 } 414 415 func rvSetUint16(rv reflect.Value, v uint16) { 416 rv.SetUint(uint64(v)) 417 } 418 419 func rvSetUint32(rv reflect.Value, v uint32) { 420 rv.SetUint(uint64(v)) 421 } 422 423 func rvSetUint64(rv reflect.Value, v uint64) { 424 rv.SetUint(v) 425 } 426 427 // ---------------- 428 429 func rvSetDirect(rv reflect.Value, v reflect.Value) { 430 rv.Set(v) 431 } 432 433 func rvSetDirectZero(rv reflect.Value) { 434 rv.Set(reflect.Zero(rv.Type())) 435 } 436 437 // func rvSet(rv reflect.Value, v reflect.Value) { 438 // rv.Set(v) 439 // } 440 441 func rvSetIntf(rv reflect.Value, v reflect.Value) { 442 rv.Set(v) 443 } 444 445 func rvSetZero(rv reflect.Value) { 446 rv.Set(reflect.Zero(rv.Type())) 447 } 448 449 func rvSlice(rv reflect.Value, length int) reflect.Value { 450 return rv.Slice(0, length) 451 } 452 453 func rvMakeSlice(rv reflect.Value, ti *typeInfo, xlen, xcap int) (v reflect.Value, set bool) { 454 v = reflect.MakeSlice(ti.rt, xlen, xcap) 455 if rv.Len() > 0 { 456 reflect.Copy(v, rv) 457 } 458 return 459 } 460 461 func rvGrowSlice(rv reflect.Value, ti *typeInfo, cap, incr int) (v reflect.Value, newcap int, set bool) { 462 newcap = int(growCap(uint(cap), uint(ti.elemsize), uint(incr))) 463 v = reflect.MakeSlice(ti.rt, newcap, newcap) 464 if rv.Len() > 0 { 465 reflect.Copy(v, rv) 466 } 467 return 468 } 469 470 // ---------------- 471 472 func rvSliceIndex(rv reflect.Value, i int, ti *typeInfo) reflect.Value { 473 return rv.Index(i) 474 } 475 476 func rvArrayIndex(rv reflect.Value, i int, ti *typeInfo) reflect.Value { 477 return rv.Index(i) 478 } 479 480 func rvSliceZeroCap(t reflect.Type) (v reflect.Value) { 481 return reflect.MakeSlice(t, 0, 0) 482 } 483 484 func rvLenSlice(rv reflect.Value) int { 485 return rv.Len() 486 } 487 488 func rvCapSlice(rv reflect.Value) int { 489 return rv.Cap() 490 } 491 492 func rvGetArrayBytes(rv reflect.Value, scratch []byte) (bs []byte) { 493 l := rv.Len() 494 if scratch == nil || rv.CanAddr() { 495 return rv.Slice(0, l).Bytes() 496 } 497 498 if l <= cap(scratch) { 499 bs = scratch[:l] 500 } else { 501 bs = make([]byte, l) 502 } 503 reflect.Copy(reflect.ValueOf(bs), rv) 504 return 505 } 506 507 func rvGetArray4Slice(rv reflect.Value) (v reflect.Value) { 508 v = rvZeroAddrK(reflectArrayOf(rvLenSlice(rv), rvType(rv).Elem()), reflect.Array) 509 reflect.Copy(v, rv) 510 return 511 } 512 513 func rvGetSlice4Array(rv reflect.Value, v interface{}) { 514 // v is a pointer to a slice to be populated 515 516 // rv.Slice fails if address is not addressable, which can occur during encoding. 517 // Consequently, check if non-addressable, and if so, make new slice and copy into it first. 518 // MARKER: this *may* cause allocation if non-addressable, unfortunately. 519 520 rve := reflect.ValueOf(v).Elem() 521 l := rv.Len() 522 if rv.CanAddr() { 523 rve.Set(rv.Slice(0, l)) 524 } else { 525 rvs := reflect.MakeSlice(rve.Type(), l, l) 526 reflect.Copy(rvs, rv) 527 rve.Set(rvs) 528 } 529 // reflect.ValueOf(v).Elem().Set(rv.Slice(0, rv.Len())) 530 } 531 532 func rvCopySlice(dest, src reflect.Value, _ reflect.Type) { 533 reflect.Copy(dest, src) 534 } 535 536 // ------------ 537 538 func rvGetBool(rv reflect.Value) bool { 539 return rv.Bool() 540 } 541 542 func rvGetBytes(rv reflect.Value) []byte { 543 return rv.Bytes() 544 } 545 546 func rvGetTime(rv reflect.Value) time.Time { 547 return rv2i(rv).(time.Time) 548 } 549 550 func rvGetString(rv reflect.Value) string { 551 return rv.String() 552 } 553 554 func rvGetFloat64(rv reflect.Value) float64 { 555 return rv.Float() 556 } 557 558 func rvGetFloat32(rv reflect.Value) float32 { 559 return float32(rv.Float()) 560 } 561 562 func rvGetComplex64(rv reflect.Value) complex64 { 563 return complex64(rv.Complex()) 564 } 565 566 func rvGetComplex128(rv reflect.Value) complex128 { 567 return rv.Complex() 568 } 569 570 func rvGetInt(rv reflect.Value) int { 571 return int(rv.Int()) 572 } 573 574 func rvGetInt8(rv reflect.Value) int8 { 575 return int8(rv.Int()) 576 } 577 578 func rvGetInt16(rv reflect.Value) int16 { 579 return int16(rv.Int()) 580 } 581 582 func rvGetInt32(rv reflect.Value) int32 { 583 return int32(rv.Int()) 584 } 585 586 func rvGetInt64(rv reflect.Value) int64 { 587 return rv.Int() 588 } 589 590 func rvGetUint(rv reflect.Value) uint { 591 return uint(rv.Uint()) 592 } 593 594 func rvGetUint8(rv reflect.Value) uint8 { 595 return uint8(rv.Uint()) 596 } 597 598 func rvGetUint16(rv reflect.Value) uint16 { 599 return uint16(rv.Uint()) 600 } 601 602 func rvGetUint32(rv reflect.Value) uint32 { 603 return uint32(rv.Uint()) 604 } 605 606 func rvGetUint64(rv reflect.Value) uint64 { 607 return rv.Uint() 608 } 609 610 func rvGetUintptr(rv reflect.Value) uintptr { 611 return uintptr(rv.Uint()) 612 } 613 614 func rvLenMap(rv reflect.Value) int { 615 return rv.Len() 616 } 617 618 // func rvLenArray(rv reflect.Value) int { return rv.Len() } 619 620 // ------------ map range and map indexing ---------- 621 622 func mapStoresElemIndirect(elemsize uintptr) bool { return false } 623 624 func mapSet(m, k, v reflect.Value, keyFastKind mapKeyFastKind, _, _ bool) { 625 m.SetMapIndex(k, v) 626 } 627 628 func mapGet(m, k, v reflect.Value, keyFastKind mapKeyFastKind, _, _ bool) (vv reflect.Value) { 629 return m.MapIndex(k) 630 } 631 632 // func mapDelete(m, k reflect.Value) { 633 // m.SetMapIndex(k, reflect.Value{}) 634 // } 635 636 func mapAddrLoopvarRV(t reflect.Type, k reflect.Kind) (r reflect.Value) { 637 return // reflect.New(t).Elem() 638 } 639 640 // ---------- ENCODER optimized --------------- 641 642 func (e *Encoder) jsondriver() *jsonEncDriver { 643 return e.e.(*jsonEncDriver) 644 } 645 646 // ---------- DECODER optimized --------------- 647 648 func (d *Decoder) checkBreak() bool { 649 return d.d.CheckBreak() 650 } 651 652 func (d *Decoder) jsondriver() *jsonDecDriver { 653 return d.d.(*jsonDecDriver) 654 } 655 656 func (d *Decoder) stringZC(v []byte) (s string) { 657 return d.string(v) 658 } 659 660 func (d *Decoder) mapKeyString(callFnRvk *bool, kstrbs, kstr2bs *[]byte) string { 661 return d.string(*kstr2bs) 662 } 663 664 // ---------- structFieldInfo optimized --------------- 665 666 func (n *structFieldInfoPathNode) rvField(v reflect.Value) reflect.Value { 667 return v.Field(int(n.index)) 668 } 669 670 // ---------- others ---------------