File: catch_text.hpp

package info (click to toggle)
catch 1.0%2Bm10git1e2f1d16-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,984 kB
  • ctags: 2,218
  • sloc: cpp: 14,476; ansic: 738; python: 173; objc: 39; makefile: 17
file content (92 lines) | stat: -rw-r--r-- 3,234 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 *  Created by Phil on 20/4/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_TEXT_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_TEXT_HPP_INCLUDED

#include <string>
#include <vector>

namespace Catch {

    Text::Text( std::string const& _str, TextAttributes const& _attr )
    : attr( _attr )
    {
        std::string wrappableChars = " [({.,/|\\-";
        std::size_t indent = _attr.initialIndent != std::string::npos
            ? _attr.initialIndent
            : _attr.indent;
        std::string remainder = _str;

        while( !remainder.empty() ) {
            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 ) {
                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 );
            }
            else if( remainder[width] == '\n' ) {
                spliceLine( indent, remainder, width );
                if( width <= 1 || remainder.size() != 1 )
                    remainder = remainder.substr( 1 );
                indent = _attr.indent;
            }
            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 );
                }
                else {
                    spliceLine( indent, remainder, width-1 );
                    lines.back() += "-";
                }
                if( lines.size() == 1 )
                    indent = _attr.indent;
                if( tabPos != std::string::npos )
                    indent += tabPos;
            }
        }
    }

    void Text::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 );
    }

    std::string Text::toString() const {
        std::ostringstream oss;
        oss << *this;
        return oss.str();
    }

    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;
    }

} // end namespace Catch

#endif // TWOBLUECUBES_CATCH_TEXT_HPP_INCLUDED