util.string.codepoint

Shared

Retrieves the Unicode codepoints of characters starting within a byte range


Syntax

local ... = util.string.codepoint(
    input,
    start_at = 1,
    end_at = start_at,
    lax = false
)

Parameters

TypeNameDescription
stringinputString to read codepoints from
intstart_atByte position to begin from - must be the start of a character - negative counts from end
intend_atByte position to end at (inclusive) - negative counts from end
boollaxWhen true - skips validation and decodes even malformed sequences

Returns

TypeNameDescription
any...Numeric codepoint values for every character starting in the given byte range

Examples

Get the codepoint of the first character
local code = util.string.codepoint("A")

core.engine.print("info", code) -- 65
Get the codepoint of a multi-byte character
local code = util.string.codepoint("€")

core.engine.print("info", code) -- 8364
Get every codepoint across a byte range
local a, b, c = util.string.codepoint("ABC", 1, 3)

core.engine.print("info", a, b, c) -- 65  66  67
Indices are byte positions, not character positions
-- "€" is 3 bytes long, so the next character starts at byte 4
local euro, dollar = util.string.codepoint("€$", 1, 4)

core.engine.print("info", euro, dollar) -- 8364  36

On this page