diff --git a/liblist.sh b/liblist.sh index d3a2144..5a98738 100644 --- a/liblist.sh +++ b/liblist.sh @@ -75,7 +75,9 @@ list_push_back () { # list_insert () { test "$#" -ne 3 && return 1 - i="$2"; [ "$i" != '$' ] && i=$((i+1)); echo "$3" | sed "${i}i${1}" + i="$2"; [ "$i" != '$' ] && i=$((i+1)); echo "$3" | sed "${i}i\\ +${1} +" } ## diff --git a/liblist_unsafe.sh b/liblist_unsafe.sh index 4ae9951..09a3d0b 100644 --- a/liblist_unsafe.sh +++ b/liblist_unsafe.sh @@ -79,7 +79,9 @@ list_push_back () { # list_insert () { test "$#" -ne 3 && return 1; i="$3"; [ "$i" != '$' ] && i=$((i+1)) - eval "$1=\"\$(echo \"\$$1\" | sed \"${i}i${2}\")\"" + eval "$1=\"\$(echo \"\$$1\" | sed \"${i}i\\\\ +${2} +\")\"" } ## diff --git a/tests/test_freebsd.sh b/tests/test_freebsd.sh new file mode 100755 index 0000000..5edfb0d --- /dev/null +++ b/tests/test_freebsd.sh @@ -0,0 +1,40 @@ +#!/bin/sh + +# Test to make sure BSD sed and GNU sed work for +# list insertion. Assumes that both version of +# sed are present in PATH + +set -e + +. ./liblist.sh + + +insert_test() { + type sed + lst="$(list 'C' 'A' 'B')" + lst_expected="$(list 'D' 'C' 'A' 'B')" + + lst="$(list_insert "D" 0 "$lst")" + + if [ "$lst" == "$lst_expected" ]; then + echo "test: insert pass" + return 0 + else + echo "test: insert fail" + return 1 + fi +} + +insert_test_gsed() { + # Keep gsed override wrapped + sed() { + gsed "$@" + } + insert_test + # Make sure this doesnt pollute other tests + unset -f sed +} + +insert_test +echo '--------------------------' +insert_test_gsed diff --git a/tests/test_unsafe_freebsd.sh b/tests/test_unsafe_freebsd.sh new file mode 100755 index 0000000..5e57fea --- /dev/null +++ b/tests/test_unsafe_freebsd.sh @@ -0,0 +1,40 @@ +#!/bin/sh + +# Test to make sure BSD sed and GNU sed work for +# list insertion. Assumes that both version of +# sed are present in PATH + +set -e + +. ./liblist_unsafe.sh + + +insert_test() { + type sed + lst="$(list 'C' 'A' 'B')" + lst_expected="$(list 'D' 'C' 'A' 'B')" + + list_insert lst "D" 0 + + if [ "$lst" == "$lst_expected" ]; then + echo "test: insert pass" + return 0 + else + echo "test: insert fail" + return 1 + fi +} + +insert_test_gsed() { + # Keep gsed override wrapped + sed() { + gsed "$@" + } + # Make sure this doesnt pollute other tests + insert_test + unset -f sed +} + +insert_test +echo '--------------------------' +insert_test_gsed