core/encoding/entity
encoding_unicode_entity
Types
4Error
Error :: enum u8 {
None = 0,
Tokenizer_Is_Nil = 1,
Illegal_NUL_Character = 2,
Illegal_UTF_Encoding = 3,
Illegal_BOM = 4,
CDATA_Not_Terminated = 5,
Comment_Not_Terminated = 6,
Invalid_Entity_Encoding = 7,
}SourceTokenizer
Tokenizer :: struct {
r: rune,
w: int,
src: string,
offset: int,
read_offset: int,
}SourceXML_Decode_Option
XML_Decode_Option :: enum u8 {
// Do not decode & entities. It decodes by default. If given, overrides `Decode_CDATA`.
No_Entity_Decode = 0,
// CDATA is unboxed.
Unbox_CDATA = 1,
// Unboxed CDATA is decoded as well. Ignored if `.Unbox_CDATA` is not given.
Decode_CDATA = 2,
// Comments are stripped.
Comment_Strip = 3,
// Normalize whitespace
Normalize_Whitespace = 4,
}SourceDefault: CDATA and comments are passed through unchanged.
XML_Decode_Options
XML_Decode_Options :: bit_set[XML_Decode_Option; u8]SourceConstants
7CDATA_END
CDATA_END :: "]]>"SourceCDATA_START
CDATA_START :: "<![CDATA["SourceCOMMENT_END
COMMENT_END :: "-->"SourceCOMMENT_START
COMMENT_START :: "<!--"SourceMAX_RUNE_CODEPOINT
MAX_RUNE_CODEPOINT :: int = int(unicode.MAX_RUNE)SourceXML_NAME_TO_RUNE_MAX_LENGTH
XML_NAME_TO_RUNE_MAX_LENGTH :: 31Source∳
XML_NAME_TO_RUNE_MIN_LENGTH
XML_NAME_TO_RUNE_MIN_LENGTH :: 2Source------ GENERATED ------ DO NOT EDIT ------ GENERATED ------ DO NOT EDIT ------ GENERATED ------ This file is generated from "https://github.com/w3c/xml-entities/blob/gh-pages/unicode.xml".
UPDATE:
- Ensure the XML file was downloaded using "tests\core\download_assets.py", given the path to the "tests\assets" directory.
- Run "core/unicode/tools/generate_entity_table.odin"
Odin unicode generated tables: https://github.com/odin-lang/Odin/tree/master/core/encoding/entity
Copyright David Carlisle 1999-2025
Use and distribution of this code are permitted under the terms of the
W3C Software Notice and License.
http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html
This file is a collection of information about how to map
Unicode entities to LaTeX, and various SGML/XML entity
sets (ISO and MathML/HTML). A Unicode character may be mapped
to several entities.
Originally designed by Sebastian Rahtz in conjunction with
Barbara Beeton for the STIX project
See also: LICENSE_table.md
`<`Procedures
9advance
advance :: proc(t: ^Tokenizer) -> (err: Error)Sourcedecode_xml
decode_xml :: proc(input: string, options: XML_Decode_Options = XML_Decode_Options{}, allocator: mem.Allocator = context.allocator) -> (decoded: string, err: Error)SourceDecode a string that may include SGML/XML/HTML entities. The caller has to free the result.
escape_html
escape_html :: proc(s: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (output: string, was_allocation: bool)Sourceescape_html escapes special characters like '&' to become '&'. It escapes only 5 different characters: & ' < > and "
named_xml_entity_to_rune
named_xml_entity_to_rune :: proc(name: string) -> (decoded: [2]rune, rune_count: int, ok: bool)SourceInput:
entity_name - a string, like "copy" that describes a user-encoded Unicode entity as used in XML.
Returns:
"decoded" - The decoded runes if found by name, or all zero otherwise.
"rune_count" - The number of decoded runes
"ok" - true if found, false if not.
IMPORTANT: XML processors (including browsers) treat these names as case-sensitive. So do we.unescape_entity
unescape_entity :: proc(s: string) -> (b: [8]u8, w: int, j: int)SourceReturns an unescaped string of an encoded XML/HTML entity.
unescape_html
unescape_html :: proc(s: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (output: string, was_allocation: bool, err: runtime.Allocator_Error)Sourcewrite_rune
write_rune :: proc(b: ^Builder, r: rune) -> (res: int, err: io.Error)Sourcewrite_string
write_string :: proc(b: ^Builder, s: string, loc = #caller_location) -> (n: int)Sourcexml_decode_entity
xml_decode_entity :: proc(entity: string) -> (decoded: [2]rune, rune_count: int, ok: bool)Source