strconv包学习整理(golang)

atob.go

// 将布尔值转换为字符串 true 或 false
func FormatBool(b bool) string

// 将字符串转换为布尔值
// 它接受真值:1, t, T, TRUE, true, True
// 它接受假值:0, f, F, FALSE, false, False
// 其它任何值都返回一个错误。
func ParseBool(str string) (bool, error)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// ParseBool returns the boolean value represented by the string.
// It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
// Any other value returns an error.
func ParseBool(str string) (bool, error) {
switch str {
case "1", "t", "T", "true", "TRUE", "True":
return true, nil
case "0", "f", "F", "false", "FALSE", "False":
return false, nil
}
return false, syntaxError("ParseBool", str)
}

// FormatBool returns "true" or "false" according to the value of b.
func FormatBool(b bool) string {
if b {
return "true"
}
return "false"
}

// AppendBool appends "true" or "false", according to the value of b,
// to dst and returns the extended buffer.
func AppendBool(dst []byte, b bool) []byte {
if b {
return append(dst, "true"...)
}
return append(dst, "false"...)
}

atof.go && ftoa.go

// FormatFloat 将浮点数 f 转换为字符串形式
// f:要转换的浮点数
// fmt:格式标记(b、e、E、f、g、G)
// prec:精度(数字部分的长度,不包括指数部分)
// bitSize:指定浮点类型(32:float32、64:float64),结果会据此进行舍入。
//
// 格式标记:
// ‘b’ (-ddddp±ddd,二进制指数)
// ‘e’ (-d.dddde±dd,十进制指数)
// ‘E’ (-d.ddddE±dd,十进制指数)
// ‘f’ (-ddd.dddd,没有指数)
// ‘g’ (‘e’:大指数,’f’:其它情况)
// ‘G’ (‘E’:大指数,’f’:其它情况)
//
// 如果格式标记为 ‘e’,’E’和’f’,则 prec 表示小数点后的数字位数
// 如果格式标记为 ‘g’,’G’,则 prec 表示总的数字位数(整数部分+小数部分)
// 参考格式化输入输出中的旗标和精度说明
func FormatFloat(f float64, fmt byte, prec, bitSize int) string

