Embedded version of Clara with TextFlow fix for embedded newlines

This commit is contained in:
Phil Nash 2017-07-20 19:42:06 +01:00
parent f0890dcdf8
commit b2a4dfcda4
2 changed files with 28 additions and 14 deletions

View File

@ -35,8 +35,7 @@
#endif #endif
namespace Catch { namespace clara { namespace TextFlow namespace Catch { namespace clara { namespace TextFlow {
{
inline auto isWhitespace( char c ) -> bool { inline auto isWhitespace( char c ) -> bool {
static std::string chars = " \t\n\r"; static std::string chars = " \t\n\r";
@ -68,6 +67,7 @@ namespace Catch { namespace clara { namespace TextFlow
size_t m_pos = 0; size_t m_pos = 0;
size_t m_len = 0; size_t m_len = 0;
size_t m_end = 0;
bool m_suffix = false; bool m_suffix = false;
iterator( Column const& column, size_t stringIndex ) iterator( Column const& column, size_t stringIndex )
@ -92,8 +92,12 @@ namespace Catch { namespace clara { namespace TextFlow
m_suffix = false; m_suffix = false;
auto width = m_column.m_width-indent(); auto width = m_column.m_width-indent();
if( line().size() < m_pos + width ) { m_end = m_pos;
m_len = line().size() - m_pos; while( m_end < line().size() && line()[m_end] != '\n' )
++m_end;
if( m_end < m_pos + width ) {
m_len = m_end - m_pos;
} }
else { else {
size_t len = width; size_t len = width;
@ -131,15 +135,18 @@ namespace Catch { namespace clara { namespace TextFlow
auto operator *() const -> std::string { auto operator *() const -> std::string {
assert( m_stringIndex < m_column.m_strings.size() ); assert( m_stringIndex < m_column.m_strings.size() );
assert( m_pos < line().size() ); assert( m_pos < m_end );
if( m_pos + m_column.m_width < line().size() ) if( m_pos + m_column.m_width < m_end )
return addIndentAndSuffix(line().substr(m_pos, m_len)); return addIndentAndSuffix(line().substr(m_pos, m_len));
else else
return addIndentAndSuffix(line().substr(m_pos)); return addIndentAndSuffix(line().substr(m_pos, m_end - m_pos));
} }
auto operator ++() -> iterator& { auto operator ++() -> iterator& {
m_pos += m_len; m_pos += m_len;
if( m_pos < line().size() && line()[m_pos] == '\n' )
m_pos += 1;
while( m_pos < line().size() && isWhitespace( line()[m_pos] ) ) while( m_pos < line().size() && isWhitespace( line()[m_pos] ) )
++m_pos; ++m_pos;

21
third_party/clara.hpp vendored
View File

@ -33,8 +33,7 @@
#endif #endif
namespace clara { namespace TextFlow namespace clara { namespace TextFlow {
{
inline auto isWhitespace( char c ) -> bool { inline auto isWhitespace( char c ) -> bool {
static std::string chars = " \t\n\r"; static std::string chars = " \t\n\r";
@ -66,6 +65,7 @@ namespace clara { namespace TextFlow
size_t m_pos = 0; size_t m_pos = 0;
size_t m_len = 0; size_t m_len = 0;
size_t m_end = 0;
bool m_suffix = false; bool m_suffix = false;
iterator( Column const& column, size_t stringIndex ) iterator( Column const& column, size_t stringIndex )
@ -90,8 +90,12 @@ namespace clara { namespace TextFlow
m_suffix = false; m_suffix = false;
auto width = m_column.m_width-indent(); auto width = m_column.m_width-indent();
if( line().size() < m_pos + width ) { m_end = m_pos;
m_len = line().size() - m_pos; while( m_end < line().size() && line()[m_end] != '\n' )
++m_end;
if( m_end < m_pos + width ) {
m_len = m_end - m_pos;
} }
else { else {
size_t len = width; size_t len = width;
@ -129,15 +133,18 @@ namespace clara { namespace TextFlow
auto operator *() const -> std::string { auto operator *() const -> std::string {
assert( m_stringIndex < m_column.m_strings.size() ); assert( m_stringIndex < m_column.m_strings.size() );
assert( m_pos < line().size() ); assert( m_pos < m_end );
if( m_pos + m_column.m_width < line().size() ) if( m_pos + m_column.m_width < m_end )
return addIndentAndSuffix(line().substr(m_pos, m_len)); return addIndentAndSuffix(line().substr(m_pos, m_len));
else else
return addIndentAndSuffix(line().substr(m_pos)); return addIndentAndSuffix(line().substr(m_pos, m_end - m_pos));
} }
auto operator ++() -> iterator& { auto operator ++() -> iterator& {
m_pos += m_len; m_pos += m_len;
if( m_pos < line().size() && line()[m_pos] == '\n' )
m_pos += 1;
while( m_pos < line().size() && isWhitespace( line()[m_pos] ) ) while( m_pos < line().size() && isWhitespace( line()[m_pos] ) )
++m_pos; ++m_pos;