From d2c874140e481f56abbd625f468323060ce1b118 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Mon, 6 Jun 2022 08:57:48 +0900 Subject: [PATCH] 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. --- include/gitlab.sh | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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() { -- 2.47.3