diff options
| author | 2018-08-19 17:26:12 +0200 | |
|---|---|---|
| committer | 2018-08-19 17:26:12 +0200 | |
| commit | 16fbd4299406d069a142871a444c462f9a7fd57e (patch) | |
| tree | 293cc1b40d6c717282d3af560241e686b9f640bf /dicealias.cpp | |
| parent | acb001bc1909d873a37fbe1c8cbabade76a8015e (diff) | |
| download | OneRoll-16fbd4299406d069a142871a444c462f9a7fd57e.tar.gz OneRoll-16fbd4299406d069a142871a444c462f9a7fd57e.zip | |
make sure aliases don't affect variable names or string
Diffstat (limited to 'dicealias.cpp')
| -rw-r--r-- | dicealias.cpp | 80 |
1 files changed, 79 insertions, 1 deletions
diff --git a/dicealias.cpp b/dicealias.cpp index a2683e3..a27919d 100644 --- a/dicealias.cpp +++ b/dicealias.cpp @@ -24,6 +24,83 @@ #include <QDebug> +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<int> patternPosList; + std::vector<std::pair<int,int>> 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) |