URL Escape and Unescape

JavaScript contains a handy built-in function, 'escape', that encodes text so that it can appear as part of a URL. For instance, the space character is changed to %20, ampersand to %3B, and so on. The 'unescape' function converts these "percent" codes back to their original representation. These are helpful functions to have available when you write Domino agents. These agents could use unescape to read arguments from their own URLs, and use escape when they need to print a new URL when they finish execution.


Function escape(Byval s$) As String
' Scan a string for characters that aren't OK to appear in a URL and change them to "%" hex codes.
' This is intended to be as nearly as possible a match for the same name function in JavaScript.
Dim i%, result$, c$
For i = 1 To Len(s)
c = Mid$(s, i, 1)
If Instr(" ""#%&/:;<=>?@[]^`{|}~+", c) = 0 Then
' these are the special URL characters as identified by HTML reference books, and + which Notes uses to represent a space. If this char is not one of these, we can leave as is.
result = result & c
result = result & "%" & Right$("0" & Hex(Uni(c)), 2)
End If
escape = result
End Function


Function unescape(Byval s$) As String
' Given a string that contains "%" codes for spaces and other punctuation, as they would appear in a URL, this function converts them back to the original string using the Uchr$ function.
Dim pos%, lpos%, ccod$, result$
lpos = 1
pos = Instr(s, "%")
Do Until pos = 0
' the characters up until the % are ones we can just add to the string.
If pos > lpos Then result = result & Mid$(s, lpos, pos-lpos)
ccod = Mid$(s, pos+1, 2) ' get the hex code of the character
If ccod Like "[a-fA-F0-9][a-fA-F0-9]" Then
result = result & Uchr$(Cint("&H" & ccod))
lpos = pos + 3
result = result & "%"
lpos = pos + 1
End If
pos = Instr(lpos, s, "%")
unescape = result & Mid$(s, lpos)
End Function

Written by Andre Guirard

Posted by fbrefere001 on Thursday August 28, 2014