Skip to content

Commit b6a6121

Browse files
committed
Refactoring: replace QRegExp by std::regex in configimpl.l
1 parent bcca6a9 commit b6a6121

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

src/configimpl.l

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929

3030
#include <qfileinfo.h>
3131
#include <qdir.h>
32-
#include <qregexp.h>
3332

3433
#include <thread>
3534
#include <algorithm>
35+
#include <regex>
3636

3737
#include "configimpl.h"
3838
#include "version.h"
@@ -1132,25 +1132,31 @@ void ConfigImpl::emptyValueToDefault()
11321132
}
11331133
}
11341134

1135-
static void substEnvVarsInString(QCString &s)
1135+
static void substEnvVarsInString(QCString &str)
11361136
{
1137-
static QRegExp re("\\$\\([a-z_A-Z0-9.-]+\\)");
1138-
static QRegExp re2("\\$\\([a-z_A-Z0-9.-]+\\([a-z_A-Z0-9.-]+\\)\\)"); // For e.g. PROGRAMFILES(X86)
1139-
if (s.isEmpty()) return;
1140-
int p=0;
1141-
int i,l;
1142-
//printf("substEnvVarInString(%s) start\n",s.data());
1143-
while ((i=re.match(s,p,&l))!=-1 || (i=re2.match(s,p,&l))!=-1)
1137+
if (str.isEmpty()) return;
1138+
// match e.g. $(HOME) but also $(PROGRAMFILES(X86))
1139+
static std::regex re("\\$\\([[:alpha:]_][[:alnum:].-]*(\\([[:alpha:]_][[:alnum:].-]*\\))?\\)");
1140+
std::string s = str.str();
1141+
std::sregex_iterator it(s.begin(),s.end(),re);
1142+
std::sregex_iterator end;
1143+
std::string result;
1144+
size_t p = 0;
1145+
for (; it!=end ; ++it)
11441146
{
1145-
//printf("Found environment var s.mid(%d,%d)='%s'\n",i+2,l-3,s.mid(i+2,l-3).data());
1146-
QCString env=Portable::getenv(s.mid(i+2,l-3));
1147+
const auto &match = *it;
1148+
size_t i = match.position();
1149+
size_t l = match.length();
1150+
result+=s.substr(p,i-p);
1151+
std::string matchStr = match.str();
1152+
std::string matchContents = matchStr.substr(2,matchStr.length()-3);
1153+
QCString env=Portable::getenv(matchContents.c_str()); // get content of $(..) match
11471154
substEnvVarsInString(env); // recursively expand variables if needed.
1148-
s = s.left(i)+env+s.right(s.length()-i-l);
1149-
p=i+env.length(); // next time start at the end of the expanded string
1155+
result+=env.str();
1156+
p=i+l;
11501157
}
1151-
s=s.stripWhiteSpace(); // to strip the bogus space that was added when an argument
1152-
// has quotes
1153-
//printf("substEnvVarInString(%s) end\n",s.data());
1158+
result+=s.substr(p);
1159+
str = QCString(result).stripWhiteSpace();
11541160
}
11551161

11561162
static void substEnvVarsInStrList(StringVector &sl)
@@ -1641,16 +1647,16 @@ void Config::checkAndCorrect()
16411647
//------------------------
16421648
// check ALIASES
16431649
const StringVector &aliasList = Config_getList(ALIASES);
1644-
for (const auto &s : aliasList)
1650+
for (const auto &alias : aliasList)
16451651
{
1646-
QRegExp re1("[a-z_A-Z][a-z_A-Z0-9]*[ \t]*="); // alias without argument
1647-
QRegExp re2("[a-z_A-Z][a-z_A-Z0-9]*{[0-9]+}[ \t]*="); // alias with argument
1648-
QCString alias=s.c_str();
1649-
alias=alias.stripWhiteSpace();
1650-
if (alias.find(re1)!=0 && alias.find(re2)!=0)
1652+
// match aliases of the form 'name=' and 'name{2} ='
1653+
static std::regex re("[[:alpha:]_][[:alnum:]_]*(\\{[[:digit:]]+\\})?[[:space:]]*=");
1654+
std::sregex_iterator it(alias.begin(),alias.end(),re);
1655+
std::sregex_iterator end;
1656+
if (it==end)
16511657
{
16521658
err("Illegal ALIASES format '%s'. Use \"name=value\" or \"name{n}=value\", where n is the number of arguments\n",
1653-
alias.data());
1659+
alias.c_str());
16541660
}
16551661
}
16561662

0 commit comments

Comments
 (0)