dreamworkAPI
Ctrl K
GitHub
libraryDocs / unicode

unicode

librarysharedmenusource

The unicode library provides functions for working with unicode code points, unicode escape sequences, case mapping, display width and blocks of UTF-8 strings.

Fields

NameTypeDescription
MAXintegerThe largest valid unicode code point, U+10FFFF. = 0x10FFFF
REPLACEMENTintegerThe replacement character code point, U+FFFD. = 0xFFFD
BOMintegerThe byte order mark code point, U+FEFF. = 0xFEFF

Functions

unicode.blockReturns the name and the range of the unicode block that contains the given code point.
unicode.blocksReturns an iterator over all unicode blocks in code point order.
unicode.capitalizeCapitalizes a UTF-8 string: the first case-able character within the given range is converted to uppercase (left untouc…
unicode.displayWidthReturns the number of terminal columns the given UTF-8 string takes in a monospace font.
unicode.escapeConverts each character in the input string into a unicode escape sequence, suitable for serialization, logging, or saf…
unicode.fromSurrogatesCombines a UTF-16 surrogate pair into a supplementary code point.
unicode.isHighSurrogateChecks if the given code point is a UTF-16 high (leading) surrogate, in range U+D800 - U+DBFF.
unicode.isLowSurrogateChecks if the given code point is a UTF-16 low (trailing) surrogate, in range U+DC00 - U+DFFF.
unicode.isNoncharacterChecks if the given code point is a noncharacter, U+FDD0 - U+FDEF or the last two code points of any plane (U+xFFFE, U+…
unicode.isPrivateUseChecks if the given code point belongs to a private use area, U+E000 - U+F8FF, U+F0000 - U+FFFFD or U+100000 - U+10FFFD.
unicode.isSurrogateChecks if the given code point is a UTF-16 surrogate, in range U+D800 - U+DFFF.
unicode.isValidChecks if the given value is a unicode scalar value, an integer in range U+0000 - U+10FFFF that is not a surrogate.
unicode.lowerConverts a UTF-8 string to lowercase within the given byte range.
unicode.padPads a UTF-8 string with a repeated padding string until it is desired_width columns wide (see unicode.displayWidth), o…
unicode.planeReturns the unicode plane of the given code point, 0 for the Basic Multilingual Plane up to 16.
unicode.tagReturns a string representation of the given unicode code point.
unicode.titleConverts a UTF-8 string to title case within the given range: the first case-able character of each word is converted t…
unicode.toSurrogatesSplits a supplementary code point (U+10000 - U+10FFFF) into a UTF-16 surrogate pair.
unicode.unescapeConverts unicode escape sequences into actual characters.
unicode.untagReturns the unicode code point represented by the given string.
unicode.upperConverts a UTF-8 string to uppercase within the given byte range.
unicode.widthReturns the number of terminal columns the given code point takes in a monospace font.
unicode.wrapWraps a UTF-8 string so that no line is wider than max_width columns (see unicode.displayWidth), breaking on the last A…

#unicode.block

sharedmenunodiscardsource
function unicode.block(utf8_codepoint: integer): string | nil, integer | nil, integer | nil

Returns the name and the range of the unicode block that contains the given code point.

Some ranges are not assigned to any block, nil is returned for them.

Parameters
NameTypeDescription
utf8_codepointintegerThe code point.
Returns
TypeNameDescription
string | nilblock_nameThe name of the block, e.g. "Basic Latin" or "CJK Unified Ideographs".
integer | nilfirst_codepointThe first code point of the block.
integer | nillast_codepointThe last code point of the block.

#unicode.blocks

sharedmenunodiscardsource
function unicode.blocks(): (fun(): string), integer, integer

Returns an iterator over all unicode blocks in code point order.

lua
for block_name, first_codepoint, last_codepoint in unicode.blocks() do
    print( block_name, unicode.tag( first_codepoint ), unicode.tag( last_codepoint ) )
end
Returns
TypeNameDescription
fun(): string
integer
integeriteratorThe iterator function, returns the name, the first and the last code point of each block.

#unicode.capitalize

sharedmenunodiscardsource
function unicode.capitalize(utf8_string: string, start_position?: integer, end_position?: integer, lax?: boolean): string

Capitalizes a UTF-8 string: the first case-able character within the given range is converted to uppercase (left untouched if it's already uppercase), and every case-able character after it is converted to lowercase.

Characters with no case mapping (digits, symbols, whitespace, etc.) are skipped and do not count as "the first character".

Parameters
NameTypeDescription
utf8_stringstringThe UTF-8 string to capitalize.
start_positionoptionalintegerThe position to start from in bytes.
end_positionoptionalintegerThe position to end at in bytes.
laxoptionalbooleanWhether to lax the UTF-8 validity check.
Returns
TypeNameDescription
stringcapitalized_utf8_strThe capitalized UTF-8 string.

#unicode.displayWidth

sharedmenunodiscardsource
function unicode.displayWidth(utf8_string: string, start_position?: integer, end_position?: integer, lax?: boolean): integer

Returns the number of terminal columns the given UTF-8 string takes in a monospace font.

Unlike utf8.len, wide characters (CJK, emoji) count as 2, combining marks and other zero-width characters count as 0, emoji ZWJ sequences (👨‍👩‍👧), skin tones (👍🏽) and flags (🇺🇦) count as a single wide character.

The result depends on the font and terminal, emoji are rendered inconsistently across them.

Parameters
NameTypeDescription
utf8_stringstringThe UTF-8 string to measure.
start_positionoptionalintegerThe position to start from in bytes.
end_positionoptionalintegerThe position to end at in bytes.
laxoptionalbooleanWhether to lax the UTF-8 validity check, invalid sequences count as U+FFFD.
Returns
TypeNameDescription
integerwidthThe width of the string in columns.

#unicode.escape

sharedmenunodiscardsource
function unicode.escape(utf8_str: string, style?: unicode.EscapeStyle, keep_ascii?: boolean, start_position?: integer, end_position?: integer, lax?: boolean): string

Converts each character in the input string into a unicode escape sequence, suitable for serialization, logging, or safe ASCII output.

Parameters
NameTypeDescription
utf8_strstringThe input string containing unicode characters.
styleoptionalunicode.EscapeStyleThe escape sequence style, "json" by default.
keep_asciioptionalbooleanIf true, printable ASCII characters (except \) are left as is.
start_positionoptionalintegerThe position to start from in bytes.
end_positionoptionalintegerThe position to end at in bytes.
laxoptionalbooleanWhether to lax the UTF-8 validity check, invalid sequences are escaped as U+FFFD.
Returns
TypeNameDescription
stringescaped_strA string where each character is replaced with its unicode escape sequence.

#unicode.fromSurrogates

sharedmenunodiscardsource
function unicode.fromSurrogates(high_surrogate: integer, low_surrogate: integer): integer

Combines a UTF-16 surrogate pair into a supplementary code point.

Parameters
NameTypeDescription
high_surrogateintegerThe high (leading) surrogate, U+D800 - U+DBFF.
low_surrogateintegerThe low (trailing) surrogate, U+DC00 - U+DFFF.
Returns
TypeNameDescription
integerutf8_codepointThe code point in range U+10000 - U+10FFFF.

#unicode.isHighSurrogate

sharedmenunodiscardsource
function unicode.isHighSurrogate(utf8_codepoint: integer): boolean

Checks if the given code point is a UTF-16 high (leading) surrogate, in range U+D800 - U+DBFF.

Parameters
NameTypeDescription
utf8_codepointintegerThe code point to check.
Returns
TypeNameDescription
booleanis_high_surrogatetrue if the code point is a high surrogate, false otherwise.

#unicode.isLowSurrogate

sharedmenunodiscardsource
function unicode.isLowSurrogate(utf8_codepoint: integer): boolean

Checks if the given code point is a UTF-16 low (trailing) surrogate, in range U+DC00 - U+DFFF.

Parameters
NameTypeDescription
utf8_codepointintegerThe code point to check.
Returns
TypeNameDescription
booleanis_low_surrogatetrue if the code point is a low surrogate, false otherwise.

#unicode.isNoncharacter

sharedmenunodiscardsource
function unicode.isNoncharacter(utf8_codepoint: integer): boolean

Checks if the given code point is a noncharacter, U+FDD0 - U+FDEF or the last two code points of any plane (U+xFFFE, U+xFFFF).

Parameters
NameTypeDescription
utf8_codepointintegerThe code point to check.
Returns
TypeNameDescription
booleanis_noncharactertrue if the code point is a noncharacter, false otherwise.

#unicode.isPrivateUse

sharedmenunodiscardsource
function unicode.isPrivateUse(utf8_codepoint: integer): boolean

Checks if the given code point belongs to a private use area, U+E000 - U+F8FF, U+F0000 - U+FFFFD or U+100000 - U+10FFFD.

Parameters
NameTypeDescription
utf8_codepointintegerThe code point to check.
Returns
TypeNameDescription
booleanis_private_usetrue if the code point is a private use code point, false otherwise.

#unicode.isSurrogate

sharedmenunodiscardsource
function unicode.isSurrogate(utf8_codepoint: integer): boolean

Checks if the given code point is a UTF-16 surrogate, in range U+D800 - U+DFFF.

Parameters
NameTypeDescription
utf8_codepointintegerThe code point to check.
Returns
TypeNameDescription
booleanis_surrogatetrue if the code point is a surrogate, false otherwise.

#unicode.isValid

sharedmenunodiscardsource
function unicode.isValid(utf8_codepoint: integer): boolean

Checks if the given value is a unicode scalar value, an integer in range U+0000 - U+10FFFF that is not a surrogate.

Only scalar values can be encoded in UTF-8.

Parameters
NameTypeDescription
utf8_codepointintegerThe code point to check.
Returns
TypeNameDescription
booleanis_validtrue if the code point is a unicode scalar value, false otherwise.

#unicode.lower

sharedmenunodiscardsource
function unicode.lower(utf8_string: string, start_position?: integer, end_position?: integer, lax?: boolean): string

Converts a UTF-8 string to lowercase within the given byte range.

Each decoded codepoint that has a known uppercase-to-lowercase mapping is replaced with its lowercase form; any codepoint with no such mapping (already lowercase, digits, symbols, whitespace, or characters outside the known case tables) is left unchanged.

Bytes outside the given range are always left untouched.

Parameters
NameTypeDescription
utf8_stringstringThe UTF-8 string to convert.
start_positionoptionalintegerThe byte position to start converting from (inclusive). Defaults to 1. Negative values count from the end of the string.
end_positionoptionalintegerThe byte position to stop converting at (inclusive). Defaults to the end of the string. Negative values count from the end of the string.
laxoptionalbooleanWhether to lax the UTF-8 validity check.
Returns
TypeNameDescription
stringlowercase_utf8_strThe string with the given range converted to lowercase; identical to the input if nothing in range needed conversion.

#unicode.pad

sharedmenunodiscardsource
function unicode.pad(utf8_string: string, desired_width: integer, left?: boolean, right?: boolean, padding?: string, lax?: boolean): string

Pads a UTF-8 string with a repeated padding string until it is desired_width columns wide (see unicode.displayWidth), on the left, the right, or both sides evenly.

If neither left nor right is true, padding has no width, or the string is already at least desired_width columns wide, utf8_string is returned unchanged.

When padding both sides, the missing width is split as evenly as possible between the left and right, with any extra leftover column going to the right side; if it doesn't divide evenly by whole copies of padding, the shortfall is made up with a partial copy of padding, and with spaces where a wide character of padding doesn't fit.

Parameters
NameTypeDescription
utf8_stringstringThe UTF-8 string to pad.
desired_widthintegerThe target width, in columns, that the result should reach.
leftoptionalbooleanWhether to add padding on the left side. Defaults to true.
rightoptionalbooleanWhether to add padding on the right side. Defaults to true.
paddingoptionalstringThe string to pad with, repeated as needed. Defaults to a single space.
laxoptionalbooleanWhether to lax the UTF-8 validity check.
Returns
TypeNameDescription
stringresultThe padded string, or utf8_string unchanged if no padding was needed or requested.

#unicode.plane

sharedmenunodiscardsource
function unicode.plane(utf8_codepoint: integer): integer

Returns the unicode plane of the given code point, 0 for the Basic Multilingual Plane up to 16.

Parameters
NameTypeDescription
utf8_codepointintegerThe code point.
Returns
TypeNameDescription
integerplaneThe plane number.

#unicode.tag

sharedmenunodiscardsource
function unicode.tag(utf8_codepoint: integer): string

Returns a string representation of the given unicode code point.

Parameters
NameTypeDescription
utf8_codepointintegerThe unicode code point to represent.
Returns
TypeNameDescription
stringutf8_tagThe string representation of the code point, in the format U+XXXX - U+XXXXXX.

#unicode.title

sharedmenunodiscardsource
function unicode.title(utf8_string: string, start_position?: integer, end_position?: integer, lax?: boolean): string

Converts a UTF-8 string to title case within the given range: the first case-able character of each word is converted to uppercase (left alone if already uppercase), and every other case-able character in the word is converted to lowercase.

Words are separated by whitespace and punctuation, apostrophes (' and ’), digits and letters without case (ß, CJK, etc.) don't break a word.

Parameters
NameTypeDescription
utf8_stringstringThe UTF-8 string to convert to title case.
start_positionoptionalintegerThe position to start from in bytes.
end_positionoptionalintegerThe position to end at in bytes.
laxoptionalbooleanWhether to lax the UTF-8 validity check.
Returns
TypeNameDescription
stringtitle_utf8_strThe title-cased UTF-8 string.

#unicode.toSurrogates

sharedmenunodiscardsource
function unicode.toSurrogates(utf8_codepoint: integer): integer, integer

Splits a supplementary code point (U+10000 - U+10FFFF) into a UTF-16 surrogate pair.

Parameters
NameTypeDescription
utf8_codepointintegerThe supplementary code point.
Returns
TypeNameDescription
integerhigh_surrogateThe high (leading) surrogate.
integerlow_surrogateThe low (trailing) surrogate.

#unicode.unescape

sharedmenunodiscardsource
function unicode.unescape(escaped_str: string, lax?: boolean): string

Converts unicode escape sequences into actual characters.

Supported formats are \uXXXX (exactly 4 hex digits), UTF-16 surrogate pairs \uXXXX\uXXXX, \u{X} (any number of hex digits) and \UXXXXXXXX (exactly 8 hex digits).

\\ is treated as an escaped backslash and left as is, malformed sequences are left as is.

Parameters
NameTypeDescription
escaped_strstringA string containing unicode escape sequences.
laxoptionalbooleanIf true, lone surrogates and code points above U+10FFFF are replaced with U+FFFD instead of raising an error.
Returns
TypeNameDescription
stringutf8_strA string with the escape sequences converted to real unicode characters.

#unicode.untag

sharedmenunodiscardsource
function unicode.untag(utf8_tag: string): integer | nil

Returns the unicode code point represented by the given string.

Parameters
NameTypeDescription
utf8_tagstringThe string representation of a unicode code point, in the format U+XXXX - U+XXXXXX.
Returns
TypeNameDescription
integer | nilutf8_codepointThe unicode code point represented by the string, or nil if the string is not a valid tag.

#unicode.upper

sharedmenunodiscardsource
function unicode.upper(utf8_string: string, start_position?: integer, end_position?: integer, lax?: boolean): string

Converts a UTF-8 string to uppercase within the given byte range.

Each decoded codepoint that has a known lowercase-to-uppercase mapping is replaced with its uppercase form; any codepoint with no such mapping (already uppercase, digits, symbols, whitespace, or characters outside the known case tables) is left unchanged.

Bytes outside the given range are always left untouched.

Parameters
NameTypeDescription
utf8_stringstringThe UTF-8 string to convert.
start_positionoptionalintegerThe byte position to start converting from (inclusive). Defaults to 1. Negative values count from the end of the string.
end_positionoptionalintegerThe byte position to stop converting at (inclusive). Defaults to the end of the string. Negative values count from the end of the string.
laxoptionalbooleanWhether to lax the UTF-8 validity check.
Returns
TypeNameDescription
stringuppercase_utf8_strThe string with the given range converted to uppercase; identical to the input if nothing in range needed conversion.

#unicode.width

sharedmenunodiscardsource
function unicode.width(utf8_codepoint: integer): integer

Returns the number of terminal columns the given code point takes in a monospace font.

  • 0 for control characters, combining marks, format characters (ZWJ, ZWSP, BOM, ...) and variation selectors.
  • 2 for East Asian wide and fullwidth characters (CJK, kana, hangul) and emoji.
  • 1 for everything else.

Use unicode.displayWidth for strings, it also handles emoji sequences.

Parameters
NameTypeDescription
utf8_codepointintegerThe code point.
Returns
TypeNameDescription
integerwidthThe width of the code point in columns, 0, 1 or 2.

#unicode.wrap

sharedmenunodiscardsource
function unicode.wrap(utf8_string: string, max_width: integer, start_position?: integer, end_position?: integer, lax?: boolean): string

Wraps a UTF-8 string so that no line is wider than max_width columns (see unicode.displayWidth), breaking on the last ASCII space before the limit when one is available within the current line, or hard-splitting mid-word otherwise.

Zero-width characters stay with the character they belong to, so a line can only exceed max_width when a single wide character doesn't fit into it.

Lines are joined with "\n", existing "\n" characters start a new line.

Bytes outside the given range are left untouched and don't count towards the line width.

Parameters
NameTypeDescription
utf8_stringstringThe UTF-8 string to wrap.
max_widthintegerThe maximum width of each line, in columns.
start_positionoptionalintegerThe position to start from in bytes.
end_positionoptionalintegerThe position to end at in bytes.
laxoptionalbooleanWhether to lax the UTF-8 validity check.
Returns
TypeNameDescription
stringwrapped_stringThe string with newlines inserted so no line is wider than max_width columns.
Generated from the dreamwork meta branch.