diff options
| author | Baruch Burstein | 2017-07-02 11:52:29 +0300 |
|---|---|---|
| committer | Baruch Burstein | 2017-07-02 11:52:29 +0300 |
| commit | 694fe61ae3f3d36b9fc11300eb133b467e7de8a5 (patch) | |
| tree | 1592833731b98253fe036079e497bdf7c77d2401 /include | |
| parent | 0016362f69804ae18953a0ec11d0c108e8402e1d (diff) | |
| download | catch3-694fe61ae3f3d36b9fc11300eb133b467e7de8a5.tar.gz | |
Add ability to format any streamable class
This should probably be done at the source of this external file, but I couldn't find where that is. Perhaps it should be mentioned in the file header comment?
Diffstat (limited to 'include')
| -rw-r--r-- | include/external/tbc_text_format.h | 85 |
1 files changed, 48 insertions, 37 deletions
diff --git a/include/external/tbc_text_format.h b/include/external/tbc_text_format.h index 5209d804..39030ab0 100644 --- a/include/external/tbc_text_format.h +++ b/include/external/tbc_text_format.h @@ -54,12 +54,54 @@ namespace Tbc { Text( std::string const& _str, TextAttributes const& _attr = TextAttributes() ) : attr( _attr ) { + init( _str ); + } + + template<typename T> + Text( T const& _val, TextAttributes const& _attr = TextAttributes() ) + : attr( _attr ) + { + std::ostringstream oss; + oss << _val; + init( oss.str() ); + } + + typedef std::vector<std::string>::const_iterator const_iterator; + + const_iterator begin() const { return lines.begin(); } + const_iterator end() const { return lines.end(); } + std::string const& last() const { return lines.back(); } + std::size_t size() const { return lines.size(); } + std::string const& operator[]( std::size_t _index ) const { return lines[_index]; } + std::string toString() const { + std::ostringstream oss; + oss << *this; + return oss.str(); + } + + inline friend std::ostream& operator << ( std::ostream& _stream, Text const& _text ) { + for( Text::const_iterator it = _text.begin(), itEnd = _text.end(); + it != itEnd; ++it ) { + if( it != _text.begin() ) + _stream << "\n"; + _stream << *it; + } + return _stream; + } + + + private: + TextAttributes attr; + std::vector<std::string> lines; + + void init( std::string const& _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, ' ' ); + 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(); @@ -74,7 +116,7 @@ namespace Tbc { std::string suffix; - std::size_t width = (std::min)( static_cast<size_t>( strEnd-it ), _attr.width-static_cast<size_t>( indent.size() ) ); + 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(); @@ -121,42 +163,11 @@ namespace Tbc { } lines.push_back( indent + std::string( it, itEnd ) + suffix ); - if( indent.size() != _attr.indent ) - indent = std::string( _attr.indent, ' ' ); + if( indent.size() != attr.indent ) + indent = std::string( attr.indent, ' ' ); it = itNext; } } - - - - typedef std::vector<std::string>::const_iterator const_iterator; - - const_iterator begin() const { return lines.begin(); } - const_iterator end() const { return lines.end(); } - std::string const& last() const { return lines.back(); } - std::size_t size() const { return lines.size(); } - std::string const& operator[]( std::size_t _index ) const { return lines[_index]; } - std::string toString() const { - std::ostringstream oss; - oss << *this; - return oss.str(); - } - - inline friend std::ostream& operator << ( std::ostream& _stream, Text const& _text ) { - for( Text::const_iterator it = _text.begin(), itEnd = _text.end(); - it != itEnd; ++it ) { - if( it != _text.begin() ) - _stream << "\n"; - _stream << *it; - } - return _stream; - } - - - private: - std::string str; - TextAttributes attr; - std::vector<std::string> lines; }; } // end namespace Tbc |