[svn:parrot] r47730 - in trunk: . config/gen/makefiles runtime/parrot/library/URI t/library

NotFound at svn.parrot.org NotFound at svn.parrot.org
Sun Jun 20 14:42:38 UTC 2010


Author: NotFound
Date: Sun Jun 20 14:42:37 2010
New Revision: 47730
URL: https://trac.parrot.org/parrot/changeset/47730

Log:
add basic URI escape functions to runtime library

Added:
   trunk/runtime/parrot/library/URI/
   trunk/runtime/parrot/library/URI/Escape.pir   (contents, props changed)
   trunk/t/library/uri_escape.t   (contents, props changed)
Modified:
   trunk/MANIFEST
   trunk/config/gen/makefiles/root.in

Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST	Sun Jun 20 13:26:06 2010	(r47729)
+++ trunk/MANIFEST	Sun Jun 20 14:42:37 2010	(r47730)
@@ -1,7 +1,7 @@
 # ex: set ro:
 # $Id$
 #
-# generated by tools/dev/mk_manifest_and_skip.pl Mon Jun 14 16:49:50 2010 UT
+# generated by tools/dev/mk_manifest_and_skip.pl Sun Jun 20 14:27:22 2010 UT
 #
 # See below for documentation on the format of this file.
 #
@@ -1219,6 +1219,7 @@
 runtime/parrot/library/Test/Class.pir                       [library]
 runtime/parrot/library/Test/More.pir                        [library]
 runtime/parrot/library/URI.pir                              [library]
+runtime/parrot/library/URI/Escape.pir                       [library]
 runtime/parrot/library/YAML/Dumper.pir                      [library]
 runtime/parrot/library/YAML/Dumper/Base.pir                 [library]
 runtime/parrot/library/YAML/Dumper/Default.pir              [library]
@@ -1725,6 +1726,7 @@
 t/library/test_class.t                                      [test]
 t/library/test_more.t                                       [test]
 t/library/uri.t                                             [test]
+t/library/uri_escape.t                                      [test]
 t/library/uuid.t                                            [test]
 t/library/yaml_dumper.t                                     [test]
 t/manifest/01-basic.t                                       [test]

Modified: trunk/config/gen/makefiles/root.in
==============================================================================
--- trunk/config/gen/makefiles/root.in	Sun Jun 20 13:26:06 2010	(r47729)
+++ trunk/config/gen/makefiles/root.in	Sun Jun 20 14:42:37 2010	(r47730)
@@ -320,6 +320,7 @@
     $(LIBRARY_DIR)/Tcl/Glob.pbc \
     $(LIBRARY_DIR)/TclLibrary.pbc \
     $(LIBRARY_DIR)/URI.pbc \
+    $(LIBRARY_DIR)/URI/Escape.pbc \
     $(LIBRARY_DIR)/uuid.pbc \
     $(LIBRARY_DIR)/YAML/Dumper/Base.pbc \
     $(LIBRARY_DIR)/YAML/Dumper/Default.pbc \

