码迷,mamicode.com
首页 > Web开发 > 详细

newlisp url 编码

时间:2015-04-10 13:37:12      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:url编码

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)
	)




newlisp url 编码

标签:url编码

原文地址:http://blog.csdn.net/csfreebird/article/details/44978003

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!