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. Public Function

parse

Public Function
std.fmt.parse_float.decimal.Decimal.parse
pub fn parse(s: []const u8) Self

Parse a bit integer representation of the float as a decimal.

Params

  • s: []const u8

File

fmt/parse_float/decimal.zig:240
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
        pub fn parse(s: []const u8) Self {
            var d = Self.new();
            var stream = FloatStream.init(s);

            stream.skipChars("0_");
            while (stream.scanDigit(10)) |digit| {
                d.tryAddDigit(digit);
            }

            if (stream.firstIs(".")) {
                stream.advance(1);
                const marker = stream.offsetTrue();

                // Skip leading zeroes
                if (d.num_digits == 0) {
                    stream.skipChars("0");
                }

                while (stream.hasLen(8) and d.num_digits + 8 < max_digits) {
                    const v = stream.readU64Unchecked();
                    if (!isEightDigits(v)) {
                        break;
                    }
                    std.mem.writeInt(u64, d.digits[d.num_digits..][0..8], v - 0x3030_3030_3030_3030, .little);
                    d.num_digits += 8;
                    stream.advance(8);
                }

                while (stream.scanDigit(10)) |digit| {
                    d.tryAddDigit(digit);
                }
                d.decimal_point = @as(i32, @intCast(marker)) - @as(i32, @intCast(stream.offsetTrue()));
            }
            if (d.num_digits != 0) {
                // Ignore trailing zeros if any
                var n_trailing_zeros: usize = 0;
                var i = stream.offsetTrue() - 1;
                while (true) {
                    if (s[i] == '0') {
                        n_trailing_zeros += 1;
                    } else if (s[i] != '.') {
                        break;
                    }

                    i -= 1;
                    if (i == 0) break;
                }
                d.decimal_point += @as(i32, @intCast(n_trailing_zeros));
                d.num_digits -= n_trailing_zeros;
                d.decimal_point += @as(i32, @intCast(d.num_digits));
                if (d.num_digits > max_digits) {
                    d.truncated = true;
                    d.num_digits = max_digits;
                }
            }
            if (stream.firstIsLower("e")) {
                stream.advance(1);
                var neg_exp = false;
                if (stream.firstIs("-")) {
                    neg_exp = true;
                    stream.advance(1);
                } else if (stream.firstIs("+")) {
                    stream.advance(1);
                }
                var exp_num: i32 = 0;
                while (stream.scanDigit(10)) |digit| {
                    if (exp_num < 0x10000) {
                        exp_num = 10 * exp_num + digit;
                    }
                }
                d.decimal_point += if (neg_exp) -exp_num else exp_num;
            }

            var i = d.num_digits;
            while (i < max_digits_without_overflow) : (i += 1) {
                d.digits[i] = 0;
            }

            return d;
        }