newlisp标准库没有提供url编码的支持,因此从dragonfly库中找到下面的代码,经过测试可以使用。
;===============================================================================
; !UTF8 Compatible URL encoding/decoding
;===============================================================================
(constant ‘REGEX_HTTP_SPECIAL_STR (regex-comp {([^.0-9a-z]+)} 1))
(constant ‘REGEX_HEX_ENCODED_CHAR (regex-comp {%([0-9A-F][0-9A-F])} 1))
(define (hex-encode-str str , cnvrt)
(setf cnvrt (dup "%%%X" (length str)))
(eval (append ‘(format cnvrt) (unpack (dup "b" (length str)) str)))
)
;; @syntax (utf8-urlencode <str> [<bool-everything>])
;; @param str the string to encode
;; @param bool-everything whether to escape the entire string or just most of the "non-ascii friendly" parts.
;; <p>Use this function to safely encode data that might have foreign characters in it, or simply
;; characters that should be placed into URLs:</p>
;; <b>example:</b>
;; <pre> (utf8-urlencode "What time is it?") => "What%20time%20is%20it%3F"</pre>
(define (utf8-urlencode str everything)
(if everything
(hex-encode-str str)
(replace REGEX_HTTP_SPECIAL_STR str (hex-encode-str $1) 0x10000)
)
)
;; @syntax (utf8-urldecode <str>)
;; <p>Decodes a utf8-urlencoded string. Converts ‘+‘‘s to spaces.</p>
(define (utf8-urldecode str)
(replace "+" str " ")
(replace REGEX_HEX_ENCODED_CHAR str (pack "b" (int $1 nil 16)) 0x10000)
)
原文:http://blog.csdn.net/csfreebird/article/details/44978003