#include once "window9.bi"

#ifdef UNICODE
	Function UrlEncoder(ByRef sUrl As USTRING) As USTRING Export
		Dim sRetStr As USTRING
		dim as string sUTF8 = sUrl.UTF8
		For i As Integer = 0 To Len(sUTF8)-1
			Select case sUTF8[i] 
				Case 45 To 58,65 To 90,97 To 122,95
					sRetStr &= Chr(sUTF8[i])
				Case Else
					sRetStr &= "%" & Hex(sUTF8[i])      
			End Select
		Next		
		Return sRetStr
	End Function
	
	Function UrlDecoder(ByRef sUrl As USTRING) As USTRING Export
		Dim As USTRING sRetStr, sTemp
		dim as string sUTF8
		For i As Integer = 1 To Len(sUrl)
			sTemp = Mid(sUrl,i,1)
			Select case sTemp 
				case "%"
					sTemp =  "&h" & Mid(sUrl,i+1,2)
					sUTF8 &= Chr(Val(sTemp))
					i+=2
				Case Else
					sUTF8 &= sTemp      
			End Select
		Next
		sRetStr.UTF8 = sUTF8
		Return sRetStr
	End Function	
#else	
	Function UrlEncoder(ByRef sUrl As USTRING) As USTRING Export
		Dim sRetStr As USTRING
		For i As Integer = 0 To Len(sUrl)-1
			Select case sUrl[i] 
				Case 45 To 58,65 To 90,97 To 122,95
					sRetStr &= Chr(sUrl[i])
				Case Else
					sRetStr &= "%" & Hex(sUrl[i])      
			End Select
		Next
		Return sRetStr
	End Function
	
	Function UrlDecoder(ByRef sUrl As USTRING) As USTRING Export
		Dim As USTRING sRetStr, sTemp
		For i As Integer = 1 To Len(sUrl)
			sTemp = Mid(sUrl,i,1)
			Select case sTemp 
				Case "%"
					sTemp =  "&h" & Mid(sUrl,i+1,2)
					sRetStr &= Chr(Val(sTemp))
					i+=2
				Case Else
					sRetStr &= sTemp      
			End Select
		Next
		Return sRetStr
	End Function	
#EndIf
