2013-01-13 22:51:44 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil on 11/1/2013.
|
|
|
|
* Copyright 2013 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_LINE_WRAP_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_LINE_WRAP_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "catch_line_wrap.h"
|
|
|
|
|
|
|
|
namespace Catch {
|
|
|
|
|
2013-03-27 20:08:16 +01:00
|
|
|
LineWrapper::LineWrapper()
|
|
|
|
: right( CATCH_CONFIG_CONSOLE_WIDTH-1 ),
|
|
|
|
nextTab( 0 ),
|
2013-03-28 23:13:31 +01:00
|
|
|
tab( 0 ),
|
2013-04-05 21:55:57 +02:00
|
|
|
indent( 0 ),
|
|
|
|
initialIndent( (std::size_t)-1 ), // use indent by default
|
2013-04-01 11:30:36 +02:00
|
|
|
wrappableChars( " [({.,/|\\" ),
|
2013-04-05 21:55:57 +02:00
|
|
|
tabChar( '\t' ),
|
2013-03-28 23:13:31 +01:00
|
|
|
recursionCount( 0 )
|
2013-03-27 20:08:16 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
LineWrapper& LineWrapper::setIndent( std::size_t _indent ) {
|
2013-04-05 21:55:57 +02:00
|
|
|
indent = _indent;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
LineWrapper& LineWrapper::setInitialIndent( std::size_t _initialIndent ) {
|
|
|
|
initialIndent = _initialIndent;
|
2013-03-27 20:08:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
LineWrapper& LineWrapper::setRight( std::size_t _right ) {
|
|
|
|
right = _right;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
LineWrapper& LineWrapper::wrap( std::string const& _str ) {
|
|
|
|
nextTab = tab = 0;
|
|
|
|
wrapInternal( _str );
|
|
|
|
return *this;
|
|
|
|
}
|
2013-04-05 21:55:57 +02:00
|
|
|
LineWrapper& LineWrapper::setTabChar( char _tabChar ) {
|
|
|
|
tabChar = _tabChar;
|
|
|
|
return *this;
|
|
|
|
}
|
2013-03-28 23:13:31 +01:00
|
|
|
bool LineWrapper::isWrapPoint( char c ) {
|
|
|
|
return wrappableChars.find( c ) != std::string::npos;
|
|
|
|
}
|
2013-03-27 20:08:16 +01:00
|
|
|
void LineWrapper::wrapInternal( std::string const& _str ) {
|
2013-04-19 20:08:32 +02:00
|
|
|
assert( ++recursionCount < 1000 );
|
2013-03-28 23:13:31 +01:00
|
|
|
|
2013-04-05 21:55:57 +02:00
|
|
|
std::size_t width = right - getCurrentIndent();
|
2013-03-27 20:08:16 +01:00
|
|
|
std::size_t wrapPoint = width-tab;
|
|
|
|
for( std::size_t pos = 0; pos < _str.size(); ++pos ) {
|
|
|
|
if( _str[pos] == '\n' )
|
|
|
|
{
|
|
|
|
addLine( _str.substr( 0, pos ) );
|
|
|
|
nextTab = tab = 0;
|
|
|
|
return wrapInternal( _str.substr( pos+1 ) );
|
|
|
|
}
|
|
|
|
if( pos == width-tab ) {
|
|
|
|
if( _str[wrapPoint] == ' ' ) {
|
|
|
|
addLine( _str.substr( 0, wrapPoint ) );
|
|
|
|
while( _str[++wrapPoint] == ' ' );
|
2013-01-13 22:51:44 +01:00
|
|
|
}
|
2013-03-28 23:13:31 +01:00
|
|
|
else if( isWrapPoint( _str[wrapPoint] ) ) {
|
|
|
|
addLine( _str.substr( 0, wrapPoint ) );
|
|
|
|
}
|
2013-03-27 20:08:16 +01:00
|
|
|
else {
|
|
|
|
addLine( _str.substr( 0, --wrapPoint ) + '-' );
|
2013-01-13 22:51:44 +01:00
|
|
|
}
|
2013-03-27 20:08:16 +01:00
|
|
|
return wrapInternal( _str.substr( wrapPoint ) );
|
|
|
|
}
|
2013-04-05 21:55:57 +02:00
|
|
|
if( _str[pos] == tabChar ) {
|
2013-03-27 20:08:16 +01:00
|
|
|
nextTab = pos;
|
|
|
|
std::string withoutTab = _str.substr( 0, nextTab ) + _str.substr( nextTab+1 );
|
|
|
|
return wrapInternal( withoutTab );
|
|
|
|
}
|
2013-03-29 12:42:10 +01:00
|
|
|
else if( pos > 0 && isWrapPoint( _str[pos] ) ) {
|
2013-03-27 20:08:16 +01:00
|
|
|
wrapPoint = pos;
|
2013-01-13 22:51:44 +01:00
|
|
|
}
|
|
|
|
}
|
2013-03-27 20:08:16 +01:00
|
|
|
addLine( _str );
|
2013-01-13 22:51:44 +01:00
|
|
|
}
|
|
|
|
|
2013-03-28 00:36:58 +01:00
|
|
|
std::ostream& operator << ( std::ostream& _stream, LineWrapper const& _lineWrapper ) {
|
|
|
|
for( LineWrapper::const_iterator it = _lineWrapper.begin(), itEnd = _lineWrapper.end();
|
2013-03-27 20:08:16 +01:00
|
|
|
it != itEnd; ++it ) {
|
2013-03-28 00:36:58 +01:00
|
|
|
if( it != _lineWrapper.begin() )
|
|
|
|
_stream << "\n";
|
|
|
|
_stream << *it;
|
2013-01-13 22:51:44 +01:00
|
|
|
}
|
2013-03-28 00:36:58 +01:00
|
|
|
return _stream;
|
2013-01-13 22:51:44 +01:00
|
|
|
}
|
2013-03-27 20:08:16 +01:00
|
|
|
std::string LineWrapper::toString() const {
|
2013-01-13 22:51:44 +01:00
|
|
|
std::ostringstream oss;
|
2013-03-28 00:36:58 +01:00
|
|
|
oss << *this;
|
2013-03-27 20:08:16 +01:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineWrapper::addLine( const std::string& _line ) {
|
2013-04-05 21:55:57 +02:00
|
|
|
lines.push_back( std::string( tab + getCurrentIndent(), ' ' ) + _line );
|
2013-03-27 20:08:16 +01:00
|
|
|
if( nextTab > 0 )
|
|
|
|
tab = nextTab;
|
2013-01-13 22:51:44 +01:00
|
|
|
}
|
|
|
|
|
2013-04-05 21:55:57 +02:00
|
|
|
std::size_t LineWrapper::getCurrentIndent() const
|
|
|
|
{
|
|
|
|
return (initialIndent != (std::size_t)-1 && lines.empty() )
|
|
|
|
? initialIndent
|
|
|
|
: indent;
|
|
|
|
}
|
|
|
|
|
2013-01-13 22:51:44 +01:00
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_LINE_WRAP_HPP_INCLUDED
|