1 |
tim |
1792 |
/* |
2 |
|
|
* Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project |
3 |
|
|
* |
4 |
|
|
* Contact: oopse@oopse.org |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU Lesser General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2.1 |
9 |
|
|
* of the License, or (at your option) any later version. |
10 |
|
|
* All we ask is that proper credit is given for our work, which includes |
11 |
|
|
* - but is not limited to - adding the above copyright notice to the beginning |
12 |
|
|
* of your source code files, and to any copyright notice that you may distribute |
13 |
|
|
* with programs based on this work. |
14 |
|
|
* |
15 |
|
|
* This program is distributed in the hope that it will be useful, |
16 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
|
|
* GNU Lesser General Public License for more details. |
19 |
|
|
* |
20 |
|
|
* You should have received a copy of the GNU Lesser General Public License |
21 |
|
|
* along with this program; if not, write to the Free Software |
22 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 |
|
|
* |
24 |
|
|
*/ |
25 |
|
|
|
26 |
|
|
#ifndef UTILS_TRIM_HPP |
27 |
|
|
#define UTILS_TRIM_HPP |
28 |
|
|
|
29 |
|
|
#include <string> |
30 |
|
|
#include <cctype> |
31 |
|
|
|
32 |
|
|
/** |
33 |
|
|
* @file Trim.hpp |
34 |
|
|
* Defines trim algorithms. Trim algorithms are used to remove trailing and leading spaces from a string. |
35 |
|
|
*/ |
36 |
|
|
namespace oopse { |
37 |
|
|
|
38 |
|
|
/** |
39 |
|
|
* Remove all leading spaces in-place. The supplied predicate is used to determine which |
40 |
|
|
* characters are considered spaces |
41 |
|
|
* @param str An input sequence |
42 |
|
|
* @param IsSpace An unary predicate identifying spaces |
43 |
|
|
*/ |
44 |
|
|
template<typename Predict> |
45 |
|
|
void trimLeftIf(std::string& str, Predict isSpace) { |
46 |
|
|
std::string::iterator i = str.begin(); |
47 |
|
|
|
48 |
|
|
for (; i != str.end(); ++i) { |
49 |
|
|
if (!isSpace(*i)) { |
50 |
|
|
break; |
51 |
|
|
} |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
str.erase(str.begin(), i); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
/** |
58 |
|
|
* Remove all trailing spaces in-place. The supplied predicate is used to determine which |
59 |
|
|
* characters are considered spaces |
60 |
|
|
* @param str An input sequence |
61 |
|
|
*/ |
62 |
|
|
template<typename Predict> |
63 |
|
|
void trimRightIf(std::string& str, Predict isSpace) { |
64 |
|
|
std::string::iterator i = str.end(); |
65 |
|
|
|
66 |
|
|
for (; i != str.begin();) { |
67 |
|
|
if (!isSpace(*(--i))) { |
68 |
|
|
++i; |
69 |
|
|
break; |
70 |
|
|
} |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
str.erase(i, str.end()); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
/** |
77 |
|
|
*Remove all leading and trailing spaces in-place. The supplied predicate is used to determine |
78 |
|
|
* which characters are considered spaces |
79 |
|
|
* @param str An input sequence |
80 |
|
|
*/ |
81 |
|
|
template<typename Predict> |
82 |
|
|
void trimIf(std::string& str, Predict isSpace) { |
83 |
|
|
trimLeftIf(str, isSpace); |
84 |
|
|
trimRightIf(str, isSpace); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
/** |
88 |
|
|
* Remove all leading spaces from the input. The supplied predicate is used to determine |
89 |
|
|
* which characters are considered spaces |
90 |
|
|
* @return A trimmed copy of the input |
91 |
|
|
* @param input An input sequence |
92 |
|
|
*/ |
93 |
|
|
template<typename Predict> |
94 |
|
|
std::string trimLeftCopyIf(const std::string& input, Predict isSpace) { |
95 |
|
|
std::string result(input); |
96 |
|
|
trimLeftIf(result, isSpace); |
97 |
|
|
return result; |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
/** |
101 |
|
|
* Remove all trailing spaces from the input. The supplied predicate is used to determine |
102 |
|
|
* which characters are considered spaces |
103 |
|
|
* @return A trimmed copy of the input |
104 |
|
|
* @param input An input sequence |
105 |
|
|
*/ |
106 |
|
|
template<typename Predict> |
107 |
|
|
std::string trimRightCopyIf(const std::string& input, Predict isSpace) { |
108 |
|
|
std::string result(input); |
109 |
|
|
trimRightIf(result, isSpace); |
110 |
|
|
return result; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
/** |
114 |
|
|
* Remove all leading and trailing spaces from the input. The supplied predicate is used to |
115 |
|
|
* determine which characters are considered spaces |
116 |
|
|
* @return A trimmed copy of the input |
117 |
|
|
* @param input An input sequence |
118 |
|
|
*/ |
119 |
|
|
template<typename Predict> |
120 |
|
|
std::string trimCopyIf(const std::string& input, Predict isSpace) { |
121 |
|
|
std::string result(input); |
122 |
|
|
trimIf(result, isSpace); |
123 |
|
|
return result; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
|
127 |
|
|
/** |
128 |
|
|
* Remove all leading spaces in-place. |
129 |
|
|
* @param str An input sequence |
130 |
|
|
*/ |
131 |
tim |
1840 |
void trimLeft(std::string& str); |
132 |
tim |
1792 |
|
133 |
|
|
/** |
134 |
|
|
* Remove all trailing spaces in-place. |
135 |
|
|
* @param str An input sequence |
136 |
|
|
*/ |
137 |
tim |
1840 |
void trimRight(std::string& str); |
138 |
tim |
1792 |
|
139 |
|
|
/** |
140 |
|
|
*Remove all leading and trailing spaces in-place |
141 |
|
|
* @param str An input sequence |
142 |
|
|
*/ |
143 |
tim |
1840 |
void trim(std::string& str); |
144 |
tim |
1792 |
|
145 |
|
|
/** |
146 |
|
|
* Remove all leading spaces from the input. |
147 |
|
|
* @return A trimmed copy of the input |
148 |
|
|
* @param input An input sequence |
149 |
|
|
*/ |
150 |
tim |
1840 |
std::string trimLeftCopy(const std::string& input); |
151 |
tim |
1792 |
|
152 |
|
|
/** |
153 |
|
|
* Remove all trailing spaces from the input. |
154 |
|
|
* @return A trimmed copy of the input |
155 |
|
|
* @param input An input sequence |
156 |
|
|
*/ |
157 |
tim |
1840 |
std::string trimRightCopy(const std::string& input); |
158 |
tim |
1792 |
|
159 |
|
|
/** |
160 |
|
|
*Remove all leading and trailing spaces from the input. |
161 |
|
|
* @return A trimmed copy of the input |
162 |
|
|
* @param input An input sequence |
163 |
|
|
*/ |
164 |
tim |
1840 |
std::string trimCopy(const std::string& input); |
165 |
tim |
1792 |
|
166 |
|
|
}//end namespace oopse |
167 |
|
|
#endif //UTILS_TRIM_HPP |