changeset 596:1b40b875b281

farray.sh: farray_set() now accepts general indices
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 18 Sep 2024 16:44:47 +0200
parents 24f4dccaea45
children f6ba8ad6f76e
files share/local-bsdtools/farray.sh tests/farray-array.t
diffstat 2 files changed, 97 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/farray.sh	Wed Sep 18 16:15:19 2024 +0200
+++ b/share/local-bsdtools/farray.sh	Wed Sep 18 16:44:47 2024 +0200
@@ -515,10 +515,8 @@
     local __farr_token __farr_gvrname __farr_len __farr_len_1
 
     _farr_array_get_meta "$@"
-    __farr_index="${2-}"
-    [ -z "${__farr_index}" ] && _farr_fatal "no valid index for set given"
-    # make it to a number
-    __farr_index=$((__farr_index + 0))
+    _farr_make_index __farr_index "${2-}" "${__farr_len}"
+    # first check
     [ ${__farr_index} -lt 1 ] && _farr_fatal "index must be >= 1"
     __farr_value="${3-}"
 
--- a/tests/farray-array.t	Wed Sep 18 16:15:19 2024 +0200
+++ b/tests/farray-array.t	Wed Sep 18 16:44:47 2024 +0200
@@ -236,6 +236,101 @@
   $ check_no_array_artifacts
 
 
+Setting
+=======
+
+Append (relative)
+
+  $ farray_create TEST 0 1 2 '3  4   5' $'" 678" \\\'910 ' 11
+  $ farray_set TEST "" $'the new value \\ \''
+  $ farray_debug TEST
+  DEBUG: array `TEST' has length 7
+  DEBUG:   its contents:
+  DEBUG:     1: `0'
+  DEBUG:     2: `1'
+  DEBUG:     3: `2'
+  DEBUG:     4: `3  4   5'
+  DEBUG:     5: `" 678" \'910 '
+  DEBUG:     6: `11'
+  DEBUG:     7: `the new value \ ''
+  $ farray_destroy TEST
+  $ check_no_array_artifacts
+
+Append (explicit)
+
+  $ farray_create TEST 0 1 2 '3  4   5' $'" 678" \\\'910 ' 11
+  $ farray_set TEST 7 $'the new value \\ \''
+  $ farray_debug TEST
+  DEBUG: array `TEST' has length 7
+  DEBUG:   its contents:
+  DEBUG:     1: `0'
+  DEBUG:     2: `1'
+  DEBUG:     3: `2'
+  DEBUG:     4: `3  4   5'
+  DEBUG:     5: `" 678" \'910 '
+  DEBUG:     6: `11'
+  DEBUG:     7: `the new value \ ''
+  $ farray_destroy TEST
+  $ check_no_array_artifacts
+
+Replace the last element
+
+  $ farray_create TEST 0 1 2 '3  4   5' $'" 678" \\\'910 ' 11
+  $ farray_set TEST 0 $'the new replaced value \\ \''
+  $ farray_debug TEST
+  DEBUG: array `TEST' has length 6
+  DEBUG:   its contents:
+  DEBUG:     1: `0'
+  DEBUG:     2: `1'
+  DEBUG:     3: `2'
+  DEBUG:     4: `3  4   5'
+  DEBUG:     5: `" 678" \'910 '
+  DEBUG:     6: `the new replaced value \ ''
+  $ farray_destroy TEST
+  $ check_no_array_artifacts
+
+Replace the first element
+
+  $ farray_create TEST 0 1 2 '3  4   5' $'" 678" \\\'910 ' 11
+  $ farray_set TEST 1 $'the new replaced value \\ \''
+  $ farray_debug TEST
+  DEBUG: array `TEST' has length 6
+  DEBUG:   its contents:
+  DEBUG:     1: `the new replaced value \ ''
+  DEBUG:     2: `1'
+  DEBUG:     3: `2'
+  DEBUG:     4: `3  4   5'
+  DEBUG:     5: `" 678" \'910 '
+  DEBUG:     6: `11'
+  $ farray_destroy TEST
+  $ check_no_array_artifacts
+
+Replace some element in the middle
+
+  $ farray_create TEST 0 1 2 '3  4   5' $'" 678" \\\'910 ' 11
+  $ farray_set TEST 5 $'the new replaced value \\ \' 2 '
+  $ farray_debug TEST
+  DEBUG: array `TEST' has length 6
+  DEBUG:   its contents:
+  DEBUG:     1: `0'
+  DEBUG:     2: `1'
+  DEBUG:     3: `2'
+  DEBUG:     4: `3  4   5'
+  DEBUG:     5: `the new replaced value \ ' 2 '
+  DEBUG:     6: `11'
+  $ farray_destroy TEST
+  $ check_no_array_artifacts
+
+Out of bounds
+
+  $ farray_create TEST 0 1 2 '3  4   5' $'" 678" \\\'910 ' 11
+  $ ( farray_set TEST 8 )
+  ERROR: array index out of bounds (cannot create holes)
+  [70]
+  $ farray_destroy TEST
+  $ check_no_array_artifacts
+
+
 Splicing
 ========