Skip to main content

Module utf8

Module utf8 

Source
Expand description

Basic support for UTF-8 encoding.

This library does not provide any support for Unicode other than the handling of the encoding itself. Any operation that needs the meaning of a character, such as character classification, is outside its scope.

Unless stated otherwise, all functions that expect a byte position as a parameter assume that the given position is either the start of a byte sequence or one plus the length of the subject string.

Constants§

charpattern
The pattern "[\0-\x7F\xC2-\xFD][\x80-\xBF]*", which matches exactly one UTF-8 byte sequence, assuming that the subject is a valid UTF-8 string.

Functions§

char
Receives zero or more integers, converts each one to its corresponding UTF-8 byte sequence, and returns a string with the concatenation of all these sequences.
codepoint
Returns the code points (as integers) from all characters in s that start between byte positions i and j (both included). The default for i is 1 and for j is i. It raises an error if it meets any invalid byte sequence.
codes
Returns values so that the construction
len
Returns the number of UTF-8 characters in string s that start between byte positions i and j (default 1 and −1). If it finds any invalid byte sequence, returns nil plus the position of the first invalid byte.
offset
Returns the position (in bytes) where the encoding of the n-th character of s (counting from byte position i) starts, and — since Lua 5.5 — also the position of the final byte of that character. A negative n gets characters before position i. utf8.offset(s, -1) gets the last character of the string.