(defun url-decode (string &key (external-format :utf-8))
"用external-format指定的编码来解码string"
(flet ((decode-and-output (string out external-format)
(when string
(format out (octets-to-string (decode-hex string) :external-format external-format)))))
(with-output-to-string (out)
(let (s)
(loop :for i := 0 :then (1+ i)
:while (< i (length string))
:do (let ((c (char string i)))
(cond
((char= c #\%) (setf s (concatenate 'string s (subseq string (+ i 1) (+ i 3))))
(incf i 2))
((char= c #\+) (decode-and-output s out external-format)
(setf s nil)
(write-char #\Space out))
(t (decode-and-output s out external-format)
(setf s nil)
(write-char c out))))
:finally (decode-and-output s out external-format))))))
里面的decode-hex是binascii里面的。
"用external-format指定的编码来解码string"
(flet ((decode-and-output (string out external-format)
(when string
(format out (octets-to-string (decode-hex string) :external-format external-format)))))
(with-output-to-string (out)
(let (s)
(loop :for i := 0 :then (1+ i)
:while (< i (length string))
:do (let ((c (char string i)))
(cond
((char= c #\%) (setf s (concatenate 'string s (subseq string (+ i 1) (+ i 3))))
(incf i 2))
((char= c #\+) (decode-and-output s out external-format)
(setf s nil)
(write-char #\Space out))
(t (decode-and-output s out external-format)
(setf s nil)
(write-char c out))))
:finally (decode-and-output s out external-format))))))
里面的decode-hex是binascii里面的。