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