...

Source file src/cuelang.org/go/pkg/strconv/strconv.go

Documentation: cuelang.org/go/pkg/strconv

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

View as plain text