package core:encoding/entity

⌘K
Ctrl+K
or
/

    Overview

    Encode and decode runes to/from a Unicode &entity;.

    This code has several procedures to map unicode runes to/from different textual encodings.

    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

    Tokenizer ¶

    Tokenizer :: struct {
    	r:           rune,
    	w:           int,
    	src:         string,
    	offset:      int,
    	read_offset: int,
    }

    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_START ¶

    CDATA_START :: "<![CDATA["

    MAX_RUNE_CODEPOINT ¶

    MAX_RUNE_CODEPOINT :: int(unicode.MAX_RUNE)

    XML_NAME_TO_RUNE_MAX_LENGTH ¶

    XML_NAME_TO_RUNE_MAX_LENGTH :: 31
     

    &CounterClockwiseContourIntegral;

    XML_NAME_TO_RUNE_MIN_LENGTH ¶

    XML_NAME_TO_RUNE_MIN_LENGTH :: 2
     

    &lt;

    Variables

    This section is empty.

    Procedures

    advance ¶

    advance :: proc(t: ^Tokenizer) -> (err: Error) {…}

    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 ¶

    named_xml_entity_to_rune :: proc(name: string) -> (decoded: [2]rune, rune_count: int, ok: bool) {…}
     

    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 ¶

    @(require_results)
    unescape_entity :: proc(s: string) -> (b: [8]u8, w: int, j: int) {…}
     

    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, nil otherwise

    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
    }
    
    Output:
    ä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
    }
    
    Output:
    abc
    

    xml_decode_entity ¶

    xml_decode_entity :: proc(entity: string) -> (decoded: [2]rune, rune_count: int, ok: bool) {…}

    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