From 54ca219aad1e8164cacc425c388c727d3dd146d1 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Sat, 20 Apr 2013 11:27:28 +0100 Subject: [PATCH] Text class is now full replacement for LineWrap --- include/internal/catch_text.h | 65 ++++++++++++++++++---------------- projects/SelfTest/TestMain.cpp | 7 ++-- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/include/internal/catch_text.h b/include/internal/catch_text.h index 31c4e56d..7adcaa3b 100644 --- a/include/internal/catch_text.h +++ b/include/internal/catch_text.h @@ -42,45 +42,50 @@ namespace Catch { ? _attr.initialIndent : _attr.indent; std::string remainder = _str; - std::size_t tabPos = std::string::npos; while( !remainder.empty() ) { - std::size_t width = _attr.width - indent; - std::size_t wrapPos = width; + assert( lines.size() < 1000 ); + std::size_t tabPos = std::string::npos; + std::size_t width = (std::min)( remainder.size(), _attr.width - indent ); std::size_t pos = remainder.find_first_of( '\n' ); if( pos <= width ) { - wrapPos = pos; - addLine( indent, remainder.substr( 0, pos ) ); - remainder = remainder.substr( pos+1 ); - if( remainder.empty() ) - addLine (indent, "" ); // Trailing newlines result in extra line + width = pos; + } + pos = remainder.find_last_of( _attr.tabChar, width ); + if( pos != std::string::npos ) { + tabPos = pos; + if( remainder[width] == '\n' ) + width--; + remainder = remainder.substr( 0, tabPos ) + remainder.substr( tabPos+1 ); + } + if( width == remainder.size() ) { + addLine( indent, remainder ); + remainder = std::string(); + } + else if( remainder[width] == '\n' ) { + addLine( indent, remainder.substr( 0, width ) ); + indent = _attr.indent; + if( width > 1 && width == remainder.size()-1 ) + remainder = remainder.substr( width ); + else + remainder = remainder.substr( width+1 ); } else { - pos = remainder.find_last_of( _attr.tabChar, width ); + pos = remainder.find_last_of( wrappableChars, width ); if( pos != std::string::npos ) { - tabPos = pos; - remainder = remainder.substr( 0, tabPos ) + remainder.substr( tabPos+1 ); + addLine( indent, remainder.substr( 0, pos ) ); + if( remainder[pos] == ' ' ) + pos++; + remainder = remainder.substr( pos ); } - if( remainder.size() <= width ) { - addLine( indent, remainder ); - remainder = std::string(); - } - else { - pos = remainder.find_last_of( wrappableChars, width ); - if( pos == std::string::npos ) { - addLine( indent, remainder.substr( 0, width-1 ) + "-" ); - remainder = remainder.substr( width-1 ); - } - else { - addLine( indent, remainder.substr( 0, pos ) ); - if( remainder[pos] == ' ' ) - pos++; - remainder = remainder.substr( pos ); - } + else { + addLine( indent, remainder.substr( 0, width-1 ) + "-" ); + remainder = remainder.substr( width-1 ); } + if( lines.size() == 1 ) + indent = _attr.indent; + if( tabPos != std::string::npos ) + indent += tabPos; } - indent = tabPos == std::string::npos - ? _attr.indent - : indent + tabPos; }; } void addLine( std::size_t indent, std::string const& _line ) { diff --git a/projects/SelfTest/TestMain.cpp b/projects/SelfTest/TestMain.cpp index 7e47725e..19e8f68b 100644 --- a/projects/SelfTest/TestMain.cpp +++ b/projects/SelfTest/TestMain.cpp @@ -507,10 +507,10 @@ TEST_CASE( "Long strings can be wrapped", "[wrap]" ) { SECTION( "With tabs", "" ) { // guide: 1234567890123456789 - std::string testString = "one two /tthree four five six"; + std::string testString = "one two \tthree four five six"; - CHECK( Text( testString, TextAttributes().setWidth( 18 ) ).toString() - == "one two three/n four/n five/n six" ); + CHECK( Text( testString, TextAttributes().setWidth( 15 ) ).toString() + == "one two three\n four\n five\n six" ); } @@ -609,7 +609,6 @@ TEST_CASE( "Strings can be rendered with colour", "[colour]" ) { } TEST_CASE( "Text can be formatted using the Text class", "" ) { - Text text( "hi there" ); CHECK( Text( "hi there" ).toString() == "hi there" );