diff options
author | Oswald Buddenhagen <oswald.buddenhagen@nokia.com> | 2010-11-08 21:09:19 +0100 |
---|---|---|
committer | Oswald Buddenhagen <oswald.buddenhagen@nokia.com> | 2010-11-17 13:19:06 +0100 |
commit | 9bd95ade0853ff7fb3295c8e119232820be7139b (patch) | |
tree | a7e58c6dbbd76b824b46573d7dc9c7fb38a8c5a4 /src/libs/utils/stringutils.cpp | |
parent | a2b6391b313bc4e53e5cdf767ab4697cf9775e81 (diff) |
add generic macro expansion classes & functions to Utils
AbstractMacroExpander (any macros), AbstractQtcMacroExpander (%{var}
style macros, which is a hybrid of printf format specifiers and unix
environment expansions) and expandMacros() for the actual string
manipulation.
Diffstat (limited to 'src/libs/utils/stringutils.cpp')
-rw-r--r-- | src/libs/utils/stringutils.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/libs/utils/stringutils.cpp b/src/libs/utils/stringutils.cpp index 10a2d418391..1bead7193ed 100644 --- a/src/libs/utils/stringutils.cpp +++ b/src/libs/utils/stringutils.cpp @@ -119,4 +119,42 @@ QTCREATOR_UTILS_EXPORT QString withTildeHomePath(const QString &path) return outPath; } +int AbstractQtcMacroExpander::findMacro(const QString &str, int *pos, QString *ret) +{ + forever { + int openPos = str.indexOf(QLatin1String("%{"), *pos); + if (openPos < 0) + return 0; + int varPos = openPos + 2; + int closePos = str.indexOf(QLatin1Char('}'), varPos); + if (closePos < 0) + return 0; + int varLen = closePos - varPos; + if (resolveMacro(str.mid(varPos, varLen), ret)) { + *pos = openPos; + return varLen + 3; + } + // An actual expansion may be nested into a "false" one, + // so we continue right after the last %{. + *pos = varPos; + } +} + +QTCREATOR_UTILS_EXPORT void expandMacros(QString *str, AbstractMacroExpander *mx) +{ + QString rsts; + + for (int pos = 0; int len = mx->findMacro(*str, &pos, &rsts); ) { + str->replace(pos, len, rsts); + pos += rsts.length(); + } +} + +QTCREATOR_UTILS_EXPORT QString expandMacros(const QString &str, AbstractMacroExpander *mx) +{ + QString ret = str; + expandMacros(&ret, mx); + return ret; +} + } // namespace Utils |