blob: 06213d5ed83a3ca6231691836bddabb5072e860e [file] [log] [blame]
Marcel van Lohuizenb5dc1922018-12-11 11:49:57 +01001// Copyright 2018 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
Marcel van Lohuizen730165c2019-08-01 16:11:41 +020019//go:generate qgo -exclude=Append,Unquote,Itoa,CanBackquote extract strconv
Marcel van Lohuizenb5dc1922018-12-11 11:49:57 +010020
21package strconv
22
23import "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.
28func ParseBool(str string) (bool, error) {
29 return strconv.ParseBool(str)
30}
31
32// FormatBool returns "true" or "false" according to the value of b.
33func FormatBool(b bool) string {
34 return strconv.FormatBool(b)
35}
36
37// ParseFloat converts the string s to a floating-point number
38// with the precision specified by bitSize: 32 for float32, or 64 for float64.
39// When bitSize=32, the result still has type float64, but it will be
40// convertible to float32 without changing its value.
41//
42// If s is well-formed and near a valid floating point number,
43// ParseFloat returns the nearest floating point number rounded
44// using IEEE754 unbiased rounding.
45//
46// The errors that ParseFloat returns have concrete type *NumError
47// and include err.Num = s.
48//
49// If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.
50//
51// If s is syntactically well-formed but is more than 1/2 ULP
52// away from the largest floating point number of the given size,
53// ParseFloat returns f = ±Inf, err.Err = ErrRange.
54func ParseFloat(s string, bitSize int) (float64, error) {
55 return strconv.ParseFloat(s, bitSize)
56}
57
58// IntSize is the size in bits of an int or uint value.
59const IntSize = 64
60
61// ParseUint is like ParseInt but for unsigned numbers.
62func ParseUint(s string, base int, bitSize int) (uint64, error) {
63 return strconv.ParseUint(s, base, bitSize)
64}
65
66// ParseInt interprets a string s in the given base (0, 2 to 36) and
67// bit size (0 to 64) and returns the corresponding value i.
68//
69// If base == 0, the base is implied by the string's prefix:
70// base 16 for "0x", base 8 for "0", and base 10 otherwise.
71// For bases 1, below 0 or above 36 an error is returned.
72//
73// The bitSize argument specifies the integer type
74// that the result must fit into. Bit sizes 0, 8, 16, 32, and 64
75// correspond to int, int8, int16, int32, and int64.
76// For a bitSize below 0 or above 64 an error is returned.
77//
78// The errors that ParseInt returns have concrete type *NumError
79// and include err.Num = s. If s is empty or contains invalid
80// digits, err.Err = ErrSyntax and the returned value is 0;
81// if the value corresponding to s cannot be represented by a
82// signed integer of the given size, err.Err = ErrRange and the
83// returned value is the maximum magnitude integer of the
84// appropriate bitSize and sign.
85func ParseInt(s string, base int, bitSize int) (i int64, err error) {
86 return strconv.ParseInt(s, base, bitSize)
87}
88
Marcel van Lohuizen6081b522019-06-22 15:43:52 +020089// Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.
Marcel van Lohuizenb5dc1922018-12-11 11:49:57 +010090func Atoi(s string) (int, error) {
91 return strconv.Atoi(s)
92}
93
94// FormatFloat converts the floating-point number f to a string,
95// according to the format fmt and precision prec. It rounds the
96// result assuming that the original was obtained from a floating-point
97// value of bitSize bits (32 for float32, 64 for float64).
98//
99// The format fmt is one of
100// 'b' (-ddddp±ddd, a binary exponent),
101// 'e' (-d.dddde±dd, a decimal exponent),
102// 'E' (-d.ddddE±dd, a decimal exponent),
103// 'f' (-ddd.dddd, no exponent),
104// 'g' ('e' for large exponents, 'f' otherwise), or
105// 'G' ('E' for large exponents, 'f' otherwise).
106//
107// The precision prec controls the number of digits (excluding the exponent)
108// printed by the 'e', 'E', 'f', 'g', and 'G' formats.
109// For 'e', 'E', and 'f' it is the number of digits after the decimal point.
110// For 'g' and 'G' it is the maximum number of significant digits (trailing
111// zeros are removed).
112// The special precision -1 uses the smallest number of digits
113// necessary such that ParseFloat will return f exactly.
114func FormatFloat(f float64, fmt byte, prec, bitSize int) string {
115 return strconv.FormatFloat(f, fmt, prec, bitSize)
116}
117
118// FormatUint returns the string representation of i in the given base,
119// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
120// for digit values >= 10.
121func FormatUint(i uint64, base int) string {
122 return strconv.FormatUint(i, base)
123}
124
125// FormatInt returns the string representation of i in the given base,
126// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
127// for digit values >= 10.
128func FormatInt(i int64, base int) string {
129 return strconv.FormatInt(i, base)
130}
131
132// Quote returns a double-quoted Go string literal representing s. The
133// returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for
134// control characters and non-printable characters as defined by
135// IsPrint.
136func Quote(s string) string {
137 return strconv.Quote(s)
138}
139
140// QuoteToASCII returns a double-quoted Go string literal representing s.
141// The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for
142// non-ASCII characters and non-printable characters as defined by IsPrint.
143func QuoteToASCII(s string) string {
144 return strconv.QuoteToASCII(s)
145}
146
147// QuoteToGraphic returns a double-quoted Go string literal representing s.
148// The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for
149// non-ASCII characters and non-printable characters as defined by IsGraphic.
150func QuoteToGraphic(s string) string {
151 return strconv.QuoteToGraphic(s)
152}
153
154// QuoteRune returns a single-quoted Go character literal representing the
155// rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100)
156// for control characters and non-printable characters as defined by IsPrint.
157func QuoteRune(r rune) string {
158 return strconv.QuoteRune(r)
159}
160
161// QuoteRuneToASCII returns a single-quoted Go character literal representing
162// the rune. The returned string uses Go escape sequences (\t, \n, \xFF,
163// \u0100) for non-ASCII characters and non-printable characters as defined
164// by IsPrint.
165func QuoteRuneToASCII(r rune) string {
166 return strconv.QuoteRuneToASCII(r)
167}
168
169// QuoteRuneToGraphic returns a single-quoted Go character literal representing
170// the rune. The returned string uses Go escape sequences (\t, \n, \xFF,
171// \u0100) for non-ASCII characters and non-printable characters as defined
172// by IsGraphic.
173func QuoteRuneToGraphic(r rune) string {
174 return strconv.QuoteRuneToGraphic(r)
175}
176
Marcel van Lohuizenb5dc1922018-12-11 11:49:57 +0100177// IsPrint reports whether the rune is defined as printable by Go, with
178// the same definition as unicode.IsPrint: letters, numbers, punctuation,
179// symbols and ASCII space.
180func IsPrint(r rune) bool {
181 return strconv.IsPrint(r)
182}
183
184// IsGraphic reports whether the rune is defined as a Graphic by Unicode. Such
185// characters include letters, marks, numbers, punctuation, symbols, and
186// spaces, from categories L, M, N, P, S, and Zs.
187func IsGraphic(r rune) bool {
188 return strconv.IsGraphic(r)
189}