*remove UNESCAPE-URI, HEX-DIGIT?
*move HEXCHAR->INT to url.scm
This commit is contained in:
parent
932f03a638
commit
9e71b351d4
|
@ -15,64 +15,6 @@
|
||||||
;;; General Web page of URI pointers.
|
;;; General Web page of URI pointers.
|
||||||
|
|
||||||
|
|
||||||
;;; Caution:
|
|
||||||
;;; Don't use this proc until *after* you've parsed the URL -- unescaping
|
|
||||||
;;; might introduce reserved chars (like slashes and colons) that could
|
|
||||||
;;; blow your parse.
|
|
||||||
|
|
||||||
(define (unescape-uri s . maybe-start/end)
|
|
||||||
(let-optionals maybe-start/end ((start 0)
|
|
||||||
(end (string-length s)))
|
|
||||||
(let* ((esc-seq? (lambda (i) (and (< (+ i 2) end)
|
|
||||||
(char=? (string-ref s i) #\%)
|
|
||||||
(hex-digit? (string-ref s (+ i 1)))
|
|
||||||
(hex-digit? (string-ref s (+ i 2))))))
|
|
||||||
(hits (let lp ((i start) (hits 0)) ; count # of esc seqs.
|
|
||||||
(if (< i end)
|
|
||||||
(if (esc-seq? i)
|
|
||||||
(lp (+ i 3) (+ hits 1))
|
|
||||||
(lp (+ i 1) hits))
|
|
||||||
hits))))
|
|
||||||
|
|
||||||
(if (and (zero? hits) (zero? start) (= end (string-length s)))
|
|
||||||
s
|
|
||||||
(let* ((nlen (- (- end start) (* hits 2))) ; the new length
|
|
||||||
; of the
|
|
||||||
; unescaped
|
|
||||||
; string stores
|
|
||||||
; the result
|
|
||||||
(ns (make-string nlen)))
|
|
||||||
|
|
||||||
(let lp ((i start) (j 0)) ; sweep over the string
|
|
||||||
(if (< j nlen)
|
|
||||||
(lp (cond
|
|
||||||
((esc-seq? i) ; unescape
|
|
||||||
; escape-sequence
|
|
||||||
(string-set! ns j
|
|
||||||
(let ((d1 (string-ref s (+ i 1)))
|
|
||||||
(d2 (string-ref s (+ i 2))))
|
|
||||||
(ascii->char (+ (* 16 (hexchar->int d1))
|
|
||||||
(hexchar->int d2)))))
|
|
||||||
(+ i 3))
|
|
||||||
(else (string-set! ns j (string-ref s i))
|
|
||||||
(+ i 1)))
|
|
||||||
(+ j 1))))
|
|
||||||
ns)))))
|
|
||||||
|
|
||||||
(define hex-digit?
|
|
||||||
(let ((hex-digits (string->char-set "0123456789abcdefABCDEF")))
|
|
||||||
(lambda (c) (char-set-contains? hex-digits c))))
|
|
||||||
|
|
||||||
; make use of the fact that numbers and characters are in order in the ascii table
|
|
||||||
(define (hexchar->int c)
|
|
||||||
(- (char->ascii c)
|
|
||||||
(if (char-numeric? c)
|
|
||||||
(char->ascii #\0)
|
|
||||||
(- (if (char-upper-case? c)
|
|
||||||
(char->ascii #\A)
|
|
||||||
(char->ascii #\a))
|
|
||||||
10))))
|
|
||||||
|
|
||||||
(define int->hexchar
|
(define int->hexchar
|
||||||
(let ((table '#(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9
|
(let ((table '#(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9
|
||||||
#\A #\B #\C #\D #\E #\F)))
|
#\A #\B #\C #\D #\E #\F)))
|
||||||
|
|
Loading…
Reference in New Issue