this repo has no description
1// Copyright 2020 The CUE Authors
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// Copyright 2018 The Go Authors. All rights reserved.
16// Use of this source code is governed by a BSD-style
17// license that can be found in the LICENSE file.
18
19// Originally generated with: go run qgo.go -exclude=Append,Unquote,Itoa,CanBackquote,FormatComplex extract strconv
20
21package strconv
22
23import (
24 "fmt"
25 "math/big"
26 "strconv"
27
28 "cuelang.org/go/cue"
29 "cuelang.org/go/cue/literal"
30 "cuelang.org/go/internal"
31)
32
33// ParseBool returns the boolean value represented by the string.
34// It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
35// Any other value returns an error.
36func ParseBool(str string) (bool, error) {
37 return strconv.ParseBool(str)
38}
39
40// FormatBool returns "true" or "false" according to the value of b.
41func FormatBool(b bool) string {
42 return strconv.FormatBool(b)
43}
44
45// ParseFloat converts the string s to a floating-point number
46// with the precision specified by bitSize: 32 for float32, or 64 for float64.
47// When bitSize=32, the result still has type float64, but it will be
48// convertible to float32 without changing its value.
49//
50// ParseFloat accepts decimal and hexadecimal floating-point number syntax.
51// If s is well-formed and near a valid floating-point number,
52// ParseFloat returns the nearest floating-point number rounded
53// using IEEE754 unbiased rounding.
54// (Parsing a hexadecimal floating-point value only rounds when
55// there are more bits in the hexadecimal representation than
56// will fit in the mantissa.)
57//
58// The errors that ParseFloat returns have concrete type *NumError
59// and include err.Num = s.
60//
61// If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.
62//
63// If s is syntactically well-formed but is more than 1/2 ULP
64// away from the largest floating point number of the given size,
65// ParseFloat returns f = ±Inf, err.Err = ErrRange.
66//
67// ParseFloat recognizes the strings "NaN", and the (possibly signed) strings "Inf" and "Infinity"
68// as their respective special floating point values. It ignores case when matching.
69func ParseFloat(s string, bitSize int) (float64, error) {
70 return strconv.ParseFloat(s, bitSize)
71}
72
73// ParseNumber interprets s using the full CUE number literal syntax and returns
74// the resulting value as an arbitrary-precision decimal. It accepts decimal
75// and non-decimal bases, underscores as separators, fractional syntax, and
76// the decimal or binary multiplier suffixes defined by CUE (for example "1Ki"
77// and "10M").
78//
79// If s is not syntactically well-formed, ParseNumber returns a *strconv.NumError
80// with Err containing detailed syntax information. Semantic errors, such as a
81// multiplier that cannot be represented, are reported in the same way.
82func ParseNumber(s string) (*internal.Decimal, error) {
83 var info literal.NumInfo
84 if err := literal.ParseNum(s, &info); err != nil {
85 return nil, &strconv.NumError{
86 Func: "ParseNumber",
87 Num: s,
88 Err: err,
89 }
90 }
91
92 var dec internal.Decimal
93 if err := info.Decimal(&dec); err != nil {
94 return nil, &strconv.NumError{
95 Func: "ParseNumber",
96 Num: s,
97 Err: err,
98 }
99 }
100 return &dec, nil
101}
102
103// IntSize is the size in bits of an int or uint value.
104const IntSize = 64
105
106// ParseUint is like [ParseInt] but for unsigned numbers.
107func ParseUint(s string, base int, bitSize int) (*big.Int, error) {
108 if bitSize < 0 {
109 return nil, &strconv.NumError{
110 Func: "ParseUint",
111 Num: s,
112 Err: strconv.ErrRange,
113 }
114 }
115
116 // Parse the number using big.Int to handle arbitrary precision
117 i := new(big.Int)
118 i, ok := i.SetString(s, base)
119 if !ok {
120 return nil, &strconv.NumError{
121 Func: "ParseUint",
122 Num: s,
123 Err: strconv.ErrSyntax,
124 }
125 }
126
127 // Check if the value is negative (not allowed for unsigned)
128 if i.Sign() < 0 {
129 return nil, &strconv.NumError{
130 Func: "ParseUint",
131 Num: s,
132 Err: strconv.ErrRange,
133 }
134 }
135
136 // If bitSize is 0, return unlimited precision result
137 if bitSize == 0 {
138 return i, nil
139 }
140
141 // Check if the value fits in the specified bit size
142 // For unsigned integers, the range is [0, 2^bitSize-1]
143 if i.BitLen() <= bitSize {
144 return i, nil
145 }
146
147 return nil, &strconv.NumError{
148 Func: "ParseUint",
149 Num: s,
150 Err: strconv.ErrRange,
151 }
152}
153
154// ParseInt interprets a string s in the given base (0, 2 to 36) and
155// bit size and returns the corresponding value i.
156//
157// If the base argument is 0, the true base is implied by the string's
158// prefix: 2 for "0b", 8 for "0" or "0o", 16 for "0x", and 10 otherwise.
159// Also, for argument base 0 only, underscore characters are permitted
160// as defined by the Go syntax for integer literals.
161//
162// The bitSize argument specifies the integer type that the result must fit into.
163// If bitSize is 0, the result is unconstrained (unlimited precision).
164// If bitSize is positive, the result must fit in a signed integer of that many bits.
165// If bitSize is negative, an error is returned.
166func ParseInt(s string, base int, bitSize int) (*big.Int, error) {
167 if bitSize < 0 {
168 return nil, &strconv.NumError{
169 Func: "ParseInt",
170 Num: s,
171 Err: strconv.ErrRange,
172 }
173 }
174
175 // Parse the number using big.Int to handle arbitrary precision
176 i := new(big.Int)
177 i, ok := i.SetString(s, base)
178 if !ok {
179 return nil, &strconv.NumError{
180 Func: "ParseInt",
181 Num: s,
182 Err: strconv.ErrSyntax,
183 }
184 }
185
186 // If bitSize is 0, return unlimited precision result
187 if bitSize == 0 {
188 return i, nil
189 }
190 // Check if the value fits in the specified bit size
191 // For signed integers, the range is [-2^(bitSize-1), 2^(bitSize-1)-1]
192 bitLen := i.BitLen()
193 if bitLen <= bitSize-1 {
194 return i, nil
195 }
196 if i.Sign() < 0 && bitLen == bitSize {
197 // It might be all 1s; add one and see if it fits.
198 x := big.NewInt(1)
199 x.Add(i, x)
200 if x.BitLen() <= bitSize-1 {
201 return i, nil
202 }
203 }
204 return nil, &strconv.NumError{
205 Func: "ParseInt",
206 Num: s,
207 Err: strconv.ErrRange,
208 }
209}
210
211// Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.
212func Atoi(s string) (*big.Int, error) {
213 n, err := ParseInt(s, 10, 0)
214 if err == nil {
215 return n, nil
216 }
217 if nerr, ok := err.(*strconv.NumError); ok {
218 nerr.Func = "Atoi"
219 }
220 return nil, err
221}
222
223// FormatFloat converts the floating-point number f to a string,
224// according to the format fmt and precision prec. It rounds the
225// result assuming that the original was obtained from a floating-point
226// value of bitSize bits (32 for float32, 64 for float64).
227//
228// The format fmt is a string or integer: one of
229// "b" (-ddddp±ddd, a binary exponent),
230// "e" (-d.dddde±dd, a decimal exponent),
231// "E" (-d.ddddE±dd, a decimal exponent),
232// "f" (-ddd.dddd, no exponent),
233// "g" ("e" for large exponents, "f" otherwise),
234// "G" ("E" for large exponents, "f" otherwise),
235// "x" (-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent), or
236// "X" (-0Xd.ddddP±ddd, a hexadecimal fraction and binary exponent).
237//
238// For historical reasons, an integer (the ASCII code point of a
239// format character) is also accepted for the format.
240//
241// The precision prec controls the number of digits (excluding the exponent)
242// printed by the "e", "E", "f", "g", "G", "x", and "X" formats.
243// For "e", "E", "f", "x", and "X", it is the number of digits after the decimal point.
244// For "g" and "G" it is the maximum number of significant digits (trailing
245// zeros are removed).
246// The special precision -1 uses the smallest number of digits
247// necessary such that ParseFloat will return f exactly.
248func FormatFloat(f float64, fmtVal cue.Value, prec, bitSize int) (string, error) {
249 var fmtByte byte
250 switch k := fmtVal.Kind(); k {
251 case cue.StringKind:
252 s, err := fmtVal.String()
253 if err != nil {
254 return "", err
255 }
256 if len(s) != 1 {
257 return "", fmt.Errorf("expected single character string")
258 }
259 fmtByte = s[0]
260 case cue.IntKind:
261 n, err := fmtVal.Int64()
262 if err != nil {
263 return "", err
264 }
265 if n < 0 || n > 255 {
266 return "", fmt.Errorf("format value %d out of range [0, 255]", n)
267 }
268 fmtByte = byte(n)
269 default:
270 return "", fmt.Errorf("unexpected kind %v", k)
271 }
272 return strconv.FormatFloat(f, fmtByte, prec, bitSize), nil
273}
274
275// FormatUint returns the string representation of i in the given base,
276// for 2 <= base <= 62. The result uses:
277// For 10 <= digit values <= 35, the lower-case letters 'a' to 'z'
278// For 36 <= digit values <= 61, the upper-case letters 'A' to 'Z'
279func FormatUint(i *big.Int, base int) string {
280 return i.Text(base)
281}
282
283// FormatInt returns the string representation of i in the given base,
284// for 2 <= base <= 62. The result uses:
285// For 10 <= digit values <= 35, the lower-case letters 'a' to 'z'
286// For 36 <= digit values <= 61, the upper-case letters 'A' to 'Z'
287func FormatInt(i *big.Int, base int) string {
288 return i.Text(base)
289}
290
291// Quote returns a double-quoted Go string literal representing s. The
292// returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for
293// control characters and non-printable characters as defined by
294// IsPrint.
295func Quote(s string) string {
296 return strconv.Quote(s)
297}
298
299// QuoteToASCII returns a double-quoted Go string literal representing s.
300// The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for
301// non-ASCII characters and non-printable characters as defined by IsPrint.
302func QuoteToASCII(s string) string {
303 return strconv.QuoteToASCII(s)
304}
305
306// QuoteToGraphic returns a double-quoted Go string literal representing s.
307// The returned string leaves Unicode graphic characters, as defined by
308// IsGraphic, unchanged and uses Go escape sequences (\t, \n, \xFF, \u0100)
309// for non-graphic characters.
310func QuoteToGraphic(s string) string {
311 return strconv.QuoteToGraphic(s)
312}
313
314// QuoteRune returns a single-quoted Go character literal representing the
315// rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100)
316// for control characters and non-printable characters as defined by IsPrint.
317func QuoteRune(r rune) string {
318 return strconv.QuoteRune(r)
319}
320
321// QuoteRuneToASCII returns a single-quoted Go character literal representing
322// the rune. The returned string uses Go escape sequences (\t, \n, \xFF,
323// \u0100) for non-ASCII characters and non-printable characters as defined
324// by IsPrint.
325func QuoteRuneToASCII(r rune) string {
326 return strconv.QuoteRuneToASCII(r)
327}
328
329// QuoteRuneToGraphic returns a single-quoted Go character literal representing
330// the rune. If the rune is not a Unicode graphic character,
331// as defined by IsGraphic, the returned string will use a Go escape sequence
332// (\t, \n, \xFF, \u0100).
333func QuoteRuneToGraphic(r rune) string {
334 return strconv.QuoteRuneToGraphic(r)
335}
336
337// IsPrint reports whether the rune is defined as printable by Go, with
338// the same definition as unicode.IsPrint: letters, numbers, punctuation,
339// symbols and ASCII space.
340func IsPrint(r rune) bool {
341 return strconv.IsPrint(r)
342}
343
344// IsGraphic reports whether the rune is defined as a Graphic by Unicode. Such
345// characters include letters, marks, numbers, punctuation, symbols, and
346// spaces, from categories L, M, N, P, S, and Zs.
347func IsGraphic(r rune) bool {
348 return strconv.IsGraphic(r)
349}