AbstractMacroExpander: Allow escaping of %{Macros}

Use "%{}" to put a literal "%" into the output of the macro expander.
E.g. "%{}{Macro}" will be turned into "%{Macro}"

Change-Id: I592789e5cd8f2d52df424db679baf7ba04723202
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
This commit is contained in:
Tobias Hunger
2014-07-14 18:05:38 +02:00
parent 4114297992
commit 0ed377466c
2 changed files with 15 additions and 0 deletions

View File

@@ -126,6 +126,11 @@ int AbstractMacroExpander::findMacro(const QString &str, int *pos, QString *ret)
if (closePos < 0) if (closePos < 0)
return 0; return 0;
int varLen = closePos - varPos; int varLen = closePos - varPos;
if (varLen == 0) { // replace "%{}" with "%"
*pos = openPos;
*ret = QString(QLatin1Char('%'));
return 3;
}
if (resolveMacro(str.mid(varPos, varLen), ret)) { if (resolveMacro(str.mid(varPos, varLen), ret)) {
*pos = openPos; *pos = openPos;
return varLen + 3; return varLen + 3;

View File

@@ -42,6 +42,10 @@ public:
*ret = QLatin1String("hi"); *ret = QLatin1String("hi");
return true; return true;
} }
if (name == QLatin1String("foo")) {
*ret = QLatin1String("a");
return true;
}
return false; return false;
} }
}; };
@@ -102,11 +106,17 @@ void tst_StringUtils::testMacroExpander_data()
} vals[] = { } vals[] = {
{ "text", "text" }, { "text", "text" },
{ "%{a}", "hi" }, { "%{a}", "hi" },
{ "%%{a}", "%hi" },
{ "%%%{a}", "%%hi" },
{ "%{b}", "%{b}" },
{ "pre%{a}", "prehi" }, { "pre%{a}", "prehi" },
{ "%{a}post", "hipost" }, { "%{a}post", "hipost" },
{ "pre%{a}post", "prehipost" }, { "pre%{a}post", "prehipost" },
{ "%{a}%{a}", "hihi" }, { "%{a}%{a}", "hihi" },
{ "%{a}text%{a}", "hitexthi" }, { "%{a}text%{a}", "hitexthi" },
{ "%{foo}%{a}text%{a}", "ahitexthi" },
{ "%{}{a}", "%{a}" },
{ "%{abc", "%{abc" }
}; };
for (unsigned i = 0; i < sizeof(vals)/sizeof(vals[0]); i++) for (unsigned i = 0; i < sizeof(vals)/sizeof(vals[0]); i++)