Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Private Function

parsePartialNumberBase

Private Function
std.fmt.parse_float.parse.parsePartialNumberBase
fn parsePartialNumberBase(comptime T: type, stream: *FloatStream, negative: bool, n: *usize, comptime info: ParseInfo) ?Number(T)

Params

File

fmt/parse_float/parse.zig:102
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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);

    // parse initial digits before dot
    var mantissa: MantissaT = 0;
    tryParseDigits(MantissaT, stream, &mantissa, info.base);
    const int_end = stream.offsetTrue();
    var n_digits = @as(isize, @intCast(stream.offsetTrue()));

    // handle dot with the following digits
    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));
    }

    // adjust required shift to offset mantissa for base-16 (2^4)
    if (info.base == 16) {
        exponent *= 4;
    }

    if (n_digits == 0) {
        return null;
    }

    // handle scientific format
    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 must be complete parsed length
    n.* += len;

    if (stream.underscore_count > 0 and !validUnderscores(stream.slice, info.base)) {
        return null;
    }

    // common case with not many digits
    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(); // re-parse from beginning
    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) {
        // at this point we have more than max_mantissa_digits significant digits, let's try again
        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)) {
                // big int
                break :blk @as(i64, @intCast(int_end)) - @as(i64, @intCast(stream.offsetTrue()));
            } else {
                // the next byte must be present and be '.'
                // We know this is true because we had more than 19
                // digits previously, so we overflowed a 64-bit integer,
                // but parsing only the integral digits produced less
                // than 19 digits. That means we must have a decimal
                // point, and at least 1 fractional digit.
                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;
        }
        // add back the explicit part
        exponent += exp_number;
    }

    return Number(T){
        .exponent = exponent,
        .mantissa = mantissa,
        .negative = negative,
        .many_digits = many_digits,
        .hex = info.base == 16,
    };
}