~ubuntu-branches/ubuntu/wily/psi/wily

« back to all changes in this revision

Viewing changes to third-party/cppunit/cppunit/src/cppunit/StringTools.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-08-28 18:46:52 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080828184652-iiik12dl91nq7cdi
Tags: 0.12-2
Uploading to unstable (Closes: Bug#494352)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cppunit/tools/StringTools.h>
 
2
#include <cppunit/portability/Stream.h>
 
3
#include <algorithm>
 
4
 
 
5
 
 
6
CPPUNIT_NS_BEGIN
 
7
 
 
8
 
 
9
std::string 
 
10
StringTools::toString( int value )
 
11
{
 
12
  OStringStream stream;
 
13
  stream << value;
 
14
  return stream.str();
 
15
}
 
16
 
 
17
 
 
18
std::string 
 
19
StringTools::toString( double value )
 
20
{
 
21
  OStringStream stream;
 
22
  stream << value;
 
23
  return stream.str();
 
24
}
 
25
 
 
26
 
 
27
StringTools::Strings
 
28
StringTools::split( const std::string &text, 
 
29
                    char separator )
 
30
{
 
31
  Strings splittedText;
 
32
 
 
33
  std::string::const_iterator itStart = text.begin();
 
34
  while ( !text.empty() )
 
35
  {
 
36
    std::string::const_iterator itSeparator = std::find( itStart, 
 
37
                                                         text.end(), 
 
38
                                                         separator );
 
39
    splittedText.push_back( text.substr( itStart - text.begin(),
 
40
                                         itSeparator - itStart ) );
 
41
    if ( itSeparator == text.end() )
 
42
      break;
 
43
    itStart = itSeparator +1;
 
44
  }
 
45
 
 
46
  return splittedText;
 
47
}
 
48
 
 
49
 
 
50
std::string 
 
51
StringTools::wrap( const std::string &text,
 
52
                   int wrapColumn )
 
53
{
 
54
  const char lineBreak = '\n';
 
55
  Strings lines = split( text, lineBreak );
 
56
 
 
57
  std::string wrapped;
 
58
  for ( Strings::const_iterator it = lines.begin(); it != lines.end(); ++it )
 
59
  {
 
60
    if ( it != lines.begin() )
 
61
      wrapped += lineBreak;
 
62
 
 
63
    const std::string &line = *it;
 
64
    unsigned int index =0;
 
65
    while ( index < line.length() )
 
66
    {
 
67
      std::string lineSlice( line.substr( index, wrapColumn ) );
 
68
      wrapped += lineSlice;
 
69
      index += wrapColumn;
 
70
      if ( index < line.length() )
 
71
        wrapped += lineBreak;
 
72
    }
 
73
  }
 
74
 
 
75
  return wrapped;
 
76
}
 
77
 
 
78
 
 
79
CPPUNIT_NS_END
 
80