|
21 | 21 |
|
22 | 22 | . "$_GO_USE_MODULES" 'validation' |
23 | 23 |
|
| 24 | +# DO NOT EDIT: Number of stack trace levels to skip when validation fails. |
| 25 | +export __GO_STRINGS_VALIDATION_SKIP_CALLERS=2 |
| 26 | + |
24 | 27 | # Splits fields from a delimited string into an array defined by the caller |
25 | 28 | # |
26 | 29 | # While `IFS= read -ra array_name <<<"$value"` is idiomatic, this function |
|
72 | 75 | # var_name: Name of caller's variable to which to assign the common prefix |
73 | 76 | # ...: Strings to examine to determine the common prefix |
74 | 77 | @go.common_prefix() { |
75 | | - @go.validate_identifier_or_die 'Result variable name' "$1" |
| 78 | + @go.validate_identifier_or_die 'Result variable name' "$1" \ |
| 79 | + "${__GO_STRINGS_VALIDATION_SKIP_CALLERS}" |
76 | 80 | local _gcp_prefix="$2" |
77 | 81 | local _gcp_prefix_len="${#_gcp_prefix}" |
78 | 82 | local _gcp_item |
|
93 | 97 | printf -v "$1" -- '%s' "$_gcp_prefix" |
94 | 98 | } |
95 | 99 |
|
96 | | -# Removes the common path prefix from a set of file paths |
| 100 | +# Determines the common parent directory path from a set of file paths |
97 | 101 | # |
98 | 102 | # Arguments: |
99 | | -# array_name: Name of caller's array into which to store the updated paths |
100 | | -# ...: File paths from which to remove the common path prefix |
101 | | -@go.remove_common_path_prefix() { |
102 | | - @go.validate_identifier_or_die 'Result variable name' "$1" |
103 | | - local _grcpp_prefix |
104 | | - local _grcpp_paths=("${@:2}") |
105 | | - |
106 | | - @go.common_prefix '_grcpp_prefix' "${_grcpp_paths[@]}" |
| 103 | +# var_name: Name of caller's variable to which to assign the parent path |
| 104 | +# ...: File paths to examine to determine the common parent directory |
| 105 | +@go.common_parent_path() { |
| 106 | + ((++__GO_STRINGS_VALIDATION_SKIP_CALLERS)) |
| 107 | + @go.common_prefix "$1" "${@:2}" |
| 108 | + ((--__GO_STRINGS_VALIDATION_SKIP_CALLERS)) |
107 | 109 |
|
108 | | - if [[ "$_grcpp_prefix" =~ / ]]; then |
109 | | - _grcpp_prefix="${_grcpp_prefix%/*}/" |
| 110 | + if [[ "${!1}" =~ / ]]; then |
| 111 | + printf -v "$1" -- '%s' "${!1%/*}/" |
110 | 112 | else |
111 | | - _grcpp_prefix='' |
| 113 | + # Bash 3.2 won't perform the assignment if the format is the empty string. |
| 114 | + printf -v "$1" '%s' '' |
112 | 115 | fi |
113 | | - |
114 | | - local IFS=$'\x1f' |
115 | | - read -ra "$1" <<<"${_grcpp_paths[*]#$_grcpp_prefix}" |
116 | 116 | } |
0 commit comments