Zectbumo fixes 19824 (#19825)

* borrowed `$` to make Time string friendly

* added sep character parameter

* Revert "added sep character parameter"

This reverts commit 45f4b019a4.

* added sep character parameter

* Revert "borrowed `$` to make Time string friendly"

This reverts commit 10e2e44c9a.

* added uri tests and made changelong entry

* Update lib/pure/uri.nim

Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>

* Update lib/pure/uri.nim

Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>

* Update tests/stdlib/turi.nim

Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>

* Update tests/stdlib/turi.nim

Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>

Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
This commit is contained in:
Alfred Morgan
2022-05-30 03:09:18 -07:00
committed by GitHub
parent 497af2c0d9
commit 15f0b48676
3 changed files with 13 additions and 6 deletions

View File

@@ -39,6 +39,7 @@ becomes an alias for `addr`.
- Added `getIsoWeeksInYear` in `times` to return the number of weeks in an ISO week-based year.
- Added [`Array.shift`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift) for JavaScript targets.
- Added `std/oserrors` for OS error reporting. Added `std/envvars` for environment variables handling.
- Added `sep` parameter in `std/uri` to specify the query separator.
- Removed deprecated `oids.oidToString`.
- Remove define `nimExperimentalAsyncjsThen` for `std/asyncjs.then` and `std/jsfetch`.

View File

@@ -123,13 +123,13 @@ func decodeUrl*(s: string, decodePlus = true): string =
setLen(result, j)
func encodeQuery*(query: openArray[(string, string)], usePlus = true,
omitEq = true): string =
omitEq = true, sep = '&'): string =
## Encodes a set of (key, value) parameters into a URL query string.
##
## Every (key, value) pair is URL-encoded and written as `key=value`. If the
## value is an empty string then the `=` is omitted, unless `omitEq` is
## false.
## The pairs are joined together by a `&` character.
## The pairs are joined together by the `sep` character.
##
## The `usePlus` parameter is passed down to the `encodeUrl` function that
## is used for the URL encoding of the string values.
@@ -140,9 +140,10 @@ func encodeQuery*(query: openArray[(string, string)], usePlus = true,
assert encodeQuery({: }) == ""
assert encodeQuery({"a": "1", "b": "2"}) == "a=1&b=2"
assert encodeQuery({"a": "1", "b": ""}) == "a=1&b"
assert encodeQuery({"a": "1", "b": ""}, omitEq = false, sep = ';') == "a=1;b="
for elem in query:
# Encode the `key = value` pairs and separate them with a '&'
if result.len > 0: result.add('&')
# Encode the `key = value` pairs and separate them with 'sep'
if result.len > 0: result.add(sep)
let (key, val) = elem
result.add(encodeUrl(key, usePlus))
# Omit the '=' if the value string is empty
@@ -150,7 +151,7 @@ func encodeQuery*(query: openArray[(string, string)], usePlus = true,
result.add('=')
result.add(encodeUrl(val, usePlus))
iterator decodeQuery*(data: string): tuple[key, value: string] =
iterator decodeQuery*(data: string, sep = '&'): tuple[key, value: string] =
## Reads and decodes the query string `data` and yields the `(key, value)` pairs
## the data consists of. If compiled with `-d:nimLegacyParseQueryStrict`,
## a `UriParseError` is raised when there is an unencoded `=` character in a decoded
@@ -158,6 +159,7 @@ iterator decodeQuery*(data: string): tuple[key, value: string] =
runnableExamples:
import std/sequtils
assert toSeq(decodeQuery("foo=1&bar=2=3")) == @[("foo", "1"), ("bar", "2=3")]
assert toSeq(decodeQuery("foo=1;bar=2=3", ';')) == @[("foo", "1"), ("bar", "2=3")]
assert toSeq(decodeQuery("&a&=b&=&&")) == @[("", ""), ("a", ""), ("", "b"), ("", ""), ("", "")]
proc parseData(data: string, i: int, field: var string, sep: char): int =
@@ -186,7 +188,7 @@ iterator decodeQuery*(data: string): tuple[key, value: string] =
when defined(nimLegacyParseQueryStrict):
i = parseData(data, i, value, '=')
else:
i = parseData(data, i, value, '&')
i = parseData(data, i, value, sep)
yield (name, value)
if i < data.len:
when defined(nimLegacyParseQueryStrict):

View File

@@ -274,7 +274,9 @@ template main() =
doAssert encodeQuery({"foo": ""}) == "foo"
doAssert encodeQuery({"foo": ""}, omitEq = false) == "foo="
doAssert encodeQuery({"a": "1", "b": "", "c": "3"}) == "a=1&b&c=3"
doAssert encodeQuery({"a": "1", "b": "", "c": "3"}, sep = ';') == "a=1;b;c=3"
doAssert encodeQuery({"a": "1", "b": "", "c": "3"}, omitEq = false) == "a=1&b=&c=3"
doAssert encodeQuery({"a": "1", "b": "", "c": "3"}, omitEq = false, sep = ';') == "a=1;b=;c=3"
block: # `?`
block:
@@ -300,7 +302,9 @@ template main() =
block: # decodeQuery
doAssert toSeq(decodeQuery("a=1&b=0")) == @[("a", "1"), ("b", "0")]
doAssert toSeq(decodeQuery("a=1;b=0", sep = ';')) == @[("a", "1"), ("b", "0")]
doAssert toSeq(decodeQuery("a=1&b=2c=6")) == @[("a", "1"), ("b", "2c=6")]
doAssert toSeq(decodeQuery("a=1;b=2c=6", sep = ';')) == @[("a", "1"), ("b", "2c=6")]
block: # bug #17481
let u1 = parseUri("./")