Skip to content

Commit

Permalink
add RawURLDecode, RawURLEncode, URLDecode, URLEncode
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperjiang committed Jan 15, 2020
1 parent 26dc16c commit 01fd25e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ This package implements some PHP functions by Golang. Please note that it's impo
| [get_meta_tags](https://www.php.net/manual/en/function.get-meta-tags.php) | [GetMetaTags](https://godoc.org/github.com/hyperjiang/php#GetMetaTags) |
| [http_build_query](https://www.php.net/manual/en/function.http-build-query.php) | [HTTPBuildQuery](https://godoc.org/github.com/hyperjiang/php#HTTPBuildQuery) |
| [parse_url](https://www.php.net/manual/en/function.parse-url.php) | [ParseURL](https://godoc.org/github.com/hyperjiang/php#ParseURL) |
| [rawurldecode](https://www.php.net/manual/en/function.rawurldecode.php) | [RawURLDecode](https://godoc.org/github.com/hyperjiang/php#RawURLDecode) |
| [rawurlencode](https://www.php.net/manual/en/function.rawurlencode.php) | [RawURLEncode](https://godoc.org/github.com/hyperjiang/php#RawURLEncode) |
| [urldecode](https://www.php.net/manual/en/function.urldecode.php) | [URLDecode](https://godoc.org/github.com/hyperjiang/php#URLDecode) |
| [urlencode](https://www.php.net/manual/en/function.urlencode.php) | [URLEncode](https://godoc.org/github.com/hyperjiang/php#URLEncode) |

### Misc Functions

Expand Down
30 changes: 30 additions & 0 deletions url.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,33 @@ func HTTPBuildQuery(data map[string]interface{}) string {
func ParseURL(str string) (*url.URL, error) {
return url.Parse(str)
}

// RawURLDecode decodes URL-encoded string.
//
// RawURLDecode is identical to URLDecode except that it does not unescape '+' to ' ' (space).
func RawURLDecode(str string) string {
res, _ := url.PathUnescape(str)
return res
}

// RawURLEncode is URL-encode according to RFC 3986.
//
// RawURLEncode is identical to URLEncode except that it does not escape space to +.
func RawURLEncode(str string) string {
return url.PathEscape(str)
}

// URLDecode decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
func URLDecode(str string) string {
res, _ := url.QueryUnescape(str)
return res
}

// URLEncode returns a string in which all non-alphanumeric characters except -_.
// have been replaced with a percent (%) sign followed by two hex digits and
// spaces encoded as plus (+) signs.
// It is encoded the same way that the posted data from a WWW form is encoded,
// that is the same way as in application/x-www-form-urlencoded media type.
func URLEncode(str string) string {
return url.QueryEscape(str)
}
52 changes: 52 additions & 0 deletions url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,56 @@ var _ = Describe("URL Functions", func() {
_, err = php.ParseURL("::::")
Expect(err).To(HaveOccurred())
})
It("RawURLDecode", func() {
tests := []struct {
input string
want string
}{
{"my=apples&are=green+and+red", "my=apples&are=green+and+red"},
{"one%20%26%20two", "one & two"},
{"myvar=%BA", "myvar=\xba"},
{"myvar=%B", ""},
}
for _, t := range tests {
Expect(php.RawURLDecode(t.input)).To(Equal(t.want))
}
})
It("RawURLEncode", func() {
tests := []struct {
input string
want string
}{
{"some=weird/value", "some=weird%2Fvalue"},
{" ", "%20"},
}
for _, t := range tests {
Expect(php.RawURLEncode(t.input)).To(Equal(t.want))
}
})
It("URLDecode", func() {
tests := []struct {
input string
want string
}{
{"my=apples&are=green+and+red", "my=apples&are=green and red"},
{"one%20%26%20two", "one & two"},
{"myvar=%BA", "myvar=\xba"},
{"myvar=%B", ""},
}
for _, t := range tests {
Expect(php.URLDecode(t.input)).To(Equal(t.want))
}
})
It("URLEncode", func() {
tests := []struct {
input string
want string
}{
{"some=weird/value", "some%3Dweird%2Fvalue"},
{" ", "+"},
}
for _, t := range tests {
Expect(php.URLEncode(t.input)).To(Equal(t.want))
}
})
})

0 comments on commit 01fd25e

Please sign in to comment.