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
Types (4)
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, Illegal_BOM, CDATA_Not_Terminated, Comment_Not_Terminated, Invalid_Entity_Encoding, }
Related Procedures With Returns
XML_Decode_Option ¶
XML_Decode_Option :: enum u8 { // Do not decode & entities. It decodes by default. If given, overrides `Decode_CDATA`. No_Entity_Decode, // CDATA is unboxed. Unbox_CDATA, // Unboxed CDATA is decoded as well. Ignored if `.Unbox_CDATA` is not given. Decode_CDATA, // Comments are stripped. Comment_Strip, // Normalize whitespace Normalize_Whitespace, }
Default: CDATA and comments are passed through unchanged.
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.
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 to the Builder and returns the number of bytes written and an io.Error
Inputs:
- b A pointer to the Builder
- r The rune to be appended
Returns:
- res The number of bytes written
- err An io.Error if one occured,
nilotherwise
NOTE: 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_rune_example :: proc() {
builder := strings.builder_make()
strings.write_rune(&builder, 'ä') // 2 None
strings.write_rune(&builder, 'b') // 1 None
fmt.println(strings.to_string(builder)) // -> äb
}
äb
write_string ¶
write_string :: strings.write_string
Appends a string to the Builder 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
NOTE: 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-10 (vendor "odin") Windows_amd64 @ 2026-10-14 00:04:22.980089500 +0000 UTC