I was also looking for a good way to decode strings in LotusScript. The formula above does work, but doesn't handle UTF-8 always correctly. It fails when the string to be decoded contains 2 or 3 escape sequences for a single character. A ü (u-umlaut) for instance is replaced by a %C3%BC sequence when encoded to UTF-8 with the (JavaScript) encodeURIComponent function.
Another problem with the function above is bad performance when decoding large strings due all the string concatenations (see also http://blog.lotusnotes.be/domino/archive/2008-01-14-concatenate-string-test.html)
If you use @URLDecode("utf-8", "%C3%BC") the string is correctly decoded to the original character but as Rick already pointed out, that function is limited in LotusScript. Tests showed that the limit for the input string when calling @URLDecode from LotusScript using Evaluate is 2048 characters.
To work around this I wrote a LotusScript function that uses Evaluate(@URLDecode), but splits the input string in chunks of 2048 characters (or less) and also handles the situation that the input string is split in the middle of an escape sequence. The only situation in which it still incorrectly decodes the input string is when it is split in the middle of multiple escape sequences for a single character.
Public Function decodeURI$(txt$)
'decodes a UTF-8 encoding string
decodeURI = txt
'replace + by spaces first: @URLDecode doesn't conver these back to spaces
If Instr(decodeURI, "+")>0 Then
decodeURI = Replace(decodeURI, "+", " ")
End If
Const MAX_LENGTH = 2048
'NOTE: evaluate has an input limit of 2 Kb (len = 2048)
I was also looking for a good way to decode strings in LotusScript. The formula above does work, but doesn't handle UTF-8 always correctly. It fails when the string to be decoded contains 2 or 3 escape sequences for a single character. A ü (u-umlaut) for instance is replaced by a %C3%BC sequence when encoded to UTF-8 with the (JavaScript) encodeURIComponent function.
Another problem with the function above is bad performance when decoding large strings due all the string concatenations (see also http://blog.lotusnotes.be/domino/archive/2008-01-14-concatenate-string-test.html)
If you use @URLDecode("utf-8", "%C3%BC") the string is correctly decoded to the original character but as Rick already pointed out, that function is limited in LotusScript. Tests showed that the limit for the input string when calling @URLDecode from LotusScript using Evaluate is 2048 characters.
To work around this I wrote a LotusScript function that uses Evaluate(@URLDecode), but splits the input string in chunks of 2048 characters (or less) and also handles the situation that the input string is split in the middle of an escape sequence. The only situation in which it still incorrectly decodes the input string is when it is split in the middle of multiple escape sequences for a single character.
Public Function decodeURI$(txt$)
'decodes a UTF-8 encoding string
decodeURI = txt
'replace + by spaces first: @URLDecode doesn't conver these back to spaces
If Instr(decodeURI, "+")>0 Then
decodeURI = Replace(decodeURI, "+", " ")
End If
Const MAX_LENGTH = 2048
'NOTE: evaluate has an input limit of 2 Kb (len = 2048)
if Len(decodeURI) <= MAX_LENGTH then
decodeURI = Join( Evaluate( {@URLDecode("utf-8"; "} & decodeURI & {")} ), "" )
Else
Dim s As New NotesSession
Dim stream As NotesSTream
Set stream = s.CreateStream
Dim pStart As Long
Dim part As String, i As Integer, intLen As Integer, char As String
pStart = 1
While pStart <= Len(decodeURI)
part = Mid$( decodeURI, pStart, MAX_LENGTH )
intLen = Len(part)
For i=1 To 2
char = Left(Right(part, i), 1)
If char = "%" Then 'dont split in the middle of an encoded char
part = Left( part, intLen-i )
intLen = Len(part)
Exit For
End If
Next
stream.WriteText Join( Evaluate( {@URLDecode("utf-8"; "} & part & {")} ) )
pStart = pStart + intLen 'new start position
Wend
stream.Position = 0
decodeURI = stream.ReadText
end if
End Function