Function encodeJsonStringChars [src]

Alias for std.json.stringify.encodeJsonStringChars

Write chars to writer as JSON encoded string characters.

Prototype

pub fn encodeJsonStringChars(chars: []const u8, options: StringifyOptions, writer: anytype) !void

Parameters

chars: []const u8options: StringifyOptions

Source

pub fn encodeJsonStringChars(chars: []const u8, options: StringifyOptions, writer: anytype) !void { var write_cursor: usize = 0; var i: usize = 0; if (options.escape_unicode) { while (i < chars.len) : (i += 1) { switch (chars[i]) { // normal ascii character 0x20...0x21, 0x23...0x5B, 0x5D...0x7E => {}, 0x00...0x1F, '\\', '\"' => { // Always must escape these. try writer.writeAll(chars[write_cursor..i]); try outputSpecialEscape(chars[i], writer); write_cursor = i + 1; }, 0x7F...0xFF => { try writer.writeAll(chars[write_cursor..i]); const ulen = std.unicode.utf8ByteSequenceLength(chars[i]) catch unreachable; const codepoint = std.unicode.utf8Decode(chars[i..][0..ulen]) catch unreachable; try outputUnicodeEscape(codepoint, writer); i += ulen - 1; write_cursor = i + 1; }, } } } else { while (i < chars.len) : (i += 1) { switch (chars[i]) { // normal bytes 0x20...0x21, 0x23...0x5B, 0x5D...0xFF => {}, 0x00...0x1F, '\\', '\"' => { // Always must escape these. try writer.writeAll(chars[write_cursor..i]); try outputSpecialEscape(chars[i], writer); write_cursor = i + 1; }, } } } try writer.writeAll(chars[write_cursor..chars.len]); }