cuelang.org/go@v0.13.0/pkg/strconv/strconv.go (about) 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 21 package strconv 22 23 import ( 24 "math/big" 25 "strconv" 26 ) 27 28 // ParseBool returns the boolean value represented by the string. 29 // It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. 30 // Any other value returns an error. 31 func ParseBool(str string) (bool, error) { 32 return strconv.ParseBool(str) 33 } 34 35 // FormatBool returns "true" or "false" according to the value of b. 36 func FormatBool(b bool) string { 37 return strconv.FormatBool(b) 38 } 39 40 // ParseFloat converts the string s to a floating-point number 41 // with the precision specified by bitSize: 32 for float32, or 64 for float64. 42 // When bitSize=32, the result still has type float64, but it will be 43 // convertible to float32 without changing its value. 44 // 45 // ParseFloat accepts decimal and hexadecimal floating-point number syntax. 46 // If s is well-formed and near a valid floating-point number, 47 // ParseFloat returns the nearest floating-point number rounded 48 // using IEEE754 unbiased rounding. 49 // (Parsing a hexadecimal floating-point value only rounds when 50 // there are more bits in the hexadecimal representation than 51 // will fit in the mantissa.) 52 // 53 // The errors that ParseFloat returns have concrete type *NumError 54 // and include err.Num = s. 55 // 56 // If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax. 57 // 58 // If s is syntactically well-formed but is more than 1/2 ULP 59 // away from the largest floating point number of the given size, 60 // ParseFloat returns f = ±Inf, err.Err = ErrRange. 61 // 62 // ParseFloat recognizes the strings "NaN", and the (possibly signed) strings "Inf" and "Infinity" 63 // as their respective special floating point values. It ignores case when matching. 64 func ParseFloat(s string, bitSize int) (float64, error) { 65 return strconv.ParseFloat(s, bitSize) 66 } 67 68 // IntSize is the size in bits of an int or uint value. 69 const IntSize = 64 70 71 // ParseUint is like ParseInt but for unsigned numbers. 72 func ParseUint(s string, base int, bitSize int) (uint64, error) { 73 return strconv.ParseUint(s, base, bitSize) 74 } 75 76 // ParseInt interprets a string s in the given base (0, 2 to 36) and 77 // bit size (0 to 64) and returns the corresponding value i. 78 // 79 // If the base argument is 0, the true base is implied by the string's 80 // prefix: 2 for "0b", 8 for "0" or "0o", 16 for "0x", and 10 otherwise. 81 // Also, for argument base 0 only, underscore characters are permitted 82 // as defined by the Go syntax for integer literals. 83 // 84 // The bitSize argument specifies the integer type 85 // that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 86 // correspond to int, int8, int16, int32, and int64. 87 // If bitSize is below 0 or above 64, an error is returned. 88 // 89 // The errors that ParseInt returns have concrete type *NumError 90 // and include err.Num = s. If s is empty or contains invalid 91 // digits, err.Err = ErrSyntax and the returned value is 0; 92 // if the value corresponding to s cannot be represented by a 93 // signed integer of the given size, err.Err = ErrRange and the 94 // returned value is the maximum magnitude integer of the 95 // appropriate bitSize and sign. 96 func ParseInt(s string, base int, bitSize int) (i int64, err error) { 97 return strconv.ParseInt(s, base, bitSize) 98 } 99 100 // Atoi is equivalent to ParseInt(s, 10, 0), converted to type int. 101 func Atoi(s string) (int, error) { 102 return strconv.Atoi(s) 103 } 104 105 // FormatFloat converts the floating-point number f to a string, 106 // according to the format fmt and precision prec. It rounds the 107 // result assuming that the original was obtained from a floating-point 108 // value of bitSize bits (32 for float32, 64 for float64). 109 // 110 // The format fmt is one of 111 // 'b' (-ddddp±ddd, a binary exponent), 112 // 'e' (-d.dddde±dd, a decimal exponent), 113 // 'E' (-d.ddddE±dd, a decimal exponent), 114 // 'f' (-ddd.dddd, no exponent), 115 // 'g' ('e' for large exponents, 'f' otherwise), 116 // 'G' ('E' for large exponents, 'f' otherwise), 117 // 'x' (-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent), or 118 // 'X' (-0Xd.ddddP±ddd, a hexadecimal fraction and binary exponent). 119 // 120 // The precision prec controls the number of digits (excluding the exponent) 121 // printed by the 'e', 'E', 'f', 'g', 'G', 'x', and 'X' formats. 122 // For 'e', 'E', 'f', 'x', and 'X', it is the number of digits after the decimal point. 123 // For 'g' and 'G' it is the maximum number of significant digits (trailing 124 // zeros are removed). 125 // The special precision -1 uses the smallest number of digits 126 // necessary such that ParseFloat will return f exactly. 127 func FormatFloat(f float64, fmt byte, prec, bitSize int) string { 128 return strconv.FormatFloat(f, fmt, prec, bitSize) 129 } 130 131 // FormatUint returns the string representation of i in the given base, 132 // for 2 <= base <= 62. The result uses: 133 // For 10 <= digit values <= 35, the lower-case letters 'a' to 'z' 134 // For 36 <= digit values <= 61, the upper-case letters 'A' to 'Z' 135 func FormatUint(i *big.Int, base int) string { 136 return i.Text(base) 137 } 138 139 // FormatInt returns the string representation of i in the given base, 140 // for 2 <= base <= 62. The result uses: 141 // For 10 <= digit values <= 35, the lower-case letters 'a' to 'z' 142 // For 36 <= digit values <= 61, the upper-case letters 'A' to 'Z' 143 func FormatInt(i *big.Int, base int) string { 144 return i.Text(base) 145 } 146 147 // Quote returns a double-quoted Go string literal representing s. The 148 // returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for 149 // control characters and non-printable characters as defined by 150 // IsPrint. 151 func Quote(s string) string { 152 return strconv.Quote(s) 153 } 154 155 // QuoteToASCII returns a double-quoted Go string literal representing s. 156 // The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for 157 // non-ASCII characters and non-printable characters as defined by IsPrint. 158 func QuoteToASCII(s string) string { 159 return strconv.QuoteToASCII(s) 160 } 161 162 // QuoteToGraphic returns a double-quoted Go string literal representing s. 163 // The returned string leaves Unicode graphic characters, as defined by 164 // IsGraphic, unchanged and uses Go escape sequences (\t, \n, \xFF, \u0100) 165 // for non-graphic characters. 166 func QuoteToGraphic(s string) string { 167 return strconv.QuoteToGraphic(s) 168 } 169 170 // QuoteRune returns a single-quoted Go character literal representing the 171 // rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) 172 // for control characters and non-printable characters as defined by IsPrint. 173 func QuoteRune(r rune) string { 174 return strconv.QuoteRune(r) 175 } 176 177 // QuoteRuneToASCII returns a single-quoted Go character literal representing 178 // the rune. The returned string uses Go escape sequences (\t, \n, \xFF, 179 // \u0100) for non-ASCII characters and non-printable characters as defined 180 // by IsPrint. 181 func QuoteRuneToASCII(r rune) string { 182 return strconv.QuoteRuneToASCII(r) 183 } 184 185 // QuoteRuneToGraphic returns a single-quoted Go character literal representing 186 // the rune. If the rune is not a Unicode graphic character, 187 // as defined by IsGraphic, the returned string will use a Go escape sequence 188 // (\t, \n, \xFF, \u0100). 189 func QuoteRuneToGraphic(r rune) string { 190 return strconv.QuoteRuneToGraphic(r) 191 } 192 193 // IsPrint reports whether the rune is defined as printable by Go, with 194 // the same definition as unicode.IsPrint: letters, numbers, punctuation, 195 // symbols and ASCII space. 196 func IsPrint(r rune) bool { 197 return strconv.IsPrint(r) 198 } 199 200 // IsGraphic reports whether the rune is defined as a Graphic by Unicode. Such 201 // characters include letters, marks, numbers, punctuation, symbols, and 202 // spaces, from categories L, M, N, P, S, and Zs. 203 func IsGraphic(r rune) bool { 204 return strconv.IsGraphic(r) 205 }