349 lines
9.2 KiB
Bash
349 lines
9.2 KiB
Bash
#!/bin/bash
|
|
# shellcheck disable=SC2155
|
|
if ! type "include" > /dev/null 2>&1; then
|
|
include() {
|
|
. "$(cd "$(dirname "$1")" && pwd)/${2}";
|
|
};
|
|
fi
|
|
include "${BASH_SOURCE[0]}" ../pragma_once.bash
|
|
if ! pragma_once ${BASH_SOURCE[0]}; then return 0; fi
|
|
include "${BASH_SOURCE[0]}" ./global_rely/export.bash
|
|
# shellcheck source=./global_rely/export.bash
|
|
|
|
|
|
|
|
|
|
|
|
list_size() {
|
|
local list_str=$1
|
|
IFS=":" read -r -a array <<<"$list_str" # 用冒号分割字符串到数组
|
|
_ret_=${#array[@]} # 将数组的大小存入全局变量 _ret_
|
|
}
|
|
|
|
list_insert_element() {
|
|
local list_str=$1
|
|
local pos=$2
|
|
local element=$3 # 要插入的新元素
|
|
IFS=":" read -r -a array <<<"$list_str"
|
|
list_size "$list_str"
|
|
local size=$_ret_
|
|
if ((pos < 0 || pos > size)); then
|
|
echo "list_insert_element error! pos:${pos} size:${size} list:$list_str"
|
|
exit 1
|
|
fi
|
|
new_array=("${array[@]:0:pos}" "$element" "${array[@]:pos}")
|
|
_ret_=$(
|
|
IFS=":"
|
|
echo "${new_array[*]}"
|
|
)
|
|
}
|
|
|
|
list_insert_element_if_not_exist() {
|
|
local list_str=$1
|
|
local pos=$2
|
|
local element=$3
|
|
list_find_element "$list_str" "$element"
|
|
if [[ $_ret_ != "-1" ]]; then
|
|
_ret_="$list_str"
|
|
return 0
|
|
fi
|
|
list_insert_element "$list_str" "$pos" "$element"
|
|
}
|
|
|
|
list_append_element() {
|
|
local list_str=$1
|
|
local element=$2
|
|
list_size "$list_str"
|
|
list_insert_element "$list_str" "$_ret_" "$element"
|
|
}
|
|
|
|
list_prepend_all_element() {
|
|
local list_str=$1
|
|
local element=$2
|
|
local result=""
|
|
IFS=":" read -r -a array <<<"$list_str"
|
|
for i in "${!array[@]}"; do
|
|
result+="$element${array[$i]}:"
|
|
done
|
|
_ret_="${result%:}"
|
|
}
|
|
|
|
list_append_all_element() {
|
|
local list_str=$1
|
|
local element=$2
|
|
local result=""
|
|
IFS=":" read -r -a array <<<"$list_str"
|
|
for i in "${!array[@]}"; do
|
|
result+="${array[$i]}$element:"
|
|
done
|
|
_ret_="${result%:}"
|
|
}
|
|
|
|
list_append_element_if_not_exist() {
|
|
local list_str=$1
|
|
local element=$2
|
|
list_size "$list_str"
|
|
list_insert_element_if_not_exist "$list_str" "$_ret_" "$element"
|
|
}
|
|
|
|
list_prepend_element() {
|
|
local list_str=$1
|
|
local element=$2
|
|
list_insert_element "$list_str" "0" "$element"
|
|
}
|
|
|
|
list_prepend_element_if_not_exist() {
|
|
local list_str=$1
|
|
local element=$2
|
|
list_insert_element_if_not_exist "$list_str" "0" "$element"
|
|
}
|
|
|
|
list_remove_at() {
|
|
local list_str=$1
|
|
local pos=$2
|
|
IFS=":" read -r -a array <<<"$list_str"
|
|
list_size "$list_str"
|
|
local size=$_ret_
|
|
if [[ ${size} == 1 ]]; then
|
|
_ret_=""
|
|
fi
|
|
if ((pos < 0 || pos >= size)); then
|
|
echo "list_remove_at error! pos:${pos} size:${size} list:$list_str"
|
|
exit 1
|
|
fi
|
|
new_array=("${array[@]:0:pos}" "${array[@]:pos+1}")
|
|
_ret_=$(
|
|
IFS=":"
|
|
echo "${new_array[*]}"
|
|
)
|
|
}
|
|
|
|
list_remove_last() {
|
|
local list_str=$1
|
|
list_size "$list_str"
|
|
local index=$((_ret_ - 1))
|
|
list_remove_at "$list_str" "$index"
|
|
}
|
|
|
|
list_remove_first() {
|
|
local list_str=$1
|
|
list_remove_at "$list_str" "0"
|
|
}
|
|
|
|
list_find_element_regex() {
|
|
local list_str=$1
|
|
local regex=$2
|
|
IFS=":" read -r -a array <<<"$list_str"
|
|
local i
|
|
for i in "${!array[@]}"; do
|
|
if [[ "${array[$i]}" =~ $regex ]]; then
|
|
_ret_="$i"
|
|
return 0
|
|
fi
|
|
done
|
|
_ret_="-1"
|
|
}
|
|
|
|
list_find_element() {
|
|
local list_str=$1
|
|
local element=$2
|
|
local regex="^${element}$"
|
|
list_find_element_regex "$list_str" "$regex"
|
|
}
|
|
|
|
list_remove_element_if_exist() {
|
|
local list_str=$1
|
|
local element=$2
|
|
list_find_element "$list_str" "$element"
|
|
local pos=$_ret_
|
|
if [[ "$pos" == "-1" ]]; then
|
|
_ret_="$list_str"
|
|
return 0
|
|
fi
|
|
list_remove_at "$list_str" "$pos"
|
|
}
|
|
|
|
# =========================== 测试 ===========================
|
|
test_list_size() {
|
|
local test_string="apple:banana:cherry:date:grape"
|
|
local expected_size=5
|
|
list_size "$test_string"
|
|
local size=$_ret_
|
|
if [[ "$size" -ne $expected_size ]]; then
|
|
echo "Size test failed:"
|
|
echo " Input list: '$test_string'"
|
|
echo " Expected: $expected_size"
|
|
echo " Got: $size"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
test_list_insert_element() {
|
|
local test_string="apple:banana:cherry:date:grape"
|
|
local insert_pos=2
|
|
local insert_element="orange"
|
|
local expected_inserted="apple:banana:orange:cherry:date:grape"
|
|
list_insert_element "$test_string" "$insert_pos" "$insert_element"
|
|
local inserted=$_ret_
|
|
if [[ "$inserted" != "$expected_inserted" ]]; then
|
|
echo "Insert test failed:"
|
|
echo " Input list: '$test_string'"
|
|
echo " Insert Position: $insert_pos"
|
|
echo " Insert Element: '$insert_element'"
|
|
echo " Expected: '$expected_inserted'"
|
|
echo " Got: '$inserted'"
|
|
exit 1
|
|
fi
|
|
|
|
# Test inserting into an empty string
|
|
local empty_string=""
|
|
local expected_inserted_empty="orange"
|
|
list_insert_element "$empty_string" 0 "$insert_element"
|
|
local inserted_empty=$_ret_
|
|
if [[ "$inserted_empty" != "$expected_inserted_empty" ]]; then
|
|
echo "Insert test with empty string failed:"
|
|
echo " Input list: '$empty_string'"
|
|
echo " Insert Position: 0"
|
|
echo " Insert Element: '$insert_element'"
|
|
echo " Expected: '$expected_inserted_empty'"
|
|
echo " Got: '$inserted_empty'"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
test_list_remove_at() {
|
|
local test_string="apple:banana:orange:cherry:date:grape"
|
|
local remove_pos=1
|
|
local expected_removed_at="apple:orange:cherry:date:grape"
|
|
list_remove_at "$test_string" "$remove_pos"
|
|
local removed_at=$_ret_
|
|
if [[ "$removed_at" != "$expected_removed_at" ]]; then
|
|
echo "Remove at test failed:"
|
|
echo " Input list: '$test_string'"
|
|
echo " Remove Position: $remove_pos"
|
|
echo " Expected: '$expected_removed_at'"
|
|
echo " Got: '$removed_at'"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
test_list_find_element() {
|
|
local test_string="apple:banana:cherry:date:grape"
|
|
local find_element="banana"
|
|
local expected_find_index=1
|
|
list_find_element "$test_string" "$find_element"
|
|
local find_index=$_ret_
|
|
if [[ "$find_index" -ne $expected_find_index ]]; then
|
|
echo "Find element test failed:"
|
|
echo " Input list: '$test_string'"
|
|
echo " Element: '$find_element'"
|
|
echo " Expected: $expected_find_index"
|
|
echo " Got: $find_index"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
test_list_remove_element_if_exist() {
|
|
local test_string="apple:banana:cherry:date:grape"
|
|
local remove_element="cherry"
|
|
local expected_after_remove="apple:banana:date:grape"
|
|
list_remove_element_if_exist "$test_string" "$remove_element"
|
|
local after_remove=$_ret_
|
|
if [[ "$after_remove" != "$expected_after_remove" ]]; then
|
|
echo "Remove '$remove_element' test failed:"
|
|
echo " Input list: '$test_string'"
|
|
echo " Expected: '$expected_after_remove'"
|
|
echo " Got: '$after_remove'"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
test_list_remove_non_existent_element() {
|
|
local test_string="apple:banana:cherry:date:grape"
|
|
local expected_after_not_exist="apple:banana:cherry:date:grape"
|
|
list_remove_element_if_exist "$test_string" "pear"
|
|
local after_not_exist=$_ret_
|
|
if [[ "$after_not_exist" != "$expected_after_not_exist" ]]; then
|
|
echo "Remove 'pear' (not exist) test failed:"
|
|
echo " Input list: '$test_string'"
|
|
echo " Expected: '$expected_after_not_exist'"
|
|
echo " Got: '$after_not_exist'"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
test_list_append_element() {
|
|
local test_string="apple:banana:cherry:date:grape"
|
|
local append_element="new_element"
|
|
local expected_appended="apple:banana:cherry:date:grape:new_element"
|
|
list_append_element "$test_string" "$append_element"
|
|
local appended=$_ret_
|
|
if [[ "$appended" != "$expected_appended" ]]; then
|
|
echo "Append test failed:"
|
|
echo " Input list: '$test_string'"
|
|
echo " Append Element: '$append_element'"
|
|
echo " Expected: '$expected_appended'"
|
|
echo " Got: '$appended'"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
test_list_prepend_element() {
|
|
local test_string="apple:banana:cherry:date:grape"
|
|
local prepend_element="new_element"
|
|
local expected_prepended="new_element:apple:banana:cherry:date:grape"
|
|
list_prepend_element "$test_string" "$prepend_element"
|
|
local prepended=$_ret_
|
|
if [[ "$prepended" != "$expected_prepended" ]]; then
|
|
echo "Prepend test failed:"
|
|
echo " Input list: '$test_string'"
|
|
echo " Prepend Element: '$prepend_element'"
|
|
echo " Expected: '$expected_prepended'"
|
|
echo " Got: '$prepended'"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
test_list_find_element_regex() {
|
|
local test_string="apple:banana:cherry:date:grape"
|
|
|
|
# Test element found
|
|
local regex="^banana$"
|
|
local expected_index=1
|
|
list_find_element_regex "$test_string" "$regex"
|
|
local found_index=$_ret_
|
|
if [[ "$found_index" -ne $expected_index ]]; then
|
|
echo "Find element regex test failed:"
|
|
echo " Input list: '$test_string'"
|
|
echo " Regex: '$regex'"
|
|
echo " Expected index: $expected_index"
|
|
echo " Got index: $found_index"
|
|
exit 1
|
|
fi
|
|
|
|
# Test element not found
|
|
local not_found_regex="^pear$"
|
|
local expected_not_found="-1"
|
|
list_find_element_regex "$test_string" "$not_found_regex"
|
|
local not_found_index=$_ret_
|
|
if [[ "$not_found_index" != "$expected_not_found" ]]; then
|
|
echo "Find element regex test for not found failed:"
|
|
echo " Input list: '$test_string'"
|
|
echo " Regex: '$not_found_regex'"
|
|
echo " Expected index: $expected_not_found"
|
|
echo " Got index: $not_found_index"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# test_list_size
|
|
# test_list_insert_element
|
|
# test_list_remove_at
|
|
# test_list_find_element
|
|
# test_list_remove_element_if_exist
|
|
# test_list_remove_non_existent_element
|
|
# test_list_append_element
|
|
# test_list_prepend_element
|
|
# test_list_find_element_regex
|
|
|
|
return 0 |