Library utf8

GLua's utf8 library.

Functions

char( ... ) Builds a UTF-8 string with the given codepoints.
codepoint( string, startPos, endPos ) Returns the codepoints contained in the given UTF-8 string between a given start and end position.
codes( string ) Returns an iterator over the codepoints in the given string.
force( string ) Returns the given string with any invalid UTF-8 byte sequences replaced by the replacement character (U+FFFD).
getChar( string, index ) Returns the UTF-8 character at the given character index in the given string.
len( string, startPos, endPos ) Returns the length of the given string in UTF-8 codepoints rather than bytes.
offset( string, n, startPos ) Returns the byte offset of the n-th UTF-8 codepoint in the given string.
sub( string, startPos, endPos ) Returns the UTF-8 characters between the given start and end character positions.

Fields

charpattern A pattern that matches a single UTF-8 character (for use in find/match/gmatch etc.)


Functions

utf8.char( ... )
Builds a UTF-8 string with the given codepoints.

Environment:

Shared

Parameters:

  • (number...) ...: One or more codepoints.

Return value:

    (string) The UTF-8 character for each of the given codepoints concatenated into a single string.
utf8.codepoint( string, startPos, endPos )
Returns the codepoints contained in the given UTF-8 string between a given start and end position.

Environment:

Shared

Parameters:

  • (string) string: The string to retrieve the codepoint(s) from.
  • (number?) startPos: The starting byte position in the string to read from (inclusive, default = 1).
  • (number?) endPos: The ending byte position in the string to read up to (inclusive, default = 1).

Return value:

    (number...) Each of the codepoints contained between the two byte positions in the string.
utf8.codes( string )
Returns an iterator over the codepoints in the given string. The loop values are the byte position, and the codepoint respectively.

Environment:

Shared

Parameters:

  • (string) string: The string to iterate over.

Usage:

    for BytePos, CodePoint in utf8.codes( "test" ) do 
        print( BytePos, utf8.char( CodePoint ) ) 
    end

Return value:

    (function) An iterator function that iterates over the given string's UTF-8 codepoints.
utf8.force( string )
Returns the given string with any invalid UTF-8 byte sequences replaced by the replacement character (U+FFFD).

Environment:

Shared

Parameters:

  • (string) string: The string to force into valid UTF-8.

Return value:

    (string) The string as a valid UTF-8 byte sequence.
utf8.getChar( string, index )
Returns the UTF-8 character at the given character index in the given string.

Environment:

Shared

Parameters:

  • (string) string: The string to get the character from.
  • (number) index: The index of the character to retrieve.

Return value:

    (string) The character at the given index.
utf8.len( string, startPos, endPos )
Returns the length of the given string in UTF-8 codepoints rather than bytes.

Environment:

Shared

Parameters:

  • (string) string: The string to get the length of.
  • (number?) startPos: The starting byte position in the string to count from (default = 1).
  • (number?) endPos: The ending byte position in the string to count up to (default = #string).

Return values:

  1. (number | boolean) The string's length in UTF-8 codepoints, or false if the string is not valid UTF-8.
  2. (number | nil) If the string is not valid UTF-8, the byte position of the first invalid byte sequence.
utf8.offset( string, n, startPos )
Returns the byte offset of the n-th UTF-8 codepoint in the given string.

Environment:

Shared

Parameters:

  • (string) string: The string to search.
  • (number) n: The index of the codepoint to retrieve the byte offset of (negative values start from the end of the string, e.g. -1 is the last character).
  • (number?) startPos: The byte position to start the search from (default = 1 if N >= 0, #string otherwise).

Return value:

    (number) The byte offset of the given character.
utf8.sub( string, startPos, endPos )
Returns the UTF-8 characters between the given start and end character positions.

Environment:

Shared

Parameters:

  • (string) string: The string to get the characters from.
  • (number) startPos: The starting character position (inclusive).
  • (number?) endPos: The ending character position (inclusive, default = utf8.len( string )).

Return value:

    (string) A string containing the characters between the given start and end positions.

Valid XHTML 1.0!