podman

Форк
0
208 строк · 5.2 Кб
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

15
package swag
16

17
import (
18
	"math"
19
	"strconv"
20
	"strings"
21
)
22

23
// same as ECMA Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER
24
const (
25
	maxJSONFloat         = float64(1<<53 - 1)  // 9007199254740991.0 	 	 2^53 - 1
26
	minJSONFloat         = -float64(1<<53 - 1) //-9007199254740991.0	-2^53 - 1
27
	epsilon      float64 = 1e-9
28
)
29

30
// IsFloat64AJSONInteger allow for integers [-2^53, 2^53-1] inclusive
31
func IsFloat64AJSONInteger(f float64) bool {
32
	if math.IsNaN(f) || math.IsInf(f, 0) || f < minJSONFloat || f > maxJSONFloat {
33
		return false
34
	}
35
	fa := math.Abs(f)
36
	g := float64(uint64(f))
37
	ga := math.Abs(g)
38

39
	diff := math.Abs(f - g)
40

41
	// more info: https://floating-point-gui.de/errors/comparison/#look-out-for-edge-cases
42
	switch {
43
	case f == g: // best case
44
		return true
45
	case f == float64(int64(f)) || f == float64(uint64(f)): // optimistic case
46
		return true
47
	case f == 0 || g == 0 || diff < math.SmallestNonzeroFloat64: // very close to 0 values
48
		return diff < (epsilon * math.SmallestNonzeroFloat64)
49
	}
50
	// check the relative error
51
	return diff/math.Min(fa+ga, math.MaxFloat64) < epsilon
52
}
53

54
var evaluatesAsTrue map[string]struct{}
55

56
func init() {
57
	evaluatesAsTrue = map[string]struct{}{
58
		"true":     {},
59
		"1":        {},
60
		"yes":      {},
61
		"ok":       {},
62
		"y":        {},
63
		"on":       {},
64
		"selected": {},
65
		"checked":  {},
66
		"t":        {},
67
		"enabled":  {},
68
	}
69
}
70

71
// ConvertBool turn a string into a boolean
72
func ConvertBool(str string) (bool, error) {
73
	_, ok := evaluatesAsTrue[strings.ToLower(str)]
74
	return ok, nil
75
}
76

77
// ConvertFloat32 turn a string into a float32
78
func ConvertFloat32(str string) (float32, error) {
79
	f, err := strconv.ParseFloat(str, 32)
80
	if err != nil {
81
		return 0, err
82
	}
83
	return float32(f), nil
84
}
85

86
// ConvertFloat64 turn a string into a float64
87
func ConvertFloat64(str string) (float64, error) {
88
	return strconv.ParseFloat(str, 64)
89
}
90

91
// ConvertInt8 turn a string into an int8
92
func ConvertInt8(str string) (int8, error) {
93
	i, err := strconv.ParseInt(str, 10, 8)
94
	if err != nil {
95
		return 0, err
96
	}
97
	return int8(i), nil
98
}
99

100
// ConvertInt16 turn a string into an int16
101
func ConvertInt16(str string) (int16, error) {
102
	i, err := strconv.ParseInt(str, 10, 16)
103
	if err != nil {
104
		return 0, err
105
	}
106
	return int16(i), nil
107
}
108

109
// ConvertInt32 turn a string into an int32
110
func ConvertInt32(str string) (int32, error) {
111
	i, err := strconv.ParseInt(str, 10, 32)
112
	if err != nil {
113
		return 0, err
114
	}
115
	return int32(i), nil
116
}
117

118
// ConvertInt64 turn a string into an int64
119
func ConvertInt64(str string) (int64, error) {
120
	return strconv.ParseInt(str, 10, 64)
121
}
122

123
// ConvertUint8 turn a string into an uint8
124
func ConvertUint8(str string) (uint8, error) {
125
	i, err := strconv.ParseUint(str, 10, 8)
126
	if err != nil {
127
		return 0, err
128
	}
129
	return uint8(i), nil
130
}
131

132
// ConvertUint16 turn a string into an uint16
133
func ConvertUint16(str string) (uint16, error) {
134
	i, err := strconv.ParseUint(str, 10, 16)
135
	if err != nil {
136
		return 0, err
137
	}
138
	return uint16(i), nil
139
}
140

141
// ConvertUint32 turn a string into an uint32
142
func ConvertUint32(str string) (uint32, error) {
143
	i, err := strconv.ParseUint(str, 10, 32)
144
	if err != nil {
145
		return 0, err
146
	}
147
	return uint32(i), nil
148
}
149

150
// ConvertUint64 turn a string into an uint64
151
func ConvertUint64(str string) (uint64, error) {
152
	return strconv.ParseUint(str, 10, 64)
153
}
154

155
// FormatBool turns a boolean into a string
156
func FormatBool(value bool) string {
157
	return strconv.FormatBool(value)
158
}
159

160
// FormatFloat32 turns a float32 into a string
161
func FormatFloat32(value float32) string {
162
	return strconv.FormatFloat(float64(value), 'f', -1, 32)
163
}
164

165
// FormatFloat64 turns a float64 into a string
166
func FormatFloat64(value float64) string {
167
	return strconv.FormatFloat(value, 'f', -1, 64)
168
}
169

170
// FormatInt8 turns an int8 into a string
171
func FormatInt8(value int8) string {
172
	return strconv.FormatInt(int64(value), 10)
173
}
174

175
// FormatInt16 turns an int16 into a string
176
func FormatInt16(value int16) string {
177
	return strconv.FormatInt(int64(value), 10)
178
}
179

180
// FormatInt32 turns an int32 into a string
181
func FormatInt32(value int32) string {
182
	return strconv.Itoa(int(value))
183
}
184

185
// FormatInt64 turns an int64 into a string
186
func FormatInt64(value int64) string {
187
	return strconv.FormatInt(value, 10)
188
}
189

190
// FormatUint8 turns an uint8 into a string
191
func FormatUint8(value uint8) string {
192
	return strconv.FormatUint(uint64(value), 10)
193
}
194

195
// FormatUint16 turns an uint16 into a string
196
func FormatUint16(value uint16) string {
197
	return strconv.FormatUint(uint64(value), 10)
198
}
199

200
// FormatUint32 turns an uint32 into a string
201
func FormatUint32(value uint32) string {
202
	return strconv.FormatUint(uint64(value), 10)
203
}
204

205
// FormatUint64 turns an uint64 into a string
206
func FormatUint64(value uint64) string {
207
	return strconv.FormatUint(value, 10)
208
}
209

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.