Created
August 17, 2014 07:37
-
-
Save arolle/cbf02a38201890473510 to your computer and use it in GitHub Desktop.
decode-from-uri – inverse for fn:encode-for-uri#1 recreates original uri from an encoded string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare namespace functx = "http://www.functx.com"; | |
(: copied from functx :) | |
declare function functx:replace-multi | |
( $arg as xs:string? , | |
$changeFrom as xs:string* , | |
$changeTo as xs:string* ) as xs:string? { | |
if (count($changeFrom) > 0) | |
then functx:replace-multi( | |
replace($arg, $changeFrom[1], ($changeTo[1],'')[1]), | |
tail($changeFrom), | |
tail($changeTo) | |
) | |
else $arg | |
} ; | |
(:~ | |
: decode-from-uri | |
: the inverse to fn:encode-for-uri#1 | |
: | |
: @param $input string | |
: @return | |
:) | |
declare function local:decode-from-uri( | |
$input as xs:string | |
) as xs:string? { | |
(: converts string to character sequence :) | |
let $chars := | |
function ( | |
$arg as xs:string | |
) as xs:string* { | |
for $ch in string-to-codepoints($arg) | |
return codepoints-to-string($ch) | |
} | |
(: all characters to be encoded in URL -- percent is last :) | |
let $str := ("'
" || ' ";,/?:@&=+$[](){}%') ! $chars(.) | |
(: encoded characters :) | |
let $str-enc-seq := $str ! fn:encode-for-uri(.) | |
(: chars in regexp with special meanings :) | |
let $regexp-escapes := '.\?*|^$[]' ! $chars(.) (: +{}() :) | |
let $str-esc := $str ! ( | |
if (. = $regexp-escapes) | |
then '\\' || . | |
else . | |
) | |
return | |
functx:replace-multi($input, $str-enc-seq, $str-esc) | |
}; | |
fn:encode-for-uri("http://basex.org/") | |
! (., local:decode-from-uri(.)) | |
(: | |
outputs: | |
http%3A%2F%2Fbasex.org%2F | |
http://basex.org/ | |
:) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment