URL escape and unescape

Recently, I study the package  net/url of Golang.

I was puzzled about the escape and unescape of url string.

then I find a clear and accurate answer at: http://www.sislands.com/coin70/week6/encoder.htm

the code in golang:

 1 // unescape unescapes a string; the mode specifies
 2 // which section of the URL string is being unescaped.
 3 func unescape(s string, mode encoding) (string, error) {
 4     // Count %, check that they‘re well-formed.
 5     n := 0
 6     hasPlus := false
 7     for i := 0; i < len(s); {
 8         switch s[i] {
 9         case ‘%‘:
10             n++
11             if i+2 >= len(s) || !ishex(s[i+1]) || !ishex(s[i+2]) {
12                 s = s[i:]
13                 if len(s) > 3 {
14                     s = s[0:3]
15                 }
16                 return "", EscapeError(s)
17             }
18             i += 3
19         case ‘+‘:
20             hasPlus = mode == encodeQueryComponent
21             i++
22         default:
23             i++
24         }
25     }
26
27     if n == 0 && !hasPlus {
28         return s, nil
29     }
30
31     t := make([]byte, len(s)-2*n)
32     j := 0
33     for i := 0; i < len(s); {
34         switch s[i] {
35         case ‘%‘:
36             t[j] = unhex(s[i+1])<<4 | unhex(s[i+2])
37             j++
38             i += 3
39         case ‘+‘:
40             if mode == encodeQueryComponent {
41                 t[j] = ‘ ‘
42             } else {
43                 t[j] = ‘+‘
44             }
45             j++
46             i++
47         default:
48             t[j] = s[i]
49             j++
50             i++
51         }
52     }
53     return string(t), nil
54 }

After a period of home work,  Now I am confident about implimenting the project: gocrawel by myself.

时间: 2024-08-06 16:06:26

URL escape and unescape的相关文章

javascript中escape()、unescape()、encodeURI()、encodeURIComponent()、decodeUR...

这些URI方法encodeURI.encodeURIComponent().decodeURI().decodeURIComponent()代替了BOM的escape()和unescape()方法.URI方法更可取,因为它们对所有Unicode符号编码,而BOM方法只能对ASCII符号正确编码.尽量避免使用escape()和unescape()方法.摘自 javascript advanced book. js对文字进行编码涉及3个函数:escape,encodeURI,encodeURICom

javascript中escape()、unescape()、encodeURI()、encodeURIComponent()、decodeURI()、decodeURIComponent()比较

js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1.   传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断. 例如:<script language="javascript">document.write('<a href="http://passport.bai

JavaScript编码encode和decode escape和unescape

encodeURI() 函数可把字符串作为 URI 进行编码. 语法 encodeURI(URIstring) 参数 描述 URIstring 必需.一个字符串,含有 URI 或其他要编码的文本. 返回值 URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换. 说明 该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) . 该方法的目的是对 URI 进行完整的编码,因此对以下在 URI 中具有特殊含义的

escape和unescape给字符串编码

var before = "\xxx\xxx" var after = escape(before); var after2 = unescape(after );

一段网上java常见escape和unescape方法的BUG

escape编码和unescape编码,就是将一个字符转换为16进制unicode编码,前面加%字符进行标识. 此处不再多做解释,参考这里:http://www.jb51.net/article/23657.htm. 原本是js的一个方法,后来被转成java方法.具体参考这里 http://blog.sina.com.cn/s/blog_4bb52a160100d9tm.html ,是被程序员们copy和paste最多的通用代码. 先看一下escape源码: /** * 实现js前台的escap

使用php实现javascript的escape和unescape函数

javascript有编码函数escape()和对应的解码函数unescape(),而php中只有个urlencode和urldecode,这个编码和解码函数对encodeURI和encodeURIComponent有效,但是对escape的是无效的.javascript中的escape()函数和unescape()函数用户字符串编码,类似于php中的urlencode()函数,下面是php实现的escape函数代码: /** * js escape php 实现 * @param $strin

Python中escape和unescape

Python处理HTML转义字符 在抓网页数据经常遇到例如>或者 这种HTML转义符,抓到字符串里很是烦人. 比方说一个从网页中抓到的字符串 p ='<abc>' 用Python可以这样处理: import html p = '<abc>' txt= html.unescape(p) print (txt) #这样就得到了txt= '<abc>' 如果还想转回去,可以这样: import cgi q = cgi.escape(html) print(q) #这样又

LUA 捕获模式 URL编码的例子解析

function escape(s) s=string.gsub(s,"([&=+%c])",function(c) return string.format("%%%02X",string.byte(c)) end ) s=string.gsub(s," ","+") return s end 第一个gsub , [ ]中 匹配 & = + 及其他符号 %c 中的任意一个 替换为 格式化成 以 % 开头的(%

js的url中传递中文参数乱码,如何获取url中参数问题

一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码: 1.传参页面Javascript代码: <script type=”text/javascript”> function send(){ var url = "test01.html"; var userName = $("#userName").html(); window.open(encodeURI(url + "?userName="