Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,4 +1,5 @@
This task (the reverse of [[URL encoding]]) is to provide a function or mechanism to convert an URL-encoded string into its original unencoded form.
This task (the reverse of [[URL encoding]]) is to provide a function
or mechanism to convert an URL-encoded string into its original unencoded form.
'''Example'''

View file

@ -0,0 +1,3 @@
package URL is
function Decode (URL : in String) return String;
end URL;

View file

@ -0,0 +1,28 @@
package body URL is
function Decode (URL : in String) return String is
Buffer : String (1 .. URL'Length);
Filled : Natural := 0;
Position : Positive := URL'First;
begin
while Position in URL'Range loop
Filled := Filled + 1;
case URL (Position) is
when '+' =>
Buffer (Filled) := ' ';
Position := Position + 1;
when '%' =>
Buffer (Filled) :=
Character'Val
(Natural'Value
("16#" & URL (Position + 1 .. Position + 2) & "#"));
Position := Position + 3;
when others =>
Buffer (Filled) := URL (Position);
Position := Position + 1;
end case;
end loop;
return Buffer (1 .. Filled);
end Decode;
end URL;

View file

@ -0,0 +1,16 @@
with Ada.Command_Line,
Ada.Text_IO;
with URL;
procedure Test_URL_Decode is
use Ada.Command_Line, Ada.Text_IO;
begin
if Argument_Count = 0 then
Put_Line (URL.Decode ("http%3A%2F%2Ffoo%20bar%2F"));
else
for I in 1 .. Argument_Count loop
Put_Line (URL.Decode (Argument (I)));
end loop;
end if;
end Test_URL_Decode;

View file

@ -0,0 +1,15 @@
import qualified Data.Char as Char
urlDecode :: String -> Maybe String
urlDecode [] = Just []
urlDecode ('%':xs) =
case xs of
(a:b:xss) ->
urlDecode xss
>>= return . ((Char.chr . read $ "0x" ++ [a,b]) :)
_ -> Nothing
urlDecode ('+':xs) = urlDecode xs >>= return . (' ' :)
urlDecode (x:xs) = urlDecode xs >>= return . (x :)
main :: IO ()
main = putStrLn . maybe "Bad decode" id $ urlDecode "http%3A%2F%2Ffoo%20bar%2F"

View file

@ -0,0 +1 @@
StringTools:-Decode("http%3A%2F%2Ffoo%20bar%2F", encoding=percent);

View file

@ -0,0 +1,7 @@
;; universal decoder, works for ASCII and UTF-8
;; (source http://www.newlisp.org/index.cgi?page=Code_Snippets)
(define (url-decode url (opt nil))
(if opt (replace "+" url " "))
(replace "%([0-9a-f][0-9a-f])" url (pack "b" (int $1 0 16)) 1))
(url-decode "http%3A%2F%2Ffoo%20bar%2F")

View file

@ -0,0 +1 @@
URLdecode("http%3A%2F%2Ffoo%20bar%2F")

View file

@ -1,4 +1,16 @@
import java.net._
val encoded="http%3A%2F%2Ffoo%20bar%2F"
val decoded=URLDecoder.decode(encoded, "UTF-8")
println(decoded) // -> http://foo bar/
import java.net.{URLDecoder, URLEncoder}
import scala.compat.Platform.currentTime
object UrlCoded extends App {
val original = """http://foo bar/"""
val encoded: String = URLEncoder.encode(original, "UTF-8")
assert(encoded == "http%3A%2F%2Ffoo+bar%2F", s"Original: $original not properly encoded: $encoded")
val percentEncoding = encoded.replace("+", "%20")
assert(percentEncoding == "http%3A%2F%2Ffoo%20bar%2F", s"Original: $original not properly percent-encoded: $percentEncoding")
assert(URLDecoder.decode(encoded, "UTF-8") == URLDecoder.decode(percentEncoding, "UTF-8"))
println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]")
}

View file

@ -0,0 +1,14 @@
Function UrlDecode(s)
For i = 1 To Len(s)
If Mid(s,i,1) = "%" Then
UrlDecode = UrlDecode & Chr("&H" & Mid(s,i+1,2))
i = i + 2
Else
UrlDecode = UrlDecode & Mid(s,i,1)
End If
Next
End Function
url = "http%3A%2F%2Ffoo%20bar%2F"
WScript.Echo "Encoded URL: " & url & vbCrLf &_
"Decoded URL: " & UrlDecode(url)