Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: off by ten error

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
titleRecent feature

This is a new feature (available in the engine starting at v0.45.5), which allows the operator replace to accept a function as its second argument. The function is passed the match and then the capture group and returns a string to be used as the replacement string. Because of the g in the regular expression, it will be called for each match.

The function hex2char could be written as:

Code Block
linenumberstrue
    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
linenumberstrue
    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
    }

...

If all is well, we determine (line 6) whether the character is in the range "0" to "9" or not. If so, we subtract the code for "0" from its code to produce a number in the range 0 to 9. Otherwise, we subtract the code for "a" from its code and add ten to produce a number in the range 10 to 15, and one of these numbers will be returned as the value of the hexDigit2dec function.

...