dreamworkAPI
Ctrl K
GitHub
libraryDocs / utf32

utf32

librarysharedmenusource

The utf32 library provides functions for the manipulation of UTF-32 strings.

Positions are in bytes, except utf32.sub, which takes code point positions. Code units are aligned to the start of the string, all functions use little-endian byte order unless big_endian is true, a byte order mark is not handled.

Functions that take lax raise an error on the first invalid code unit (a surrogate, a value above U+10FFFF or a truncated tail), unless lax is true; in lax mode each invalid code unit counts as one code point, decoded as U+FFFD.

Functions

utf32.charEncodes code points into a little-endian UTF-32 string.
utf32.codepointReturns the code points of all characters that start between byte positions start_position and end_position (both inclu…
utf32.codesReturns an iterator function that iterates over the code points of a UTF-32 string.
utf32.decodeDecodes a single UTF-32 sequence starting at the given byte position.
utf32.encodeEncodes a single code point into a UTF-32 sequence of 1 code unit (4 bytes).
utf32.lenReturns the number of code points that start between byte positions start_position and end_position (both inclusive).
utf32.normalizeReturns a well-formed copy of a UTF-32 string, every invalid sequence (surrogates, values above U+10FFFF, a truncated t…
utf32.offsetReturns the byte positions where the n-th code point, counting from byte position, starts and ends.
utf32.packEncodes a table of code points into a UTF-32 string.
utf32.reverseReturns the code points between byte positions start_position and end_position in reverse order.
utf32.subReturns the substring between code points start_position and end_position (both inclusive).
utf32.unpackDecodes a UTF-32 string into a table of code points.

#utf32.char

sharedmenunodiscardsource
function utf32.char(a?: integer, b?: integer, ...: integer): string

Encodes code points into a little-endian UTF-32 string.

This functions similarly to string.char, invalid code points raise an error, use utf32.pack for big-endian strings.

Parameters
NameTypeDescription
aoptionalintegerThe first code point.
boptionalintegerThe second code point.
...integerThe rest of the code points.
Returns
TypeNameDescription
stringutf32_stringThe UTF-32 string.

#utf32.codepoint

sharedmenunodiscardsource
function utf32.codepoint(utf32_string: string, start_position?: integer, end_position?: integer, lax?: boolean, big_endian?: boolean): integer

Returns the code points of all characters that start between byte positions start_position and end_position (both inclusive).

This functions similarly to string.byte, use utf32.unpack for long strings.

The last code point is decoded completely even if it ends after end_position.

Parameters
NameTypeDescription
utf32_stringstringThe UTF-32 string to decode.
start_positionoptionalintegerThe position to start from in bytes, 1 by default.
end_positionoptionalintegerThe position to end at in bytes, start_position by default.
laxoptionalbooleanIf true, invalid sequences are decoded as U+FFFD instead of raising an error.
big_endianoptionalbooleanWhether the string is big-endian.
Returns
TypeNameDescription
integer...The code points.

#utf32.codes

sharedmenunodiscardsource
function utf32.codes(utf32_string: string, lax?: boolean, big_endian?: boolean): ( fun( utf32_string: string, index: integer ): integer | nil, integer | nil ), string, integer

Returns an iterator function that iterates over the code points of a UTF-32 string.

Yields the byte position and the code point of every character, like utf8.codes.

Invalid sequences raise an error, or yield U+FFFD if lax is true.

Parameters
NameTypeDescription
utf32_stringstringThe UTF-32 string to iterate over.
laxoptionalbooleanIf true, invalid sequences are yielded as U+FFFD instead of raising an error.
big_endianoptionalbooleanWhether the string is big-endian.
Returns
TypeNameDescription
( fun( utf32_string: string, index: integer ): integer | nil, integer | nil )
string
integer

#utf32.decode

sharedmenunodiscardsource
function utf32.decode(utf32_string: string, index?: integer, end_index?: integer, lax?: boolean, big_endian?: boolean, stack_level?: integer): integer | nil, integer, integer | nil, utf32.ErrorCode | nil

Decodes a single UTF-32 sequence starting at the given byte position.

In lax mode invalid sequences do not raise an error, nil is returned as the code point together with the number of bytes to skip, the position of the sequence and the error code.

Parameters
NameTypeDescription
utf32_stringstringThe UTF-32 string to decode from.
indexoptionalintegerThe position of the sequence in bytes, 1 by default.
end_indexoptionalintegerThe last byte that can be read, the end of the string by default; a sequence cut by it is invalid.
laxoptionalbooleanIf true, invalid sequences are returned as nil instead of raising an error.
big_endianoptionalbooleanWhether the string is big-endian.
stack_leveloptionalintegerThe error level, relative to the caller, 1 (default) blames the caller of this function.
Returns
TypeNameDescription
integer | nilutf32_codepointThe decoded code point, or nil if the sequence is invalid.
integersequence_lengthThe length of the sequence in bytes, at least 1.
integer | nilerror_positionThe position of the invalid sequence, nil if the sequence is valid.
utf32.ErrorCode | nilerror_codeThe error code, nil if the sequence is valid.

#utf32.encode

sharedmenunodiscardsource
function utf32.encode(utf32_codepoint: integer, lax?: boolean, big_endian?: boolean, stack_level?: integer): string

Encodes a single code point into a UTF-32 sequence of 1 code unit (4 bytes).

Parameters
NameTypeDescription
utf32_codepointintegerThe code point to encode.
laxoptionalbooleanIf true, invalid code points (surrogates, negative or above U+10FFFF) are encoded as U+FFFD instead of raising an error.
big_endianoptionalbooleanWhether to encode as big-endian.
stack_leveloptionalintegerThe error level, relative to the caller, 1 (default) blames the caller of this function.
Returns
TypeNameDescription
stringutf32_sequenceThe UTF-32 sequence.

#utf32.len

sharedmenunodiscardsource
function utf32.len(utf32_string: string, start_position?: integer, end_position?: integer, lax?: boolean, big_endian?: boolean): integer

Returns the number of code points that start between byte positions start_position and end_position (both inclusive).

Parameters
NameTypeDescription
utf32_stringstringThe UTF-32 string to count the code points of.
start_positionoptionalintegerThe position to start from in bytes, 1 by default.
end_positionoptionalintegerThe position to end at in bytes, the end of the string by default; a sequence cut by it is invalid.
laxoptionalbooleanIf true, every invalid sequence counts as one code point instead of raising an error.
big_endianoptionalbooleanWhether the string is big-endian.
Returns
TypeNameDescription
integerutf32_codepoint_countThe number of code points.

#utf32.normalize

sharedmenunodiscardsource
function utf32.normalize(utf32_string: string, replacement_str?: string, start_position?: integer, end_position?: integer, big_endian?: boolean): string

Returns a well-formed copy of a UTF-32 string, every invalid sequence (surrogates, values above U+10FFFF, a truncated tail) is replaced with replacement_str.

This is not Unicode normalization (NFC, NFD, NFKC, NFKD): valid sequences are copied as is.

Only the range between start_position and end_position is returned, a sequence cut by end_position is invalid.

Parameters
NameTypeDescription
utf32_stringstringThe UTF-32 string to sanitize.
replacement_stroptionalstringThe string to replace invalid sequences with, U+FFFD in the string's byte order by default.
start_positionoptionalintegerThe position to start from in bytes, 1 by default.
end_positionoptionalintegerThe position to end at in bytes, the end of the string by default.
big_endianoptionalbooleanWhether the string is big-endian.
Returns
TypeNameDescription
stringutf32_normalizedThe well-formed UTF-32 string.

#utf32.offset

sharedmenunodiscardsource
function utf32.offset(utf32_string: string, n: integer, position?: integer, lax?: boolean, big_endian?: boolean): integer | nil, integer | nil

Returns the byte positions where the n-th code point, counting from byte position, starts and ends.

This functions similarly to utf8.offset from Lua 5.5:

  • n > 0 counts forward, 1 is the code point that starts at position.
  • n < 0 counts backward, -1 is the code point right before position.
  • n = 0 returns the code point that contains byte position.

#utf32_string + 1 is the position right after the last code point and is returned as #utf32_string + 1, #utf32_string + 1.

Code units are aligned to the start of the string, a truncated tail (1 to 3 bytes) is one code point. In lax mode positions are computed without reading the string, otherwise any invalid code unit that is walked over raises an error.

Raises an error if n ~= 0 and position points inside a code point.

Parameters
NameTypeDescription
utf32_stringstringThe UTF-32 string to search in.
nintegerThe number of the code point, relative to position.
positionoptionalintegerThe byte position to count from, 1 if n >= 0, otherwise #utf32_string + 1, negative positions count from the end of the string, out of range positions are clamped.
laxoptionalbooleanIf true, invalid code units count as code points instead of raising an error.
big_endianoptionalbooleanWhether the string is big-endian.
Returns
TypeNameDescription
integer | nilstart_positionThe position of the first byte of the code point, or nil if the string has no such code point.
integer | nilend_positionThe position of the last byte of the code point, or nil if the string has no such code point.

#utf32.pack

sharedmenunodiscardsource
function utf32.pack(utf32_codepoints: integer[], utf32_codepoint_count?: integer, lax?: boolean, big_endian?: boolean): string

Encodes a table of code points into a UTF-32 string.

Parameters
NameTypeDescription
utf32_codepointsinteger[]The code points to encode.
utf32_codepoint_countoptionalintegerThe number of code points to encode, #utf32_codepoints by default.
laxoptionalbooleanIf true, invalid code points (surrogates, negative or above U+10FFFF) are encoded as U+FFFD instead of raising an error.
big_endianoptionalbooleanWhether to produce a big-endian string.
Returns
TypeNameDescription
stringutf32_stringThe UTF-32 string.

#utf32.reverse

sharedmenunodiscardsource
function utf32.reverse(utf32_string: string, start_position?: integer, end_position?: integer, lax?: boolean, big_endian?: boolean): string

Returns the code points between byte positions start_position and end_position in reverse order.

This functions similarly to string.reverse, but the bytes of each code point keep their order.

Parameters
NameTypeDescription
utf32_stringstringThe UTF-32 string to reverse.
start_positionoptionalintegerThe position to start from in bytes, 1 by default.
end_positionoptionalintegerThe position to end at in bytes, the end of the string by default; a sequence cut by it is invalid.
laxoptionalbooleanIf true, invalid sequences are replaced with U+FFFD instead of raising an error.
big_endianoptionalbooleanWhether the string is big-endian.
Returns
TypeNameDescription
stringutf32_reversedThe reversed UTF-32 string.

#utf32.sub

sharedmenunodiscardsource
function utf32.sub(utf32_string: string, start_position?: integer, end_position?: integer, lax?: boolean, big_endian?: boolean): string

Returns the substring between code points start_position and end_position (both inclusive).

This functions similarly to string.sub, but positions are in code points, negative positions count from the end of the string.

The bytes are copied as is, in lax mode invalid sequences are kept (each counts as one code point).

Parameters
NameTypeDescription
utf32_stringstringThe UTF-32 string to get the substring of.
start_positionoptionalintegerThe position of the first code point, 1 by default.
end_positionoptionalintegerThe position of the last code point, the last code point of the string by default.
laxoptionalbooleanIf true, every invalid sequence counts as one code point instead of raising an error.
big_endianoptionalbooleanWhether the string is big-endian.
Returns
TypeNameDescription
stringutf32_subThe substring.

#utf32.unpack

sharedmenunodiscardsource
function utf32.unpack(utf32_string: string, start_position?: integer, end_position?: integer, lax?: boolean, big_endian?: boolean): integer[], integer

Decodes a UTF-32 string into a table of code points.

Parameters
NameTypeDescription
utf32_stringstringThe UTF-32 string to decode.
start_positionoptionalintegerThe position to start from in bytes, 1 by default.
end_positionoptionalintegerThe position to end at in bytes, the end of the string by default; a sequence cut by it is invalid.
laxoptionalbooleanIf true, invalid sequences are decoded as U+FFFD instead of raising an error.
big_endianoptionalbooleanWhether the string is big-endian.
Returns
TypeNameDescription
integer[]utf32_codepointsThe code points.
integerutf32_codepoint_countThe number of code points.
Generated from the dreamwork meta branch.