helper_internal.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.
  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. "math"
  10. "reflect"
  11. )
  12. var (
  13. raisePanicAfterRecover = false
  14. debugging = true
  15. )
  16. func panicValToErr(panicVal interface{}, err *error) {
  17. switch xerr := panicVal.(type) {
  18. case error:
  19. *err = xerr
  20. case string:
  21. *err = errors.New(xerr)
  22. default:
  23. *err = fmt.Errorf("%v", panicVal)
  24. }
  25. if raisePanicAfterRecover {
  26. panic(panicVal)
  27. }
  28. return
  29. }
  30. func isEmptyValueDeref(v reflect.Value, deref bool) bool {
  31. switch v.Kind() {
  32. case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
  33. return v.Len() == 0
  34. case reflect.Bool:
  35. return !v.Bool()
  36. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  37. return v.Int() == 0
  38. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  39. return v.Uint() == 0
  40. case reflect.Float32, reflect.Float64:
  41. return v.Float() == 0
  42. case reflect.Interface, reflect.Ptr:
  43. if deref {
  44. if v.IsNil() {
  45. return true
  46. }
  47. return isEmptyValueDeref(v.Elem(), deref)
  48. } else {
  49. return v.IsNil()
  50. }
  51. case reflect.Struct:
  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 !isEmptyValueDeref(v.Field(i), deref) {
  58. return false
  59. }
  60. }
  61. return true
  62. }
  63. return false
  64. }
  65. func isEmptyValue(v reflect.Value) bool {
  66. return isEmptyValueDeref(v, true)
  67. }
  68. func debugf(format string, args ...interface{}) {
  69. if debugging {
  70. if len(format) == 0 || format[len(format)-1] != '\n' {
  71. format = format + "\n"
  72. }
  73. fmt.Printf(format, args...)
  74. }
  75. }
  76. func pruneSignExt(v []byte, pos bool) (n int) {
  77. if len(v) < 2 {
  78. } else if pos && v[0] == 0 {
  79. for ; v[n] == 0 && n+1 < len(v) && (v[n+1]&(1<<7) == 0); n++ {
  80. }
  81. } else if !pos && v[0] == 0xff {
  82. for ; v[n] == 0xff && n+1 < len(v) && (v[n+1]&(1<<7) != 0); n++ {
  83. }
  84. }
  85. return
  86. }
  87. func implementsIntf(typ, iTyp reflect.Type) (success bool, indir int8) {
  88. if typ == nil {
  89. return
  90. }
  91. rt := typ
  92. // The type might be a pointer and we need to keep
  93. // dereferencing to the base type until we find an implementation.
  94. for {
  95. if rt.Implements(iTyp) {
  96. return true, indir
  97. }
  98. if p := rt; p.Kind() == reflect.Ptr {
  99. indir++
  100. if indir >= math.MaxInt8 { // insane number of indirections
  101. return false, 0
  102. }
  103. rt = p.Elem()
  104. continue
  105. }
  106. break
  107. }
  108. // No luck yet, but if this is a base type (non-pointer), the pointer might satisfy.
  109. if typ.Kind() != reflect.Ptr {
  110. // Not a pointer, but does the pointer work?
  111. if reflect.PtrTo(typ).Implements(iTyp) {
  112. return true, -1
  113. }
  114. }
  115. return false, 0
  116. }
  117. // validate that this function is correct ...
  118. // culled from OGRE (Object-Oriented Graphics Rendering Engine)
  119. // function: halfToFloatI (http://stderr.org/doc/ogre-doc/api/OgreBitwise_8h-source.html)
  120. func halfFloatToFloatBits(yy uint16) (d uint32) {
  121. y := uint32(yy)
  122. s := (y >> 15) & 0x01
  123. e := (y >> 10) & 0x1f
  124. m := y & 0x03ff
  125. if e == 0 {
  126. if m == 0 { // plu or minus 0
  127. return s << 31
  128. } else { // Denormalized number -- renormalize it
  129. for (m & 0x00000400) == 0 {
  130. m <<= 1
  131. e -= 1
  132. }
  133. e += 1
  134. const zz uint32 = 0x0400
  135. m &= ^zz
  136. }
  137. } else if e == 31 {
  138. if m == 0 { // Inf
  139. return (s << 31) | 0x7f800000
  140. } else { // NaN
  141. return (s << 31) | 0x7f800000 | (m << 31)
  142. }
  143. }
  144. e = e + (127 - 15)
  145. m = m << 13
  146. return (s << 31) | (e << 23) | m
  147. }