urls.encoding: cleanup.

db4
John Benediktsson 2014-11-30 21:22:54 -08:00
parent 76761b2e59
commit b00e5a855b
1 changed files with 11 additions and 17 deletions

View File

@ -1,9 +1,8 @@
! Copyright (C) 2008, 2010 Slava Pestov. ! Copyright (C) 2008, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel ascii combinators combinators.short-circuit USING: arrays ascii assocs combinators combinators.short-circuit
sequences splitting fry namespaces make assocs arrays strings fry io.encodings.string io.encodings.utf8 kernel make math
io.encodings.string io.encodings.utf8 math math.parser accessors math.parser present sequences splitting strings ;
hashtables present ;
IN: urls.encoding IN: urls.encoding
: url-quotable? ( ch -- ? ) : url-quotable? ( ch -- ? )
@ -39,17 +38,16 @@ IN: urls.encoding
1string utf8 encode 1string utf8 encode
[ CHAR: % , >hex >upper 2 CHAR: 0 pad-head % ] each ; [ CHAR: % , >hex >upper 2 CHAR: 0 pad-head % ] each ;
: (url-encode) ( str quot: ( ch -- ? ) -- encoded )
'[ [ dup @ [ , ] [ push-utf8 ] if ] each ] "" make ; inline
PRIVATE> PRIVATE>
: url-encode ( str -- encoded ) : url-encode ( str -- encoded )
[ [ url-quotable? ] (url-encode) ;
[ dup url-quotable? [ , ] [ push-utf8 ] if ] each
] "" make ;
: url-encode-full ( str -- encoded ) : url-encode-full ( str -- encoded )
[ [ unreserved? ] (url-encode) ;
[ dup unreserved? [ , ] [ push-utf8 ] if ] each
] "" make ;
<PRIVATE <PRIVATE
@ -60,15 +58,12 @@ PRIVATE>
[ 1 + dup 2 + ] dip subseq hex> [ , ] when* [ 1 + dup 2 + ] dip subseq hex> [ , ] when*
] if ; ] if ;
: url-decode-% ( index str -- index str )
2dup url-decode-hex ;
: url-decode-iter ( index str -- ) : url-decode-iter ( index str -- )
2dup length >= [ 2dup length >= [
2drop 2drop
] [ ] [
2dup nth dup CHAR: % = [ 2dup nth dup CHAR: % = [
drop url-decode-% [ 3 + ] dip drop 2dup url-decode-hex [ 3 + ] dip
] [ ] [
, [ 1 + ] dip , [ 1 + ] dip
] if url-decode-iter ] if url-decode-iter
@ -80,8 +75,7 @@ PRIVATE>
[ 0 swap url-decode-iter ] "" make utf8 decode ; [ 0 swap url-decode-iter ] "" make utf8 decode ;
: query-decode ( str -- decoded ) : query-decode ( str -- decoded )
[ dup CHAR: + = [ drop "%20" ] [ 1string ] if ] { } map-as "+" split "%20" join url-decode ;
concat url-decode ;
<PRIVATE <PRIVATE