Sorry for multiple postings, but I want to keep you guys up to date on the problem.
I managed to create working functions that always do the same in ASCII and Unicode mode:
Code: Select all
Procedure.s URLEncode(URL$)
Protected Result$, Char$, Char, Index, *Buffer
For Index = 1 To Len(URL$)
Char$ = Right(Left(URL$, Index), 1)
Char = Asc(Char$)
If Char < 128
If (Char > 32 And Char <> 37 And Char <> 34 And Char <> 60 And Char < 62) Or (Char > 62 And Char < 91) Or (Char > 94 And Char <> 96 And Char < 123) Or (Char = 126)
Result$ + Char$
Else
Result$ + "%" + RSet(Hex(Char), 2, "0")
EndIf
Else
*Buffer = AllocateMemory(4)
If *Buffer
PokeS(*Buffer, Char$, -1, #PB_UTF8)
Result$ + "%" + RSet(Hex(PeekB(*Buffer)&$FF), 2, "0") + "%" + RSet(Hex(PeekB(*Buffer+1)&$FF), 2, "0")
FreeMemory(*Buffer)
EndIf
EndIf
Next
ProcedureReturn Result$
EndProcedure
Procedure.s URLDecode(URL$)
Protected Result$, Char$, Char, Index, *Buffer
Result$ = URL$
Index = FindString(URL$, "%", 1)
If Index
Repeat
Char$ = Right(Left(Result$, Index+2), 2)
Char = Val("$"+Char$)
If Char < 128
Result$ = ReplaceString(Result$, "%"+Char$, Chr(Char))
ElseIf Right(Left(Result$, Index+3), 1) = "%"
*Buffer = AllocateMemory(4)
If *Buffer
PokeB(*Buffer, Char)
PokeB(*Buffer+1, Val("$"+Right(Left(Result$, Index+5), 2)))
Result$ = ReplaceString(Result$, Right(Left(Result$, Index+5), 6), PeekS(*Buffer, -1, #PB_UTF8))
FreeMemory(*Buffer)
EndIf
EndIf
Index = FindString(Result$, "%", Index+1)
Until Not Index
EndIf
ProcedureReturn Result$
EndProcedure
Procedure.s DummyString1()
Protected Result$, Char
For Char = 32 To 126
Result$ + Chr(Char)
Next
ProcedureReturn Result$
EndProcedure
Procedure.s DummyString2()
Protected Result$, Char
For Char = 128 To 255
Result$ + Chr(Char)
Next
ProcedureReturn Result$
EndProcedure
DummyString1$ = DummyString1()
DummyString2$ = DummyString2()
If URLEncoder(DummyString1$) = URLEncode(DummyString1$)
Debug "ASCII Test 1 passed!"
Else
Debug "ASCII Test 1 failed!"
EndIf
If URLDecode(URLEncode(DummyString1$)) = DummyString1$
Debug "ASCII Test 2 passed!"
Else
Debug "ASCII Test 2 failed!"
EndIf
If URLDecode(URLEncode(DummyString2$)) = DummyString2$
Debug "UTF8 Test passed!"
Else
Debug "UTF8 Test failed!"
EndIf
Fred, any chance to have PB's functions updated?
The only problem that still exists is that you can't use LCase or UCase with the returned string in Unicode mode