// 将字符串解析为浮点数,使用 IEEE754 规范进行舍入。
// bigSize 取值有 32 和 64 两种,表示转换结果的精度。
// 如果有语法错误,则 err.Error = ErrSyntax
// 如果结果超出范围,则返回 ±Inf,err.Error = ErrRange
func ParseFloat(s string, bitSize int) (float64, error)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// ParseFloat converts the string s to a floating-point number
// with the precision specified by bitSize: 32 for float32, or 64 for float64.
// When bitSize=32, the result still has type float64, but it will be
// convertible to float32 without changing its value.
//
// If s is well-formed and near a valid floating point number,
// ParseFloat returns the nearest floating point number rounded
// using IEEE754 unbiased rounding.
//
// The errors that ParseFloat returns have concrete type *NumError
// and include err.Num = s.
//
// If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.
//
// If s is syntactically well-formed but is more than 1/2 ULP
// away from the largest floating point number of the given size,
// ParseFloat returns f = ±Inf, err.Err = ErrRange.
func ParseFloat(s string, bitSize int) (float64, error) {
if bitSize == 32 {
f, err := atof32(s)
return float64(f), err
}
return atof64(s)
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// FormatFloat converts the floating-point number f to a string,
// according to the format fmt and precision prec. It rounds the
// result assuming that the original was obtained from a floating-point
// value of bitSize bits (32 for float32, 64 for float64).
//
// The format fmt is one of
// 'b' (-ddddp±ddd, a binary exponent),
// 'e' (-d.dddde±dd, a decimal exponent),
// 'E' (-d.ddddE±dd, a decimal exponent),
// 'f' (-ddd.dddd, no exponent),
// 'g' ('e' for large exponents, 'f' otherwise), or
// 'G' ('E' for large exponents, 'f' otherwise).
//
// The precision prec controls the number of digits (excluding the exponent)
// printed by the 'e', 'E', 'f', 'g', and 'G' formats.
// For 'e', 'E', and 'f' it is the number of digits after the decimal point.
// For 'g' and 'G' it is the maximum number of significant digits (trailing
// zeros are removed).
// The special precision -1 uses the smallest number of digits
// necessary such that ParseFloat will return f exactly.
func FormatFloat(f float64, fmt byte, prec, bitSize int) string {
return string(genericFtoa(make([]byte, 0, max(prec+4, 24)), f, fmt, prec, bitSize))
}

// AppendFloat appends the string form of the floating-point number f,
// as generated by FormatFloat, to dst and returns the extended buffer.
func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte {
return genericFtoa(dst, f, fmt, prec, bitSize)
}

atoi.go && itoa.go

// ErrRange 表示值超出范围
var ErrRange = errors.New(“value out of range”)

// ErrSyntax 表示语法不正确
var ErrSyntax = errors.New(“invalid syntax”)

// 将整数转换为字符串形式。base 表示转换进制,取值在 2 到 36 之间。
// 结果中大于 10 的数字用小写字母 a - z 表示。
func FormatInt(i int64, base int) string
func FormatUint(i uint64, base int) string

// 将字符串解析为整数,ParseInt 支持正负号,ParseUint 不支持正负号。
// base 表示进位制(2 到 36),如果 base 为 0,则根据字符串前缀判断,
// 前缀 0x 表示 16 进制,前缀 0 表示 8 进制,否则是 10 进制。
// bitSize 表示结果的位宽(包括符号位),0 表示最大位宽。
func ParseInt(s string, base int, bitSize int) (i int64, err error)
func ParseUint(s string, base int, bitSize int) (uint64, error)

// 将整数转换为十进制字符串形式(即:FormatInt(i, 10) 的简写)
func Itoa(i int) string

// 将字符串转换为十进制整数,即:ParseInt(s, 10, 0) 的简写)
func Atoi(s string) (int, error)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// ErrRange indicates that a value is out of range for the target type.
var ErrRange = errors.New("value out of range")

// ErrSyntax indicates that a value does not have the right syntax for the target type.
var ErrSyntax = errors.New("invalid syntax")
// Atoi returns the result of ParseInt(s, 10, 0) converted to type int.
func Atoi(s string) (int, error) {
const fnAtoi = "Atoi"

sLen := len(s)
if intSize == 32 && (0 < sLen && sLen < 10) ||
intSize == 64 && (0 < sLen && sLen < 19) {
// Fast path for small integers that fit int type.
s0 := s
if s[0] == '-' || s[0] == '+' {
s = s[1:]
if len(s) < 1 {
return 0, &NumError{fnAtoi, s0, ErrSyntax}
}
}

n := 0
for _, ch := range []byte(s) {
ch -= '0'
if ch > 9 {
return 0, &NumError{fnAtoi, s0, ErrSyntax}
}
n = n*10 + int(ch)
}
if s0[0] == '-' {
n = -n
}
return n, nil
}

// Slow path for invalid or big integers.
i64, err := ParseInt(s, 10, 0)
if nerr, ok := err.(*NumError); ok {
nerr.Func = fnAtoi
}
return int(i64), err
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// FormatInt returns the string representation of i in the given base,
// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
// for digit values >= 10.
func FormatInt(i int64, base int) string {
if fastSmalls && 0 <= i && i < nSmalls && base == 10 {
return small(int(i))
}
_, s := formatBits(nil, uint64(i), base, i < 0, false)
return s
}

// Itoa is shorthand for FormatInt(int64(i), 10).
func Itoa(i int) string {
return FormatInt(int64(i), 10)
}

// AppendInt appends the string form of the integer i,
// as generated by FormatInt, to dst and returns the extended buffer.
func AppendInt(dst []byte, i int64, base int) []byte {
if fastSmalls && 0 <= i && i < nSmalls && base == 10 {
return append(dst, small(int(i))...)
}
dst, _ = formatBits(dst, uint64(i), base, i < 0, true)
return dst
}

其他

// 判断字符串是否可以不被修改的表示为一个单行的反引号字符串。
// 字符串中不能含有控制字符(除了 \t)和“反引号”字符,否则返回 false
func CanBackquote(s string) bool

// 判断 r 是否为可打印字符
// 可否打印并不是你想象的那样,比如空格可以打印,而\t则不能打印
func IsPrint(r rune) bool

// 判断 r 是否为 Unicode 定义的图形字符。
func IsGraphic(r rune) bool

// 将 s 转换为双引号字符串
func Quote(s string) string

// 功能同上,非 ASCII 字符和不可打印字符会被转义
func QuoteToASCII(s string) string

// 功能同上,非图形字符会被转义
func QuoteToGraphic(s string) string

// 将 r 转换为单引号字符
func QuoteRune(r rune) string

// 功能同上,非 ASCII 字符和不可打印字符会被转义
func QuoteRuneToASCII(r rune) string

// 功能同上,非图形字符会被转义
func QuoteRuneToGraphic(r rune) string