helper_internal.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. "reflect"
  10. )
  11. func panicValToErr(panicVal interface{}, err *error) {
  12. if panicVal == nil {
  13. return
  14. }
  15. // case nil
  16. switch xerr := panicVal.(type) {
  17. case error:
  18. *err = xerr
  19. case string:
  20. *err = errors.New(xerr)
  21. default:
  22. *err = fmt.Errorf("%v", panicVal)
  23. }
  24. return
  25. }
  26. func hIsEmptyValue(v reflect.Value, deref, checkStruct bool) bool {
  27. switch v.Kind() {
  28. case reflect.Invalid:
  29. return true
  30. case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
  31. return v.Len() == 0
  32. case reflect.Bool:
  33. return !v.Bool()
  34. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  35. return v.Int() == 0
  36. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  37. return v.Uint() == 0
  38. case reflect.Float32, reflect.Float64:
  39. return v.Float() == 0
  40. case reflect.Interface, reflect.Ptr:
  41. if deref {
  42. if v.IsNil() {
  43. return true
  44. }
  45. return hIsEmptyValue(v.Elem(), deref, checkStruct)
  46. } else {
  47. return v.IsNil()
  48. }
  49. case reflect.Struct:
  50. if !checkStruct {
  51. return false
  52. }
  53. // return true if all fields are empty. else return false.
  54. // we cannot use equality check, because some fields may be maps/slices/etc
  55. // and consequently the structs are not comparable.
  56. // return v.Interface() == reflect.Zero(v.Type()).Interface()
  57. for i, n := 0, v.NumField(); i < n; i++ {
  58. if !hIsEmptyValue(v.Field(i), deref, checkStruct) {
  59. return false
  60. }
  61. }
  62. return true
  63. }
  64. return false
  65. }
  66. func isEmptyValue(v reflect.Value, deref, checkStruct bool) bool {
  67. return hIsEmptyValue(v, deref, checkStruct)
  68. }
  69. func pruneSignExt(v []byte, pos bool) (n int) {
  70. if len(v) < 2 {
  71. } else if pos && v[0] == 0 {
  72. for ; v[n] == 0 && n+1 < len(v) && (v[n+1]&(1<<7) == 0); n++ {
  73. }
  74. } else if !pos && v[0] == 0xff {
  75. for ; v[n] == 0xff && n+1 < len(v) && (v[n+1]&(1<<7) != 0); n++ {
  76. }
  77. }
  78. return
  79. }
  80. // validate that this function is correct ...
  81. // culled from OGRE (Object-Oriented Graphics Rendering Engine)
  82. // function: halfToFloatI (http://stderr.org/doc/ogre-doc/api/OgreBitwise_8h-source.html)
  83. func halfFloatToFloatBits(yy uint16) (d uint32) {
  84. y := uint32(yy)
  85. s := (y >> 15) & 0x01
  86. e := (y >> 10) & 0x1f
  87. m := y & 0x03ff
  88. if e == 0 {
  89. if m == 0 { // plu or minus 0
  90. return s << 31
  91. } else { // Denormalized number -- renormalize it
  92. for (m & 0x00000400) == 0 {
  93. m <<= 1
  94. e -= 1
  95. }
  96. e += 1
  97. const zz uint32 = 0x0400
  98. m &= ^zz
  99. }
  100. } else if e == 31 {
  101. if m == 0 { // Inf
  102. return (s << 31) | 0x7f800000
  103. } else { // NaN
  104. return (s << 31) | 0x7f800000 | (m << 13)
  105. }
  106. }
  107. e = e + (127 - 15)
  108. m = m << 13
  109. return (s << 31) | (e << 23) | m
  110. }
  111. // GrowCap will return a new capacity for a slice, given the following:
  112. // - oldCap: current capacity
  113. // - unit: in-memory size of an element
  114. // - num: number of elements to add
  115. func growCap(oldCap, unit, num int) (newCap int) {
  116. // appendslice logic (if cap < 1024, *2, else *1.25):
  117. // leads to many copy calls, especially when copying bytes.
  118. // bytes.Buffer model (2*cap + n): much better for bytes.
  119. // smarter way is to take the byte-size of the appended element(type) into account
  120. // maintain 3 thresholds:
  121. // t1: if cap <= t1, newcap = 2x
  122. // t2: if cap <= t2, newcap = 1.75x
  123. // t3: if cap <= t3, newcap = 1.5x
  124. // else newcap = 1.25x
  125. //
  126. // t1, t2, t3 >= 1024 always.
  127. // i.e. if unit size >= 16, then always do 2x or 1.25x (ie t1, t2, t3 are all same)
  128. //
  129. // With this, appending for bytes increase by:
  130. // 100% up to 4K
  131. // 75% up to 8K
  132. // 50% up to 16K
  133. // 25% beyond that
  134. // unit can be 0 e.g. for struct{}{}; handle that appropriately
  135. var t1, t2, t3 int // thresholds
  136. if unit <= 1 {
  137. t1, t2, t3 = 4*1024, 8*1024, 16*1024
  138. } else if unit < 16 {
  139. t3 = 16 / unit * 1024
  140. t1 = t3 * 1 / 4
  141. t2 = t3 * 2 / 4
  142. } else {
  143. t1, t2, t3 = 1024, 1024, 1024
  144. }
  145. var x int // temporary variable
  146. // x is multiplier here: one of 5, 6, 7 or 8; incr of 25%, 50%, 75% or 100% respectively
  147. if oldCap <= t1 { // [0,t1]
  148. x = 8
  149. } else if oldCap > t3 { // (t3,infinity]
  150. x = 5
  151. } else if oldCap <= t2 { // (t1,t2]
  152. x = 7
  153. } else { // (t2,t3]
  154. x = 6
  155. }
  156. newCap = x * oldCap / 4
  157. if num > 0 {
  158. newCap += num
  159. }
  160. // ensure newCap is a multiple of 64 (if it is > 64) or 16.
  161. if newCap > 64 {
  162. if x = newCap % 64; x != 0 {
  163. x = newCap / 64
  164. newCap = 64 * (x + 1)
  165. }
  166. } else {
  167. if x = newCap % 16; x != 0 {
  168. x = newCap / 16
  169. newCap = 16 * (x + 1)
  170. }
  171. }
  172. return
  173. }