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