summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoerg Bornemann <joerg.bornemann@nokia.com>2012-09-03 18:01:05 +0200
committerJoerg Bornemann <joerg.bornemann@nokia.com>2012-09-05 16:30:04 +0200
commit57efad2a72f8738ca6c9e7d61d6ea3071a4e3dce (patch)
treed710a2128b135dce615c9a4eb621b719ce1c928f
parenta2bffc8822f30ef93ff3c822f9dd19b441214e8a (diff)
small performance improvement
Use lazy evaluation of macroValue in MacroTable::internalSetMacroValue. This patch essentially avoids the call to QString::contains. Change-Id: Ic8875ea97d7ca10e6b97b30e3c54c18d484dfed6 Reviewed-by: Joerg Bornemann <joerg.bornemann@nokia.com>
-rw-r--r--src/jomlib/macrotable.cpp37
1 files changed, 35 insertions, 2 deletions
diff --git a/src/jomlib/macrotable.cpp b/src/jomlib/macrotable.cpp
index d324d4d..00d91d8 100644
--- a/src/jomlib/macrotable.cpp
+++ b/src/jomlib/macrotable.cpp
@@ -110,6 +110,40 @@ void MacroTable::setEnvironmentVariable(const QString& name, const QString& valu
m_environment.append(namePlusEq + value);
}
+/**
+ * String replace with lazy replacement evaluation.
+ * getValue is only called if the search string is present.
+ */
+template <typename F>
+void replaceStringWithLazyValue(QString &str, const QString &searchString, F getValue)
+{
+ int idx = str.indexOf(searchString);
+ if (idx >= 0) {
+ const QString oldValue = getValue();
+ do {
+ str.replace(idx, searchString.length(), oldValue);
+ idx = str.indexOf(searchString, idx + oldValue.length());
+ } while (idx >= 0);
+ }
+}
+
+class MacroValueOp
+{
+public:
+ MacroValueOp(MacroTable *mt, const QString &str)
+ : mt(mt), str(str)
+ {}
+
+ QString operator()()
+ {
+ return mt->macroValue(str);
+ }
+
+private:
+ const MacroTable *const mt;
+ const QString &str;
+};
+
MacroTable::MacroData* MacroTable::internalSetMacroValue(const QString& name, const QString& value)
{
QString expandedName = expandMacros(name);
@@ -119,8 +153,7 @@ MacroTable::MacroData* MacroTable::internalSetMacroValue(const QString& name, co
MacroData* result = 0;
const QString instantiatedName = QLatin1Literal("$(") + expandedName + QLatin1Literal(")");
QString newValue = value;
- if (value.contains(instantiatedName))
- newValue.replace(instantiatedName, macroValue(expandedName));
+ replaceStringWithLazyValue(newValue, instantiatedName, MacroValueOp(this, expandedName));
result = &m_macros[expandedName];
if (!result->isReadOnly)