17.6.2.2375 UNESCAPE STRING EXT

( c-addr1 u1 c-addr2 -- c-addr2 u2 )

Replace each `%' character in the input string c-addr1 u1 by two `%' characters. The output is represented by c-addr2 u2. The buffer at c-addr2 shall be big enough to hold the unescaped string. An ambiguous condition occurs if the resulting string will not fit into the destination buffer (c-addr2).

See:

Implementation:

: UNESCAPE \ c-addr1 len1 c-addr2 -- c-addr2 len2
\ Replace each '%' character in the input string c-addr1 len1 with two '%' characters.
\ The output is represented by c-addr2 len2.
\ If you pass a string through UNESCAPE and then SUBSTITUTE, you get the original string.
   DUP 2SWAP OVER + SWAP ?DO
     I C@ [CHAR] % = IF
       [CHAR] % OVER C! 1+
     THEN
     I C@ OVER C! 1+
   LOOP
   OVER -
;

Testing:

Using subbuff, sub5 and sub6 from F.17.6.2.2255 SUBSTITUTE.

T{ sub6 subbuff UNESCAPE sub5 COMPARE -> 0 }T

ContributeContributions

ruvavatar of ruv [85] Etymology and naming issueRequest for clarification2019-06-25 09:18:19

Why this word was called unescape?

Usually escape function does what this word — it replaces some characters with an encoded form, and unescape function does reverse transformation — it replaces an encoded form with its original. For example, see in JavaScript, Python, Rust, T-SQL, etc.

Did the committee have examples when the conventional Forth practice for unescape word was so opposite to common usage of escape and unescape functions in many other languages?

AntonErtlavatar of AntonErtl

I don't remember how we came up with the name, but I think the idea was that, after processing the result with SUBSTITUTE, the % characters would not work as escapes, but be unchanged wrt the input to UNESCAPE.

IIRC we did not look at other languages when choosing this name. There was no Forth practice for UNESCAPE, and actually only single-vendor practice for SUBSTITUTE, but experiences with this kind of string processing in other contexts suggested that the SUBSTITUTE proposal was missing the UNESCAPE functionality.

Reply New Version