helper_internal.go 4.4 KB

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