Here implementation of similar function but for which index position is specified from the start of the string.
How looks implementation of general function for which index position can be specified from the end of the string?
Examples with input parameters and expected results for the required function / procedure:
Example:
string: "2278"
string_to_insert: "."
# After inserting "." to 3-rd index position from the end of "string" result should be:
expected_result="2.278"
Related question:
- How to insert string (or character) into another string at specified index position in bash?
( This question will be self-answered.
This question is part of the project related to my another question: How to retrieve text from the current line at specified cursor position before and after up to specified boundary characters? )
[ Note for gurus. If you want to mark this question and my answer with downvote you of course can do this, but please describe the reason in the comment. I’m always interesting in such feedbacks. And I promise not to follow you with “downvotes revenge”.
Why I am adding such meta note you can understand by reading my post on the topic of “research effort” for questions. ]
cat insert_string.sh
#!/bin/bash
insert_at_position_from_end() {
local string=$1; shift
local string_to_insert=$1; shift
local index=$1; shift
declare -n out_result=$1; shift
local length=${#string}
if (( $index < 0 || $index > $length )); then
error_msg="index out of bounds error: index=$index, string length=$length; expected indexes: [0..$length]"
out_result=""
echo "$error_msg" >&2
return 2 # exit 2 # for intruppting the script
fi
local left_part=${string:0:$length-$index}
local right_part=${string:$length-$index:$index}
out_result="${left_part}${string_to_insert}${right_part}"
}
capture_stderr() {
local -n stderr="${1:?}"; shift
{ stderr="$({ "$@" 1>&3; } 2>&1)"; } 3>&1
}
assert_equals() {
local error_message=$1; shift
local expected_result=$1; shift
local actual_result=$1; shift
if [ "$expected_result" != "$actual_result" ]; then
echo "$error_message; Expected: $expected_result; Actual: $actual_result"
fi
}
test_insert_at_position_from_end() {
# insert to the middle
local test_string="2278"
local expected="2.278"
local actual=""
insert_at_position_from_end "$test_string" "." 3 actual
assert_equals "test_insert_at_position_from_end-10" "$expected" "$actual"
# insert to the beginning
local test_string="278"
local expected=".278"
local actual="" # reset from previous test
insert_at_position_from_end "$test_string" "." 3 actual
assert_equals "test_insert_at_position_from_end-40" "$expected" "$actual"
# insert to the end
local test_string="278"
local expected="278."
local actual=""
insert_at_position_from_end "$test_string" "." 0 actual
assert_equals "test_insert_at_position_from_end-50" "$expected" "$actual"
# try to insert out of the right border
local test_string="278"
local actual=""
local expected_stderr="index out of bounds error: index=-1, string length=3; expected indexes: [0..3]"
capture_stderr actual_stderr insert_at_position_from_end "$test_string" "." -1 actual
assert_equals "test_insert_at_position_from_end-70" "$expected_stderr" "$actual_stderr"
# try to insert out of the left border
local test_string="278"
local actual=""
local expected_stderr="index out of bounds error: index=4, string length=3; expected indexes: [0..3]"
capture_stderr actual_stderr insert_at_position_from_end "$test_string" "." 4 actual
assert_equals "test_insert_at_position_from_end-110" "$expected_stderr" "$actual_stderr"
}
echo "test started"
test_insert_at_position_from_end
echo "test finished"
To run test:
./insert_string.sh
No output with errors means that test have passed successfully.
insert_at_position_from_end
— implementation of function that inserts one string into another at specified index position from the endtest_insert_at_position_from_end
— test for this function. Examples of usages forinsert_at_position_from_end
function can be found here.assert_equals
— function that checks if expected result equals to actual. Prints error message if not.capture_stderr
— function that allows to capturestderr
for called function.
More extended general solution for capturing all possible outcomes of tested function to separate var:
How to put all called function/command output results to different corresponding vars: for stderr, for stdout, for string result and for return code?