Commit 3be096f1 authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh
Browse files

Utils: Support default value substitution in macro expansion



Syntax is similar to bash substitution:

%{variable:-default}

Change-Id: I50e4e18a186c5408dc8b7b95cebbf891d13e03bf
Reviewed-by: default avatarOswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Reviewed-by: default avatarhjk <hjk@qt.io>
parent eece696d
......@@ -128,6 +128,7 @@ bool AbstractMacroExpander::expandNestedMacros(const QString &str, int *pos, QSt
{
QString varName;
QString pattern, replace;
QString defaultValue;
QString *currArg = &varName;
QChar prev;
QChar c;
......@@ -172,12 +173,20 @@ bool AbstractMacroExpander::expandNestedMacros(const QString &str, int *pos, QSt
}
return true;
}
if (!defaultValue.isEmpty()) {
*pos = i;
*ret = defaultValue;
return true;
}
return false;
} else if (c == '{' && prev == '%') {
if (!expandNestedMacros(str, &i, ret))
return false;
varName.chop(1);
varName += ret;
} else if (currArg == &varName && c == '-' && prev == ':' && validateVarName(varName)) {
varName.chop(1);
currArg = &defaultValue;
} else if (currArg == &varName && c == '/' && validateVarName(varName)) {
currArg = &pattern;
if (i < strLen && str.at(i) == '/') {
......
......@@ -153,6 +153,7 @@ void tst_StringUtils::testMacroExpander_data()
{ "%{sl\\/sh/(.)(a)(.)/\\2\\1\\3as}", "salsash" }, // escape in variable name
{ "%{JS:foo/b/c}", "%{JS:foo/b/c}" }, // No replacement for JS (all considered varName)
{ "%{%{a}%{a}/b/c}", "car" },
{ "%{nonsense:-sense}", "sense" },
};
for (unsigned i = 0; i < sizeof(vals)/sizeof(vals[0]); i++)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment