This function decodes form-encoded content (e.g. "q=5&value=%20space%20"
)
...
The string operator replace
, finds an occurrence of its first argument, a regular expression, in the string and replaces it with the second argment string value. When the regular expression has the modifier g
then all matching occurrences are replaced. According to a post in stackoverflow, "Encoders used the "+" character as a replacement for a space character in the early days", so we'll replace them first (line 2), and then do %20
(line 3). Actual plus signs would appear as %2B
(or %2b
).
Note | ||
---|---|---|
| ||
This is a new feature (available in the engine starting at v0.45.5), which allows the operator |
The function hex2char
could be written as:
Code Block | ||
---|---|---|
| ||
hex2char = function(encoded,hex) { // string len 2 -> character or encoded val = hex2dec(hex); hex.length() == 2 && val && val < 128 => (val).chr() | encoded } |
We get the decimal equivalent of the string passed in, which would be in hexadecimal, in line 2. Line 3 verifies that everything is valid, and returns a string with a single character (line 4), using the number operator chr
. If something is wrong, we'll return instead, the value that we were asked to replace (line 5), in this case, %20
, leaving the caller's string unchanged.
The function hex2dec could be written as:
Code Block | ||
---|---|---|
| ||
hex2dec = function(hex) { // string len k -> number in [0,16^k-1] or null hex like re#^[0-9A-Fa-f]*$# => hex.split("") .map(function(h){hexDigit2dec(h)}) .reduce(function(a,h){a*16+h},0) | null } |
...
We return the value zero if we were given a character which is not a hexadecimal digit.
Ruleset
A ruleset with this code can be found at https://raw.githubusercontent.com/Picolab/pico_lessons/master/decode_content/edu.byu.picos.www.krl