URLEncode: Difference between revisions

From Pickwiki
Jump to navigationJump to search
Stuboy (talk | contribs)
mNo edit summary
 
Stuboy (talk | contribs)
mNo edit summary
Line 1: Line 1:
HomePage>>SourceCode>>BasicSource>>URLEncode
HomePage>>SourceCode>>BasicSource>><nowiki>URLEncode</nowiki>


This is a function to correctly URLEncode a string.<br>
This is a function to correctly URLEncode a string.<br>
I wrote this when I was posting data into a SQL database using XML datagrams (very useful). The posted data had to be URL encoded and this was what I came up with.<br>
URL encoding of a character consists of a "%" symbol, followed by the two-digit hexadecimal representation (case-insensitive) of the ISO-Latin code point for the character.<br>
Because the data I was dealing with was only printable ascii < char(128) I have encoded only this range + uv delimiters for speed.
See http://www.permadi.com/tutorial/urlEncoding/ for more info.




<PRE>
<PRE>
function URLEncode(string)
function URLEncode(inString)


   *// perform standard URL encoding for UV ascii.
   *// perform standard URL encoding for UV ascii. ISO-8859-1 (ISO-Latin) character set.
   *// sboydell 17-Dec-2004
   *// stuart boydell 17-Dec-2004


   *// deffun URLEncode(string) [calling *URLEncode]
  *// basic example:
   *// deffun URLEncode(string) ;* calling *URLEncode ;*- use this if you globally catalogue the program
   *// encodedString = URLEncode(unencodedString)
   *// encodedString = URLEncode(unencodedString)


Line 18: Line 19:
   *// and reserved characters used for their reserved purposes may be  
   *// and reserved characters used for their reserved purposes may be  
   *// used unencoded within a URL.
   *// used unencoded within a URL.
  *// This process encodes unsafe characters to their URL safe
  *// equivalent. eg. char(2) -> "%02"
  *// First encodes char(37) "%" to %25 then loops through other ASCII.CODES ranges.


   *// ranges of ascii codes of all characters to be encoded
   *// ranges of ascii codes of all characters to be encoded
Line 31: Line 37:
   *// 128-255 Non-ASCII Characters ' ' Unsafe
   *// 128-255 Non-ASCII Characters ' ' Unsafe
    
    
   equ E.COMMA      to ',',
   *// 'Special' encoding characters
      E.PERCENT    to '%',
  equ E.PERCENT    to '%',
       E.MAXRANGE    to 9,
       E.SPACE      to ' ',
       E.ASCII.CODES to '37,37,32,36,38,44,47,47,58,64,91,94,96,96,123,128,250,255'
       E.PLUS        to '+'


   *// define pairs of ranges to encode
   *// 'Normal' encoding start/end characters (decimal ascii)
   dim ENCODE.RANGE(E.MAXRANGE,2)
   equ E.ASCII.START to (00:@am:34:@am:38:@am:47:@am:58:@am:91:@am:96:@am:123),
  matparse ENCODE.RANGE from E.ASCII.CODES, E.COMMA
      E.ASCII.END  to (31:@am:36:@am:38:@am:47:@am:64:@am:94:@am:96:@am:255),
      E.MAXRANGE    to 8


  *// convert 'special' encoding characters "%+[sp]"
  string = change(inString,E.PERCENT,E.PERCENT:'25')
  string = change(string,E.PLUS,E.PERCENT:'2B')
  string = change(string,E.SPACE,E.PLUS)
  *// now convert 'normal' character ranges
   for i = 1 to E.MAXRANGE
   for i = 1 to E.MAXRANGE
       for j = ENCODE.RANGE(i,1) to ENCODE.RANGE(i,2)
       for j = E.ASCII.START<i> to E.ASCII.END<i>
        string = change(string,char(j),E.PERCENT:dtx(j,2))
        jchar = char(j)  ;*// get char from decimal val
        *// index() character before change() - faster
        if index(string,jchar,1) then
            string = change(string,jchar,E.PERCENT:dtx(j,2))
        end
       next j
       next j
   next i
   next i


return(string)
return(string)
the:end


the:end


</PRE>
</PRE>
HomePage>>SourceCode>>BasicSource>><nowiki>URLEncode</nowiki>

Revision as of 01:13, 30 June 2006

HomePage>>SourceCode>>BasicSource>>URLEncode

This is a function to correctly URLEncode a string.
URL encoding of a character consists of a "%" symbol, followed by the two-digit hexadecimal representation (case-insensitive) of the ISO-Latin code point for the character.
See http://www.permadi.com/tutorial/urlEncoding/ for more info.


function URLEncode(inString)

   *// perform standard URL encoding for UV ascii. ISO-8859-1 (ISO-Latin) character set.
   *// stuart boydell 17-Dec-2004

   *// basic example:
   *// deffun URLEncode(string) ;* calling *URLEncode ;*- use this if you globally catalogue the program
   *// encodedString = URLEncode(unencodedString)

   *// Only alphanumerics [0-9a-zA-Z], the special characters $-_.+!*'(), 
   *// and reserved characters used for their reserved purposes may be 
   *// used unencoded within a URL.

   *// This process encodes unsafe characters to their URL safe
   *// equivalent. eg. char(2) -> "%02"

   *// First encodes char(37) "%" to %25 then loops through other ASCII.CODES ranges.

   *// ranges of ascii codes of all characters to be encoded
   *// 0-31 	ASCII Control Characters 	These characters are not printable 	Unsafe
   *// 32-47 	Reserved Characters 	' '!?#$%&'()*+,-./ 	Unsafe
   *// 48-57 	ASCII Characters and Numbers 	0-9 	Safe
   *// 58-64 	Reserved Characters 	:;<=>?@ 	Unsafe
   *// 65-90 	ASCII Characters 	A-Z 	Safe
   *// 91-96 	Reserved Characters 	[\]^_` 	Unsafe
   *// 97-122 	ASCII Characters 	a-z 	Safe
   *// 123-126 	Reserved Characters 	{|}~ 	Unsafe
   *// 127 	Control Characters 	' ' 	Unsafe
   *// 128-255 	Non-ASCII Characters 	' ' 	Unsafe
   
   *// 'Special' encoding characters
   equ E.PERCENT     to '%',
       E.SPACE       to ' ',
       E.PLUS        to '+'

   *// 'Normal' encoding start/end characters (decimal ascii)
   equ E.ASCII.START to (00:@am:34:@am:38:@am:47:@am:58:@am:91:@am:96:@am:123),
       E.ASCII.END   to (31:@am:36:@am:38:@am:47:@am:64:@am:94:@am:96:@am:255),
       E.MAXRANGE    to 8

   *// convert 'special' encoding characters "%+[sp]"
   string = change(inString,E.PERCENT,E.PERCENT:'25')
   string = change(string,E.PLUS,E.PERCENT:'2B')
   string = change(string,E.SPACE,E.PLUS)

   *// now convert 'normal' character ranges
   for i = 1 to E.MAXRANGE
      for j = E.ASCII.START<i> to E.ASCII.END<i>
         jchar = char(j)  ;*// get char from decimal val
         *// index() character before change() - faster
         if index(string,jchar,1) then
            string = change(string,jchar,E.PERCENT:dtx(j,2))
         end
      next j
   next i

return(string)
the:end






HomePage>>SourceCode>>BasicSource>>URLEncode