Added: trunk/runtime/parrot/library/URI/Escape.pir
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/runtime/parrot/library/URI/Escape.pir	Sun Jun 20 14:42:37 2010	(r47730)
@@ -0,0 +1,155 @@
+# Copyright (C) 2010, Parrot Foundation.
+# $Id$
+
+=head1 NAME
+
+URI/Escape - percent-encoding conversions
+
+=head1 DESCRIPTION
+
+Based on CPAN URL::Escape module.
+
+Sub names in the CPAN module are not used, reserved for eventual
+implementation more compatible.
+
+http://en.wikipedia.org/wiki/Percent_encoding
+
+=cut
+
+.namespace [ 'URI' ; 'Escape' ]
+
+.const string URIcomponentunchange = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"
+.const string URIunchange          = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~!*\'();:@&=+$,/?#[]"
+
+.const string hexdigits     = '0123456789ABCDEF'
+
+=head1 Subs
+
+=over 4
+
+=item percent_encode_except
+
+$S0 = 'percent_encode_except'(source, except)
+
+Return the percent encoding of the source string preserving the characters
+present in the except string.
+
+=cut
+
+.sub percent_encode_except
+    .param string s
+    .param string except
+
+    .local int asciicharset, utf8enc, scharset, senc
+    asciicharset = find_charset 'ascii'
+    scharset = charset s
+    if scharset == asciicharset goto encodeit
+
+    utf8enc = find_encoding 'utf8'
+    senc = encoding s
+    if utf8enc == senc goto encodeit
+    s = trans_encoding s, utf8enc
+
+  encodeit:
+    .local pmc buf, result, it
+    .local string c
+    .local int code
+    buf = root_new ['parrot';'ByteBuffer']
+    buf = s
+    result = root_new ['parrot';'ByteBuffer']
+    it = iter buf
+  nextbuf:
+    unless it goto endbuf
+    code = shift it
+    c = chr code
+    $I0 = index except, c
+    if $I0 == -1 goto isreserved
+    push result, code
+    goto nextbuf
+  isreserved:
+    push result, 0x25
+    $I0 = code / 16
+    c = substr hexdigits, $I0, 1
+    $I0 = ord c
+    push result, $I0
+    $I0 = code % 16
+    c = substr hexdigits, $I0, 1
+    $I0 = ord c
+    push result, $I0
+    goto nextbuf
+  endbuf:
+    s = result.'get_string_as'(ascii:"")
+  done:
+    .return(s)
+.end
+
+=item percent_encode
+
+$S0 = 'percent_encode'(source)
+
+Return the percent encoding of the source string preserving the URI unreserved
+characters. Intended for usage with full URIs.
+
+=cut
+
+.sub percent_encode
+    .param string s
+    .tailcall 'percent_encode_except'(s, URIunchange)
+.end
+
+=item percent_encode_component
+
+$S0 = 'percent_encode_component'(source)
+
+Return the percent encoding of the source string prserving the URI unreserved
+and URI reserved characters. Intended for usage with URIs components, such as
+form data in HTTP GET requests.
+
+=cut
+
+.sub percent_encode_component
+    .param string s
+    .tailcall 'percent_encode_except'(s, URIcomponentunchange)
+.end
+
+=item main
+
+A main function for testing purposes.
+
+Prints the percent_encode and percent_encode_component of the command line
+args provided.
+
+=cut
+
+.sub main :anon :main
+    .param pmc args :optional
+    .local pmc it
+    .local string arg, encoded
+    it = iter args
+    unless it goto end
+    arg = shift it
+  next:
+    unless it goto end
+    arg = shift it
+    encoded = 'percent_encode'(arg)
+    print "'"
+    print arg
+    print "' -> '"
+    print encoded
+    print "' , '"
+    encoded = 'percent_encode_component'(arg)
+    print encoded
+    print "'\n"
+    goto next
+  end:
+.end
+
+=back
+
+=cut
+
+# Local Variables:
+#   mode: pir
+#   fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4 ft=pir:

Added: trunk/t/library/uri_escape.t
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/t/library/uri_escape.t	Sun Jun 20 14:42:37 2010	(r47730)
@@ -0,0 +1,54 @@
+#!./parrot
+# Copyright (C) 2010, Parrot Foundation.
+# $Id$
+
+=head1 NAME
+
+t/library/uri_escape.t
+
+=head1 DESCRIPTION
+
+Test the URI Escape library
+
+=head1 SYNOPSIS
+
+    % prove t/library/uri_escape.t
+
+=cut
+
+.sub 'main' :main
+    .include 'test_more.pir'
+
+    load_bytecode 'URI/Escape.pbc'
+
+    plan(6)
+
+    $P0 = new ['Exporter']
+    $P1 = get_namespace ['URI'; 'Escape']
+    $P2 = get_namespace
+    $P0.'import'($P1 :named('source'), $P2 :named('destination'), 'percent_encode percent_encode_component' :named('globals'))
+
+    $S0 = percent_encode('Hello')
+    is($S0, 'Hello', 'plain ascii without special chars')
+
+    $S0 = percent_encode_component('Hello')
+    is($S0, 'Hello', 'plain ascii without special chars - component')
+
+    $S0 = percent_encode('Hello?world')
+    is($S0, 'Hello?world', 'plain ascii')
+
+    $S1 = percent_encode_component('Hello?world')
+    is($S1, 'Hello%3Fworld', 'plain ascii - component')
+
+    $S0 = percent_encode(iso-8859-1:"A\x{D1}O#a")
+    is($S0, 'A%C3%91O#a', 'iso-8859-1 string')
+
+    $S0 = percent_encode_component(iso-8859-1:"A\x{D1}O#a")
+    is($S0, 'A%C3%91O%23a', 'iso-8859-1 string - component')
+.end
+
+# Local Variables:
+#   mode: pir
+#   fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4 ft=pir:


More information about the parrot-commits mailing list