diff options
author | Andy Wingo <wingo@pobox.com> | 2011-01-02 10:07:54 -0500 |
---|---|---|
committer | Andy Wingo <wingo@pobox.com> | 2011-01-07 09:18:36 -0800 |
commit | 91b320fe1643d80bdd1997a8575db5d540fcdabb (patch) | |
tree | 1a62d6bdf65301e9e6b278255f7ef681af068b42 | |
parent | 7d6b8b75fc5b7b5ef1901220687b36d407636877 (diff) | |
download | guile-91b320fe1643d80bdd1997a8575db5d540fcdabb.tar.gz |
uri-encode fast path
* module/web/uri.scm (uri-encode): Add a fast-path for the common case
in which the string does not contain any reserved characters.
-rw-r--r-- | module/web/uri.scm | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/module/web/uri.scm b/module/web/uri.scm index a0daa4b96..2361d87b5 100644 --- a/module/web/uri.scm +++ b/module/web/uri.scm @@ -330,27 +330,29 @@ bytes are not valid for the given encoding. Pass @code{#f} for ;; (define* (uri-encode str #:key (encoding "utf-8") (unescaped-chars unreserved-chars)) - "Percent-encode any character not in @var{unescaped-chars}. + "Percent-encode any character not in the character set, @var{unescaped-chars}. Percent-encoding first writes out the given character to a bytevector within the given @var{encoding}, then encodes each byte as @code{%@var{HH}}, where @var{HH} is the hexadecimal representation of the byte." - (call-with-output-string - (lambda (port) - (string-for-each - (lambda (ch) - (if (char-set-contains? unescaped-chars ch) - (display ch port) - (let* ((bv (encode-string (string ch) encoding)) - (len (bytevector-length bv))) - (let lp ((i 0)) - (if (< i len) - (let ((byte (bytevector-u8-ref bv i))) - (display #\% port) - (display (number->string byte 16) port) - (lp (1+ i)))))))) - str)))) + (if (string-index str unescaped-chars) + (call-with-output-string + (lambda (port) + (string-for-each + (lambda (ch) + (if (char-set-contains? unescaped-chars ch) + (display ch port) + (let* ((bv (encode-string (string ch) encoding)) + (len (bytevector-length bv))) + (let lp ((i 0)) + (if (< i len) + (let ((byte (bytevector-u8-ref bv i))) + (display #\% port) + (display (number->string byte 16) port) + (lp (1+ i)))))))) + str))) + str)) (define (split-and-decode-uri-path path) "Split @var{path} into its components, and decode each |