helper_internal.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT 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(), deref, checkStruct)
  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), deref, checkStruct) {
  60. return false
  61. }
  62. }
  63. return true
  64. }
  65. return false
  66. }
  67. func isEmptyValue(v reflect.Value, deref, checkStruct bool) bool {
  68. return hIsEmptyValue(v, deref, checkStruct)
  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. }
  142. // GrowCap will return a new capacity for a slice, given the following:
  143. // - oldCap: current capacity
  144. // - unit: in-memory size of an element
  145. // - num: number of elements to add
  146. func growCap(oldCap, unit, num int) (newCap int) {
  147. // appendslice logic (if cap < 1024, *2, else *1.25):
  148. // leads to many copy calls, especially when copying bytes.
  149. // bytes.Buffer model (2*cap + n): much better for bytes.
  150. // smarter way is to take the byte-size of the appended element(type) into account
  151. // maintain 3 thresholds:
  152. // t1: if cap <= t1, newcap = 2x
  153. // t2: if cap <= t2, newcap = 1.75x
  154. // t3: if cap <= t3, newcap = 1.5x
  155. // else newcap = 1.25x
  156. //
  157. // t1, t2, t3 >= 1024 always.
  158. // i.e. if unit size >= 16, then always do 2x or 1.25x (ie t1, t2, t3 are all same)
  159. //
  160. // With this, appending for bytes increase by:
  161. // 100% up to 4K
  162. // 75% up to 8K
  163. // 50% up to 16K
  164. // 25% beyond that
  165. // unit can be 0 e.g. for struct{}{}; handle that appropriately
  166. var t1, t2, t3 int // thresholds
  167. if unit <= 1 {
  168. t1, t2, t3 = 4*1024, 8*1024, 16*1024
  169. } else if unit < 16 {
  170. t3 = 16 / unit * 1024
  171. t1 = t3 * 1 / 4
  172. t2 = t3 * 2 / 4
  173. } else {
  174. t1, t2, t3 = 1024, 1024, 1024
  175. }
  176. var x int // temporary variable
  177. // x is multiplier here: one of 5, 6, 7 or 8; incr of 25%, 50%, 75% or 100% respectively
  178. if oldCap <= t1 { // [0,t1]
  179. x = 8
  180. } else if oldCap > t3 { // (t3,infinity]
  181. x = 5
  182. } else if oldCap <= t2 { // (t1,t2]
  183. x = 7
  184. } else { // (t2,t3]
  185. x = 6
  186. }
  187. newCap = x * oldCap / 4
  188. if num > 0 {
  189. newCap += num
  190. }
  191. // ensure newCap is a multiple of 64 (if it is > 64) or 16.
  192. if newCap > 64 {
  193. if x = newCap % 64; x != 0 {
  194. x = newCap / 64
  195. newCap = 64 * (x + 1)
  196. }
  197. } else {
  198. if x = newCap % 16; x != 0 {
  199. x = newCap / 16
  200. newCap = 16 * (x + 1)
  201. }
  202. }
  203. return
  204. }
  205. func expandSliceValue(s reflect.Value, num int) reflect.Value {
  206. if num <= 0 {
  207. return s
  208. }
  209. l0 := s.Len()
  210. l1 := l0 + num // new slice length
  211. if l1 < l0 {
  212. panic("ExpandSlice: slice overflow")
  213. }
  214. c0 := s.Cap()
  215. if l1 <= c0 {
  216. return s.Slice(0, l1)
  217. }
  218. st := s.Type()
  219. c1 := growCap(c0, int(st.Elem().Size()), num)
  220. s2 := reflect.MakeSlice(st, l1, c1)
  221. // println("expandslicevalue: cap-old: ", c0, ", cap-new: ", c1, ", len-new: ", l1)
  222. reflect.Copy(s2, s)
  223. return s2
  224. }