本站源代码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

209 rindas
5.2KB

  1. // Copyright 2015 go-swagger maintainers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package swag
  15. import (
  16. "math"
  17. "strconv"
  18. "strings"
  19. )
  20. // same as ECMA Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER
  21. const (
  22. maxJSONFloat = float64(1<<53 - 1) // 9007199254740991.0 2^53 - 1
  23. minJSONFloat = -float64(1<<53 - 1) //-9007199254740991.0 -2^53 - 1
  24. epsilon float64 = 1e-9
  25. )
  26. // IsFloat64AJSONInteger allow for integers [-2^53, 2^53-1] inclusive
  27. func IsFloat64AJSONInteger(f float64) bool {
  28. if math.IsNaN(f) || math.IsInf(f, 0) || f < minJSONFloat || f > maxJSONFloat {
  29. return false
  30. }
  31. fa := math.Abs(f)
  32. g := float64(uint64(f))
  33. ga := math.Abs(g)
  34. diff := math.Abs(f - g)
  35. // more info: https://floating-point-gui.de/errors/comparison/#look-out-for-edge-cases
  36. switch {
  37. case f == g: // best case
  38. return true
  39. case f == float64(int64(f)) || f == float64(uint64(f)): // optimistic case
  40. return true
  41. case f == 0 || g == 0 || diff < math.SmallestNonzeroFloat64: // very close to 0 values
  42. return diff < (epsilon * math.SmallestNonzeroFloat64)
  43. }
  44. // check the relative error
  45. return diff/math.Min(fa+ga, math.MaxFloat64) < epsilon
  46. }
  47. var evaluatesAsTrue map[string]struct{}
  48. func init() {
  49. evaluatesAsTrue = map[string]struct{}{
  50. "true": {},
  51. "1": {},
  52. "yes": {},
  53. "ok": {},
  54. "y": {},
  55. "on": {},
  56. "selected": {},
  57. "checked": {},
  58. "t": {},
  59. "enabled": {},
  60. }
  61. }
  62. // ConvertBool turn a string into a boolean
  63. func ConvertBool(str string) (bool, error) {
  64. _, ok := evaluatesAsTrue[strings.ToLower(str)]
  65. return ok, nil
  66. }
  67. // ConvertFloat32 turn a string into a float32
  68. func ConvertFloat32(str string) (float32, error) {
  69. f, err := strconv.ParseFloat(str, 32)
  70. if err != nil {
  71. return 0, err
  72. }
  73. return float32(f), nil
  74. }
  75. // ConvertFloat64 turn a string into a float64
  76. func ConvertFloat64(str string) (float64, error) {
  77. return strconv.ParseFloat(str, 64)
  78. }
  79. // ConvertInt8 turn a string into int8 boolean
  80. func ConvertInt8(str string) (int8, error) {
  81. i, err := strconv.ParseInt(str, 10, 8)
  82. if err != nil {
  83. return 0, err
  84. }
  85. return int8(i), nil
  86. }
  87. // ConvertInt16 turn a string into a int16
  88. func ConvertInt16(str string) (int16, error) {
  89. i, err := strconv.ParseInt(str, 10, 16)
  90. if err != nil {
  91. return 0, err
  92. }
  93. return int16(i), nil
  94. }
  95. // ConvertInt32 turn a string into a int32
  96. func ConvertInt32(str string) (int32, error) {
  97. i, err := strconv.ParseInt(str, 10, 32)
  98. if err != nil {
  99. return 0, err
  100. }
  101. return int32(i), nil
  102. }
  103. // ConvertInt64 turn a string into a int64
  104. func ConvertInt64(str string) (int64, error) {
  105. return strconv.ParseInt(str, 10, 64)
  106. }
  107. // ConvertUint8 turn a string into a uint8
  108. func ConvertUint8(str string) (uint8, error) {
  109. i, err := strconv.ParseUint(str, 10, 8)
  110. if err != nil {
  111. return 0, err
  112. }
  113. return uint8(i), nil
  114. }
  115. // ConvertUint16 turn a string into a uint16
  116. func ConvertUint16(str string) (uint16, error) {
  117. i, err := strconv.ParseUint(str, 10, 16)
  118. if err != nil {
  119. return 0, err
  120. }
  121. return uint16(i), nil
  122. }
  123. // ConvertUint32 turn a string into a uint32
  124. func ConvertUint32(str string) (uint32, error) {
  125. i, err := strconv.ParseUint(str, 10, 32)
  126. if err != nil {
  127. return 0, err
  128. }
  129. return uint32(i), nil
  130. }
  131. // ConvertUint64 turn a string into a uint64
  132. func ConvertUint64(str string) (uint64, error) {
  133. return strconv.ParseUint(str, 10, 64)
  134. }
  135. // FormatBool turns a boolean into a string
  136. func FormatBool(value bool) string {
  137. return strconv.FormatBool(value)
  138. }
  139. // FormatFloat32 turns a float32 into a string
  140. func FormatFloat32(value float32) string {
  141. return strconv.FormatFloat(float64(value), 'f', -1, 32)
  142. }
  143. // FormatFloat64 turns a float64 into a string
  144. func FormatFloat64(value float64) string {
  145. return strconv.FormatFloat(value, 'f', -1, 64)
  146. }
  147. // FormatInt8 turns an int8 into a string
  148. func FormatInt8(value int8) string {
  149. return strconv.FormatInt(int64(value), 10)
  150. }
  151. // FormatInt16 turns an int16 into a string
  152. func FormatInt16(value int16) string {
  153. return strconv.FormatInt(int64(value), 10)
  154. }
  155. // FormatInt32 turns an int32 into a string
  156. func FormatInt32(value int32) string {
  157. return strconv.Itoa(int(value))
  158. }
  159. // FormatInt64 turns an int64 into a string
  160. func FormatInt64(value int64) string {
  161. return strconv.FormatInt(value, 10)
  162. }
  163. // FormatUint8 turns an uint8 into a string
  164. func FormatUint8(value uint8) string {
  165. return strconv.FormatUint(uint64(value), 10)
  166. }
  167. // FormatUint16 turns an uint16 into a string
  168. func FormatUint16(value uint16) string {
  169. return strconv.FormatUint(uint64(value), 10)
  170. }
  171. // FormatUint32 turns an uint32 into a string
  172. func FormatUint32(value uint32) string {
  173. return strconv.FormatUint(uint64(value), 10)
  174. }
  175. // FormatUint64 turns an uint64 into a string
  176. func FormatUint64(value uint64) string {
  177. return strconv.FormatUint(value, 10)
  178. }
上海开阖软件有限公司 沪ICP备12045867号-1