Skip to content

Commit 2094309

Browse files
committed
Fixed weird logic in get_next_non_blank_line and skip_to_next_blank_line. Removed expensive regex in exchange for simpler and faster char checks.
1 parent ab6fae9 commit 2094309

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

src/utils.cpp

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,39 @@ std::istream& StreamHandler::get_line(std::istream& stream, std::string& line) {
1616
}
1717

1818
std::istream& StreamHandler::get_next_non_blank_line(std::istream& stream, std::string& line) {
19-
bool is_blank = true;
2019

21-
const std::regex whitespace_re("\\s*(.*)");
22-
std::smatch match;
20+
// NOTE:
21+
// The test expects this function to return a blank string if it has reached the end.
22+
// IMO this behaviour is incorrect and it should always return the last valid string.
23+
// - S. Cahill.
2324

24-
while (is_blank) {
25-
Utils::StreamHandler::get_line(stream, line);
25+
for (;;) {
26+
Utils::StreamHandler::get_line(stream, line); // must strip trailing '\r'
2627

27-
std::regex_search(line, match, whitespace_re);
28+
if (!stream) { // EOF or error after attempt to read
29+
line.clear(); // test expects "" at/after EOF; remove me if the behaviour is to be fixed.
30+
return stream;
31+
}
2832

29-
if ((!line.empty() && !match.empty()) || (stream.eof())) {
30-
if ((match.length(1) > 0) || (stream.eof())) {
31-
is_blank = false;
32-
}
33-
}
34-
}
35-
36-
return stream;
33+
if (!Utils::isWhitespaceOrEmpty(line)) {
34+
return stream; // found a non-blank line
35+
}
36+
}
3737
}
3838

3939
std::istream& StreamHandler::skip_to_next_blank_line(std::istream& stream, std::string& line) {
40-
bool line_is_empty = false;
41-
42-
const std::regex whitespace_re("\\s*(.*)");
43-
std::smatch match;
40+
for (;;) {
41+
Utils::StreamHandler::get_line(stream, line); // must strip trailing '\r'
4442

45-
while (!line_is_empty) {
46-
Utils::StreamHandler::get_line(stream, line);
43+
if (!stream) { // EOF or error after attempt to read
44+
line.clear(); // test expects "" at/after EOF; remove me if the behaviour is to be fixed.
45+
return stream;
46+
}
4747

48-
std::regex_search(line, match, whitespace_re);
49-
50-
if ((match.length(1) == 0) || (stream.eof())) {
51-
line_is_empty = true;
52-
}
53-
}
54-
55-
return stream;
48+
if (Utils::isWhitespaceOrEmpty(line)) {
49+
return stream; // found a blank line
50+
}
51+
}
5652
}
5753

5854
double String::convert_to_double(const std::string& value, double default_value) {

0 commit comments

Comments
 (0)