Skip to content

Commit

Permalink
Clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed Apr 3, 2024
1 parent 9ccf930 commit 3819226
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 86 deletions.
149 changes: 87 additions & 62 deletions src/catch2/internal/catch_textflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,58 +31,76 @@ namespace {
namespace Catch {
namespace TextFlow {
void AnsiSkippingString::preprocessString() {
for(auto it = m_string.begin(); it != m_string.end(); ) {
for ( auto it = m_string.begin(); it != m_string.end(); ) {
// try to read through an ansi sequence
while(it != m_string.end() && *it == '\033' && it + 1 != m_string.end() && *(it + 1) == '[') {
while ( it != m_string.end() && *it == '\033' &&
it + 1 != m_string.end() && *( it + 1 ) == '[' ) {
auto cursor = it + 2;
while(cursor != m_string.end() && (isdigit(*cursor) || *cursor == ';')) {
while ( cursor != m_string.end() &&
( isdigit( *cursor ) || *cursor == ';' ) ) {
++cursor;
}
if(cursor != m_string.end() && *cursor == 'm') {
if ( cursor != m_string.end() && *cursor == 'm' ) {
// 'm' -> 0xff
*cursor = AnsiSkippingString::sentinel;
// if we've read an ansi sequence, set the iterator and return to the top of the loop
// if we've read an ansi sequence, set the iterator and
// return to the top of the loop
it = cursor + 1;
} else {
break;

Check warning on line 50 in src/catch2/internal/catch_textflow.cpp

View check run for this annotation

Codecov / codecov/patch

src/catch2/internal/catch_textflow.cpp#L50

Added line #L50 was not covered by tests
}
}
if(it != m_string.end()) {
if ( it != m_string.end() ) {
++m_size;
++it;
}
}
}

AnsiSkippingString::AnsiSkippingString(std::string const& text) : m_string(text) {
AnsiSkippingString::AnsiSkippingString( std::string const& text ):
m_string( text ) {
preprocessString();
}

AnsiSkippingString::const_iterator AnsiSkippingString::begin() const { return const_iterator(m_string); }
AnsiSkippingString::const_iterator AnsiSkippingString::begin() const {
return const_iterator( m_string );
}

AnsiSkippingString::const_iterator AnsiSkippingString::end() const { return const_iterator(m_string, const_iterator::EndTag{}); }
AnsiSkippingString::const_iterator AnsiSkippingString::end() const {
return const_iterator( m_string, const_iterator::EndTag{} );
}

std::string AnsiSkippingString::substring(const_iterator begin, const_iterator end) const {
// There's one caveat here to an otherwise simple substring: when making a begin iterator we might have
// skipped ansi sequences at the start. If `begin` here is a begin iterator, skipped over initial ansi
// sequences, we'll use the true beginning of the string.
// Lastly: We need to transform any chars we replaced with 0xff back to 'm'
auto str = std::string(begin == this->begin() ? m_string.begin() : begin.m_it, end.m_it);
std::transform(str.begin(), str.end(), str.begin(), [](char c) {
std::string AnsiSkippingString::substring( const_iterator begin,
const_iterator end ) const {
// There's one caveat here to an otherwise simple substring: when
// making a begin iterator we might have skipped ansi sequences at
// the start. If `begin` here is a begin iterator, skipped over
// initial ansi sequences, we'll use the true beginning of the
// string. Lastly: We need to transform any chars we replaced with
// 0xff back to 'm'
auto str = std::string( begin == this->begin() ? m_string.begin()
: begin.m_it,
end.m_it );
std::transform( str.begin(), str.end(), str.begin(), []( char c ) {
return c == AnsiSkippingString::sentinel ? 'm' : c;
});
} );
return str;
}

void AnsiSkippingString::const_iterator::tryParseAnsiEscapes() {
// check if we've landed on an ansi sequence, and if so read through it
while(m_it != m_string->end() && *m_it == '\033' && m_it + 1 != m_string->end() && *(m_it + 1) == '[') {
// check if we've landed on an ansi sequence, and if so read through
// it
while ( m_it != m_string->end() && *m_it == '\033' &&
m_it + 1 != m_string->end() && *( m_it + 1 ) == '[' ) {
auto cursor = m_it + 2;
while(cursor != m_string->end() && (isdigit(*cursor) || *cursor == ';')) {
while ( cursor != m_string->end() &&
( isdigit( *cursor ) || *cursor == ';' ) ) {
++cursor;
}
if(cursor != m_string->end() && *cursor == AnsiSkippingString::sentinel) {
// if we've read an ansi sequence, set the iterator and return to the top of the loop
if ( cursor != m_string->end() &&
*cursor == AnsiSkippingString::sentinel ) {
// if we've read an ansi sequence, set the iterator and
// return to the top of the loop
m_it = cursor + 1;
} else {
break;

Check warning on line 106 in src/catch2/internal/catch_textflow.cpp

View check run for this annotation

Codecov / codecov/patch

src/catch2/internal/catch_textflow.cpp#L106

Added line #L106 was not covered by tests
Expand All @@ -96,46 +114,48 @@ namespace Catch {
}

void AnsiSkippingString::const_iterator::unadvance() {
assert(m_it != m_string->begin());
assert( m_it != m_string->begin() );
m_it--;
// if *m_it is 0xff, scan back to the \033 and then m_it-- once more (and repeat check)
while(*m_it == AnsiSkippingString::sentinel) {
while(*m_it != '\033') {
assert(m_it != m_string->begin());
// if *m_it is 0xff, scan back to the \033 and then m_it-- once more
// (and repeat check)
while ( *m_it == AnsiSkippingString::sentinel ) {
while ( *m_it != '\033' ) {
assert( m_it != m_string->begin() );
m_it--;
}
// if this happens, we must have been a begin iterator that had skipped over ansi sequences at the
// start of a string
assert(m_it != m_string->begin());
assert(*m_it == '\033');
// if this happens, we must have been a begin iterator that had
// skipped over ansi sequences at the start of a string
assert( m_it != m_string->begin() );
assert( *m_it == '\033' );
m_it--;
}
}

static bool isBoundary( AnsiSkippingString const& line, AnsiSkippingString::const_iterator it ) {
static bool isBoundary( AnsiSkippingString const& line,
AnsiSkippingString::const_iterator it ) {
return it == line.end() ||
( isWhitespace( *it ) && !isWhitespace( *it.oneBefore() ) ) ||
isBreakableBefore( *it ) ||
isBreakableAfter( *it.oneBefore() );
( isWhitespace( *it ) &&
!isWhitespace( *it.oneBefore() ) ) ||
isBreakableBefore( *it ) ||
isBreakableAfter( *it.oneBefore() );
}

void Column::const_iterator::calcLength() {
m_addHyphen = false;
m_parsedTo = m_lineStart;
AnsiSkippingString const& current_line = m_column.m_string;

if(m_parsedTo == current_line.end()) {
if ( m_parsedTo == current_line.end() ) {
m_lineEnd = m_parsedTo;
return;
}

if ( *m_lineStart == '\n' ) {
++m_parsedTo;
}
if ( *m_lineStart == '\n' ) { ++m_parsedTo; }

const auto maxLineLength = m_column.m_width - indentSize();
std::size_t lineLength = 0;
while ( m_parsedTo != current_line.end() && lineLength < maxLineLength && *m_parsedTo != '\n' ) {
while ( m_parsedTo != current_line.end() &&
lineLength < maxLineLength && *m_parsedTo != '\n' ) {
++m_parsedTo;
++lineLength;
}
Expand All @@ -150,16 +170,19 @@ namespace Catch {
// (We look from the end, so that the first found boundary is
// the right one)
m_lineEnd = m_parsedTo;
while ( lineLength > 0 && !isBoundary( current_line, m_lineEnd )) {
while ( lineLength > 0 &&
!isBoundary( current_line, m_lineEnd ) ) {
--lineLength;
--m_lineEnd;
}
while ( lineLength > 0 && isWhitespace( *m_lineEnd.oneBefore() ) ) {
while ( lineLength > 0 &&
isWhitespace( *m_lineEnd.oneBefore() ) ) {
--lineLength;
--m_lineEnd;
}

// If we found one, then that is where we linebreak, otherwise we have to split text with a hyphen
// If we found one, then that is where we linebreak, otherwise
// we have to split text with a hyphen
if ( lineLength == 0 ) {
m_addHyphen = true;
m_lineEnd = m_parsedTo.oneBefore();
Expand All @@ -168,28 +191,31 @@ namespace Catch {
}

size_t Column::const_iterator::indentSize() const {
auto initial =
m_lineStart == m_column.m_string.begin() ? m_column.m_initialIndent : std::string::npos;
auto initial = m_lineStart == m_column.m_string.begin()
? m_column.m_initialIndent
: std::string::npos;
return initial == std::string::npos ? m_column.m_indent : initial;
}

std::string
Column::const_iterator::addIndentAndSuffix( AnsiSkippingString::const_iterator start,
AnsiSkippingString::const_iterator end ) const {
std::string Column::const_iterator::addIndentAndSuffix(
AnsiSkippingString::const_iterator start,
AnsiSkippingString::const_iterator end ) const {
std::string ret;
const auto desired_indent = indentSize();
// ret.reserve( desired_indent + (end - start) + m_addHyphen );
ret.append( desired_indent, ' ' );
// ret.append( start, end );
ret += m_column.m_string.substring(start, end);
if ( m_addHyphen ) {
ret.push_back( '-' );
}
ret += m_column.m_string.substring( start, end );
if ( m_addHyphen ) { ret.push_back( '-' ); }

return ret;
}

Column::const_iterator::const_iterator( Column const& column ): m_column( column ), m_lineStart(column.m_string.begin()), m_lineEnd(column.m_string.begin()), m_parsedTo(column.m_string.begin()) {
Column::const_iterator::const_iterator( Column const& column ):
m_column( column ),
m_lineStart( column.m_string.begin() ),
m_lineEnd( column.m_string.begin() ),
m_parsedTo( column.m_string.begin() ) {
assert( m_column.m_width > m_column.m_indent );
assert( m_column.m_initialIndent == std::string::npos ||
m_column.m_width > m_column.m_initialIndent );
Expand All @@ -210,14 +236,13 @@ namespace Catch {
if ( m_lineStart != current_line.end() && *m_lineStart == '\n' ) {
m_lineStart++;
} else {
while ( m_lineStart != current_line.end() && isWhitespace( *m_lineStart ) ) {
while ( m_lineStart != current_line.end() &&
isWhitespace( *m_lineStart ) ) {
++m_lineStart;
}
}

if ( m_lineStart != current_line.end() ) {
calcLength();
}
if ( m_lineStart != current_line.end() ) { calcLength(); }
return *this;
}

Expand Down Expand Up @@ -314,25 +339,25 @@ namespace Catch {
return os;
}

Columns operator+(Column const& lhs, Column const& rhs) {
Columns operator+( Column const& lhs, Column const& rhs ) {

Check warning on line 342 in src/catch2/internal/catch_textflow.cpp

View check run for this annotation

Codecov / codecov/patch

src/catch2/internal/catch_textflow.cpp#L342

Added line #L342 was not covered by tests
Columns cols;
cols += lhs;
cols += rhs;
return cols;
}
Columns operator+(Column&& lhs, Column&& rhs) {
Columns operator+( Column&& lhs, Column&& rhs ) {
Columns cols;
cols += CATCH_MOVE( lhs );
cols += CATCH_MOVE( rhs );
return cols;
}

Columns& operator+=(Columns& lhs, Column const& rhs) {
Columns& operator+=( Columns& lhs, Column const& rhs ) {

Check warning on line 355 in src/catch2/internal/catch_textflow.cpp

View check run for this annotation

Codecov / codecov/patch

src/catch2/internal/catch_textflow.cpp#L355

Added line #L355 was not covered by tests
lhs.m_columns.push_back( rhs );
return lhs;
}
Columns& operator+=(Columns& lhs, Column&& rhs) {
lhs.m_columns.push_back( CATCH_MOVE(rhs) );
Columns& operator+=( Columns& lhs, Column&& rhs ) {
lhs.m_columns.push_back( CATCH_MOVE( rhs ) );
return lhs;
}
Columns operator+( Columns const& lhs, Column const& rhs ) {
Expand Down
Loading

0 comments on commit 3819226

Please sign in to comment.