dreamworkAPI
Ctrl K
GitHub
libraryDocs / percent

percent

librarysharedmenusource

Percent (URL/URI) encoding/decoding library.

Percent-encoding is a mechanism to encode 8-bit characters that have specific meaning in the context of URLs.

It is sometimes called URL encoding.

The encoding consists of substitution: A '%' followed by the hexadecimal representation of the ASCII value of the replace character.

See https://en.wikipedia.org/wiki/Percent-encoding & https://datatracker.ietf.org/doc/html/rfc3986#section-2.1

Functions

percent.decodeDecodes the specified string from percent encoding.
percent.encodeEncodes the specified string to percent encoding.
percent.validateValidates the specified percent string.
percent.whitelistCreates a character whitelist for the given pattern.

#percent.decode

sharedmenunodiscardsource
function percent.decode(percent_str: string, ignore_spaces?: boolean): string

Decodes the specified string from percent encoding.

Invalid or incomplete "%" sequences are kept as is.

lua
percent.decode( "a+b%26c" ) -- "a b&c"
percent.decode( "a+b%26c", true ) -- "a+b&c"
Parameters
NameTypeDescription
percent_strstringThe string to decode.
ignore_spacesoptionalbooleanIf true, "+" is kept as is instead of being decoded as a space, optional.
Returns
TypeNameDescription
stringraw_strThe decoded string.

#percent.encode

sharedmenunodiscardsource
function percent.encode(raw_str: string, whitelist?: table<integer, boolean>, ignore_spaces?: boolean): string

Encodes the specified string to percent encoding.

Bytes not in the whitelist are encoded as "%XX" (upper-case hex), spaces are encoded as "+" unless ignore_spaces is true.

lua
percent.encode( "a b&c" ) -- "a+b%26c"
percent.encode( "a b&c", nil, true ) -- "a%20b%26c"
Parameters
NameTypeDescription
raw_strstringThe string to encode.
whitelistoptionaltable<integer, boolean>The character whitelist, bytes in it are left as is, optional.
ignore_spacesoptionalbooleanIf true, spaces are encoded as "%20" instead of "+", optional.
Returns
TypeNameDescription
stringpercent_strThe encoded string.

#percent.validate

sharedmenunodiscardsource
function percent.validate(percent_str: string, whitelist?: table<integer, boolean>, ignore_spaces?: boolean): boolean, nil | string

Validates the specified percent string.

A valid string contains only whitelisted bytes, complete "%XX" sequences and, unless ignore_spaces is true, "+".

Parameters
NameTypeDescription
percent_strstringThe percent string to validate.
whitelistoptionaltable<integer, boolean>The character whitelist, optional.
ignore_spacesoptionalbooleanIf true, "+" is not allowed unless whitelisted, optional.
Returns
TypeNameDescription
booleanis_validtrue if the percent string is valid, otherwise false.
nil | stringerr_msgThe error message.

#percent.whitelist

sharedmenunodiscardsource
function percent.whitelist(pattern_str: string, base?: table): table<integer, boolean>

Creates a character whitelist for the given pattern.

Parameters
NameTypeDescription
pattern_strstringThe pattern.
baseoptionaltableThe base table, optional.
Returns
TypeNameDescription
table<integer, boolean>whitelistThe character whitelist.
Generated from the dreamwork meta branch.