From bdc9ddf8b5791c5019b7c7bb71188d12c39cb873 Mon Sep 17 00:00:00 2001 From: Achim Gsell Date: Mon, 8 Nov 2021 11:59:21 +0100 Subject: [PATCH 1/3] std:split_fname(): changed arguments and leading slash handling - a leading slash is now removed - the filename is now passed as second argument, the number of parts is returned in an optional third argument --- Pmodules/libstd.bash | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Pmodules/libstd.bash b/Pmodules/libstd.bash index fedd0c4..f7bee90 100644 --- a/Pmodules/libstd.bash +++ b/Pmodules/libstd.bash @@ -135,14 +135,27 @@ std::replace_path () { # # split file name # +# Args: +# $1 upvar +# $2 fname +# $3 opt upvar: number of components +# std::split_fname() { - local -r savedIFS="${IFS}" + local parts="$1" + local -r fname="$2" + if [[ "${fname:0:1}" == '/' ]]; then + local -r std__split_fname_tmp="${fname:1}" + else + local -r std__split_fname_tmp="${fname}" + fi + IFS='/' - local std__split_fname_result__=( $(echo "${@: -1}") ) - IFS=${savedIFS} - eval $1=\(\"\${std__split_fname_result__[@]}\"\) + local std__split_fname_result=( ${std__split_fname_tmp} ) + unset IFS + std::upvar ${parts} "${std__split_fname_result[@]}" if (( $# >= 3 )); then - eval $2=${#std__split_fname_result__[@]} + # return number of parts + std::upvar "$3" ${#std__split_fname_result[@]} fi } From 8112e8b6d4fd730e372eb371317c4b91f70514b1 Mon Sep 17 00:00:00 2001 From: Achim Gsell Date: Mon, 8 Nov 2021 12:20:17 +0100 Subject: [PATCH 2/3] std::split_fname(): reviewed and renamed to std::split_abs_path() --- Pmodules/libstd.bash | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Pmodules/libstd.bash b/Pmodules/libstd.bash index f7bee90..6314872 100644 --- a/Pmodules/libstd.bash +++ b/Pmodules/libstd.bash @@ -133,29 +133,29 @@ std::replace_path () { } # -# split file name +# split an absolute path # # Args: # $1 upvar -# $2 fname +# $2 absolute path # $3 opt upvar: number of components # -std::split_fname() { +std::split_abs_path() { local parts="$1" - local -r fname="$2" - if [[ "${fname:0:1}" == '/' ]]; then - local -r std__split_fname_tmp="${fname:1}" + local -r path="$2" + if [[ "${path:0:1}" == '/' ]]; then + local -r std__split_path_tmp="${path:1}" else - local -r std__split_fname_tmp="${fname}" + std::die 255 "Oops: Internal error in '${FUNCNAME[0]}' called by '${FUNCNAME[1]}' }" fi IFS='/' - local std__split_fname_result=( ${std__split_fname_tmp} ) + local std__split_path_result=( ${std__split_path_tmp} ) unset IFS - std::upvar ${parts} "${std__split_fname_result[@]}" + std::upvar ${parts} "${std__split_path_result[@]}" if (( $# >= 3 )); then # return number of parts - std::upvar "$3" ${#std__split_fname_result[@]} + std::upvar "$3" ${#std__split_path_result[@]} fi } From 6d53069bedb6978a504cf078ed55b68bc92a43be Mon Sep 17 00:00:00 2001 From: Achim Gsell Date: Mon, 8 Nov 2021 12:25:25 +0100 Subject: [PATCH 3/3] std::split_abs_path() renamed to std::split_abspath() --- Pmodules/libstd.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pmodules/libstd.bash b/Pmodules/libstd.bash index 6314872..f56ae57 100644 --- a/Pmodules/libstd.bash +++ b/Pmodules/libstd.bash @@ -140,7 +140,7 @@ std::replace_path () { # $2 absolute path # $3 opt upvar: number of components # -std::split_abs_path() { +std::split_abspath() { local parts="$1" local -r path="$2" if [[ "${path:0:1}" == '/' ]]; then