this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

pkg/net: add Path{E,Une}sape and Query{E,Une}scape

Add the following four functions to the `net` package:

* PathEscape
* PathUnescape
* QueryEscape
* QueryUnescape

These are identical to Go's functions in `net/url`.
In fact, the documentation has been copied from them.

Fixes #1699.
Closes #1695 as merged as of commit 001f5ccb.

Signed-off-by: Jason Felice <jason.m.felice@gmail.com>
Change-Id: I467e9e0e7f1bdf5a33f29838a920bb7caa46027c
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/552890
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>

authored by

Jason Felice and committed by
Daniel Martí
9082446a 59080b65

+106
+48
pkg/net/pkg.go
··· 225 225 c.Ret, c.Err = IPString(ip) 226 226 } 227 227 }, 228 + }, { 229 + Name: "PathEscape", 230 + Params: []internal.Param{ 231 + {Kind: adt.StringKind}, 232 + }, 233 + Result: adt.StringKind, 234 + Func: func(c *internal.CallCtxt) { 235 + s := c.String(0) 236 + if c.Do() { 237 + c.Ret = PathEscape(s) 238 + } 239 + }, 240 + }, { 241 + Name: "PathUnescape", 242 + Params: []internal.Param{ 243 + {Kind: adt.StringKind}, 244 + }, 245 + Result: adt.StringKind, 246 + Func: func(c *internal.CallCtxt) { 247 + s := c.String(0) 248 + if c.Do() { 249 + c.Ret, c.Err = PathUnescape(s) 250 + } 251 + }, 252 + }, { 253 + Name: "QueryEscape", 254 + Params: []internal.Param{ 255 + {Kind: adt.StringKind}, 256 + }, 257 + Result: adt.StringKind, 258 + Func: func(c *internal.CallCtxt) { 259 + s := c.String(0) 260 + if c.Do() { 261 + c.Ret = QueryEscape(s) 262 + } 263 + }, 264 + }, { 265 + Name: "QueryUnescape", 266 + Params: []internal.Param{ 267 + {Kind: adt.StringKind}, 268 + }, 269 + Result: adt.StringKind, 270 + Func: func(c *internal.CallCtxt) { 271 + s := c.String(0) 272 + if c.Do() { 273 + c.Ret, c.Err = QueryUnescape(s) 274 + } 275 + }, 228 276 }}, 229 277 }
+8
pkg/net/testdata/gen.txtar
··· 23 23 t18: net.IPCIDR("192.168.1.0/24") 24 24 t19: net.IPCIDR("2001:db8:a0b:12f0::1/32") 25 25 t20: net.IPCIDR("172.16.12.3") 26 + t21: net.PathEscape("foo/bar") 27 + t22: net.PathUnescape("foo%2Fbar") 28 + t23: net.QueryEscape("f%o") 29 + t24: net.QueryUnescape("f%25o") 26 30 -- out/net -- 27 31 Errors: 28 32 t9: invalid value "23.23.23.2333" (does not satisfy net.IPv4): ··· 57 61 t18: true 58 62 t19: true 59 63 t20: _|_ // t20: error in call to net.IPCIDR: invalid CIDR address: 172.16.12.3 64 + t21: "foo%2Fbar" 65 + t22: "foo/bar" 66 + t23: "f%25o" 67 + t24: "f%o" 60 68
+50
pkg/net/url.go
··· 1 + // Copyright 2019 CUE Authors 2 + // 3 + // Licensed under the Apache License, Version 2.0 (the "License"); 4 + // you may not use this file except in compliance with the License. 5 + // You may obtain a copy of the License at 6 + // 7 + // http://www.apache.org/licenses/LICENSE-2.0 8 + // 9 + // Unless required by applicable law or agreed to in writing, software 10 + // distributed under the License is distributed on an "AS IS" BASIS, 11 + // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 + // See the License for the specific language governing permissions and 13 + // limitations under the License. 14 + 15 + package net 16 + 17 + import ( 18 + "net/url" 19 + ) 20 + 21 + // PathEscape escapes the string so it can be safely placed inside a URL path 22 + // segment, replacing special characters (including /) with %XX sequences as 23 + // needed. 24 + func PathEscape(s string) string { 25 + return url.PathEscape(s) 26 + } 27 + 28 + // PathUnescape does the inverse transformation of PathEscape, converting each 29 + // 3-byte encoded substring of the form "%AB" into the hex-decoded byte 0xAB. 30 + // It returns an error if any % is not followed by two hexadecimal digits. 31 + // 32 + // PathUnescape is identical to QueryUnescape except that it does not unescape 33 + // '+' to ' ' (space). 34 + func PathUnescape(s string) (string, error) { 35 + return url.PathUnescape(s) 36 + } 37 + 38 + // QueryEscape escapes the string so it can be safely placed inside a URL 39 + // query. 40 + func QueryEscape(s string) string { 41 + return url.QueryEscape(s) 42 + } 43 + 44 + // QueryUnescape does the inverse transformation of QueryEscape, converting 45 + // each 3-byte encoded substring of the form "%AB" into the hex-decoded byte 46 + // 0xAB. It returns an error if any % is not followed by two hexadecimal 47 + // digits. 48 + func QueryUnescape(s string) (string, error) { 49 + return url.QueryUnescape(s) 50 + }