Unicode module

character_utf32_to_utf8

procedure · 2 overloadsSource

Writes the UTF-8 form of ch into the buffer preallocated_string points to, and sets its count to the number of bytes used (1 to 4). The buffer must have room for them, or an assert fails; four bytes always do.

character_utf32_to_utf8 :: (ch: u32, preallocated_string: *string)

A value that is not a Unicode scalar value, which is a surrogate (U+D800 to U+DFFF) or anything above UNI_MAX_UTF16, is written as UNI_REPLACEMENT_CHAR (three bytes), so the output is always valid UTF-8.

Parameters

ch: u32
preallocated_string: *string
ExampleOpen in playground ↗
buffer: [4] u8;
encoded := string.{ 4, buffer.data };
character_utf32_to_utf8(0x20ac, *encoded);
assert(encoded == "\xe2\x82\xac");
character_utf32_to_utf8 :: (ch: u32) -> string

The UTF-8 form of ch, in a new string. Values are encoded as in the other overload.

The string is allocated with the context allocator and the caller frees it.

Parameters

ch: u32

Returns

string
ExampleOpen in playground ↗
encoded := character_utf32_to_utf8(0x1f642);
defer free(encoded);
assert(encoded.count == 4 && encoded == "\xf0\x9f\x99\x82");

See also