mirror of
https://github.com/Pmodules/Pmodules.git
synced 2026-07-07 06:10:49 +02:00
libstd.bash: upvar replaced with reference variables
This commit is contained in:
+38
-35
@@ -78,41 +78,42 @@ std::get_abspath() {
|
||||
}
|
||||
|
||||
std::append_path () {
|
||||
local -r P=$1
|
||||
local -r d=$2
|
||||
local -nr P="$1"
|
||||
shift 1
|
||||
local dir
|
||||
local dirs=''
|
||||
for dir in "$@"; do
|
||||
[[ "${P}" == @(|*:)${dir}@(|:*) ]] && continue
|
||||
dirs+=":${dir}"
|
||||
done
|
||||
|
||||
if ! echo ${!P} | egrep -q "(^|:)${d}($|:)" ; then
|
||||
if [[ -z ${!P} ]]; then
|
||||
export "$P=${d}"
|
||||
else
|
||||
export "$P=${!P}:${d}"
|
||||
fi
|
||||
fi
|
||||
if [[ -z ${P} ]]; then
|
||||
P="${dirs:1}" # remove leading ':'
|
||||
else
|
||||
P="${P}${dirs}"
|
||||
fi
|
||||
}
|
||||
|
||||
std::prepend_path () {
|
||||
local -nr P="$1"
|
||||
shift 1
|
||||
local path="$1"
|
||||
shift 1
|
||||
|
||||
local dir
|
||||
local dirs=''
|
||||
for dir in "$@"; do
|
||||
[[ "${P}" == @(|*:)${dir}@(|:*) ]] && continue
|
||||
dirs+="${dir}:"
|
||||
done
|
||||
|
||||
if [[ -z ${P} ]]; then
|
||||
for dir in "$@"; do
|
||||
path+=":${dir}"
|
||||
done
|
||||
P="${path}"
|
||||
P="${dirs:0:-1}" # remove trailing ':'
|
||||
else
|
||||
for dir in "$@"; do
|
||||
[[ "${P}" == @(|*:)${dir}@(|:*) ]] && continue
|
||||
path+=":${dir}"
|
||||
done
|
||||
P="${path}:${P}"
|
||||
P="${dirs}${P}"
|
||||
fi
|
||||
}
|
||||
|
||||
std::remove_path() {
|
||||
local -r P=$1
|
||||
local -nr P="$1"
|
||||
shift 1
|
||||
local -ar dirs="$@"
|
||||
local new_path=''
|
||||
@@ -124,8 +125,7 @@ std::remove_path() {
|
||||
[[ "${entry}" != "${dir}" ]] && new_path+=":${entry}"
|
||||
done
|
||||
done
|
||||
# remove leading ':'
|
||||
std::upvar "$P" "${new_path:1}"
|
||||
P="${new_path:1}" # remove leading ':'
|
||||
}
|
||||
|
||||
#
|
||||
@@ -175,22 +175,23 @@ std::replace_path () {
|
||||
# analog to std::split_abspath() with a relative path.
|
||||
#
|
||||
std::split_path() {
|
||||
local parts="$1"
|
||||
local -r path="$2"
|
||||
local -n parts="$1"
|
||||
local -r path="$2"
|
||||
|
||||
IFS='/'
|
||||
local std__split_path_result=( ${std__split_path_tmp} )
|
||||
unset IFS
|
||||
std::upvar ${parts} "${std__split_path_result[@]}"
|
||||
parts="${std__split_path_result[@]}"
|
||||
if (( $# >= 3 )); then
|
||||
# return number of parts
|
||||
std::upvar "$3" ${#std__split_path_result[@]}
|
||||
local -n num="$3"
|
||||
num="${#std__split_path_result[@]}"
|
||||
fi
|
||||
}
|
||||
|
||||
std::split_abspath() {
|
||||
local parts="$1"
|
||||
local -r path="$2"
|
||||
local -n parts="$1"
|
||||
local -r path="$2"
|
||||
if [[ "${path:0:1}" == '/' ]]; then
|
||||
local -r std__split_path_tmp="${path:1}"
|
||||
else
|
||||
@@ -200,16 +201,17 @@ std::split_abspath() {
|
||||
IFS='/'
|
||||
local std__split_path_result=( ${std__split_path_tmp} )
|
||||
unset IFS
|
||||
std::upvar ${parts} "${std__split_path_result[@]}"
|
||||
parts="${std__split_path_result[@]}"
|
||||
if (( $# >= 3 )); then
|
||||
# return number of parts
|
||||
std::upvar "$3" ${#std__split_path_result[@]}
|
||||
local -n num="$3"
|
||||
num="${#std__split_path_result[@]}"
|
||||
fi
|
||||
}
|
||||
|
||||
std::split_relpath() {
|
||||
local parts="$1"
|
||||
local -r path="$2"
|
||||
local -n parts="$1"
|
||||
local -r path="$2"
|
||||
if [[ "${path:0:1}" == '/' ]]; then
|
||||
std::die 255 "Oops: Internal error in '${FUNCNAME[0]}' called by '${FUNCNAME[1]}' }"
|
||||
else
|
||||
@@ -219,10 +221,11 @@ std::split_relpath() {
|
||||
IFS='/'
|
||||
local std__split_path_result=( ${std__split_path_tmp} )
|
||||
unset IFS
|
||||
std::upvar ${parts} "${std__split_path_result[@]}"
|
||||
parts="${std__split_path_result[@]}"
|
||||
if (( $# >= 3 )); then
|
||||
# return number of parts
|
||||
std::upvar "$3" ${#std__split_path_result[@]}
|
||||
local -n num="$3"
|
||||
num="${#std__split_path_result[@]}"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user