|
| 1 | +#!/usr/bin/env sh |
| 2 | + |
| 3 | +# Double include protection |
| 4 | +case "${INCLUDE_SEEN_PSST-}" in |
| 5 | + *:list:*) return |
| 6 | +esac |
| 7 | +INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-}:list:" |
| 8 | + |
| 9 | +# Ensure INCLUDE_PSST is set |
| 10 | +[ -n "${INCLUDE_PSST-}" ] || { echo "INCLUDE_PSST not set!" >&2 ; exit 1 ; } |
| 11 | + |
| 12 | + |
| 13 | +# shellcheck source=assert.inc.sh |
| 14 | +. "$INCLUDE_PSST/basic/assert.inc.sh" |
| 15 | + |
| 16 | +# shellcheck source=const.inc.sh |
| 17 | +. "$INCLUDE_PSST/basic/const.inc.sh" |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +## |
| 22 | +# SUBPROCESS |
| 23 | +# list_append_psst <list> <newValue> [<del>] |
| 24 | +# |
| 25 | +# SUMMARY |
| 26 | +# Appends a new value to the end of an existing list, using `del` as a |
| 27 | +# delimiter between list items and to terminate the list. |
| 28 | +# |
| 29 | +# PARAMETERS |
| 30 | +# list: A list or an empty string to create a new list. |
| 31 | +# newValue: Value to append to the list. |
| 32 | +# [del]: Delimiter character; NOT newline! Defaults to $RS_CHAR_PSST. |
| 33 | +# |
| 34 | +# OUTPUT |
| 35 | +# stdout: The modified `list with `newValue` appended. |
| 36 | +# |
| 37 | +# NOTE |
| 38 | +# If `newValue` contains the delimiter character, it is considered to consist |
| 39 | +# out of multiple values, separted by the delimiter, and all of these values |
| 40 | +# are appended. |
| 41 | +# |
| 42 | +# SAMPLE |
| 43 | +# newList=$( list_append_psst '' "$someValue" "$GS_CHAR_PSST" ) |
| 44 | +# |
| 45 | +# modifiedList=$( list_append_psst "$someList" "$someValue" ) |
| 46 | +# |
| 47 | +list_append_psst() |
| 48 | +( |
| 49 | + func="list_append_psst" |
| 50 | + assert_minargc_psst "$func" 2 $# |
| 51 | + |
| 52 | + list=$1 |
| 53 | + newValue=$2 |
| 54 | + del=${3-$RS_CHAR_PSST} |
| 55 | + |
| 56 | + [ ${#del} -eq 1 ] \ |
| 57 | + || assert_func_fail_psst "$func" \ |
| 58 | + "List delimiter must be single character" |
| 59 | + |
| 60 | + ! [ "$del" = "$NL_CHAR_PSST" ] \ |
| 61 | + || assert_func_fail_psst "$func" \ |
| 62 | + "List delimiter must not be newline" |
| 63 | + |
| 64 | + case $list in |
| 65 | + '' | *"$del") ;; |
| 66 | + *) assert_func_fail_psst "$func" \ |
| 67 | + "Argument \"list\" is not a valid list" |
| 68 | + esac |
| 69 | + |
| 70 | + printf "%s%s%s" "$list" "$newValue" "$del" |
| 71 | +) |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +## |
| 76 | +# SUBPROCESS |
| 77 | +# list_prepend_psst <list> <newValue> [<del>] |
| 78 | +# |
| 79 | +# SUMMARY |
| 80 | +# Prepends a new value to the end of an existing list, using `del` as a |
| 81 | +# delimiter between list items and to terminate the list. |
| 82 | +# |
| 83 | +# PARAMETERS |
| 84 | +# list: A list or an empty string to create a new list. |
| 85 | +# newValue: Value to prepend to the list. |
| 86 | +# [del]: Delimiter character; NOT newline! Defaults to $RS_CHAR_PSST. |
| 87 | +# |
| 88 | +# OUTPUT |
| 89 | +# stdout: The modified `list with `newValue` prepended. |
| 90 | +# |
| 91 | +# NOTE |
| 92 | +# If `newValue` contains the delimiter character, it is considered to consist |
| 93 | +# out of multiple values, separted by the delimiter, and all of these values |
| 94 | +# are prepended. |
| 95 | +# |
| 96 | +# SAMPLE |
| 97 | +# newList=$( list_prepend_psst '' "$someValue" "$GS_CHAR_PSST" ) |
| 98 | +# |
| 99 | +# modifiedList=$( list_prepend_psst "$someList" "$someValue" ) |
| 100 | +# |
| 101 | +list_prepend_psst() |
| 102 | +( |
| 103 | + func="list_prepend_psst" |
| 104 | + assert_minargc_psst "$func" 2 $# |
| 105 | + |
| 106 | + list=$1 |
| 107 | + newValue=$2 |
| 108 | + del=${3-$RS_CHAR_PSST} |
| 109 | + |
| 110 | + [ ${#del} -eq 1 ] \ |
| 111 | + || assert_func_fail_psst "$func" \ |
| 112 | + "List delimiter must be single character" |
| 113 | + |
| 114 | + ! [ "$del" = "$NL_CHAR_PSST" ] \ |
| 115 | + || assert_func_fail_psst "$func" \ |
| 116 | + "List delimiter must not be newline" |
| 117 | + |
| 118 | + case $list in |
| 119 | + '' | *"$del") ;; |
| 120 | + *) assert_func_fail_psst "$func" \ |
| 121 | + "Argument \"list\" is not a valid list" |
| 122 | + esac |
| 123 | + |
| 124 | + printf "%s%s%s" "$newValue" "$del" "$list" |
| 125 | +) |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +## |
| 130 | +# SUBPROCESS |
| 131 | +# list_append_list_psst <list1> <list2> [<del>] |
| 132 | +# |
| 133 | +# SUMMARY |
| 134 | +# Appends values of `list2` to `list1`, using `del` as a |
| 135 | +# delimiter between list items and to terminate the list. |
| 136 | +# |
| 137 | +# PARAMETERS |
| 138 | +# list1: A list or an empty string to create a new list. |
| 139 | +# list2: Another list or empty string to append. |
| 140 | +# [del]: Delimiter character; NOT newline! Defaults to $RS_CHAR_PSST. |
| 141 | +# |
| 142 | +# OUTPUT |
| 143 | +# stdout: The resulting list of the append operation. |
| 144 | +# |
| 145 | +# SAMPLE |
| 146 | +# newList=$( list_append_list_psst "$list1" "$list2" "$GS_CHAR_PSST" ) |
| 147 | +# |
| 148 | +list_append_list_psst() |
| 149 | +( |
| 150 | + func="list_append_list_psst" |
| 151 | + assert_minargc_psst "$func" 2 $# |
| 152 | + |
| 153 | + list1=$1 |
| 154 | + list2=$2 |
| 155 | + del=${3-$RS_CHAR_PSST} |
| 156 | + |
| 157 | + [ ${#del} -eq 1 ] \ |
| 158 | + || assert_func_fail_psst "$func" \ |
| 159 | + "List delimiter must be single character" |
| 160 | + |
| 161 | + ! [ "$del" = "$NL_CHAR_PSST" ] \ |
| 162 | + || assert_func_fail_psst "$func" \ |
| 163 | + "List delimiter must not be newline" |
| 164 | + |
| 165 | + case $list1 in |
| 166 | + '' | *"$del") ;; |
| 167 | + *) assert_func_fail_psst "$func" \ |
| 168 | + "Argument \"list1\" is not a valid list" |
| 169 | + esac |
| 170 | + |
| 171 | + case $list2 in |
| 172 | + '' | *"$del") ;; |
| 173 | + *) assert_func_fail_psst "$func" \ |
| 174 | + "Argument \"list2\" is not a valid list" |
| 175 | + esac |
| 176 | + |
| 177 | + |
| 178 | + printf "%s%s" "$list1" "$list2" |
| 179 | +) |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +## |
| 184 | +# SUBPROCESS |
| 185 | +# list_prepend_list_psst <list1> <list2> [<del>] |
| 186 | +# |
| 187 | +# SUMMARY |
| 188 | +# Prepends values of `list2` to `list1`, using `del` as a |
| 189 | +# delimiter between list items and to terminate the list. |
| 190 | +# |
| 191 | +# PARAMETERS |
| 192 | +# list1: A list or an empty string to create a new list. |
| 193 | +# list2: Another list or empty string to append. |
| 194 | +# [del]: Delimiter character; NOT newline! Defaults to $RS_CHAR_PSST. |
| 195 | +# |
| 196 | +# OUTPUT |
| 197 | +# stdout: The resulting list of the prepend operation. |
| 198 | +# |
| 199 | +# SAMPLE |
| 200 | +# newList=$( list_prepend_list_psst "$list1" "$list2" "$GS_CHAR_PSST" ) |
| 201 | +# |
| 202 | + |
| 203 | +list_prepend_list_psst() |
| 204 | +( |
| 205 | + func="list_prepend_list_psst" |
| 206 | + assert_minargc_psst "$func" 2 $# |
| 207 | + |
| 208 | + list1=$1 |
| 209 | + list2=$2 |
| 210 | + del=${3-$RS_CHAR_PSST} |
| 211 | + |
| 212 | + [ ${#del} -eq 1 ] \ |
| 213 | + || assert_func_fail_psst "$func" \ |
| 214 | + "List delimiter must be single character" |
| 215 | + |
| 216 | + ! [ "$del" = "$NL_CHAR_PSST" ] \ |
| 217 | + || assert_func_fail_psst "$func" \ |
| 218 | + "List delimiter must not be newline" |
| 219 | + |
| 220 | + case $list1 in |
| 221 | + '' | *"$del") ;; |
| 222 | + *) assert_func_fail_psst "$func" \ |
| 223 | + "Argument \"list1\" is not a valid list" |
| 224 | + esac |
| 225 | + |
| 226 | + case $list2 in |
| 227 | + '' | *"$del") ;; |
| 228 | + *) assert_func_fail_psst "$func" \ |
| 229 | + "Argument \"list2\" is not a valid list" |
| 230 | + esac |
| 231 | + |
| 232 | + |
| 233 | + printf "%s%s" "$list2" "$list1" |
| 234 | +) |
0 commit comments