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
Name
Type
Description
MAX
integer
The largest valid unicode code point, U+10FFFF. = 0x10FFFF
REPLACEMENT
integer
The replacement character code point, U+FFFD. = 0xFFFD
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".
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
Name
Type
Description
utf8_string
string
The UTF-8 string to measure.
start_positionoptional
integer
The position to start from in bytes.
end_positionoptional
integer
The position to end at in bytes.
laxoptional
boolean
Whether to lax the UTF-8 validity check, invalid sequences count as U+FFFD.
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
Name
Type
Description
utf8_string
string
The UTF-8 string to convert.
start_positionoptional
integer
The byte position to start converting from (inclusive). Defaults to 1. Negative values count from the end of the string.
end_positionoptional
integer
The byte position to stop converting at (inclusive). Defaults to the end of the string. Negative values count from the end of the string.
laxoptional
boolean
Whether to lax the UTF-8 validity check.
Returns
Type
Name
Description
string
lowercase_utf8_str
The string with the given range converted to lowercase; identical to the input if nothing in range needed conversion.
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
Name
Type
Description
utf8_string
string
The UTF-8 string to pad.
desired_width
integer
The target width, in columns, that the result should reach.
leftoptional
boolean
Whether to add padding on the left side. Defaults to true.
rightoptional
boolean
Whether to add padding on the right side. Defaults to true.
paddingoptional
string
The string to pad with, repeated as needed. Defaults to a single space.
laxoptional
boolean
Whether to lax the UTF-8 validity check.
Returns
Type
Name
Description
string
result
The padded string, or utf8_string unchanged if no padding was needed or requested.
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.
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
Name
Type
Description
utf8_string
string
The UTF-8 string to convert.
start_positionoptional
integer
The byte position to start converting from (inclusive). Defaults to 1. Negative values count from the end of the string.
end_positionoptional
integer
The byte position to stop converting at (inclusive). Defaults to the end of the string. Negative values count from the end of the string.
laxoptional
boolean
Whether to lax the UTF-8 validity check.
Returns
Type
Name
Description
string
uppercase_utf8_str
The string with the given range converted to uppercase; identical to the input if nothing in range needed conversion.
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
Name
Type
Description
utf8_string
string
The UTF-8 string to wrap.
max_width
integer
The maximum width of each line, in columns.
start_positionoptional
integer
The position to start from in bytes.
end_positionoptional
integer
The position to end at in bytes.
laxoptional
boolean
Whether to lax the UTF-8 validity check.
Returns
Type
Name
Description
string
wrapped_string
The string with newlines inserted so no line is wider than max_width columns.