From: Matthias Kruk Date: Sun, 5 Jun 2022 23:57:48 +0000 (+0900) Subject: include/gitlab: Make urlencode function more extensible X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=d2c874140e481f56abbd625f468323060ce1b118;p=toolbox-restapis include/gitlab: Make urlencode function more extensible 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. --- diff --git a/include/gitlab.sh b/include/gitlab.sh index a808bbe..1d21035 100755 --- a/include/gitlab.sh +++ b/include/gitlab.sh @@ -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() {