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