util.string.invalidoffset
Shared
Finds the byte position of the first invalid UTF-8 sequence
Syntax
local position = util.string.invalidoffset(
input,
start_at = nil
)This function returns a byte position, not a character position.
Passing that position into util.string.sub or util.string.byte - which now index by character will give incorrect results except by coincidence on pure-ASCII input.
Parameters
| Type | Name | Description |
|---|---|---|
string | input | String to scan |
int | start_at | Byte position to begin scanning from - negative counts from end |
Returns
| Type | Name | Description |
|---|---|---|
int | nil | position | Byte position of the first invalid byte, or nil if input is fully valid from start_at onward |
Examples
local pos = util.string.invalidoffset("hello")
core.engine.print("info", pos) -- nillocal pos = util.string.invalidoffset("ab\xFFcd")
core.engine.print("info", pos) -- 3local pos = util.string.invalidoffset("\xFFab\xFFcd", 2)
core.engine.print("info", pos) -- 4