DoxigAlpha

parse

Function parameters

Parameters

#
bytes:[]const u8

Type definitions in this namespace

Types

#
FormatOptions
Deprecated in favor of `Options`.
Parser
A stream based parser for format strings.

Deprecated in favor of `Writer.print`.

Functions

#
format
Deprecated in favor of `Writer.print`.
printInt
Asserts the rendered integer value fits in `buffer`.
digits2
Converts values in the range [0, 100) to a base 10 string.
Formatter
Deprecated in favor of `Alt`.
Alt
Creates a type suitable for instantiating and passing to a "{f}" placeholder.
alt
Helper for calling alternate format methods besides one named "format".
parseInt
Parses the string `buf` as signed or unsigned representation in the
parseIntWithGenericCharacter
Like `parseInt`, but with a generic `Character` type.
parseUnsigned
Parses the string `buf` as unsigned representation in the specified base
parseIntSizeSuffix
Parses a number like '2G', '2Gi', or '2GiB'.
bufPrint
Print a Formatter string into `buf`.
count
Count the characters needed for format.
bytesToHex
Encodes a sequence of bytes as hexadecimal digits.
hexToBytes
Decodes the sequence of bytes represented by the specified string of
hex
Converts an unsigned integer of any multiple of u8 to an array of lowercase

Error sets in this namespace

Error Sets

#

= 3

Values

#
hex_charset
= "0123456789abcdef"

Source

Implementation

#
pub fn parse(comptime bytes: []const u8) Placeholder {
    var parser: Parser = .{ .bytes = bytes, .i = 0 };
    const arg = parser.specifier() catch |err| @compileError(@errorName(err));
    const specifier_arg = parser.until(':');
    if (parser.char()) |b| {
        if (b != ':') @compileError("expected : or }, found '" ++ &[1]u8{b} ++ "'");
    }

    // Parse the fill byte, if present.
    //
    // When the width field is also specified, the fill byte must
    // be followed by an alignment specifier, unless it's '0' (zero)
    // (in which case it's handled as part of the width specifier).
    var fill: ?u8 = if (parser.peek(1)) |b|
        switch (b) {
            '<', '^', '>' => parser.char(),
            else => null,
        }
    else
        null;

    // Parse the alignment parameter
    const alignment: ?Alignment = if (parser.peek(0)) |b| init: {
        switch (b) {
            '<', '^', '>' => {
                // consume the character
                break :init switch (parser.char().?) {
                    '<' => .left,
                    '^' => .center,
                    else => .right,
                };
            },
            else => break :init null,
        }
    } else null;

    // When none of the fill character and the alignment specifier have
    // been provided, check whether the width starts with a zero.
    if (fill == null and alignment == null) {
        fill = if (parser.peek(0) == '0') '0' else null;
    }

    // Parse the width parameter
    const width = parser.specifier() catch |err| @compileError(@errorName(err));

    // Skip the dot, if present
    if (parser.char()) |b| {
        if (b != '.') @compileError("expected . or }, found '" ++ &[1]u8{b} ++ "'");
    }

    // Parse the precision parameter
    const precision = parser.specifier() catch |err| @compileError(@errorName(err));

    if (parser.char()) |b| @compileError("extraneous trailing character '" ++ &[1]u8{b} ++ "'");

    const specifier_array = specifier_arg[0..specifier_arg.len].*;

    return .{
        .specifier_arg = &specifier_array,
        .fill = fill orelse default_fill_char,
        .alignment = alignment orelse default_alignment,
        .arg = arg,
        .width = width,
        .precision = precision,
    };
}