| | 2225 | ## |
| | 2226 | # Percent-encode a string for safe use in a URL |
| | 2227 | # This code is from http::mapReply in http.tcl included with Tcl |
| | 2228 | # |
| | 2229 | # @param string the string to be encoded |
| | 2230 | # @return the percent-encoded string |
| | 2231 | proc urlencode {string} { |
| | 2232 | global urlencodeMap |
| | 2233 | set excluded -._~a-zA-Z0-9 |
| | 2234 | if {![info exists urlencodeMap]} { |
| | 2235 | for {set i 1} {$i <= 256} {incr i} { |
| | 2236 | set c [format %c $i] |
| | 2237 | if {![string match \[$excluded\] $c]} { |
| | 2238 | set urlencodeMap($c) %[format %.2x $i] |
| | 2239 | } |
| | 2240 | } |
| | 2241 | # These are handled specially |
| | 2242 | array set urlencodeMap { |
| | 2243 | " " + \n %0d%0a |
| | 2244 | } |
| | 2245 | } |
| | 2246 | regsub -all \[^$excluded\] $string {$urlencodeMap(&)} string |
| | 2247 | regsub -all \n $string {\\n} string |
| | 2248 | regsub -all \t $string {\\t} string |
| | 2249 | regsub -all {[][{})\\]\)} $string {\\&} string |
| | 2250 | return [subst $string] |
| | 2251 | } |