commit 75a69a3646ad58a5df37abd784fe015b43adf0b0
parent 7f8ebf5f5e52d9cfabd50006c9460287c3741d4d
Author: Florian Dold <dold@taler.net>
Date: Tue, 21 Jul 2026 20:12:17 +0200
util: accept every substitution default separator the C impl accepts
Diffstat:
1 file changed, 17 insertions(+), 12 deletions(-)
diff --git a/packages/taler-util/src/talerconfig.ts b/packages/taler-util/src/talerconfig.ts
@@ -216,6 +216,11 @@ export function expandPath(path: string): string {
* "$x" (look up "x")
* "${x}" (look up "x")
* "${x:-y}" (look up "x", fall back to expanded y)
+ *
+ * As in the C, the first ":" introduces the default and a "-" or "=" directly
+ * after it is skipped, so "${x:=y}" and "${x:y}" mean the same as "${x:-y}".
+ * An empty default expands to the empty string, which is distinct from having
+ * no default at all.
*/
export function pathsub(
x: string,
@@ -233,19 +238,12 @@ export function pathsub(
let depth = 1;
const start = l;
let p = start + 2;
- let insideNamePart = true;
- let hasDefault = false;
for (; p < s.length; p++) {
if (s[p] == "}") {
- insideNamePart = false;
depth--;
} else if (s[p] === "$" && s[p + 1] === "{") {
- insideNamePart = false;
depth++;
}
- if (insideNamePart && s[p] === ":" && s[p + 1] === "-") {
- hasDefault = true;
- }
if (depth == 0) {
break;
}
@@ -254,12 +252,19 @@ export function pathsub(
const inner = s.slice(start + 2, p);
let varname: string;
let defaultValue: string | undefined;
- if (hasDefault) {
- // Split at the first ":-" only: the default is the whole rest of
- // the expression, as in the C, and may itself contain ":-".
- const sep = inner.indexOf(":-");
+ // The first ":" separates the name from the default, and a "-" or
+ // "=" right after it is part of the separator; the rest of the
+ // expression is the default verbatim. This mirrors the strchr in
+ // the C, which likewise takes the first colon anywhere in the
+ // expression.
+ const sep = inner.indexOf(":");
+ if (sep >= 0) {
varname = inner.slice(0, sep);
- defaultValue = inner.slice(sep + 2);
+ const rest = inner.slice(sep + 1);
+ defaultValue =
+ rest.startsWith("-") || rest.startsWith("=")
+ ? rest.slice(1)
+ : rest;
} else {
varname = inner;
defaultValue = undefined;