unionstd.zig.string_literal.Error[src]

Fields

invalid_escape_character: usize

The character after backslash is missing or not recognized.

expected_hex_digit: usize

Expected hex digit at this index.

empty_unicode_escape_sequence: usize

Unicode escape sequence had no digits with rbrace at this index.

expected_hex_digit_or_rbrace: usize

Expected hex digit or '}' at this index.

invalid_unicode_codepoint: usize

Invalid unicode codepoint at this index.

expected_lbrace: usize

Expected '{' at this index.

expected_rbrace: usize

Expected '}' at this index.

expected_single_quote: usize

Expected ''' at this index.

invalid_character: usize

The character at this index cannot be represented without an escape sequence.

empty_char_literal

''. Not returned for string literals.

Functions

Functionfmt[src]

pub fn fmt(self: @This(), raw_string: []const u8) std.fmt.Formatter(formatMessage)

Parameters

self: @This()
raw_string: []const u8

Source Code

Source code
pub fn fmt(self: @This(), raw_string: []const u8) std.fmt.Formatter(formatMessage) {
    return .{ .data = .{
        .err = self,
        .raw_string = raw_string,
    } };
}

Functionoffset[src]

pub fn offset(err: Error) usize

Parameters

err: Error

Source Code

Source code
pub fn offset(err: Error) usize {
    return switch (err) {
        inline .invalid_escape_character,
        .expected_hex_digit,
        .empty_unicode_escape_sequence,
        .expected_hex_digit_or_rbrace,
        .invalid_unicode_codepoint,
        .expected_lbrace,
        .expected_rbrace,
        .expected_single_quote,
        .invalid_character,
        => |n| n,
        .empty_char_literal => 0,
    };
}

Source Code

Source code
pub const Error = union(enum) {
    /// The character after backslash is missing or not recognized.
    invalid_escape_character: usize,
    /// Expected hex digit at this index.
    expected_hex_digit: usize,
    /// Unicode escape sequence had no digits with rbrace at this index.
    empty_unicode_escape_sequence: usize,
    /// Expected hex digit or '}' at this index.
    expected_hex_digit_or_rbrace: usize,
    /// Invalid unicode codepoint at this index.
    invalid_unicode_codepoint: usize,
    /// Expected '{' at this index.
    expected_lbrace: usize,
    /// Expected '}' at this index.
    expected_rbrace: usize,
    /// Expected '\'' at this index.
    expected_single_quote: usize,
    /// The character at this index cannot be represented without an escape sequence.
    invalid_character: usize,
    /// `''`. Not returned for string literals.
    empty_char_literal,

    const FormatMessage = struct {
        err: Error,
        raw_string: []const u8,
    };

    fn formatMessage(
        self: FormatMessage,
        comptime f: []const u8,
        options: std.fmt.FormatOptions,
        writer: anytype,
    ) !void {
        _ = f;
        _ = options;
        switch (self.err) {
            .invalid_escape_character => |bad_index| try writer.print(
                "invalid escape character: '{c}'",
                .{self.raw_string[bad_index]},
            ),
            .expected_hex_digit => |bad_index| try writer.print(
                "expected hex digit, found '{c}'",
                .{self.raw_string[bad_index]},
            ),
            .empty_unicode_escape_sequence => try writer.writeAll(
                "empty unicode escape sequence",
            ),
            .expected_hex_digit_or_rbrace => |bad_index| try writer.print(
                "expected hex digit or '}}', found '{c}'",
                .{self.raw_string[bad_index]},
            ),
            .invalid_unicode_codepoint => try writer.writeAll(
                "unicode escape does not correspond to a valid unicode scalar value",
            ),
            .expected_lbrace => |bad_index| try writer.print(
                "expected '{{', found '{c}'",
                .{self.raw_string[bad_index]},
            ),
            .expected_rbrace => |bad_index| try writer.print(
                "expected '}}', found '{c}'",
                .{self.raw_string[bad_index]},
            ),
            .expected_single_quote => |bad_index| try writer.print(
                "expected single quote ('), found '{c}'",
                .{self.raw_string[bad_index]},
            ),
            .invalid_character => |bad_index| try writer.print(
                "invalid byte in string or character literal: '{c}'",
                .{self.raw_string[bad_index]},
            ),
            .empty_char_literal => try writer.writeAll(
                "empty character literal",
            ),
        }
    }

    pub fn fmt(self: @This(), raw_string: []const u8) std.fmt.Formatter(formatMessage) {
        return .{ .data = .{
            .err = self,
            .raw_string = raw_string,
        } };
    }

    pub fn offset(err: Error) usize {
        return switch (err) {
            inline .invalid_escape_character,
            .expected_hex_digit,
            .empty_unicode_escape_sequence,
            .expected_hex_digit_or_rbrace,
            .invalid_unicode_codepoint,
            .expected_lbrace,
            .expected_rbrace,
            .expected_single_quote,
            .invalid_character,
            => |n| n,
            .empty_char_literal => 0,
        };
    }
}