DoxigAlpha

printEscapedString

Function parameters

Parameters

#
slice:[]const u8
w:*Writer

Type definitions in this namespace

Types

#

Functions in this namespace

Functions

#

= 16

Values

#

Source

Implementation

#
fn printEscapedString(slice: []const u8, quotes: QuoteBehavior, w: *Writer) Writer.Error!void {
    const need_quotes = switch (quotes) {
        .always_quote => true,
        .quote_unless_valid_identifier => !isValidIdentifier(slice),
    };
    if (need_quotes) try w.writeByte('"');
    for (slice) |byte| switch (byte) {
        '\\' => try w.writeAll("\\\\"),
        ' '...'"' - 1, '"' + 1...'\\' - 1, '\\' + 1...'~' => try w.writeByte(byte),
        else => try w.print("\\{X:0>2}", .{byte}),
    };
    if (need_quotes) try w.writeByte('"');
}