URLEncode

Share your advanced PureBasic knowledge/code with the community.
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

URLEncode

Post by Bonne_den_kule »

Code updated For 5.20+

Code: Select all

Procedure.s URLEncode(string.s)
  char.s
  urlstring.s
  For i=1 To Len(string)
    char = Mid(string,i,1)
    If FindString("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()", UCase(char), 1) > 0
      urlstring+char
      Continue
    ElseIf char=" "
      urlstring+"+"
      Continue
    Else
      urlstring+"%"+RSet(Hex(Asc(char)),2,"0")
    EndIf
  Next
  ProcedureReturn urlstring
EndProcedure
EDIT:
Fixed a bug
Last edited by Bonne_den_kule on Fri Mar 03, 2006 12:19 am, edited 2 times in total.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Couldn't resist... :)

Code: Select all

Procedure.s URLEncode2(string.s)
  SEnd = @string+Len(string)
  *Seeker.BYTE = @string
  While *Seeker<SEnd
    Select *Seeker\b
      Case 32
        urlstring$+"+"
      Case 48 To 57, 65 To 90, 97 To 122
        urlstring$+Chr(*Seeker\b)
      Default
        urlstring$+"%"+Right(Hex(*Seeker\b), 2)
    EndSelect
    *Seeker+1
  Wend
  ProcedureReturn urlstring$
EndProcedure
El_Choni
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

@El_Choni!
Compiling your source with PB4b4 returns an ASM error here
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Yes, Trond reported that in the bug section. As a workaround, until this is fixed, you use this:

Code: Select all

Procedure.s URLEncode2(string.s)
  SEnd = @string+Len(string)
  *Seeker.BYTE = @string
  While *Seeker<SEnd
    Select *Seeker\b
      Case 32
        urlstring$+"+"
      Case 48 To 57, 65 To 90
        urlstring$+Chr(*Seeker\b)
      Case 97 To 122
        urlstring$+Chr(*Seeker\b)
      Default
        urlstring$+"%"+Right(Hex(*Seeker\b), 2)
    EndSelect
    *Seeker+1
  Wend
  ProcedureReturn urlstring$
EndProcedure 
El_Choni
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

And neither of your sources does a correct URLEncode.

The following (ranges of) characters should NOT be escaped:
A..Z
0..9
-
_
.
!
~
*
'
(
)
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Now?:

Code: Select all

Procedure.s URLEncode2(string.s)
  SEnd = @string+Len(string)
  *Seeker.BYTE = @string
  While *Seeker<SEnd
    Select *Seeker\b
      Case 32
        urlstring$+"+"
      Case 48 To 57, 65 To 90
        urlstring$+Chr(*Seeker\b)
      Case 97 To 122
        urlstring$+Chr(*Seeker\b)
      Case '-','_','.','!','~','*',39,'(',')'
        urlstring$+Chr(*Seeker\b)
      Default
        urlstring$+"%"+Right(Hex(*Seeker\b), 2)
    EndSelect
    *Seeker+1
  Wend
  ProcedureReturn urlstring$
EndProcedure 
El_Choni
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

And it isn't unicode enabled :lol:
This should be though I didn't test very much:

Code: Select all

Procedure.s URLEncode2(string.s)
  SEnd = @string+Len(string)
  *Seeker.CHARACTER = @string
  Repeat
    Select *Seeker\c
      Case 32
        urlstring$+"+"
      Case 48 To 57, 65 To 90
        urlstring$+Chr(*Seeker\c)
      Case 97 To 122
        urlstring$+Chr(*Seeker\c)
      Case '-','_','.','!','~','*',39,'(',')'
        urlstring$+Chr(*Seeker\c)
      Default
        urlstring$+"%"+Right(Hex(*Seeker\c), 2)
    EndSelect
    *Seeker+SizeOf(CHARACTER)
  Until *Seeker>=@string+(Len(String)*SizeOf(Character))
  ProcedureReturn urlstring$
EndProcedure 
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

And it could be simpler/shorter :

Code: Select all

Procedure.s URLEncode2(string.s)
  *Seeker.CHARACTER = @string
  Repeat
    Select *Seeker\c
      Case 32
        urlstring$+"+"
      Case 48 To 57, 65 To 90
        urlstring$+Chr(*Seeker\c)
      Case 97 To 122
        urlstring$+Chr(*Seeker\c)
      Case '-','_','.','!','~','*',39,'(',')'
        urlstring$+Chr(*Seeker\c)
      Default
        urlstring$+"%"+Right(Hex(*Seeker\c), 2)
    EndSelect
    *Seeker+SizeOf(CHARACTER)
  Until *Seeker\c = 0
  ProcedureReturn urlstring$
EndProcedure 
Last edited by Flype on Fri Mar 03, 2006 12:37 am, edited 1 time in total.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

My ownage code :lol: :

Code: Select all

Procedure.s URLEncode(string.s)
  char.s
  urlstring.s
  For i=1 To Len(string)
    char = Mid(string,i,1)
    If FindString("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()", UCase(char), 1) > 0
      urlstring+char
      Continue
    ElseIf char=" "
      urlstring+"+"
      Continue
    Else
      urlstring+"%"+RSet(Hex(Asc(char)),2,"0")
    EndIf
  Next
  ProcedureReturn urlstring
EndProcedure
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

Can someone compare the speed of all these URLEncode functions?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

There is also an API function called UrlEncode() that should do this.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

But ours are better :wink:
El_Choni
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

Our codes are pure...
:lol:
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

:P
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

El_Choni wrote:But ours are better :wink:
why are your codes better than an api call? :?: just wondering...
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Post Reply