std/utf

March 17, 2026 · View on GitHub

UTF-8 and UTF-16 encoding conversion and manipulation.

UTF-8 Functions

std::utf8::utflen

usize utflen(uchar* str)

Returns number of UTF-8 characters (not bytes).

std::utf8::charlen

usize charlen(uchar ch)
usize charlen(uchar* ch, usize idx)

Returns byte length of UTF-8 character.

std::utf8::isStart

bool isStart(char c)

Checks if byte is start of UTF-8 character.

std::utf8::next

usize next(uchar* str)

Returns index of next UTF-8 character.

std::utf8::toCodepoint

uint toCodepoint(uchar* str, usize at)

Converts UTF-8 character at position to Unicode codepoint.

std::utf8::toUTF16Buffer

usize toUTF16Buffer(char* u8, usize u8Length, ushort* buffer, usize bufferSize)
usize toUTF16Buffer(char* u8, ushort* buffer, usize bufferSize)

Converts UTF-8 to UTF-16. Returns required buffer size.

std::utf8::toUTF16

ushort* toUTF16(char* u8, usize u8Length)
ushort* toUTF16(char* u8)

Converts UTF-8 string to UTF-16 (allocates memory).

UTF-16 Functions

std::utf16::strlen

int strlen(ushort* wstr)

Returns length of UTF-16 string in code units.

std::utf16::utflen

int utflen(ushort* wstr)

Returns number of Unicode characters (handles surrogates).

std::utf16::toUTF8Buffer

usize toUTF8Buffer(ushort* u16, usize u16Length, char* buffer, usize bufferSize)
usize toUTF8Buffer(ushort* u16, char* buffer, usize bufferSize)

Converts UTF-16 to UTF-8. Returns required buffer size.

std::utf16::toUTF8

char* toUTF8(ushort* u16, usize u16Length)
char* toUTF8(ushort* u16)

Converts UTF-16 string to UTF-8 (allocates memory).

Constants

  • std::utf16::bmpEnd - End of Basic Multilingual Plane (0xFFFF)

Example

import <std/utf>

char* utf8Str = "Hello 世界";
usize charCount = std::utf8::utflen(utf8Str);

ushort* utf16 = std::utf8::toUTF16(utf8Str);
char* backToUtf8 = std::utf16::toUTF8(utf16);