]> gitweb.factorcode.org Git - factor.git/commitdiff
urls.encoding: support byte-array values for encoding.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 9 Jan 2020 22:17:01 +0000 (14:17 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 9 Jan 2020 22:17:01 +0000 (14:17 -0800)
basis/urls/encoding/encoding-docs.factor
basis/urls/encoding/encoding-tests.factor
basis/urls/encoding/encoding.factor

index 33cfa70ea78a122ef67d6c8c511c7239e371538e..cbd95aead2300eb19a615e716e3e0f2eeab150e1 100644 (file)
@@ -6,11 +6,11 @@ HELP: url-decode
 { $description "Decodes a URL-encoded string." } ;
 
 HELP: url-encode
-{ $values { "str" string } { "encoded" string } }
+{ $values { "obj" object } { "encoded" string } }
 { $description "URL-encodes a string, excluding certain characters, such as \"/\"." } ;
 
 HELP: url-encode-full
-{ $values { "str" string } { "encoded" string } }
+{ $values { "obj" object } { "encoded" string } }
 { $description "URL-encodes a string, including all reserved characters, such as \"/\"." } ;
 
 HELP: url-quotable?
index 0aaecf564d01f641892c37959a0269fa0ee631bb..bdb41cc97962a9102e9b6d8f374c273b90bac0f6 100644 (file)
@@ -17,6 +17,9 @@ accessors linked-assocs ;
 { ":foo" } [ ":foo" url-encode ] unit-test
 { "%3Afoo" } [ ":foo" url-encode-full ] unit-test
 
+{ "%01%02%03ABC" } [ B{ 1 2 3 65 66 67 } url-encode ] unit-test
+{ "%01%02%03ABC" } [ B{ 1 2 3 65 66 67 } url-encode-full ] unit-test
+
 { "hello world" } [ "hello+world" query-decode ] unit-test
 
 { "\u001234hi\u002045" } [ "\u001234hi\u002045" url-encode url-decode ] unit-test
index 45a7b103b4fc901117997ad81ab3375776f75385..33db055b48a964b5fd2e75f16d9eb1cd252ed2e0 100644 (file)
@@ -1,8 +1,9 @@
 ! Copyright (C) 2008, 2010 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: arrays ascii assocs combinators combinators.short-circuit
-fry io.encodings.string io.encodings.utf8 kernel linked-assocs
-make math math.parser present sequences splitting strings ;
+USING: arrays ascii assocs byte-arrays combinators
+combinators.short-circuit fry io.encodings.string
+io.encodings.utf8 kernel linked-assocs make math math.parser
+present sequences splitting strings ;
 IN: urls.encoding
 
 : url-quotable? ( ch -- ? )
@@ -34,19 +35,28 @@ IN: urls.encoding
 
 <PRIVATE
 
+: hex% ( n -- )
+    CHAR: % , >hex >upper 2 CHAR: 0 pad-head % ;
+
 : push-utf8 ( ch -- )
-    1string utf8 encode
-    [ CHAR: % , >hex >upper 2 CHAR: 0 pad-head % ] each ;
+    1string utf8 encode [ hex% ] each ;
 
 : (url-encode) ( str quot: ( ch -- ? ) -- encoded )
-    '[ [ dup @ [ , ] [ push-utf8 ] if ] each ] "" make ; inline
+    [
+        over byte-array? [
+            '[ dup @ [ , ] [ hex% ] if ] each
+        ] [
+            [ present ] dip
+            '[ dup @ [ , ] [ push-utf8 ] if ] each
+        ] if
+    ] "" make ; inline
 
 PRIVATE>
 
-: url-encode ( str -- encoded )
+: url-encode ( obj -- encoded )
     [ url-quotable? ] (url-encode) ;
 
-: url-encode-full ( str -- encoded )
+: url-encode-full ( obj -- encoded )
     [ unreserved? ] (url-encode) ;
 
 <PRIVATE
@@ -105,7 +115,7 @@ PRIVATE>
         [
             [ url-encode-full ] dip [
                 dup array? [ 1array ] unless
-                [ present url-encode-full "=" glue , ] with each
+                [ url-encode-full "=" glue , ] with each
             ] [ , ] if*
         ] assoc-each
     ] { } make "&" join ;