Files
std/json/text.sakura
T
2026-12-06 13:12:06 -06:00

195 lines
5.2 KiB
Plaintext

#! @file text.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains what JSON's text is made of: UTF-8, the escapes of strings, and the
#! grammar of numbers and what they read as, which the reader, the document, and
#! the writer share.
module std::json;
# The digits a `\u` escape is written with.
const hex_digits: strview = "0123456789abcdef";
#! Writes text with JSON's escapes, as it stands between the quotes of a string:
#! `"`, `\`, and the control characters escaped, and everything else as it is.
#! @param text The text to escape.
#! @param into The string to append to.
#! @param lossy Whether each byte that breaks UTF-8 is written as the escape of
#! U+FFFD, `\ufffd`, so the whole text is written, rather than the
#! text being cut short at the first.
#! @returns Whether the text is UTF-8, which JSON's text must be. Text that is
#! not is appended only as far as its first byte that breaks UTF-8,
#! unless the escape is lossy.
pub def escape(text: strview, into: &mut string, lossy: bool = false): bool {
var run: u64 = 0; # Where the bytes written as they are begin.
var at: u64 = 0;
var whole: bool = true;
while at < text.length {
val byte := text[at];
if byte >= 0x80 {
val count := serial::utf8_length(text, at);
if count != 0 {
at += count;
continue;
}
into.append(text.substr(run, at - run));
if !lossy {
return false;
}
into.append("\\ufffd");
whole = false;
at++;
run = at;
continue;
}
if byte >= 0x20 && byte != '"' && byte != '\\' {
at++;
continue;
}
into.append(text.substr(run, at - run));
escape_byte(byte, into);
at++;
run = at;
}
into.append(text.substr(run, at - run));
return whole;
}
#! Decodes the escapes of a string's text as JSON writes them, where the text is
#! as it stands between the quotes, as a reader's token views it.
#! @param raw The text, whose escapes the reader has checked.
#! @param into The string to append the decoded text to.
pub def unescape(raw: strview, into: &mut string): void {
var run: u64 = 0;
var at: u64 = 0;
while at < raw.length {
if raw[at] != '\\' {
at++;
continue;
}
into.append(raw.substr(run, at - run));
val marker := raw[at + 1];
at += 2;
run = at;
if marker != 'u' {
into.push(unescaped(marker));
continue;
}
# A surrogate pair is two escapes, which are one character together.
var code := hex_value(raw, at);
at += 4;
if code >= 0xD800 && code <= 0xDBFF {
code = 0x10000 + ((code - 0xD800) << 10) + (hex_value(raw, at + 2) - 0xDC00);
at += 6;
}
serial::append_utf8(code, into);
run = at;
}
into.append(raw.substr(run, at - run));
}
#! Whether text is UTF-8: no overlong forms, no surrogates, nothing past
#! U+10FFFF, and nothing cut short.
#! @param text The text to check.
#! @returns Whether it is UTF-8.
pub def is_utf8(text: strview): bool {
return serial::is_utf8(text);
}
# Where the number beginning at `at` ends, following JSON's grammar,
# `-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?`, or `at` where none begins there
# or it breaks the grammar.
def number_end(text: strview, at: u64): u64 {
var index := at;
if index < text.length && text[index] == '-' {
index++;
}
if index >= text.length {
return at;
}
if text[index] == '0' {
index++;
} else if text[index] >= '1' && text[index] <= '9' {
index = digits_end(text, index);
} else {
return at;
}
if index < text.length && text[index] == '.' {
val digits := index + 1;
index = digits_end(text, digits);
if index == digits {
return at;
}
}
if index < text.length && (text[index] == 'e' || text[index] == 'E') {
index++;
if index < text.length && (text[index] == '+' || text[index] == '-') {
index++;
}
val digits := index;
index = digits_end(text, digits);
if index == digits {
return at;
}
}
return index;
}
# Where a run of decimal digits beginning at `at` ends.
def digits_end(text: strview, at: u64): u64 {
var index := at;
while index < text.length && text[index].is_digit {
index++;
}
return index;
}
# Appends the escape JSON writes for a byte it does not write as it is.
def escape_byte(byte: u8, into: &mut string): void {
into.push('\\');
match byte {
'"' => into.push('"');
'\\' => into.push('\\');
'\n' => into.push('n');
'\r' => into.push('r');
'\t' => into.push('t');
0x08 => into.push('b');
0x0C => into.push('f');
else => {
into.append("u00");
into.push(hex_digits[(byte >> 4) as u64]);
into.push(hex_digits[(byte & 0x0F) as u64]);
}
}
}
# The byte a one-letter escape stands for, as `n` for a line break.
def unescaped(marker: u8): u8 {
return match marker {
'b' => 0x08,
'f' => 0x0C,
'n' => '\n',
'r' => '\r',
't' => '\t',
else => marker,
};
}
# The number four hexadecimal digits beginning at `at` write.
def hex_value(text: strview, at: u64): u64 {
var made: u64 = 0;
for index in 0u..4u {
made = made * 16 + text[at + index].digit_value;
}
return made;
}