Skip to content

Commit

Permalink
Add bracket balance checking in input lines
Browse files Browse the repository at this point in the history
  • Loading branch information
a1henu committed May 1, 2024
1 parent 3976982 commit e528a68
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,23 @@ int main(int argc, char** argv) {
std::set<std::string> citationIDs;
std::string line;
std::regex citationRegex{"\\[(.*?)\\]"};
int bracketCount = 0;

while(std::getline(*input, line)) {
outputBuf << line << std::endl;

for (char c : line) {
if (c == '[') {
++bracketCount;
} else if (c == ']') {
--bracketCount;
}

if (bracketCount < 0) {
std::exit(1);
}
}

std::smatch matches;
std::string::const_iterator searchStart(line.cbegin());
while (std::regex_search(searchStart, line.cend(), matches, citationRegex)) {
Expand All @@ -111,6 +124,10 @@ int main(int argc, char** argv) {
}
}

if (bracketCount != 0) {
std::exit(1);
}

if (inputFile != "-") {
delete input;
}
Expand Down

0 comments on commit e528a68

Please sign in to comment.