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