Jump to content

URL encoding: Difference between revisions

Added Algol 68
(Added Algol 68)
Line 51:
{{out}}
<pre>http%3A%2F%2Ffoo%20bar%2F</pre>
 
=={{header|ALGOL 68}}==
<lang algol68>BEGIN
# encodes the specified url - 0-9, A-Z and a-z are unchanged, #
# everything else is converted to %xx where xx are hex-digits #
PROC encode url = ( STRING url )STRING:
IF url = "" THEN "" # empty string #
ELSE
# non-empty string #
# ensure result will be big enough for a string of all encodable #
# characters #
STRING hex digits = "0123456789ABCDEF";
[ 1 : ( ( UPB url - LWB url ) + 1 ) * 3 ]CHAR result;
INT r pos := 0;
FOR u pos FROM LWB url TO UPB url DO
CHAR c = url[ u pos ];
IF ( c >= "0" AND c <= "9" )
OR ( c >= "A" AND c <= "Z" )
OR ( c >= "a" AND c <= "z" )
THEN
# no need to encode this character #
result[ r pos +:= 1 ] := c
ELSE
# must encode #
INT c code = ABS c;
result[ r pos +:= 1 ] := "%";
result[ r pos +:= 1 ] := hex digits[ ( c code OVER 16 ) + 1 ];
result[ r pos +:= 1 ] := hex digits[ ( c code MOD 16 ) + 1 ]
FI
OD;
result[ 1 : r pos ]
FI; # encode url #
# task test case #
print( ( encode url( "http://foo bar/" ), newline ) )
END
</lang>
{{out}}
<pre>
http%3A%2F%2Ffoo%20bar%2F
</pre>
 
=={{header|Apex}}==
3,048

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.