package core:encoding/entity
Overview
Encode and decode runes to/from a Unicode &entity;.
This code has several procedures to map unicode runes to/from different textual encodings.
-
SGML/XML/HTML entity
-
&#<decimal>;
-
&#x<hexadecimal>;
-
&<entity name>; (If the lookup tables are compiled in). Reference: https://www.w3.org/2003/entities/2007xml/unicode.xml
-
URL encode / decode %hex entity Reference: https://datatracker.ietf.org/doc/html/rfc3986/#section-2.1
Index
Variables (0)
This section is empty.
Procedure Groups (0)
This section is empty.
Types
Error ¶
Error :: enum u8 { None = 0, Tokenizer_Is_Nil, Illegal_NUL_Character, Illegal_UTF_Encoding, CDATA_Not_Terminated, Comment_Not_Terminated, Invalid_Entity_Encoding, Unknown_Entity, Buffer_Too_Small, }
Related Procedures With Returns
Percent_Error ¶
Percent_Error :: union { runtime.Allocator_Error, Error, }
XML_Decode_Option ¶
XML_Decode_Option :: enum u8 { // Keep comments in the output. Keep_Comments, // Turn each literal tab, LF and CR into a space, as XML does for CDATA attribute values (§3.3.3). Normalize_Attribute, // Like `Normalize_Attribute`, then collapse runs of spaces and trim, as XML does for other attribute types. Collapse_Whitespace, // Also decode the HTML, MathML and ISO 8879 names from `unicode.xml`. HTML_Entities, // Keep unknown names as they are, instead of returning `.Unknown_Entity`. Keep_Unknown_Entities, }
By default decode_xml decodes the 5 predefined entities and character references, unboxes CDATA without decoding it,
drops comments, and turns CRLF and lone CR into LF.
XML_Decode_Options ¶
XML_Decode_Options :: bit_set[XML_Decode_Option; u8]
Related Procedures With Parameters
Constants
CDATA_END ¶
CDATA_END :: "]]>"
CDATA_START ¶
CDATA_START :: "<![CDATA["
COMMENT_END ¶
COMMENT_END :: "-->"
COMMENT_START ¶
COMMENT_START :: "<!--"
MAX_RUNE_CODEPOINT ¶
MAX_RUNE_CODEPOINT :: int(unicode.MAX_RUNE)
Variables
This section is empty.
Procedures
decode_xml ¶
decode_xml :: proc( input: string, options: bit_set[XML_Decode_Option; u8] = XML_Decode_Options{}, allocator := context.allocator, ) -> (decoded: string, err: Error) {…}
Decode a string that may include SGML/XML/HTML entities. The caller has to free the result.
decode_xml_sb ¶
decode_xml_sb :: proc( builder: ^strings.Builder, input: string, options: bit_set[XML_Decode_Option; u8] = XML_Decode_Options{}, allocator := context.allocator, ) -> (decoded: string, err: Error) {…}
Decode a string that may include SGML/XML/HTML entities into an existing builder.
The returned decoded string is a reference into the builder's buffer, and therefore does not need to be freed individually.
escape_html ¶
@(require_results) escape_html :: proc( s: string, allocator := context.allocator, loc := #caller_location, ) -> (output: string, was_allocation: bool) {…}
escape_html escapes special characters like '&' to become '&'. It escapes only 5 different characters: & ' < > and "
named_xml_entity_to_rune ¶
Input:
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.
percent_decode ¶
@(require_results) percent_decode :: proc( s: string, allocator := context.allocator, loc := #caller_location, ) -> (output: string, was_allocation: bool, err: Percent_Error) {…}
Unlike net.percent_decode, %% is .Invalid_Entity_Encoding instead of %, allocation failures are returned,
and s itself is returned, without allocating, when it holds no %.
percent_encode ¶
@(require_results) percent_encode :: proc( s: string, allocator := context.allocator, loc := #caller_location, ) -> (output: string, was_allocation: bool, err: runtime.Allocator_Error) {…}
Unlike net.percent_encode, this encodes bytes rather than runes, so invalid UTF-8 survives a round trip
instead of turning into %EF%BF%BD, and it returns s itself, without allocating, when nothing needs escaping.
unescape_entity ¶
Returns an unescaped string of an encoded XML/HTML entity.
unescape_html ¶
@(require_results) unescape_html :: proc( s: string, allocator := context.allocator, loc := #caller_location, ) -> (output: string, was_allocation: bool, err: runtime.Allocator_Error) {…}
write_rune ¶
write_rune :: strings.write_rune
Appends a single rune r as its UTF-8 encoded byte sequence to the Builder b, and returns the number of bytes written.
Inputs:
b: A pointer to the Builder.r: The rune to be appended.
Returns:
n: The number of bytes written.err: An allocator error if one occured,nilotherwise.
Remarks:
The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.
The rune is encoded as-is. It is not escaped or quoted.
If the rune cannot be encoded as a valid Unicode codepoint, this will append a Replacement Character (\ufffd) instead.
Example:
import "core:fmt"
import "core:strings"
write_rune_example :: proc() {
builder := strings.builder_make()
strings.write_rune(&builder, 'ä') // 2
strings.write_rune(&builder, 'b') // 1
fmt.println(strings.to_string(builder)) // -> äb
}
äb
write_string ¶
write_string :: strings.write_string
Appends string s to the Builder b, and returns the number of bytes written.
Inputs:
b: A pointer to the Builder.s: The string to be appended.
Returns:
n: The number of bytes written.err: An allocator error if one occured,nilotherwise.
Remarks:
The backing dynamic array may be fixed in capacity or fail to resize; n states the number actually written.
Example:
import "core:fmt"
import "core:strings"
write_string_example :: proc() {
builder := strings.builder_make()
strings.write_string(&builder, "a") // 1
strings.write_string(&builder, "bc") // 2
fmt.println(strings.to_string(builder)) // -> abc
}
abc
Procedure Groups
This section is empty.
Source Files
Generation Information
Generated with odin version dev-2026-11 (vendor "odin") Windows_amd64 @ 2026-11-20 21:05:09.319432300 +0000 UTC