std.fmt.parse_float.parse.common
别名
std.fmt.parse_float.parse.FloatStream
别名
std.fmt.parse_float.parse.isEightDigits
别名
std.fmt.parse_float.parse.min_n_digit_int
函数
fn min_n_digit_int(comptime T: type, digit_count: usize) T
参数
T: type
digit_count: usize
std.fmt.parse_float.parse.Number
别名
std.fmt.parse_float.parse.parse8Digits
函数
解析按小端顺序以字节加载的 8 个数字。
利用每个数字都处于 [0x030, 0x39] 范围这一特性,仅用 3 次乘法即可解析,远快于通常的 8 次。
基于《快速将数字字符串转换为整数》中描述的算法:https://johnnylee-sde.github.io/Fast-numeric-string-to-int/。
fn parse8Digits(v_: u64) u64
参数
v_: u64
std.fmt.parse_float.parse.ParseInfo
容器
字段
base: u8
max_mantissa_digits: usize
exp_char_lower: u8
std.fmt.parse_float.parse.parseInfOrNan
函数
pub fn parseInfOrNan(comptime T: type, s: []const u8, negative: bool) ?T
参数
T: type
s: []const u8
negative: bool
std.fmt.parse_float.parse.parseNumber
函数
pub fn parseNumber(comptime T: type, s: []const u8, negative: bool) ?Number(T)
参数
T: type
s: []const u8
negative: bool
std.fmt.parse_float.parse.parsePartialInfOrNan
函数
fn parsePartialInfOrNan(comptime T: type, s: []const u8, negative: bool, n: *usize) ?T
参数
T: type
s: []const u8
negative: bool
n: *usize
std.fmt.parse_float.parse.parsePartialNumber
函数
解析不完整的非特殊浮点数。
由有效数字和十进制指数 构造该浮点数的表示。
fn parsePartialNumber(comptime T: type, s: []const u8, negative: bool, n: *usize) ?Number(T)
参数
T: type
s: []const u8
negative: bool
n: *usize
std.fmt.parse_float.parse.parsePartialNumberBase
函数
fn parsePartialNumberBase(comptime T: type, stream: *FloatStream, negative: bool, n: *usize, comptime info: ParseInfo) ?Number(T)
参数
std.fmt.parse_float.parse.parseScientific
函数
解析浮点数的科学记数法部分。
fn parseScientific(stream: *FloatStream) ?i64
参数
stream: *FloatStream
std.fmt.parse_float.parse.std
别名
别名目标 std
std.fmt.parse_float.parse.tryParseDigits
函数
解析数字,直到遇到非数字字符。
fn tryParseDigits(comptime T: type, stream: *FloatStream, x: *T, comptime base: u8) void
参数
std.fmt.parse_float.parse.tryParseNDigits
函数
最多解析 N 位数字
fn tryParseNDigits(comptime T: type, stream: *FloatStream, x: *T, comptime base: u8, comptime n: usize) void
参数
std.fmt.parse_float.parse.validUnderscores
函数
pub fn validUnderscores(s: []const u8, comptime base: u8) bool
参数
s: []const u8
base: u8
源码
std/fmt/parse_float/parse.zig
const std = @import("std");
const common = @import("common.zig");
const FloatStream = @import("FloatStream.zig");
const isEightDigits = common.isEightDigits;
const Number = common.Number;
/// 解析按小端顺序以字节加载的 8 个数字。
///
/// 利用每个数字都处于 [0x030, 0x39] 范围这一特性,仅用 3 次乘法即可解析,远快于通常的 8 次。
///
/// 基于《快速将数字字符串转换为整数》中描述的算法:<https://johnnylee-sde.github.io/Fast-numeric-string-to-int/>。
///
///
///
fn parse8Digits(v_: u64) u64 {
var v = v_;
const mask = 0x0000_00ff_0000_00ff;
const mul1 = 0x000f_4240_0000_0064;
const mul2 = 0x0000_2710_0000_0001;
v -= 0x3030_3030_3030_3030;
v = (v * 10) + (v >> 8); // 不会溢出,可容纳于 63 位
const v1 = (v & mask) *% mul1;
const v2 = ((v >> 16) & mask) *% mul2;
return @as(u64, @as(u32, @truncate((v1 +% v2) >> 32)));
}
/// 解析数字,直到遇到非数字字符。
fn tryParseDigits(comptime T: type, stream: *FloatStream, x: *T, comptime base: u8) void {
// 使用优化算法尝试一次解析 8 位数字。
// 仅支持十进制数字。
if (base == 10) {
while (stream.hasLen(8)) {
const v = stream.readU64Unchecked();
if (!isEightDigits(v)) {
break;
}
x.* = x.* *% 1_0000_0000 +% parse8Digits(v);
stream.advance(8);
}
}
while (stream.scanDigit(base)) |digit| {
x.* *%= base;
x.* +%= digit;
}
}
fn min_n_digit_int(comptime T: type, digit_count: usize) T {
var n: T = 1;
var i: usize = 1;
while (i < digit_count) : (i += 1) n *= 10;
return n;
}
/// 最多解析 N 位数字
fn tryParseNDigits(comptime T: type, stream: *FloatStream, x: *T, comptime base: u8, comptime n: usize) void {
while (x.* < min_n_digit_int(T, n)) {
if (stream.scanDigit(base)) |digit| {
x.* *%= base;
x.* +%= digit;
} else {
break;
}
}
}
/// 解析浮点数的科学记数法部分。
fn parseScientific(stream: *FloatStream) ?i64 {
var exponent: i64 = 0;
var negative = false;
if (stream.first()) |c| {
negative = c == '-';
if (c == '-' or c == '+') {
stream.advance(1);
}
}
if (stream.firstIsDigit(10)) {
while (stream.scanDigit(10)) |digit| {
// 此处不会溢出,在远未溢出前即达到饱和值。
if (exponent < 0x1000_0000) {
exponent = 10 * exponent + digit;
}
}
return if (negative) -exponent else exponent;
}
return null;
}
const ParseInfo = struct {
// 10 或 16
base: u8,
// 10^19 可放入 u64,16^16 可放入 u64
max_mantissa_digits: usize,
// 例如 e 或 p(也检查 E 和 P)
exp_char_lower: u8,
};
fn parsePartialNumberBase(comptime T: type, stream: *FloatStream, negative: bool, n: *usize, comptime info: ParseInfo) ?Number(T) {
std.debug.assert(info.base == 10 or info.base == 16);
const MantissaT = common.mantissaType(T);
// 解析小数点前的起始数字
var mantissa: MantissaT = 0;
tryParseDigits(MantissaT, stream, &mantissa, info.base);
const int_end = stream.offsetTrue();
var n_digits = @as(isize, @intCast(stream.offsetTrue()));
// 处理点号及其后的数字
var exponent: i64 = 0;
if (stream.firstIs(".")) {
stream.advance(1);
const marker = stream.offsetTrue();
tryParseDigits(MantissaT, stream, &mantissa, info.base);
const n_after_dot = stream.offsetTrue() - marker;
exponent = -@as(i64, @intCast(n_after_dot));
n_digits += @as(isize, @intCast(n_after_dot));
}
// 调整所需移位量,以补偿十六进制(2^4)的尾数偏移
if (info.base == 16) {
exponent *= 4;
}
if (n_digits == 0) {
return null;
}
// 处理科学计数格式
var exp_number: i64 = 0;
if (stream.firstIsLower(&.{info.exp_char_lower})) {
stream.advance(1);
exp_number = parseScientific(stream) orelse return null;
exponent += exp_number;
}
const len = stream.offset; // length 必须为完整解析后的长度
n.* += len;
if (stream.underscore_count > 0 and !validUnderscores(stream.slice, info.base)) {
return null;
}
// 位数较少的常见情况
if (n_digits <= info.max_mantissa_digits) {
return Number(T){
.exponent = exponent,
.mantissa = mantissa,
.negative = negative,
.many_digits = false,
.hex = info.base == 16,
};
}
n_digits -= info.max_mantissa_digits;
var many_digits = false;
stream.reset(); // 从头重新解析
while (stream.firstIs("0._")) {
// '0' = '.' + 2
const next = stream.firstUnchecked();
if (next != '_') {
n_digits -= @as(isize, @intCast(next -| ('0' - 1)));
} else {
stream.underscore_count += 1;
}
stream.advance(1);
}
if (n_digits > 0) {
// 此时有效数字已超过 max_mantissa_digits,重新尝试。
many_digits = true;
mantissa = 0;
stream.reset();
tryParseNDigits(MantissaT, stream, &mantissa, info.base, info.max_mantissa_digits);
exponent = blk: {
if (mantissa >= min_n_digit_int(MantissaT, info.max_mantissa_digits)) {
// 大整数
break :blk @as(i64, @intCast(int_end)) - @as(i64, @intCast(stream.offsetTrue()));
} else {
// 下一个字节必须存在且为“.”。
// 之所以能确定,是因为此前数字超过 19 位,导致 64 位整数溢出,但仅解析整数部分时数字少于 19 位。
// 这意味着一定存在小数点,且至少有 1 位小数。
//
//
//
stream.advance(1);
const marker = stream.offsetTrue();
tryParseNDigits(MantissaT, stream, &mantissa, info.base, info.max_mantissa_digits);
break :blk @as(i64, @intCast(marker)) - @as(i64, @intCast(stream.offsetTrue()));
}
};
if (info.base == 16) {
exponent *= 4;
}
// 加回显式部分
exponent += exp_number;
}
return Number(T){
.exponent = exponent,
.mantissa = mantissa,
.negative = negative,
.many_digits = many_digits,
.hex = info.base == 16,
};
}
/// 解析不完整的非特殊浮点数。
///
/// 由有效数字和十进制指数
/// 构造该浮点数的表示。
fn parsePartialNumber(comptime T: type, s: []const u8, negative: bool, n: *usize) ?Number(T) {
std.debug.assert(s.len != 0);
const MantissaT = common.mantissaType(T);
n.* = 0;
if (s.len >= 2 and s[0] == '0' and std.ascii.toLower(s[1]) == 'x') {
var stream = FloatStream.init(s[2..]);
n.* += 2;
return parsePartialNumberBase(T, &stream, negative, n, .{
.base = 16,
.max_mantissa_digits = if (MantissaT == u64) 16 else 32,
.exp_char_lower = 'p',
});
} else {
var stream = FloatStream.init(s);
return parsePartialNumberBase(T, &stream, negative, n, .{
.base = 10,
.max_mantissa_digits = if (MantissaT == u64) 19 else 38,
.exp_char_lower = 'e',
});
}
}
pub fn parseNumber(comptime T: type, s: []const u8, negative: bool) ?Number(T) {
var consumed: usize = 0;
if (parsePartialNumber(T, s, negative, &consumed)) |number| {
// 必须消耗整个浮点数(不能有尾随数据)
if (s.len == consumed) {
return number;
}
}
return null;
}
fn parsePartialInfOrNan(comptime T: type, s: []const u8, negative: bool, n: *usize) ?T {
// inf/infinity;infxxx 应只消耗 inf。
if (std.ascii.startsWithIgnoreCase(s, "inf")) {
n.* = 3;
if (std.ascii.startsWithIgnoreCase(s[3..], "inity")) {
n.* = 8;
}
return if (!negative) std.math.inf(T) else -std.math.inf(T);
}
if (std.ascii.startsWithIgnoreCase(s, "nan")) {
n.* = 3;
return std.math.nan(T);
}
return null;
}
pub fn parseInfOrNan(comptime T: type, s: []const u8, negative: bool) ?T {
var consumed: usize = 0;
if (parsePartialInfOrNan(T, s, negative, &consumed)) |special| {
if (s.len == consumed) {
return special;
}
}
return null;
}
pub fn validUnderscores(s: []const u8, comptime base: u8) bool {
var i: usize = 0;
while (i < s.len) : (i += 1) {
if (s[i] == '_') {
// 开头或结尾处的下划线
if (i == 0 or i + 1 == s.len) {
return false;
}
// 连续下划线
if (!common.isDigit(s[i - 1], base) or !common.isDigit(s[i + 1], base)) {
return false;
}
// 下一项保证为数字,再跳过一项
i += 1;
}
}
return true;
}