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 標準の時代には、手作りのループや条件を使わずにこれを行うためのより簡単な方法が必要です。助言がありますか?