diff options
| author | Martin Hořeňovský | 2021-11-01 19:14:37 +0100 |
|---|---|---|
| committer | Martin Hořeňovský | 2021-11-01 19:14:37 +0100 |
| commit | 70c4ec78fbbfadb19d520c72b83f33cd0915c078 (patch) | |
| tree | d69fa4c9cd661625b6e72d42b91b63b611e0adb0 /src/catch2/internal/catch_textflow.cpp | |
| parent | 455ae0c561b6254e05c5fe619b08968187d28a70 (diff) | |
| download | catch3-70c4ec78fbbfadb19d520c72b83f33cd0915c078.tar.gz | |
Improve comments and names in TextFlow::Column
Diffstat (limited to 'src/catch2/internal/catch_textflow.cpp')
| -rw-r--r-- | src/catch2/internal/catch_textflow.cpp | 80 |
1 files changed, 44 insertions, 36 deletions
diff --git a/src/catch2/internal/catch_textflow.cpp b/src/catch2/internal/catch_textflow.cpp index 1e39f39a..f8b8e7f4 100644 --- a/src/catch2/internal/catch_textflow.cpp +++ b/src/catch2/internal/catch_textflow.cpp @@ -40,42 +40,50 @@ namespace Catch { namespace TextFlow { void Column::const_iterator::calcLength() { - m_suffix = false; - auto width = m_column.m_width - indent(); - m_end = m_pos; + 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_pos] == '\n' ) { - ++m_end; + if ( current_line[m_lineStart] == '\n' ) { + ++m_parsedTo; } - while ( m_end < current_line.size() && - current_line[m_end] != '\n' ) { - ++m_end; + while ( m_parsedTo < current_line.size() && + current_line[m_parsedTo] != '\n' ) { + ++m_parsedTo; } - if ( m_end < m_pos + width ) { - m_len = m_end - m_pos; + // If we encountered a newline before the column is filled, + // then we linebreak at the newline and consider this line + // finished. + if ( m_parsedTo < m_lineStart + maxLineLength ) { + m_lineLength = m_parsedTo - m_lineStart; } else { - size_t len = width; - while ( len > 0 && !isBoundary( current_line, m_pos + len ) ) { - --len; + // Look for a natural linebreak boundary in the column + // (We look from the end, so that the first found boundary is + // the right one) + size_t newLineLength = maxLineLength; + while ( newLineLength > 0 && !isBoundary( current_line, m_lineStart + newLineLength ) ) { + --newLineLength; } - while ( len > 0 && - isWhitespace( current_line[m_pos + len - 1] ) ) { - --len; + while ( newLineLength > 0 && + isWhitespace( current_line[m_lineStart + newLineLength - 1] ) ) { + --newLineLength; } - if ( len > 0 ) { - m_len = len; + // If we found one, then that is where we linebreak + if ( newLineLength > 0 ) { + m_lineLength = newLineLength; } else { - m_suffix = true; - m_len = width - 1; + // Otherwise we have to split text with a hyphen + m_addHyphen = true; + m_lineLength = maxLineLength - 1; } } } - size_t Column::const_iterator::indent() const { + size_t Column::const_iterator::indentSize() const { auto initial = - m_pos == 0 ? m_column.m_initialIndent : std::string::npos; + m_lineStart == 0 ? m_column.m_initialIndent : std::string::npos; return initial == std::string::npos ? m_column.m_indent : initial; } @@ -83,11 +91,11 @@ namespace Catch { Column::const_iterator::addIndentAndSuffix( size_t position, size_t length ) const { std::string ret; - const auto desired_indent = indent(); - ret.reserve( desired_indent + length + m_suffix ); + const auto desired_indent = indentSize(); + ret.reserve( desired_indent + length + m_addHyphen ); ret.append( desired_indent, ' ' ); ret.append( m_column.m_string, position, length ); - if ( m_suffix ) { + if ( m_addHyphen ) { ret.push_back( '-' ); } @@ -99,29 +107,29 @@ namespace Catch { assert( m_column.m_initialIndent == std::string::npos || m_column.m_width > m_column.m_initialIndent ); calcLength(); - if ( m_len == 0 ) { - m_pos = m_column.m_string.size(); + if ( m_lineLength == 0 ) { + m_lineStart = m_column.m_string.size(); } } std::string Column::const_iterator::operator*() const { - assert( m_pos <= m_end ); - return addIndentAndSuffix( m_pos, m_len ); + assert( m_lineStart <= m_parsedTo ); + return addIndentAndSuffix( m_lineStart, m_lineLength ); } Column::const_iterator& Column::const_iterator::operator++() { - m_pos += m_len; + m_lineStart += m_lineLength; std::string const& current_line = m_column.m_string; - if ( m_pos < current_line.size() && current_line[m_pos] == '\n' ) { - m_pos += 1; + if ( m_lineStart < current_line.size() && current_line[m_lineStart] == '\n' ) { + m_lineStart += 1; } else { - while ( m_pos < current_line.size() && - isWhitespace( current_line[m_pos] ) ) { - ++m_pos; + while ( m_lineStart < current_line.size() && + isWhitespace( current_line[m_lineStart] ) ) { + ++m_lineStart; } } - if ( m_pos != current_line.size() ) { + if ( m_lineStart != current_line.size() ) { calcLength(); } return *this; |