Text formatting rework

Rewrote main wrapping loop. Now uses iterators instead of indices and intermediate strings.
Differentiates between chars to wrap before, after or instead of.
Doesn’t preserve trailing newlines.
Wraps or more characters.
Dropped support for using tab character as an indent setting control char.
Hopefully avoids all the undefined behaviour and other bugs of the previous implementation.
This commit is contained in:
Phil Nash 2017-01-17 17:13:23 +00:00
parent 9a56609569
commit 4a04682e49
6 changed files with 325 additions and 130 deletions

View File

@ -37,19 +37,16 @@ namespace Tbc {
TextAttributes()
: initialIndent( std::string::npos ),
indent( 0 ),
width( consoleWidth-1 ),
tabChar( '\t' )
width( consoleWidth-1 )
{}
TextAttributes& setInitialIndent( std::size_t _value ) { initialIndent = _value; return *this; }
TextAttributes& setIndent( std::size_t _value ) { indent = _value; return *this; }
TextAttributes& setWidth( std::size_t _value ) { width = _value; return *this; }
TextAttributes& setTabChar( char _value ) { tabChar = _value; return *this; }
std::size_t initialIndent; // indent of first line, or npos
std::size_t indent; // indent of subsequent lines, or all if initialIndent is npos
std::size_t width; // maximum width of text, including indent. Longer text will wrap
char tabChar; // If this char is seen the indent is changed to current pos
};
class Text {
@ -57,63 +54,74 @@ namespace Tbc {
Text( std::string const& _str, TextAttributes const& _attr = TextAttributes() )
: attr( _attr )
{
std::string wrappableChars = " [({.,/|\\-";
std::size_t indent = _attr.initialIndent != std::string::npos
? _attr.initialIndent
: _attr.indent;
std::string remainder = _str;
const std::string wrappableBeforeChars = "[({<\t";
const std::string wrappableAfterChars = "])}>-,./|\\";
const std::string wrappableInsteadOfChars = " \n\r";
std::string indent = _attr.initialIndent != std::string::npos
? std::string( _attr.initialIndent, ' ' )
: std::string( _attr.indent, ' ' );
typedef std::string::const_iterator iterator;
iterator it = _str.begin();
const iterator strEnd = _str.end();
while( it != strEnd ) {
while( !remainder.empty() ) {
if( lines.size() >= 1000 ) {
lines.push_back( "... message truncated due to excessive size" );
return;
}
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 ) {
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() ) {
spliceLine( indent, remainder, width );
std::string suffix;
std::size_t width = (std::min)( static_cast<size_t>( strEnd-it ), _attr.width-static_cast<size_t>( indent.size() ) );
iterator itEnd = it+width;
iterator itNext = _str.end();
iterator itNewLine = std::find( it, itEnd, '\n' );
if( itNewLine != itEnd )
itEnd = itNewLine;
if( itEnd != strEnd ) {
bool foundWrapPoint = false;
for( iterator findIt = itEnd; !foundWrapPoint & findIt >= it; --findIt ) {
if( wrappableAfterChars.find( *findIt ) != std::string::npos && findIt != itEnd ) {
itEnd = findIt+1;
itNext = findIt+1;
foundWrapPoint = true;
}
else if( remainder[width] == '\n' ) {
spliceLine( indent, remainder, width );
if( width <= 1 || remainder.size() != 1 )
remainder = remainder.substr( 1 );
indent = _attr.indent;
else if( findIt > it && wrappableBeforeChars.find( *findIt ) != std::string::npos ) {
itEnd = findIt;
itNext = findIt;
foundWrapPoint = true;
}
else if( wrappableInsteadOfChars.find( *findIt ) != std::string::npos ) {
itNext = findIt+1;
itEnd = findIt;
foundWrapPoint = true;
}
}
if( !foundWrapPoint ) {
// No good wrap char, so we'll break mid word and add a hyphen
--itEnd;
itNext = itEnd;
suffix = "-";
}
else {
pos = remainder.find_last_of( wrappableChars, width );
if( pos != std::string::npos && pos > 0 ) {
spliceLine( indent, remainder, pos );
if( remainder[0] == ' ' )
remainder = remainder.substr( 1 );
while( itEnd > it && wrappableInsteadOfChars.find( *(itEnd-1) ) != std::string::npos )
--itEnd;
}
else {
spliceLine( indent, remainder, width-1 );
lines.back() += "-";
}
if( lines.size() == 1 )
indent = _attr.indent;
if( tabPos != std::string::npos )
indent += tabPos;
}
lines.push_back( indent + std::string( it, itEnd ) + suffix );
if( indent.size() != _attr.indent )
indent = std::string( _attr.indent, ' ' );
it = itNext;
}
}
void spliceLine( std::size_t _indent, std::string& _remainder, std::size_t _pos ) {
lines.push_back( std::string( _indent, ' ' ) + _remainder.substr( 0, _pos ) );
_remainder = _remainder.substr( _pos );
}
typedef std::vector<std::string>::const_iterator const_iterator;
@ -138,6 +146,7 @@ namespace Tbc {
return _stream;
}
private:
std::string str;
TextAttributes attr;

View File

@ -831,5 +831,5 @@ with expansion:
===============================================================================
test cases: 159 | 115 passed | 42 failed | 2 failed as expected
assertions: 909 | 813 passed | 78 failed | 18 failed as expected
assertions: 915 | 819 passed | 78 failed | 18 failed as expected

View File

@ -2932,13 +2932,9 @@ TestMain.cpp:<line number>
TestMain.cpp:<line number>:
PASSED:
CHECK( Text( "abcdef\n", TextAttributes().setWidth( 10 ) ).toString() == "abcdef\n" )
CHECK( Text( "abcdef\n", TextAttributes().setWidth( 10 ) ).toString() == "abcdef" )
with expansion:
"abcdef
"
==
"abcdef
"
"abcdef" == "abcdef"
TestMain.cpp:<line number>:
PASSED:
@ -2948,13 +2944,19 @@ with expansion:
TestMain.cpp:<line number>:
PASSED:
CHECK( Text( "abcdef\n", TextAttributes().setWidth( 6 ) ).toString() == "abcdef\n" )
CHECK( Text( "abcdef\n", TextAttributes().setWidth( 6 ) ).toString() == "abcdef" )
with expansion:
"abcdef
"
"abcdef" == "abcdef"
TestMain.cpp:<line number>:
PASSED:
CHECK( Text( "abcdef\n", TextAttributes().setWidth( 5 ) ).toString() == "abcd-\nef" )
with expansion:
"abcd-
ef"
==
"abcdef
"
"abcd-
ef"
-------------------------------------------------------------------------------
Long strings can be wrapped
@ -3024,24 +3026,107 @@ with expansion:
-------------------------------------------------------------------------------
Long strings can be wrapped
With tabs
With wrap-before/ after characters
No wrapping
-------------------------------------------------------------------------------
TestMain.cpp:<line number>
...............................................................................
TestMain.cpp:<line number>:
PASSED:
CHECK( Text( testString, TextAttributes().setWidth( 15 ) ).toString() == "one two three\n four\n five\n six" )
CHECK( Text( testString, TextAttributes().setWidth( 80 ) ).toString() == testString )
with expansion:
"one two three
four
five
six"
"one,two(three) <here>"
==
"one two three
four
five
six"
"one,two(three) <here>"
TestMain.cpp:<line number>:
PASSED:
CHECK( Text( testString, TextAttributes().setWidth( 24 ) ).toString() == testString )
with expansion:
"one,two(three) <here>"
==
"one,two(three) <here>"
-------------------------------------------------------------------------------
Long strings can be wrapped
With wrap-before/ after characters
Wrap before
-------------------------------------------------------------------------------
TestMain.cpp:<line number>
...............................................................................
TestMain.cpp:<line number>:
PASSED:
CHECK( Text( testString, TextAttributes().setWidth( 11 ) ).toString() == "one,two\n(three)\n<here>" )
with expansion:
"one,two
(three)
<here>"
==
"one,two
(three)
<here>"
-------------------------------------------------------------------------------
Long strings can be wrapped
With wrap-before/ after characters
Wrap after
-------------------------------------------------------------------------------
TestMain.cpp:<line number>
...............................................................................
TestMain.cpp:<line number>:
PASSED:
CHECK( Text( testString, TextAttributes().setWidth( 6 ) ).toString() == "one,\ntwo\n(thre-\ne)\n<here>" )
with expansion:
"one,
two
(thre-
e)
<here>"
==
"one,
two
(thre-
e)
<here>"
TestMain.cpp:<line number>:
PASSED:
CHECK( Text( testString, TextAttributes().setWidth( 5 ) ).toString() == "one,\ntwo\n(thr-\nee)\n<her-\ne>" )
with expansion:
"one,
two
(thr-
ee)
<her-
e>"
==
"one,
two
(thr-
ee)
<her-
e>"
TestMain.cpp:<line number>:
PASSED:
CHECK( Text( testString, TextAttributes().setWidth( 4 ) ).toString() == "one,\ntwo\n(th-\nree)\n<he-\nre>" )
with expansion:
"one,
two
(th-
ree)
<he-
re>"
==
"one,
two
(th-
ree)
<he-
re>"
-------------------------------------------------------------------------------
Long text is truncted
@ -6479,8 +6564,7 @@ PASSED:
with expansion:
"error: tag alias, "[@zzz]" already registered.
First seen at file:2
Redefined at file:10" contains: "
[@zzz]"
Redefined at file:10" contains: "[@zzz]"
TagAliasTests.cpp:<line number>:
PASSED:
@ -6488,8 +6572,7 @@ PASSED:
with expansion:
"error: tag alias, "[@zzz]" already registered.
First seen at file:2
Redefined at file:10" contains:
"file"
Redefined at file:10" contains: "file"
TagAliasTests.cpp:<line number>:
PASSED:
@ -6497,8 +6580,7 @@ PASSED:
with expansion:
"error: tag alias, "[@zzz]" already registered.
First seen at file:2
Redefined at file:10" contains:
"2"
Redefined at file:10" contains: "2"
TagAliasTests.cpp:<line number>:
PASSED:
@ -6506,8 +6588,7 @@ PASSED:
with expansion:
"error: tag alias, "[@zzz]" already registered.
First seen at file:2
Redefined at file:10" contains:
"10"
Redefined at file:10" contains: "10"
-------------------------------------------------------------------------------
Tag alias can be registered against tag patterns
@ -8972,5 +9053,5 @@ PASSED:
===============================================================================
test cases: 159 | 114 passed | 43 failed | 2 failed as expected
assertions: 911 | 813 passed | 80 failed | 18 failed as expected
assertions: 917 | 819 passed | 80 failed | 18 failed as expected

View File

@ -1,6 +1,6 @@
<?xml version="1.1" encoding="UTF-8"?>
<testsuites>
<testsuite name="<exe-name>" errors="13" failures="68" tests="912" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testsuite name="<exe-name>" errors="13" failures="68" tests="918" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testcase classname="global" name="# A test name that starts with a #" time="{duration}"/>
<testcase classname="global" name="'Not' checks that should fail" time="{duration}">
<failure message="false != false" type="CHECK">
@ -242,7 +242,9 @@ ConditionTests.cpp:<line number>
<testcase classname="Long strings can be wrapped" name="With newlines/Trailing newline" time="{duration}"/>
<testcase classname="Long strings can be wrapped" name="With newlines/Wrapped once" time="{duration}"/>
<testcase classname="Long strings can be wrapped" name="With newlines/Wrapped twice" time="{duration}"/>
<testcase classname="Long strings can be wrapped" name="With tabs" time="{duration}"/>
<testcase classname="Long strings can be wrapped" name="With wrap-before/ after characters/No wrapping" time="{duration}"/>
<testcase classname="Long strings can be wrapped" name="With wrap-before/ after characters/Wrap before" time="{duration}"/>
<testcase classname="Long strings can be wrapped" name="With wrap-before/ after characters/Wrap after" time="{duration}"/>
<testcase classname="global" name="Long text is truncted" time="{duration}"/>
<testcase classname="global" name="ManuallyRegistered" time="{duration}"/>
<testcase classname="global" name="Matchers can be (AllOf) composed with the &amp;&amp; operator" time="{duration}"/>

View File

@ -2987,14 +2987,10 @@ three four"
<Section name="Trailing newline">
<Expression success="true" type="CHECK" filename="projects/<exe-name>/TestMain.cpp" >
<Original>
Text( "abcdef\n", TextAttributes().setWidth( 10 ) ).toString() == "abcdef\n"
Text( "abcdef\n", TextAttributes().setWidth( 10 ) ).toString() == "abcdef"
</Original>
<Expanded>
"abcdef
"
==
"abcdef
"
"abcdef" == "abcdef"
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/TestMain.cpp" >
@ -3007,19 +3003,27 @@ three four"
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/TestMain.cpp" >
<Original>
Text( "abcdef\n", TextAttributes().setWidth( 6 ) ).toString() == "abcdef\n"
Text( "abcdef\n", TextAttributes().setWidth( 6 ) ).toString() == "abcdef"
</Original>
<Expanded>
"abcdef
"
==
"abcdef
"
"abcdef" == "abcdef"
</Expanded>
</Expression>
<OverallResults successes="3" failures="0" expectedFailures="0"/>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/TestMain.cpp" >
<Original>
Text( "abcdef\n", TextAttributes().setWidth( 5 ) ).toString() == "abcd-\nef"
</Original>
<Expanded>
"abcd-
ef"
==
"abcd-
ef"
</Expanded>
</Expression>
<OverallResults successes="4" failures="0" expectedFailures="0"/>
</Section>
<OverallResults successes="3" failures="0" expectedFailures="0"/>
<OverallResults successes="4" failures="0" expectedFailures="0"/>
</Section>
<Section name="With newlines">
<Section name="Wrapped once">
@ -3091,25 +3095,116 @@ four"
</Section>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<Section name="With tabs">
<Section name="With wrap-before/ after characters">
<Section name="No wrapping">
<Expression success="true" type="CHECK" filename="projects/<exe-name>/TestMain.cpp" >
<Original>
Text( testString, TextAttributes().setWidth( 15 ) ).toString() == "one two three\n four\n five\n six"
Text( testString, TextAttributes().setWidth( 80 ) ).toString() == testString
</Original>
<Expanded>
"one two three
four
five
six"
"one,two(three) &lt;here>"
==
"one two three
four
five
six"
"one,two(three) &lt;here>"
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/TestMain.cpp" >
<Original>
Text( testString, TextAttributes().setWidth( 24 ) ).toString() == testString
</Original>
<Expanded>
"one,two(three) &lt;here>"
==
"one,two(three) &lt;here>"
</Expanded>
</Expression>
<OverallResults successes="2" failures="0" expectedFailures="0"/>
</Section>
<OverallResults successes="2" failures="0" expectedFailures="0"/>
</Section>
<Section name="With wrap-before/ after characters">
<Section name="Wrap before">
<Expression success="true" type="CHECK" filename="projects/<exe-name>/TestMain.cpp" >
<Original>
Text( testString, TextAttributes().setWidth( 11 ) ).toString() == "one,two\n(three)\n&lt;here>"
</Original>
<Expanded>
"one,two
(three)
&lt;here>"
==
"one,two
(three)
&lt;here>"
</Expanded>
</Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<Section name="With wrap-before/ after characters">
<Section name="Wrap after">
<Expression success="true" type="CHECK" filename="projects/<exe-name>/TestMain.cpp" >
<Original>
Text( testString, TextAttributes().setWidth( 6 ) ).toString() == "one,\ntwo\n(thre-\ne)\n&lt;here>"
</Original>
<Expanded>
"one,
two
(thre-
e)
&lt;here>"
==
"one,
two
(thre-
e)
&lt;here>"
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/TestMain.cpp" >
<Original>
Text( testString, TextAttributes().setWidth( 5 ) ).toString() == "one,\ntwo\n(thr-\nee)\n&lt;her-\ne>"
</Original>
<Expanded>
"one,
two
(thr-
ee)
&lt;her-
e>"
==
"one,
two
(thr-
ee)
&lt;her-
e>"
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/TestMain.cpp" >
<Original>
Text( testString, TextAttributes().setWidth( 4 ) ).toString() == "one,\ntwo\n(th-\nree)\n&lt;he-\nre>"
</Original>
<Expanded>
"one,
two
(th-
ree)
&lt;he-
re>"
==
"one,
two
(th-
ree)
&lt;he-
re>"
</Expanded>
</Expression>
<OverallResults successes="3" failures="0" expectedFailures="0"/>
</Section>
<OverallResults successes="3" failures="0" expectedFailures="0"/>
</Section>
<OverallResult success="true"/>
</TestCase>
<TestCase name="Long text is truncted">
@ -9432,7 +9527,7 @@ there"
</Section>
<OverallResult success="true"/>
</TestCase>
<OverallResults successes="813" failures="81" expectedFailures="18"/>
<OverallResults successes="819" failures="81" expectedFailures="18"/>
</Group>
<OverallResults successes="813" failures="80" expectedFailures="18"/>
<OverallResults successes="819" failures="80" expectedFailures="18"/>
</Catch>

View File

@ -299,9 +299,10 @@ TEST_CASE( "Long strings can be wrapped", "[wrap]" ) {
CHECK( Text( testString, TextAttributes().setWidth( 10 ) ).toString() == testString );
}
SECTION( "Trailing newline" , "" ) {
CHECK( Text( "abcdef\n", TextAttributes().setWidth( 10 ) ).toString() == "abcdef\n" );
CHECK( Text( "abcdef\n", TextAttributes().setWidth( 10 ) ).toString() == "abcdef" );
CHECK( Text( "abcdef", TextAttributes().setWidth( 6 ) ).toString() == "abcdef" );
CHECK( Text( "abcdef\n", TextAttributes().setWidth( 6 ) ).toString() == "abcdef\n" );
CHECK( Text( "abcdef\n", TextAttributes().setWidth( 6 ) ).toString() == "abcdef" );
CHECK( Text( "abcdef\n", TextAttributes().setWidth( 5 ) ).toString() == "abcd-\nef" );
}
SECTION( "Wrapped once", "" ) {
CHECK( Text( testString, TextAttributes().setWidth( 9 ) ).toString() == "one two\nthree\nfour" );
@ -313,15 +314,22 @@ TEST_CASE( "Long strings can be wrapped", "[wrap]" ) {
}
}
SECTION( "With tabs", "" ) {
SECTION( "With wrap-before/ after characters", "" ) {
std::string testString = "one,two(three) <here>";
// guide: 1234567890123456789
std::string testString = "one two \tthree four five six";
CHECK( Text( testString, TextAttributes().setWidth( 15 ) ).toString()
== "one two three\n four\n five\n six" );
SECTION( "No wrapping", "" ) {
CHECK( Text( testString, TextAttributes().setWidth( 80 ) ).toString() == testString );
CHECK( Text( testString, TextAttributes().setWidth( 24 ) ).toString() == testString );
}
SECTION( "Wrap before", "" ) {
CHECK( Text( testString, TextAttributes().setWidth( 11 ) ).toString() == "one,two\n(three)\n<here>" );
}
SECTION( "Wrap after", "" ) {
CHECK( Text( testString, TextAttributes().setWidth( 6 ) ).toString() == "one,\ntwo\n(thre-\ne)\n<here>" );
CHECK( Text( testString, TextAttributes().setWidth( 5 ) ).toString() == "one,\ntwo\n(thr-\nee)\n<her-\ne>" );
CHECK( Text( testString, TextAttributes().setWidth( 4 ) ).toString() == "one,\ntwo\n(th-\nree)\n<he-\nre>" );
}
}
}