helper_internal.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. }
  47. return v.IsNil()
  48. case reflect.Struct:
  49. if !checkStruct {
  50. return false
  51. }
  52. // return true if all fields are empty. else return false.
  53. // we cannot use equality check, because some fields may be maps/slices/etc
  54. // and consequently the structs are not comparable.
  55. // return v.Interface() == reflect.Zero(v.Type()).Interface()
  56. for i, n := 0, v.NumField(); i < n; i++ {
  57. if !hIsEmptyValue(v.Field(i), deref, checkStruct) {
  58. return false
  59. }
  60. }
  61. return true
  62. }
  63. return false
  64. }
  65. func isEmptyValue(v reflect.Value, deref, checkStruct bool) bool {
  66. return hIsEmptyValue(v, deref, checkStruct)
  67. }
  68. func pruneSignExt(v []byte, pos bool) (n int) {
  69. if len(v) < 2 {
  70. } else if pos && v[0] == 0 {
  71. for ; v[n] == 0 && n+1 < len(v) && (v[n+1]&(1<<7) == 0); n++ {
  72. }
  73. } else if !pos && v[0] == 0xff {
  74. for ; v[n] == 0xff && n+1 < len(v) && (v[n+1]&(1<<7) != 0); n++ {
  75. }
  76. }
  77. return
  78. }
  79. // validate that this function is correct ...
  80. // culled from OGRE (Object-Oriented Graphics Rendering Engine)
  81. // function: halfToFloatI (http://stderr.org/doc/ogre-doc/api/OgreBitwise_8h-source.html)
  82. func halfFloatToFloatBits(yy uint16) (d uint32) {
  83. y := uint32(yy)
  84. s := (y >> 15) & 0x01
  85. e := (y >> 10) & 0x1f
  86. m := y & 0x03ff
  87. if e == 0 {
  88. if m == 0 { // plu or minus 0
  89. return s << 31
  90. }
  91. // 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. } else if e == 31 {
  100. if m == 0 { // Inf
  101. return (s << 31) | 0x7f800000
  102. }
  103. return (s << 31) | 0x7f800000 | (m << 13) // NaN
  104. }
  105. e = e + (127 - 15)
  106. m = m << 13
  107. return (s << 31) | (e << 23) | m
  108. }
  109. // GrowCap will return a new capacity for a slice, given the following:
  110. // - oldCap: current capacity
  111. // - unit: in-memory size of an element
  112. // - num: number of elements to add
  113. func growCap(oldCap, unit, num int) (newCap int) {
  114. // appendslice logic (if cap < 1024, *2, else *1.25):
  115. // leads to many copy calls, especially when copying bytes.
  116. // bytes.Buffer model (2*cap + n): much better for bytes.
  117. // smarter way is to take the byte-size of the appended element(type) into account
  118. // maintain 3 thresholds:
  119. // t1: if cap <= t1, newcap = 2x
  120. // t2: if cap <= t2, newcap = 1.75x
  121. // t3: if cap <= t3, newcap = 1.5x
  122. // else newcap = 1.25x
  123. //
  124. // t1, t2, t3 >= 1024 always.
  125. // i.e. if unit size >= 16, then always do 2x or 1.25x (ie t1, t2, t3 are all same)
  126. //
  127. // With this, appending for bytes increase by:
  128. // 100% up to 4K
  129. // 75% up to 8K
  130. // 50% up to 16K
  131. // 25% beyond that
  132. // unit can be 0 e.g. for struct{}{}; handle that appropriately
  133. var t1, t2, t3 int // thresholds
  134. if unit <= 1 {
  135. t1, t2, t3 = 4*1024, 8*1024, 16*1024
  136. } else if unit < 16 {
  137. t3 = 16 / unit * 1024
  138. t1 = t3 * 1 / 4
  139. t2 = t3 * 2 / 4
  140. } else {
  141. t1, t2, t3 = 1024, 1024, 1024
  142. }
  143. var x int // temporary variable
  144. // x is multiplier here: one of 5, 6, 7 or 8; incr of 25%, 50%, 75% or 100% respectively
  145. if oldCap <= t1 { // [0,t1]
  146. x = 8
  147. } else if oldCap > t3 { // (t3,infinity]
  148. x = 5
  149. } else if oldCap <= t2 { // (t1,t2]
  150. x = 7
  151. } else { // (t2,t3]
  152. x = 6
  153. }
  154. newCap = x * oldCap / 4
  155. if num > 0 {
  156. newCap += num
  157. }
  158. // ensure newCap is a multiple of 64 (if it is > 64) or 16.
  159. if newCap > 64 {
  160. if x = newCap % 64; x != 0 {
  161. x = newCap / 64
  162. newCap = 64 * (x + 1)
  163. }
  164. } else {
  165. if x = newCap % 16; x != 0 {
  166. x = newCap / 16
  167. newCap = 16 * (x + 1)
  168. }
  169. }
  170. return
  171. }