Skip to content

Commit

Permalink
Fix quadratic runtime when linebreaking strings without newlines
Browse files Browse the repository at this point in the history
The problem was that every line would iterate from current line
start position to the end of the string, looking for a newline
to break on, leading to accidentally quadratic runtime. With this
change, the code only ever searches up to the current line's
length and not more.

Credit to @jorgenpt for the fix suggestion.

Closes #2315
  • Loading branch information
horenmar committed Nov 3, 2021
1 parent 931f41b commit 912df7d
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/catch2/internal/catch_textflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

// SPDX-License-Identifier: BSL-1.0
#include <catch2/internal/catch_textflow.hpp>

#include <algorithm>
#include <cstring>
#include <ostream>

Expand Down Expand Up @@ -41,13 +43,16 @@ namespace Catch {

void Column::const_iterator::calcLength() {
m_addHyphen = false;
const auto maxLineLength = m_column.m_width - indentSize();
m_parsedTo = m_lineStart;

std::string const& current_line = m_column.m_string;
if ( current_line[m_lineStart] == '\n' ) {
++m_parsedTo;
}
while ( m_parsedTo < current_line.size() &&

const auto maxLineLength = m_column.m_width - indentSize();
const auto maxParseTo = std::min(current_line.size(), m_lineStart + maxLineLength);
while ( m_parsedTo < maxParseTo &&
current_line[m_parsedTo] != '\n' ) {
++m_parsedTo;
}
Expand Down

0 comments on commit 912df7d

Please sign in to comment.