ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-4/src/utils/Trim.hpp
Revision: 1792
Committed: Mon Nov 29 17:59:56 2004 UTC (19 years, 7 months ago) by tim
File size: 5573 byte(s)
Log Message:
add Trim Algorithm for std::string

File Contents

# Content
1 /*
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 void trimLeft(std::string& str) {
132 trimLeftIf(str, std::isspace);
133 }
134
135 /**
136 * Remove all trailing spaces in-place.
137 * @param str An input sequence
138 */
139 void trimRight(std::string& str) {
140 trimRightIf(str, std::isspace);
141 }
142
143 /**
144 *Remove all leading and trailing spaces in-place
145 * @param str An input sequence
146 */
147 void trim(std::string& str) {
148 trimIf(str, std::isspace);
149 }
150
151 /**
152 * Remove all leading spaces from the input.
153 * @return A trimmed copy of the input
154 * @param input An input sequence
155 */
156 std::string trimLeftCopy(const std::string& input) {
157 return trimLeftCopyIf(input, std::isspace);
158 }
159
160 /**
161 * Remove all trailing spaces from the input.
162 * @return A trimmed copy of the input
163 * @param input An input sequence
164 */
165 std::string trimRightCopy(const std::string& input) {
166 return trimRightCopyIf(input, std::isspace);
167 }
168
169 /**
170 *Remove all leading and trailing spaces from the input.
171 * @return A trimmed copy of the input
172 * @param input An input sequence
173 */
174 std::string trimCopy(const std::string& input) {
175 return trimCopyIf(input, std::isspace);
176 }
177
178 }//end namespace oopse
179 #endif //UTILS_TRIM_HPP

Properties

Name Value
svn:executable *