stringx
Batch text-scanning kernels — read a whole run of numbers out of a string in one call.
require("@builtin::modules.api.engine.stringx") -- stringx (also available as global 'stringx')
The stringx namespace: 6 functions.
globals/stringx/decodeJson
stringx.decodeJson(s: string) -> (boolean, any)
Decode a JSON document in one call, when the document is one the
engine's Luau JSON decoder reads the same way. ok is true and value
holds the decoded value (nil for a document that is null); ok is false
for text this leaves to that decoder: malformed text, nesting past 96
levels, escapes or control bytes the two could read differently, and text
after the value. modules.json calls this first and falls back to its own
scanners on false.
Parameters
sstring— The JSON text.
Returns (boolean, any) — Whether the text was decoded here, and the value.
local ok, value = stringx.decodeJson('{"a":[1,2,3]}')
globals/stringx/encodeJson
stringx.encodeJson(value: any?) -> (boolean, string?)
Encode a value as compact JSON in one call, when the value is one the
engine's Luau JSON encoder writes the same way. ok is true and text
holds the JSON; ok is false for a value this leaves to that encoder: a
table with a metatable, a table that contains itself, a number key that is
not finite, and nesting past 192 levels. modules.json calls this first
and falls back to its own walk on false.
Parameters
valueany(optional) — The value to encode.
Returns (boolean, string?) — Whether the value was encoded here, and the text.
local ok, text = stringx.encodeJson({ a = { 1, 2, 3 } })
globals/stringx/scanNumbers
stringx.scanNumbers(s: string, pos: number?) -> ({ number }, number)
Read the run of numbers starting at pos — separated by commas and/or
whitespace — and report where the run ended.
The run stops at the first character that neither continues a number nor
separates two of them (], }, a quote, a letter), and nextPos is that
character's index, so the caller's own parser resumes exactly there. A
token that is not a valid number also ends the run, with nextPos left ON
it rather than past it, so nothing is skipped without the caller seeing it.
Parameters
sstring— The text to read.posnumber(optional) — 1-based index to start at. Defaults to 1.
Returns ({ number }, number) — The numbers found, and the 1-based position just past them.
-- A JSON array of numbers, in one crossing instead of one per token.
local values, nextPos = stringx.scanNumbers(payload, afterBracket)
-- A whitespace-separated block (OBJ, PLY, a matrix dump).
local m = stringx.scanNumbers("1 0 0 0 0 1 0 0", 1)
typed/builtin//modules/api/engine/stringx/stringx/decodeJson
stringx.decodeJson(s: string) -> (boolean, any)
Decode a JSON document in one call, when the document is one the
engine's Luau JSON decoder reads the same way. ok is true and value
holds the decoded value (nil for a document that is null); ok is false
for text this leaves to that decoder: malformed text, nesting past 96
levels, escapes or control bytes the two could read differently, and text
after the value. modules.json calls this first and falls back to its own
scanners on false.
Parameters
sstring— The JSON text.
Returns (boolean, any) — Whether the text was decoded here, and the value.
local ok, value = stringx.decodeJson('{"a":[1,2,3]}')
typed/builtin//modules/api/engine/stringx/stringx/encodeJson
stringx.encodeJson(value: any?) -> (boolean, string?)
Encode a value as compact JSON in one call, when the value is one the
engine's Luau JSON encoder writes the same way. ok is true and text
holds the JSON; ok is false for a value this leaves to that encoder: a
table with a metatable, a table that contains itself, a number key that is
not finite, and nesting past 192 levels. modules.json calls this first
and falls back to its own walk on false.
Parameters
valueany(optional) — The value to encode.
Returns (boolean, string?) — Whether the value was encoded here, and the text.
local ok, text = stringx.encodeJson({ a = { 1, 2, 3 } })
typed/builtin//modules/api/engine/stringx/stringx/scanNumbers
stringx.scanNumbers(s: string, pos: number?) -> ({ number }, number)
Read the run of numbers starting at pos — separated by commas and/or
whitespace — and report where the run ended.
The run stops at the first character that neither continues a number nor
separates two of them (], }, a quote, a letter), and nextPos is that
character's index, so the caller's own parser resumes exactly there. A
token that is not a valid number also ends the run, with nextPos left ON
it rather than past it, so nothing is skipped without the caller seeing it.
Parameters
sstring— The text to read.posnumber(optional) — 1-based index to start at. Defaults to 1.
Returns ({ number }, number) — The numbers found, and the 1-based position just past them.
-- A JSON array of numbers, in one crossing instead of one per token.
local values, nextPos = stringx.scanNumbers(payload, afterBracket)
-- A whitespace-separated block (OBJ, PLY, a matrix dump).
local m = stringx.scanNumbers("1 0 0 0 0 1 0 0", 1)