From 16fbd4299406d069a142871a444c462f9a7fd57e Mon Sep 17 00:00:00 2001 From: Renaud G Date: Sun, 19 Aug 2018 17:26:12 +0200 Subject: make sure aliases don't affect variable names or string --- dicealias.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) (limited to 'dicealias.cpp') diff --git a/dicealias.cpp b/dicealias.cpp index a2683e3..a27919d 100644 --- a/dicealias.cpp +++ b/dicealias.cpp @@ -24,6 +24,83 @@ #include +QString makeReplament(const QString& pattern, const QString& replacement, QString cmd) +{ + + // FIXME try to do the same with RegularExpression + auto hasPattern = cmd.contains(pattern); + if(hasPattern) + { + auto hasVariable = cmd.contains("${"); + auto hasQuote = cmd.contains("\""); + + if(!hasQuote && !hasVariable) + { + cmd.replace(pattern, replacement); + } + else + { + std::vector patternPosList; + std::vector> variablePos; + + int pos= 0; + QRegularExpressionMatch match; + while(pos!=-1) + { + auto start = cmd.indexOf(QRegularExpression("\\${\\N+}"),pos,&match); + if(start >=0) + { + auto end = start+match.captured().length(); + variablePos.push_back(std::make_pair(start,end)); + pos = end+1; + } + else + { + pos = start; + } + } + + pos = 0; + while(pos!=-1) + { + auto start = cmd.indexOf("\"",pos); + if(start >= 0) + { + auto end = cmd.indexOf("\"",start+1); + variablePos.push_back(std::make_pair(start,end)); + pos = end+1; + } + else + { + pos = start; + } + } + + pos= 0; + while((pos = cmd.indexOf(pattern,pos)) && pos!=-1) + { + bool isInsidePair = false; + for(auto pair : variablePos) + { + if(!isInsidePair) + isInsidePair = (pos > pair.first && pos < pair.second); + } + if(!isInsidePair) + patternPosList.push_back(pos); + pos+=1; + } + for(auto pos : patternPosList) + { + cmd.replace(pos,1,replacement); + } + } + } + return cmd; +} + + + + DiceAlias::DiceAlias(QString cmd, QString key, bool isReplace,bool isEnable) : m_command(cmd),m_value(key),m_isEnable(isEnable) { @@ -49,7 +126,8 @@ bool DiceAlias::resolved(QString & str) if((m_type == REPLACE)&&(str.contains(m_command))) { - str.replace(m_command,m_value); + str = makeReplament(m_command,m_value,str); + //str.replace(m_command,m_value); return true; } else if(m_type == REGEXP) -- cgit v1.2.3-70-g09d2 From 52343ff414f7b41e8f38e28bec3de39500b5fed2 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Wed, 22 Aug 2018 02:07:30 +0200 Subject: Fix bug on dicealias replacement (when several replacement must be done on the same command). --- dicealias.cpp | 6 ++++-- dicealias.h | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'dicealias.cpp') diff --git a/dicealias.cpp b/dicealias.cpp index a27919d..f514e9e 100644 --- a/dicealias.cpp +++ b/dicealias.cpp @@ -89,9 +89,11 @@ QString makeReplament(const QString& pattern, const QString& replacement, QStrin patternPosList.push_back(pos); pos+=1; } - for(auto pos : patternPosList) + + // TODO to be replace by C++14 when it is ready + for (auto i = patternPosList.rbegin(); i != patternPosList.rend(); ++i) { - cmd.replace(pos,1,replacement); + cmd.replace(*i,1,replacement); } } } diff --git a/dicealias.h b/dicealias.h index a85c9a0..8320445 100644 --- a/dicealias.h +++ b/dicealias.h @@ -37,7 +37,7 @@ public: * @param key * @param isReplace */ - DiceAlias(QString cmd, QString key, bool isReplace = true, bool isEnable = true); + DiceAlias(QString pattern, QString remplacement, bool isReplace = true, bool isEnable = true); /** * @brief ~DiceAlias */ -- cgit v1.2.3-70-g09d2