helper_internal.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) 2012, 2013 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license found in the LICENSE file.
  3. package codec
  4. // All non-std package dependencies live in this file,
  5. // so porting to different environment is easy (just update functions).
  6. import (
  7. "errors"
  8. "fmt"
  9. "math"
  10. "reflect"
  11. )
  12. func panicValToErr(panicVal interface{}, err *error) {
  13. if panicVal == nil {
  14. return
  15. }
  16. // case nil
  17. switch xerr := panicVal.(type) {
  18. case error:
  19. *err = xerr
  20. case string:
  21. *err = errors.New(xerr)
  22. default:
  23. *err = fmt.Errorf("%v", panicVal)
  24. }
  25. return
  26. }
  27. func hIsEmptyValue(v reflect.Value, deref, checkStruct bool) bool {
  28. switch v.Kind() {
  29. case reflect.Invalid:
  30. return true
  31. case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
  32. return v.Len() == 0
  33. case reflect.Bool:
  34. return !v.Bool()
  35. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  36. return v.Int() == 0
  37. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  38. return v.Uint() == 0
  39. case reflect.Float32, reflect.Float64:
  40. return v.Float() == 0
  41. case reflect.Interface, reflect.Ptr:
  42. if deref {
  43. if v.IsNil() {
  44. return true
  45. }
  46. return hIsEmptyValue(v.Elem(), checkStruct, deref)
  47. } else {
  48. return v.IsNil()
  49. }
  50. case reflect.Struct:
  51. if !checkStruct {
  52. return false
  53. }
  54. // return true if all fields are empty. else return false.
  55. // we cannot use equality check, because some fields may be maps/slices/etc
  56. // and consequently the structs are not comparable.
  57. // return v.Interface() == reflect.Zero(v.Type()).Interface()
  58. for i, n := 0, v.NumField(); i < n; i++ {
  59. if !hIsEmptyValue(v.Field(i), checkStruct, deref) {
  60. return false
  61. }
  62. }
  63. return true
  64. }
  65. return false
  66. }
  67. func isEmptyValue(v reflect.Value) bool {
  68. return hIsEmptyValue(v, true, checkStructForEmptyValue)
  69. }
  70. func pruneSignExt(v []byte, pos bool) (n int) {
  71. if len(v) < 2 {
  72. } else if pos && v[0] == 0 {
  73. for ; v[n] == 0 && n+1 < len(v) && (v[n+1]&(1<<7) == 0); n++ {
  74. }
  75. } else if !pos && v[0] == 0xff {
  76. for ; v[n] == 0xff && n+1 < len(v) && (v[n+1]&(1<<7) != 0); n++ {
  77. }
  78. }
  79. return
  80. }
  81. func implementsIntf(typ, iTyp reflect.Type) (success bool, indir int8) {
  82. if typ == nil {
  83. return
  84. }
  85. rt := typ
  86. // The type might be a pointer and we need to keep
  87. // dereferencing to the base type until we find an implementation.
  88. for {
  89. if rt.Implements(iTyp) {
  90. return true, indir
  91. }
  92. if p := rt; p.Kind() == reflect.Ptr {
  93. indir++
  94. if indir >= math.MaxInt8 { // insane number of indirections
  95. return false, 0
  96. }
  97. rt = p.Elem()
  98. continue
  99. }
  100. break
  101. }
  102. // No luck yet, but if this is a base type (non-pointer), the pointer might satisfy.
  103. if typ.Kind() != reflect.Ptr {
  104. // Not a pointer, but does the pointer work?
  105. if reflect.PtrTo(typ).Implements(iTyp) {
  106. return true, -1
  107. }
  108. }
  109. return false, 0
  110. }
  111. // validate that this function is correct ...
  112. // culled from OGRE (Object-Oriented Graphics Rendering Engine)
  113. // function: halfToFloatI (http://stderr.org/doc/ogre-doc/api/OgreBitwise_8h-source.html)
  114. func halfFloatToFloatBits(yy uint16) (d uint32) {
  115. y := uint32(yy)
  116. s := (y >> 15) & 0x01
  117. e := (y >> 10) & 0x1f
  118. m := y & 0x03ff
  119. if e == 0 {
  120. if m == 0 { // plu or minus 0
  121. return s << 31
  122. } else { // Denormalized number -- renormalize it
  123. for (m & 0x00000400) == 0 {
  124. m <<= 1
  125. e -= 1
  126. }
  127. e += 1
  128. const zz uint32 = 0x0400
  129. m &= ^zz
  130. }
  131. } else if e == 31 {
  132. if m == 0 { // Inf
  133. return (s << 31) | 0x7f800000
  134. } else { // NaN
  135. return (s << 31) | 0x7f800000 | (m << 13)
  136. }
  137. }
  138. e = e + (127 - 15)
  139. m = m << 13
  140. return (s << 31) | (e << 23) | m
  141. }