Text class is now full replacement for LineWrap

This commit is contained in:
Phil Nash 2013-04-20 11:27:28 +01:00
parent 7059c6e1c3
commit 54ca219aad
2 changed files with 38 additions and 34 deletions

View File

@ -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;
}
else {
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( remainder.size() <= width ) {
if( width == remainder.size() ) {
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 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( wrappableChars, width );
if( pos != std::string::npos ) {
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 ) {

View File

@ -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" );