Files
std/xml/text.sakura
T
2026-10-28 00:51:45 -05:00

263 lines
8.1 KiB
Plaintext

#! @file text.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains what XML's text is made of: UTF-8, the characters XML allows, those
#! names are made of, and escaping text to write it as XML.
module std::xml;
#! The namespace the prefix `xml` is bound to, in every document.
pub const xml_namespace: strview = "http://www.w3.org/XML/1998/namespace";
#! The namespace of the attributes binding namespaces, as `xmlns:svg`.
pub const xmlns_namespace: strview = "http://www.w3.org/2000/xmlns/";
# What breaks XML's text, and where.
struct disallowed {
var kind: error_kind;
var offset: u64;
}
#! Writes text as XML, escaping what would be read as markup or changed by
#! reading: `&`, `<`, and `>` always, and a `\r`, which reading would make a
#! `\n`. Within an attribute's value, written between double quotes, `"` is
#! escaped too, and `\t` and `\n`, which reading would make spaces.
#! @param text The text, which is written as it is otherwise, so it must
#! hold only characters XML allows.
#! @param into The string to append to.
#! @param attribute Whether the text is an attribute's value.
pub def escape(text: strview, into: &mut string, attribute: bool = false): void {
var run: u64 = 0;
for at in 0..text.length {
val byte := text[at];
val written: strview = match byte {
'&' => "&amp;",
'<' => "&lt;",
'>' => "&gt;",
'\r' => "&#13;",
'"' => attribute ? "&quot;" : "",
'\t' => attribute ? "&#9;" : "",
'\n' => attribute ? "&#10;" : "",
else => "",
};
if written.length != 0 {
into.append(text.substr(run, at - run));
into.append(written);
run = at + 1;
}
}
into.append(text.substr(run, text.length - run));
}
# The first place text breaks UTF-8, or holds a character XML does not allow.
def first_disallowed(text: strview): maybe<disallowed> {
var at: u64 = 0;
while at < text.length {
val byte := text[at];
if byte >= 0x20 && byte < 0x80 {
at++;
continue;
}
if byte < 0x20 {
if byte != '\t' && byte != '\n' && byte != '\r' {
return disallowed { kind = error_kind::invalid_character, offset = at };
}
at++;
continue;
}
val count := utf8_length(text, at);
if count == 0 {
return disallowed { kind = error_kind::invalid_utf8, offset = at };
}
if !is_char(code_at(text, at)) {
return disallowed { kind = error_kind::invalid_character, offset = at };
}
at += count;
}
return nothing {};
}
# The length of the UTF-8 sequence beginning at a byte, or 0 where the bytes
# there are not one: overlong, a surrogate, past U+10FFFF, or cut short.
def utf8_length(text: strview, at: u64): u64 {
val lead := text[at];
if lead < 0x80 {
return 1;
}
# The second byte is narrowed where the lead alone would allow an overlong form,
# a surrogate, or a character past U+10FFFF.
var count: u64 = 0;
var low: u8 = 0x80;
var high: u8 = 0xBF;
if lead >= 0xC2 && lead <= 0xDF {
count = 2;
} else if lead == 0xE0 {
count = 3;
low = 0xA0;
} else if lead == 0xED {
count = 3;
high = 0x9F;
} else if lead >= 0xE1 && lead <= 0xEF {
count = 3;
} else if lead == 0xF0 {
count = 4;
low = 0x90;
} else if lead == 0xF4 {
count = 4;
high = 0x8F;
} else if lead >= 0xF1 && lead <= 0xF3 {
count = 4;
} else {
return 0;
}
if at + count > text.length {
return 0;
}
val second := text[at + 1];
if second < low || second > high {
return 0;
}
for index in 2..count {
val next := text[at + index];
if next < 0x80 || next > 0xBF {
return 0;
}
}
return count;
}
# The character whose UTF-8 begins at a byte, which must be UTF-8.
def code_at(text: strview, at: u64): u32 {
val lead := text[at] as u32;
if lead < 0x80 {
return lead;
}
if lead < 0xE0 {
return ((lead & 0x1F) << 6) | (text[at + 1] as u32 & 0x3F);
}
if lead < 0xF0 {
return ((lead & 0x0F) << 12) | ((text[at + 1] as u32 & 0x3F) << 6) | (text[at + 2] as u32 & 0x3F);
}
return ((lead & 0x07) << 18) | ((text[at + 1] as u32 & 0x3F) << 12) | ((text[at + 2] as u32 & 0x3F) << 6)
| (text[at + 3] as u32 & 0x3F);
}
# How many bytes of UTF-8 the character beginning with a byte takes.
def width_of(lead: u8): u64 {
if lead < 0x80 {
return 1;
}
if lead < 0xE0 {
return 2;
}
return lead < 0xF0 ? 3 : 4;
}
# Appends a character as UTF-8.
def append_utf8(code: u32, into: &mut string): void {
if code < 0x80 {
into.push(code as u8);
} else if code < 0x800 {
into.push((0xC0 | (code >> 6)) as u8);
into.push((0x80 | (code & 0x3F)) as u8);
} else if code < 0x10000 {
into.push((0xE0 | (code >> 12)) as u8);
into.push((0x80 | ((code >> 6) & 0x3F)) as u8);
into.push((0x80 | (code & 0x3F)) as u8);
} else {
into.push((0xF0 | (code >> 18)) as u8);
into.push((0x80 | ((code >> 12) & 0x3F)) as u8);
into.push((0x80 | ((code >> 6) & 0x3F)) as u8);
into.push((0x80 | (code & 0x3F)) as u8);
}
}
# Whether XML allows a character: `Char`, which leaves out most control
# characters, surrogates, U+FFFE, and U+FFFF.
def is_char(code: u32): bool {
if code < 0x20 {
return code == 0x09 || code == 0x0A || code == 0x0D;
}
return code <= 0xD7FF || (code >= 0xE000 && code <= 0xFFFD) || (code >= 0x10000 && code <= 0x10FFFF);
}
# Whether a byte is whitespace, `S`: a space, a tab, or a line break.
def is_space(byte: u8): bool {
return byte == ' ' || byte == '\t' || byte == '\n' || byte == '\r';
}
# Whether a character may begin a name, `NameStartChar`.
def is_name_start(code: u32): bool {
if code < 0x80 {
return (code >= 'a' && code <= 'z') || (code >= 'A' && code <= 'Z') || code == '_' || code == ':';
}
return (code >= 0xC0 && code <= 0xD6) || (code >= 0xD8 && code <= 0xF6) || (code >= 0xF8 && code <= 0x2FF)
|| (code >= 0x370 && code <= 0x37D) || (code >= 0x37F && code <= 0x1FFF) || (code >= 0x200C && code <= 0x200D)
|| (code >= 0x2070 && code <= 0x218F) || (code >= 0x2C00 && code <= 0x2FEF) || (code >= 0x3001 && code <= 0xD7FF)
|| (code >= 0xF900 && code <= 0xFDCF) || (code >= 0xFDF0 && code <= 0xFFFD) || (code >= 0x10000 && code <= 0xEFFFF);
}
# Whether a character may stand within a name, `NameChar`.
def is_name_char(code: u32): bool {
if code < 0x80 {
return (code >= 'a' && code <= 'z') || (code >= 'A' && code <= 'Z') || (code >= '0' && code <= '9')
|| code == '_' || code == ':' || code == '-' || code == '.';
}
return is_name_start(code) || code == 0xB7 || (code >= 0x300 && code <= 0x36F) || (code >= 0x203F && code <= 0x2040);
}
# Where the name beginning at a byte ends, or the byte itself where none begins
# there. The text must be UTF-8.
def name_end(text: strview, at: u64): u64 {
if at >= text.length || !is_name_start(code_at(text, at)) {
return at;
}
return name_chars_end(text, at + width_of(text[at]));
}
# Where a run of characters that may stand within a name, beginning at a byte,
# ends: a name token, `Nmtoken`, where it is not empty.
def name_chars_end(text: strview, at: u64): u64 {
var index := at;
while index < text.length {
val byte := text[index];
if byte < 0x80 {
if !is_name_char(byte as u32) {
return index;
}
index++;
continue;
}
if !is_name_char(code_at(text, index)) {
return index;
}
index += width_of(byte);
}
return index;
}
# Whether a name is one Namespaces in XML allows an element or attribute: a
# local name, or a prefix and one, each of which begins as a name does.
def is_qualified(name: strview): bool {
val colon := name.find(":") else { return true; };
if colon == 0 || colon + 1 >= name.length {
return false;
}
val local := name.substr(colon + 1, name.length - colon - 1);
return local.find(":") is none && is_name_start(code_at(local, 0));
}
# Whether a byte may stand in a public identifier, `PubidChar`.
def is_public_char(byte: u8): bool {
if byte.is_alnum {
return true;
}
return byte.matches(' ', '\r', '\n', '-', '\'', '(', ')', '+', ',', '.', '/', ':', '=', '?', ';', '!', '*', '#', '@', '$', '_', '%');
}