summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAlfred Morgan <alfred@54.org>2022-05-30 03:09:18 -0700
committerGitHub <noreply@github.com>2022-05-30 12:09:18 +0200
commit15f0b4867679120580b2f14bbb7a8b302505b34d (patch)
tree267ef5777ebd274ffc5b918c0465dfb2058c1bf7 /lib
parent497af2c0d9f311e050ba57842a6326ca70489d55 (diff)
downloadNim-15f0b4867679120580b2f14bbb7a8b302505b34d.tar.gz
Zectbumo fixes 19824 (#19825)
* borrowed `$` to make Time string friendly

* added sep character parameter

* Revert "added sep character parameter"

This reverts commit 45f4b019a4883b6ba577ade1f94677266beb5960.

* added sep character parameter

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

This reverts commit 10e2e44c9a04970f38cf66556635bdbb50b69136.

* 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>
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/uri.nim14
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/pure/uri.nim b/lib/pure/uri.nim
index 7583dbd1e..1dbb018c2 100644
--- a/lib/pure/uri.nim
+++ b/lib/pure/uri.nim
@@ -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):