segunda-feira, 26 de julho de 2010

Função Unescape

Decodifica uma string codificada com o método escape.

Function LSunescape(strIn As String) As String
Dim i As Integer
Dim strChar As String
Dim strReturn As String

i = 1
strReturn = ""
While Not (i > Len(strIn))
strChar = Mid$(strIn, i, 1)
If Not strChar = "%" Then
strReturn = strReturn & strChar
Else
i = i + 1
strChar = "&H" & Mid$(strIn, i, 2)
strReturn = strReturn & Chr$(Val(strChar))
i = i + 1
End If
i = i + 1
Wend
LSunescape = strReturn
End Function


Function UnEscape(s As String) As String


Dim L As String
Dim M As String
Dim R As String
Dim P As Integer

P% = Instr(s,"%")
If P%>0 Then
L$=Left$(s,P%-1)
M$=Mid$(s,P%+1,2)
R$=Right$(s,Len(s)-(P%+2))
UnEscape = L$ & Chr(Cint("&h" & M$)) & R$
If Instr(UnEscape,"%") Then UnEscape = UnEscape(UnEscape)
Else
UnEscape = s
End If

Exit Function

End Function


Function Unescape(Byval s As String) As String

Dim iPercent As Integer
Dim iPlus As Integer
Dim sL As String
Dim sM As String
Dim sR As String

iPercent = Instr(s,"%")
iPlus = Instr(s,"+")
If iPlus = 0 Then
If iPercent = 0 Then 'neither percent nor plus was found.
Unescape = s
Else 'plus not found, deal with percent
sL = Left$(s,iPercent-1)
sM = Mid$(s,iPercent+1,2)
sR = Mid$(s, iPercent+3)
Unescape = sL + Chr$(Cint("&h" & sM)) + Unescape(sR)
End If
Elseif iPercent = 0 Or iPlus < iPercent Then 'percent doesn't exist or plus comes before percent, deal with plus
sL = Left$(s, iPlus - 1)
sR = Mid$(s, iPlus + 1)
Unescape = sL + " " + Unescape(sR)
Else 'percent comes before plus, deal with percent
sL = Left$(s,iPercent-1)
sM = Mid$(s,iPercent+1,2)
sR = Mid$(s, iPercent+3)
Unescape = sL + Chr$(Cint("&h" & sM)) + Unescape(sR)
End If

End Function

Nenhum comentário:

Postar um comentário