我需要对 VBScript 中的字符串进行 URL 解码。该字符串可能包含根据 UTF-8 编码为多个字节的 Unicode 字符。因此,例如“Paris%20%E2%86%92%20Z%C3%BCrich”将解码为“Paris → Zürich”。
为了完成这项工作,我使用了如下所示的一段代码:
Function URLDecode(str)
set list = CreateObject("System.Collections.ArrayList")
strLen = Len(str)
for i = 1 to strLen
sT = mid(str, i, 1)
if sT = "%" then
if i + 2 <= strLen then
list.Add cbyte("&H" & mid(str, i + 1, 2))
i = i + 2
end if
else
list.Add asc(sT)
end if
next
depth = 0
for each by in list.ToArray()
if by and &h80 then
if (by and &h40) = 0 then
if depth = 0 then Err.Raise 5
val = val * 2 ^ 6 + (by and &h3f)
depth = depth - 1
if depth = 0 then
sR = sR & chrw(val)
val = 0
end if
elseif (by and &h20) = 0 then
if depth > 0 then Err.Raise 5
val = by and &h1f
depth = 1
elseif (by and &h10) = 0 then
if depth > 0 then Err.Raise 5
val = by and &h0f
depth = 2
else
Err.Raise 5
end if
else
if depth > 0 then Err.Raise 5
sR = sR & chrw(by)
end if
next
if depth > 0 then Err.Raise 5
URLDecode = sR
End Function
这似乎运作良好,但对我来说它看起来过于复杂。在 HTML5 和 web 标准时代,必须有一种更简单的方法来完成这项工作,而无需一堆手工制作的循环和条件。有什么建议么?