]> git.corax.cc Git - toolbox-restapis/commitdiff
include/gitlab: Make urlencode function more extensible
authorMatthias Kruk <m@m10k.eu>
Sun, 5 Jun 2022 23:57:48 +0000 (08:57 +0900)
committerMatthias Kruk <m@m10k.eu>
Sun, 5 Jun 2022 23:57:48 +0000 (08:57 +0900)
Because _gitlab_urlencode() is a simple string replacement, it is
not very extensible and does not handle more than one replacement.

This commit rewrites _gitlab_urlencode() to use an associative
array to store replacement patterns, allowing the function to be
easily extended. For testing, this commit also adds a pattern to
escape comma (',') characters in URLs.

include/gitlab.sh

index a808bbe817948efd8cf5fa229082f36875a297f0..1d2103575ffc1ce5f3f9ac2a6556c7ec60f4ca90 100755 (executable)
@@ -25,9 +25,23 @@ __init() {
 }
 
 _gitlab_urlencode() {
-        local str="$1"
+       local str="$1"
 
-        echo "${str//\//%2F}"
+       declare -A patterns
+       local pattern
+
+       patterns["/"]="%2F"
+       patterns[","]="%2C"
+
+       for pattern in "${!patterns[@]}"; do
+               local replace
+
+               replace="${patterns[$pattern]}"
+               str="${str//"$pattern"/"$replace"}"
+       done
+
+       echo "$str"
+       return 0
 }
 
 _gitlab_get() {