summaryrefslogtreecommitdiff
path: root/src/cppunit/StringTools.cpp
blob: 7dc50eaecde03f9eb944aa5030c76ff69d6de83b (plain)
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
#include <cppunit/tools/StringTools.h>
#include <algorithm>


namespace CppUnit
{

namespace StringTools
{

  std::string toString( int value )
  {
    OStringStream stream;
    stream << value;
    return stream.str();
  }

  std::string toString( double value )
  {
    OStringStream stream;
    stream << value;
    return stream.str();
  }


  Strings split( const std::string &text, 
                 char separator )
  {
    Strings splittedText;

    std::string::const_iterator itStart = text.begin();
    while ( !text.empty() )
    {
      std::string::const_iterator itSeparator = std::find( itStart, 
                                                           text.end(), 
                                                           separator );
      splittedText.push_back( text.substr( itStart - text.begin(),
                                           itSeparator - itStart ) );
      if ( itSeparator == text.end() )
        break;
      itStart = itSeparator +1;
    }

    return splittedText;
  }


  std::string wrap( const std::string &text,
                    int wrapColumn )
  {
    const char lineBreak = '\n';
    Strings lines = split( text, lineBreak );

    std::string wrapped;
    for ( Strings::const_iterator it = lines.begin(); it != lines.end(); ++it )
    {
      if ( it != lines.begin() )
        wrapped += lineBreak;

      const std::string &line = *it;
      int index =0;
      while ( index < line.length() )
      {
        std::string lineSlice( line.substr( index, wrapColumn ) );
        wrapped += lineSlice;
        index += wrapColumn;
        if ( index < line.length() )
          wrapped += lineBreak;
      }
    }

    return wrapped;
  }


} // namespace StringTools


} //  namespace CppUnit