Function stringEscape [src]

Print the string as escaped contents of a double quoted or single-quoted string. Format {} treats contents as a double-quoted string. Format {'} treats contents as a single-quoted string.

Prototype

pub fn stringEscape( bytes: []const u8, comptime f: []const u8, options: std.fmt.FormatOptions, writer: anytype, ) !void

Parameters

bytes: []const u8f: []const u8options: std.fmt.FormatOptions

Source

pub fn stringEscape( bytes: []const u8, comptime f: []const u8, options: std.fmt.FormatOptions, writer: anytype, ) !void { _ = options; for (bytes) |byte| switch (byte) { '\n' => try writer.writeAll("\\n"), '\r' => try writer.writeAll("\\r"), '\t' => try writer.writeAll("\\t"), '\\' => try writer.writeAll("\\\\"), '"' => { if (f.len == 1 and f[0] == '\'') { try writer.writeByte('"'); } else if (f.len == 0) { try writer.writeAll("\\\""); } else { @compileError("expected {} or {'}, found {" ++ f ++ "}"); } }, '\'' => { if (f.len == 1 and f[0] == '\'') { try writer.writeAll("\\'"); } else if (f.len == 0) { try writer.writeByte('\''); } else { @compileError("expected {} or {'}, found {" ++ f ++ "}"); } }, ' ', '!', '#'...'&', '('...'[', ']'...'~' => try writer.writeByte(byte), // Use hex escapes for rest any unprintable characters. else => { try writer.writeAll("\\x"); try std.fmt.formatInt(byte, 16, .lower, .{ .width = 2, .fill = '0' }, writer); }, }; }