Files
build_toochain/linux_shell/build_script/base/string.bash
T
2026-07-07 15:23:39 +08:00

104 lines
3.5 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
get_relative_path_ex() {
local abs_path=${1%/}
local abs_rely_path=${2%/}
local base=$3
local abs_path_array
local abs_rely_path_array
if [[ "$abs_path" == "$abs_rely_path" ]]; then
if [[ -n "$base" ]]; then
_ret_="${base}"
else
_ret_="./"
fi
return 0
fi
IFS='/' read -r -a abs_path_array <<<"$abs_path"
IFS='/' read -r -a abs_rely_path_array <<<"$abs_rely_path"
local abs_path_length=${#abs_path_array[@]}
local abs_rely_path_length=${#abs_rely_path_array[@]}
local i=0
while [[ "${abs_path_array[i]}" == "${abs_rely_path_array[i]}" ]]; do
((i++))
done
local result=""
for ((j = i; j < abs_path_length; j++)); do
result="../$result"
done
if [[ -n "$base" ]]; then
result="${base}/$result"
fi
for ((j = i; j < abs_rely_path_length; j++)); do
result+="${abs_rely_path_array[j]}/"
done
_ret_="${result%/}"
}
get_relative_origin_path() {
get_relative_path_ex $1 $2 "\$ORIGIN"
}
get_relative_path() {
get_relative_path_ex $1 $2 ""
}
test_get_relative_path() {
# Array of test cases with each element containing:
# 1. Function name to call (either "get_relative_path" or "get_relative_path_ex")
# 2. First path argument
# 3. Second path argument
# 4. Base argument (for get_relative_path_ex, otherwise empty)
# 5. Expected result
local test_cases=(
# Format: "function_name path1 path2 base expected"
"get_relative_origin_path /home/user/project/folder /home/user/project/folder \$ORIGIN"
"get_relative_origin_path /home/user/project/folder /home/user/project \$ORIGIN/.."
"get_relative_origin_path /home/user/project/folder /home/user/tt/aa \$ORIGIN/../../tt/aa"
"get_relative_origin_path /home/user/project/folder/subfolder /home/user/project/another_folder \$ORIGIN/../../another_folder"
"get_relative_origin_path /var/www/html /home/user/tt/aa \$ORIGIN/../../../home/user/tt/aa"
# Test cases with an empty base for get_relative_path_ex
"get_relative_path /home/user/project/folder /home/user/project/folder ./"
"get_relative_path /home/user/project/folder /home/user/project .."
"get_relative_path /home/user/project/folder /home/user/tt/aa ../../tt/aa"
"get_relative_path /home/user/project/folder/subfolder /home/user/project/another_folder ../../another_folder"
"get_relative_path /var/www/html /home/user/tt/aa ../../../home/user/tt/aa"
)
# Loop through each test case
for test_case in "${test_cases[@]}"; do
# Read the elements of the test case into variables
local function_name path1 path2 expected
IFS=" " read -r function_name path1 path2 expected <<<"$test_case"
# Call the appropriate function based on the test case
if [[ "$function_name" == "get_relative_origin_path" ]]; then
get_relative_origin_path "$path1" "$path2"
elif [[ "$function_name" == "get_relative_path" ]]; then
get_relative_path "$path1" "$path2"
fi
# Check the result
if [[ "$_ret_" != "$expected" ]]; then
echo "$function_name Test Failed:"
echo " Input: $path1, $path2"
echo " Expected: '$expected', but got '$_ret_'"
else
echo "$function_name Test Passed."
fi
done
}
return 0