Skip to content

Commit c889200

Browse files
committed
Handle character range negation (i.e. "[^a-z]") in REGEX
when generating test data.
1 parent fc72292 commit c889200

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

libchecktestdata.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,11 @@ string genregex(string exp)
745745
case '[':
746746
{
747747
set<char> possible;
748-
bool escaped = false;
748+
bool escaped = false, inverted = false;
749+
if ( i + 1 < exp.length() && exp[i+1] == '^' ) {
750+
inverted = true;
751+
i++;
752+
}
749753
while (i + 1 < exp.length()) {
750754
i++;
751755
if (escaped) {
@@ -773,7 +777,13 @@ string genregex(string exp)
773777
}
774778
}
775779
vector<char> possibleVec;
776-
copy(possible.begin(), possible.end(), std::back_inserter(possibleVec));
780+
if ( inverted ) {
781+
for (char c = ' '; c <= '~'; c++) {
782+
if ( !possible.count(c) ) possibleVec.push_back(c);
783+
}
784+
} else {
785+
copy(possible.begin(), possible.end(), std::back_inserter(possibleVec));
786+
}
777787
int mult = getmult(exp, i);
778788
for (int cnt = 0; cnt < mult; cnt++) {
779789
res += possibleVec[random() % possibleVec.size()];

0 commit comments

Comments
 (0)