File: simplestring_xform.hh

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (138 lines) | stat: -rw-r--r-- 4,185 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (C) 2006 Timothy Brownawell <tbrownaw@gmail.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#ifndef __SIMPLESTRING_XFORM_HH__
#define __SIMPLESTRING_XFORM_HH__

#include "vector.hh"

std::string uppercase(std::string const & in);
std::string lowercase(std::string const & in);

void split_into_lines(std::string const & in,
                      std::vector<std::string> & out);

void split_into_lines(std::string const & in,
                      std::string const & encoding,
                      std::vector<std::string> & out);

void split_into_lines(std::string const & in,
                      std::vector<std::string> & out,
                      bool diff_compat);

void split_into_lines(std::string const & in,
                      std::string const & encoding,
                      std::vector<std::string> & out,
                      bool diff_compat);

void join_lines(std::vector<std::string> const & in,
                std::string & out,
                std::string const & linesep = "\n");

void join_lines(std::vector<std::string>::const_iterator begin,
                std::vector<std::string>::const_iterator end,
                std::string & out,
                std::string const & linesep = "\n");

template<class Thing> inline
origin::type get_made_from(Thing const & thing)
{
  return thing.made_from;
}
template<> inline
origin::type get_made_from<std::string>(std::string const & thing)
{
  return origin::internal;
}
template<class Thing> inline
Thing from_string(std::string const & str, origin::type made_from)
{
  return Thing(str, made_from);
}
template<> inline
std::string from_string<std::string>(std::string const & str, origin::type made_from)
{
  return str;
}

template< class T >
std::vector< T > split_into_words(T const & in)
{
  std::string const & instr = in();
  std::vector< T > out;

  std::string::size_type begin = 0;
  std::string::size_type end = instr.find_first_of(" ", begin);

  while (end != std::string::npos && end >= begin)
    {
      out.push_back(from_string<T>(instr.substr(begin, end-begin),
                                   get_made_from(in)));
      begin = end + 1;
      if (begin >= instr.size())
        break;
      end = instr.find_first_of(" ", begin);
    }
  if (begin < instr.size())
    out.push_back(from_string<T>(instr.substr(begin, instr.size() - begin),
                                 get_made_from(in)));

  return out;
}

template< class Container >
typename Container::value_type join_words(Container const & in,
                                          std::string const & sep = " ")
{
  origin::type made_from = origin::internal;
  std::string str;
  typename Container::const_iterator iter = in.begin();
  while (iter != in.end())
    {
      made_from = get_made_from(*iter);
      str += (*iter)();
      iter++;
      if (iter != in.end())
        str += sep;
    }
  typedef typename Container::value_type result_type;
  return from_string<result_type>(str, made_from);
}

void prefix_lines_with(std::string const & prefix,
                       std::string const & lines,
                       std::string & out);

// append after removing all whitespace
void append_without_ws(std::string & appendto, std::string const & s);

// remove all whitespace
std::string remove_ws(std::string const & s);

// remove leading chars from string
std::string trim_left(std::string const & s,
                      std::string const & chars = "\n\r\t ");

// remove trailing chars from string
std::string trim_right(std::string const & s,
                       std::string const & chars = "\n\r\t ");

// remove leading and trailing chars from string
std::string trim(std::string const & s,
                 std::string const & chars = "\n\r\t ");

#endif

// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: