1 // Copyright (c) 2012, 2013 Ugorji Nwoke. All rights reserved.
2 // Use of this source code is governed by a BSD-style license found in the LICENSE file.
6 // All non-std package dependencies live in this file,
7 // so porting to different environment is easy (just update functions).
17 raisePanicAfterRecover = false
21 func panicValToErr(panicVal interface{}, err *error) {
22 switch xerr := panicVal.(type) {
26 *err = errors.New(xerr)
28 *err = fmt.Errorf("%v", panicVal)
30 if raisePanicAfterRecover {
36 func isEmptyValueDeref(v reflect.Value, deref bool) bool {
38 case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
42 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
44 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
46 case reflect.Float32, reflect.Float64:
48 case reflect.Interface, reflect.Ptr:
53 return isEmptyValueDeref(v.Elem(), deref)
58 // 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 !isEmptyValueDeref(v.Field(i), deref) {
73 func isEmptyValue(v reflect.Value) bool {
74 return isEmptyValueDeref(v, true)
77 func debugf(format string, args ...interface{}) {
79 if len(format) == 0 || format[len(format)-1] != '\n' {
80 format = format + "\n"
82 fmt.Printf(format, args...)
86 func pruneSignExt(v []byte, pos bool) (n int) {
88 } else if pos && v[0] == 0 {
89 for ; v[n] == 0 && n+1 < len(v) && (v[n+1]&(1<<7) == 0); n++ {
91 } else if !pos && v[0] == 0xff {
92 for ; v[n] == 0xff && n+1 < len(v) && (v[n+1]&(1<<7) != 0); n++ {
98 func implementsIntf(typ, iTyp reflect.Type) (success bool, indir int8) {
103 // The type might be a pointer and we need to keep
104 // dereferencing to the base type until we find an implementation.
106 if rt.Implements(iTyp) {
109 if p := rt; p.Kind() == reflect.Ptr {
111 if indir >= math.MaxInt8 { // insane number of indirections
119 // No luck yet, but if this is a base type (non-pointer), the pointer might satisfy.
120 if typ.Kind() != reflect.Ptr {
121 // Not a pointer, but does the pointer work?
122 if reflect.PtrTo(typ).Implements(iTyp) {