mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 22:34:45 +00:00
Update Url module
This commit is contained in:
parent
59ca19b5c3
commit
a95157ad1b
1 changed files with 23 additions and 25 deletions
|
@ -109,36 +109,34 @@ appendHelp = \prefix, suffix ->
|
||||||
## https://stackoverflow.com/questions/2678551/when-should-space-be-encoded-to-plus-or-20/47188851#47188851
|
## https://stackoverflow.com/questions/2678551/when-should-space-be-encoded-to-plus-or-20/47188851#47188851
|
||||||
percentEncode : Str -> Str
|
percentEncode : Str -> Str
|
||||||
percentEncode = \input ->
|
percentEncode = \input ->
|
||||||
# TODO `Str.replaceUtf8 : Str, (U8 -> [Same, Replace Str]) -> Str` can let you translate UTF-8 bytes into strings.
|
|
||||||
#
|
|
||||||
# TODO instead of starting with "", start with Str.withCapacity based on
|
# TODO instead of starting with "", start with Str.withCapacity based on
|
||||||
# some the length of the given str. (Maybe be optimistic and assume it's the same length,
|
# some the length of the given str. (Maybe be optimistic and assume it's the same length,
|
||||||
# then let it get doubled if we're wrong.)
|
# then let it get doubled if we're wrong.)
|
||||||
#
|
Str.walkCodePts input "" \output, codePt, codePtStr ->
|
||||||
# https://www.ietf.org/rfc/rfc3986.txt
|
# Spec for percent-encoding: https://www.ietf.org/rfc/rfc3986.txt
|
||||||
List.replaceUtf8 input \byte ->
|
Str.concat output
|
||||||
if
|
if
|
||||||
(byte >= 97 && byte <= 122) # lowercase ASCII
|
(codePt >= 97 && codePt <= 122) # lowercase ASCII
|
||||||
|| (byte >= 65 && byte <= 90) # uppercase ASCII
|
|| (codePt >= 65 && codePt <= 90) # uppercase ASCII
|
||||||
|| (byte >= 48 && byte <= 57) # digit
|
|| (codePt >= 48 && codePt <= 57) # digit
|
||||||
then
|
then
|
||||||
# This is the most common case: an unreserved character,
|
# This is the most common case: an unreserved character,
|
||||||
# which needs no encoding in a path
|
# which needs no encoding in a path
|
||||||
Same
|
codePtStr
|
||||||
else
|
else
|
||||||
when byte is
|
when codePt is
|
||||||
46 # '.'
|
46 # '.'
|
||||||
| 95 # '_'
|
| 95 # '_'
|
||||||
| 126 # '~'
|
| 126 # '~'
|
||||||
| 150 -> # '-'
|
| 150 -> # '-'
|
||||||
# These special characters can all be unescaped in paths
|
# These special characters can all be unescaped in paths
|
||||||
Same
|
codePtStr
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
# This needs encoding in a path
|
# This needs encoding in a path
|
||||||
hex = Num.toHexUppercase byte
|
hex = Num.toHexUppercase codePt
|
||||||
|
|
||||||
Replace "%\(hex)"
|
"%\(hex)"
|
||||||
|
|
||||||
## Adds a [Str] query parameter to the end of the [Url]. Both the key
|
## Adds a [Str] query parameter to the end of the [Url]. Both the key
|
||||||
## and the value are [percent-encoded](https://en.wikipedia.org/wiki/Percent-encoding).
|
## and the value are [percent-encoded](https://en.wikipedia.org/wiki/Percent-encoding).
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue