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