c1dcc495 |
1 | // String.C -*- C++ -*- |
2 | // Copyright (c) 1997, 1998 Etienne BERNARD |
3 | // Copyright (C) 2002,2005 Clinton Ebadi |
4 | |
5 | // This program is free software; you can redistribute it and/or modify |
6 | // it under the terms of the GNU General Public License as published by |
7 | // the Free Software Foundation; either version 2 of the License, or |
8 | // any later version. |
9 | |
10 | // This program is distributed in the hope that it will be useful, |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | // GNU General Public License for more details. |
14 | |
15 | // You should have received a copy of the GNU General Public License |
16 | // along with this program; if not, write to the Free Software |
39b022cb |
17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
c1dcc495 |
18 | |
19 | #include "String.H" |
005c31fb |
20 | #include "Utils.H" |
c1dcc495 |
21 | |
22 | #include <cstring> |
23 | #include <cctype> |
24 | #include <sstream> |
005c31fb |
25 | #include <iostream> |
c1dcc495 |
26 | |
27 | String::String() |
28 | { |
c1dcc495 |
29 | } |
30 | |
31 | String::String(const char *s) |
32 | { |
005c31fb |
33 | my_string = s; |
c1dcc495 |
34 | } |
35 | |
36 | String::String(const std::string & s) |
37 | { |
005c31fb |
38 | my_string = s; |
c1dcc495 |
39 | } |
40 | |
41 | String::String(const String & s) |
42 | { |
005c31fb |
43 | my_string = s.my_string; |
c1dcc495 |
44 | } |
45 | |
46 | String::String(long i) |
47 | { |
48 | std::ostringstream temp; |
49 | temp << i; |
005c31fb |
50 | my_string = temp.str (); |
c1dcc495 |
51 | } |
52 | |
53 | String::String(char c) |
54 | { |
005c31fb |
55 | my_string = std::string (1, c); |
c1dcc495 |
56 | } |
57 | |
58 | String::~String() |
59 | { |
005c31fb |
60 | |
c1dcc495 |
61 | } |
62 | |
63 | String & |
64 | String::operator=(const char *s) |
65 | { |
005c31fb |
66 | my_string = s; |
67 | |
c1dcc495 |
68 | return *this; |
69 | } |
70 | |
71 | String & |
72 | String::operator=(const String & s) |
73 | { |
005c31fb |
74 | my_string = s.my_string; |
75 | |
c1dcc495 |
76 | return *this; |
77 | } |
78 | |
79 | String & |
80 | String::operator=(const std::string & s) |
81 | { |
005c31fb |
82 | my_string = s; |
c1dcc495 |
83 | |
84 | return *this; |
85 | } |
86 | |
87 | int |
88 | String::length() const |
89 | { |
005c31fb |
90 | return my_string.length (); |
c1dcc495 |
91 | } |
92 | |
93 | int |
94 | String::find(char c) |
95 | { |
005c31fb |
96 | return my_string.find (c); |
c1dcc495 |
97 | } |
98 | |
99 | void |
005c31fb |
100 | String::fill (char c) |
c1dcc495 |
101 | { |
005c31fb |
102 | my_string.replace (0, |
103 | my_string.length () - 1, |
104 | my_string.length (), |
105 | c); |
c1dcc495 |
106 | } |
107 | |
108 | String |
005c31fb |
109 | String::pad (int n) |
c1dcc495 |
110 | { |
005c31fb |
111 | std::string temp = my_string; |
c1dcc495 |
112 | |
005c31fb |
113 | temp.resize (n, ' '); |
c1dcc495 |
114 | |
005c31fb |
115 | return temp; |
c1dcc495 |
116 | } |
117 | |
118 | String |
005c31fb |
119 | String::subString (int start, int end) |
c1dcc495 |
120 | { |
005c31fb |
121 | if (end < start) return ""; |
c1dcc495 |
122 | |
005c31fb |
123 | return my_string.substr (start, (end - start) + 1); |
c1dcc495 |
124 | } |
125 | |
126 | String |
127 | String::substr (int s, int e) |
128 | { |
129 | return subString (s, e); |
130 | } |
131 | |
132 | String |
005c31fb |
133 | String::subString (int start) |
c1dcc495 |
134 | { |
005c31fb |
135 | return subString (start, my_string.length () - 1); |
c1dcc495 |
136 | } |
137 | |
138 | String |
139 | String::substr (int s) |
140 | { |
141 | return subString (s); |
142 | } |
143 | |
144 | String |
145 | String::toLower() |
146 | { |
005c31fb |
147 | return Utils::to_lower (my_string); |
c1dcc495 |
148 | } |
149 | |
150 | String |
151 | String::toUpper() |
152 | { |
005c31fb |
153 | return Utils::to_upper (my_string); |
c1dcc495 |
154 | } |
155 | |
156 | String |
157 | String::trim() |
158 | { |
005c31fb |
159 | return Utils::trim_str (my_string); |
c1dcc495 |
160 | } |
161 | |
162 | int |
005c31fb |
163 | String::indexOf (char c) |
c1dcc495 |
164 | { |
005c31fb |
165 | std::string::size_type pos = my_string.find (c); |
166 | |
167 | if (pos == std::string::npos) |
168 | return -1; |
169 | else |
170 | return pos; |
c1dcc495 |
171 | } |
172 | |
173 | char & |
cf8ea873 |
174 | String::operator[] (int i_) |
c1dcc495 |
175 | { |
cf8ea873 |
176 | unsigned int i = i_; |
177 | |
005c31fb |
178 | if (i < 0 || my_string.length () < i) { |
c1dcc495 |
179 | std::cerr << "String index out of range\n"; |
180 | std::exit(1); |
181 | } |
005c31fb |
182 | return my_string[i]; |
c1dcc495 |
183 | } |
184 | |
185 | const char & |
cf8ea873 |
186 | String::operator[](int i_) const |
c1dcc495 |
187 | { |
cf8ea873 |
188 | unsigned int i = i_; |
189 | |
005c31fb |
190 | if (i < 0 || my_string.length () < i) { |
c1dcc495 |
191 | std::cerr << "String index out of range\n"; |
192 | exit(1); |
193 | } |
005c31fb |
194 | return my_string[i]; |
c1dcc495 |
195 | } |
196 | |
197 | bool |
198 | String::operator==(const char *s) const |
199 | { |
005c31fb |
200 | return my_string == s; |
c1dcc495 |
201 | } |
202 | |
203 | bool |
204 | String::operator==(const String & s) const |
205 | { |
005c31fb |
206 | return my_string == s.my_string; |
c1dcc495 |
207 | } |
208 | |
209 | bool |
210 | String::operator==(const std::string & s) const |
211 | { |
005c31fb |
212 | return my_string == s; |
c1dcc495 |
213 | } |
214 | |
215 | bool |
216 | String::operator!=(const char *s) const |
217 | { |
005c31fb |
218 | return my_string != s; |
c1dcc495 |
219 | } |
220 | |
221 | bool |
222 | String::operator!=(const String & s) const |
223 | { |
005c31fb |
224 | return my_string != s.my_string; |
c1dcc495 |
225 | } |
226 | |
227 | bool |
228 | String::operator!=(const std::string & s) const |
229 | { |
005c31fb |
230 | return my_string != s; |
c1dcc495 |
231 | } |
232 | |
233 | bool |
234 | String::operator<(const String & s) const |
235 | { |
005c31fb |
236 | return my_string < s.my_string; |
c1dcc495 |
237 | } |
238 | |
239 | bool |
240 | String::operator<(const std::string & s) const |
241 | { |
005c31fb |
242 | return my_string < s; |
c1dcc495 |
243 | } |
244 | |
245 | bool |
246 | String::operator>(const String & s) const |
247 | { |
005c31fb |
248 | return my_string > s.my_string; |
c1dcc495 |
249 | } |
250 | |
251 | bool |
252 | String::operator<=(const String & s) const |
253 | { |
005c31fb |
254 | return my_string <= s.my_string; |
c1dcc495 |
255 | } |
256 | |
257 | bool |
258 | String::operator<=(const std::string & s) const |
259 | { |
005c31fb |
260 | return my_string <= s; |
c1dcc495 |
261 | } |
262 | |
263 | bool |
264 | String::operator>=(const String & s) const |
265 | { |
005c31fb |
266 | return my_string >= s.my_string; |
c1dcc495 |
267 | } |
268 | |
269 | bool String::operator>=(const std::string & s) const |
270 | { |
005c31fb |
271 | return my_string >= s; |
c1dcc495 |
272 | } |
273 | |
274 | String |
275 | String::operator+(const char *s) |
276 | { |
005c31fb |
277 | return my_string + s; |
c1dcc495 |
278 | } |
279 | |
280 | String |
281 | String::operator+(const String & s) |
282 | { |
005c31fb |
283 | return my_string + s.my_string; |
c1dcc495 |
284 | } |
285 | |
286 | String |
287 | String::operator+(const std::string & s) |
288 | { |
005c31fb |
289 | return my_string + s; |
c1dcc495 |
290 | } |
291 | |
292 | String::operator const char *() const |
293 | { |
005c31fb |
294 | return my_string.c_str (); |
c1dcc495 |
295 | } |
296 | |
297 | String::operator std::string () const |
298 | { |
005c31fb |
299 | return my_string; |
c1dcc495 |
300 | } |
301 | |
302 | std::ostream & |
303 | operator<<(std::ostream & s, const String & st) |
304 | { |
005c31fb |
305 | return s << st.my_string; |
c1dcc495 |
306 | } |
307 | |
308 | std::istream & |
309 | operator>>(std::istream & s, String & st) |
310 | { |
005c31fb |
311 | // The original version of string grabbed an entire line with |
312 | // operator>> so this version has to too (otherwise I'd just replace |
313 | // String with std::string everywhere because it would be just as |
314 | // painful) |
c1dcc495 |
315 | |
005c31fb |
316 | std::string temp; |
c1dcc495 |
317 | |
005c31fb |
318 | std::getline (s, temp); |
319 | |
320 | st.my_string = temp; |
c1dcc495 |
321 | |
322 | return s; |
323 | } |
324 | |
325 | std::string operator+(const std::string & s, const String & p) |
326 | { |
005c31fb |
327 | return s + p.my_string; |
c1dcc495 |
328 | } |
329 | |
330 | bool operator==(const std::string & s, const String & p) |
331 | { |
332 | return p == s; |
333 | } |
334 | |
335 | bool operator!=(const std::string & s, const String & p) |
336 | { |
337 | return p != s; |
338 | } |
339 | |
340 | bool operator>(const std::string & s, const String & p) |
341 | { |
342 | return p <= s; |
343 | } |
344 | |
345 | bool operator<(const std::string & s, const String & p) |
346 | { |
347 | return p >= s; |
348 | } |
349 | |
350 | bool operator<=(const std::string & s, const String & p) |
351 | { |
352 | return p > s; |
353 | } |
354 | |
355 | bool operator>=(const std::string & s, const String & p) |
356 | { |
357 | return p < s; |
358 | } |