Remove String->const char* conversion operator in favor of c_str method
[clinton/bobotpp.git] / source / String.C
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
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 #include "String.H"
20
21 #include <sstream>
22 #include <iostream>
23
24 #include <cstdlib>
25 #include <cstring>
26 #include <cctype>
27
28 #include "Utils.H"
29
30 String::String()
31 {
32 }
33
34 String::String(const char *s)
35 {
36 my_string = s;
37 }
38
39 String::String(const std::string & s)
40 {
41 my_string = s;
42 }
43
44 String::String(const String & s)
45 {
46 my_string = s.my_string;
47 }
48
49 String::String(long i)
50 {
51 std::ostringstream temp;
52 temp << i;
53 my_string = temp.str ();
54 }
55
56 String::String(char c)
57 {
58 my_string = std::string (1, c);
59 }
60
61 String::~String()
62 {
63
64 }
65
66 String &
67 String::operator=(const char *s)
68 {
69 my_string = s;
70
71 return *this;
72 }
73
74 String &
75 String::operator=(const String & s)
76 {
77 my_string = s.my_string;
78
79 return *this;
80 }
81
82 String &
83 String::operator=(const std::string & s)
84 {
85 my_string = s;
86
87 return *this;
88 }
89
90 int
91 String::length() const
92 {
93 return my_string.length ();
94 }
95
96 int
97 String::find(char c)
98 {
99 return my_string.find (c);
100 }
101
102 void
103 String::fill (char c)
104 {
105 my_string.replace (0,
106 my_string.length () - 1,
107 my_string.length (),
108 c);
109 }
110
111 String
112 String::pad (int n)
113 {
114 std::string temp = my_string;
115
116 temp.resize (n, ' ');
117
118 return temp;
119 }
120
121 String
122 String::subString (int start, int end)
123 {
124 if (end < start) return "";
125
126 return my_string.substr (start, (end - start) + 1);
127 }
128
129 String
130 String::substr (int s, int e)
131 {
132 return subString (s, e);
133 }
134
135 String
136 String::subString (int start)
137 {
138 return subString (start, my_string.length () - 1);
139 }
140
141 String
142 String::substr (int s)
143 {
144 return subString (s);
145 }
146
147 String
148 String::toLower()
149 {
150 return Utils::to_lower (my_string);
151 }
152
153 String
154 String::toUpper()
155 {
156 return Utils::to_upper (my_string);
157 }
158
159 String
160 String::trim()
161 {
162 return Utils::trim_str (my_string);
163 }
164
165 int
166 String::indexOf (char c)
167 {
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;
174 }
175
176 char &
177 String::operator[] (int i_)
178 {
179 unsigned int i = i_;
180
181 if (i < 0 || my_string.length () < i) {
182 std::cerr << "String index out of range\n";
183 std::exit(1);
184 }
185 return my_string[i];
186 }
187
188 const char &
189 String::operator[](int i_) const
190 {
191 unsigned int i = i_;
192
193 if (i < 0 || my_string.length () < i) {
194 std::cerr << "String index out of range\n";
195 std::exit(1);
196 }
197 return my_string[i];
198 }
199
200 bool
201 String::operator==(const char *s) const
202 {
203 return my_string == s;
204 }
205
206 bool
207 String::operator==(const String & s) const
208 {
209 return my_string == s.my_string;
210 }
211
212 bool
213 String::operator==(const std::string & s) const
214 {
215 return my_string == s;
216 }
217
218 bool
219 String::operator!=(const char *s) const
220 {
221 return my_string != s;
222 }
223
224 bool
225 String::operator!=(const String & s) const
226 {
227 return my_string != s.my_string;
228 }
229
230 bool
231 String::operator!=(const std::string & s) const
232 {
233 return my_string != s;
234 }
235
236 bool
237 String::operator<(const String & s) const
238 {
239 return my_string < s.my_string;
240 }
241
242 bool
243 String::operator<(const std::string & s) const
244 {
245 return my_string < s;
246 }
247
248 bool
249 String::operator>(const String & s) const
250 {
251 return my_string > s.my_string;
252 }
253
254 bool
255 String::operator<=(const String & s) const
256 {
257 return my_string <= s.my_string;
258 }
259
260 bool
261 String::operator<=(const std::string & s) const
262 {
263 return my_string <= s;
264 }
265
266 bool
267 String::operator>=(const String & s) const
268 {
269 return my_string >= s.my_string;
270 }
271
272 bool String::operator>=(const std::string & s) const
273 {
274 return my_string >= s;
275 }
276
277 String
278 String::operator+(const char *s)
279 {
280 return my_string + s;
281 }
282
283 String
284 String::operator+(const String & s)
285 {
286 return my_string + s.my_string;
287 }
288
289 String
290 String::operator+(const std::string & s)
291 {
292 return my_string + s;
293 }
294
295 const char*
296 String::c_str () const
297 {
298 return my_string.c_str ();
299 }
300
301 String::operator std::string () const
302 {
303 return my_string;
304 }
305
306 std::ostream &
307 operator<<(std::ostream & s, const String & st)
308 {
309 return s << st.my_string;
310 }
311
312 std::istream &
313 operator>>(std::istream & s, String & st)
314 {
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)
319
320 std::string temp;
321
322 std::getline (s, temp);
323
324 st.my_string = temp;
325
326 return s;
327 }
328
329 std::string operator+(const std::string & s, const String & p)
330 {
331 return s + p.my_string;
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 }