monotone

monotone Mtn Source Tree

Root/src/simplestring_xform.hh

1// Copyright (C) 2006 Timothy Brownawell <tbrownaw@gmail.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#ifndef __SIMPLESTRING_XFORM_HH__
11#define __SIMPLESTRING_XFORM_HH__
12
13#include "vector.hh"
14
15std::string uppercase(std::string const & in);
16std::string lowercase(std::string const & in);
17
18namespace split_flags
19{
20 enum split_flags {
21 none = 0,
22 diff_compat = 1,
23 keep_endings = 2
24 };
25}
26
27void split_into_lines(std::string const & in,
28 std::vector<std::string> & out,
29 split_flags::split_flags flags = split_flags::none);
30
31void split_into_lines(std::string const & in,
32 std::string const & encoding,
33 std::vector<std::string> & out,
34 split_flags::split_flags flags = split_flags::none);
35
36void join_lines(std::vector<std::string> const & in,
37 std::string & out,
38 std::string const & linesep = "\n");
39
40void join_lines(std::vector<std::string>::const_iterator begin,
41 std::vector<std::string>::const_iterator end,
42 std::string & out,
43 std::string const & linesep = "\n");
44
45template<class Thing> inline
46origin::type get_made_from(Thing const & thing)
47{
48 return thing.made_from;
49}
50template<> inline
51origin::type get_made_from<std::string>(std::string const & thing)
52{
53 return origin::internal;
54}
55template<class Thing> inline
56Thing from_string(std::string const & str, origin::type made_from)
57{
58 return Thing(str, made_from);
59}
60template<> inline
61std::string from_string<std::string>(std::string const & str, origin::type made_from)
62{
63 return str;
64}
65
66template< class T >
67std::vector< T > split_into_words(T const & in)
68{
69 std::string const & instr = in();
70 std::vector< T > out;
71
72 std::string::size_type begin = 0;
73 std::string::size_type end = instr.find_first_of(" ", begin);
74
75 while (end != std::string::npos && end >= begin)
76 {
77 out.push_back(from_string<T>(instr.substr(begin, end-begin),
78 get_made_from(in)));
79 begin = end + 1;
80 if (begin >= instr.size())
81 break;
82 end = instr.find_first_of(" ", begin);
83 }
84 if (begin < instr.size())
85 out.push_back(from_string<T>(instr.substr(begin, instr.size() - begin),
86 get_made_from(in)));
87
88 return out;
89}
90
91template< class Container >
92typename Container::value_type join_words(Container const & in,
93 std::string const & sep = " ")
94{
95 origin::type made_from = origin::internal;
96 std::string str;
97 typename Container::const_iterator iter = in.begin();
98 while (iter != in.end())
99 {
100 made_from = get_made_from(*iter);
101 str += (*iter)();
102 iter++;
103 if (iter != in.end())
104 str += sep;
105 }
106 typedef typename Container::value_type result_type;
107 return from_string<result_type>(str, made_from);
108}
109
110void prefix_lines_with(std::string const & prefix,
111 std::string const & lines,
112 std::string & out);
113
114// append after removing all whitespace
115void append_without_ws(std::string & appendto, std::string const & s);
116
117// remove all whitespace
118std::string remove_ws(std::string const & s);
119
120// remove leading chars from string
121std::string trim_left(std::string const & s,
122 std::string const & chars = "\n\r\t ");
123
124// remove trailing chars from string
125std::string trim_right(std::string const & s,
126 std::string const & chars = "\n\r\t ");
127
128// remove leading and trailing chars from string
129std::string trim(std::string const & s,
130 std::string const & chars = "\n\r\t ");
131
132#endif
133
134// Local Variables:
135// mode: C++
136// fill-column: 76
137// c-file-style: "gnu"
138// indent-tabs-mode: nil
139// End:
140// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status