[project @ 2006-02-03 22:08:15 by unknown_lamer]
[clinton/bobotpp.git] / source / String.C
... / ...
CommitLineData
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#include "Utils.H"
21
22#include <cstring>
23#include <cctype>
24#include <sstream>
25#include <iostream>
26
27String::String()
28{
29}
30
31String::String(const char *s)
32{
33 my_string = s;
34}
35
36String::String(const std::string & s)
37{
38 my_string = s;
39}
40
41String::String(const String & s)
42{
43 my_string = s.my_string;
44}
45
46String::String(long i)
47{
48 std::ostringstream temp;
49 temp << i;
50 my_string = temp.str ();
51}
52
53String::String(char c)
54{
55 my_string = std::string (1, c);
56}
57
58String::~String()
59{
60
61}
62
63String &
64String::operator=(const char *s)
65{
66 my_string = s;
67
68 return *this;
69}
70
71String &
72String::operator=(const String & s)
73{
74 my_string = s.my_string;
75
76 return *this;
77}
78
79String &
80String::operator=(const std::string & s)
81{
82 my_string = s;
83
84 return *this;
85}
86
87int
88String::length() const
89{
90 return my_string.length ();
91}
92
93int
94String::find(char c)
95{
96 return my_string.find (c);
97}
98
99void
100String::fill (char c)
101{
102 my_string.replace (0,
103 my_string.length () - 1,
104 my_string.length (),
105 c);
106}
107
108String
109String::pad (int n)
110{
111 std::string temp = my_string;
112
113 temp.resize (n, ' ');
114
115 return temp;
116}
117
118String
119String::subString (int start, int end)
120{
121 if (end < start) return "";
122
123 return my_string.substr (start, (end - start) + 1);
124}
125
126String
127String::substr (int s, int e)
128{
129 return subString (s, e);
130}
131
132String
133String::subString (int start)
134{
135 return subString (start, my_string.length () - 1);
136}
137
138String
139String::substr (int s)
140{
141 return subString (s);
142}
143
144String
145String::toLower()
146{
147 return Utils::to_lower (my_string);
148}
149
150String
151String::toUpper()
152{
153 return Utils::to_upper (my_string);
154}
155
156String
157String::trim()
158{
159 return Utils::trim_str (my_string);
160}
161
162int
163String::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
173char &
174String::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
185const char &
186String::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
197bool
198String::operator==(const char *s) const
199{
200 return my_string == s;
201}
202
203bool
204String::operator==(const String & s) const
205{
206 return my_string == s.my_string;
207}
208
209bool
210String::operator==(const std::string & s) const
211{
212 return my_string == s;
213}
214
215bool
216String::operator!=(const char *s) const
217{
218 return my_string != s;
219}
220
221bool
222String::operator!=(const String & s) const
223{
224 return my_string != s.my_string;
225}
226
227bool
228String::operator!=(const std::string & s) const
229{
230 return my_string != s;
231}
232
233bool
234String::operator<(const String & s) const
235{
236 return my_string < s.my_string;
237}
238
239bool
240String::operator<(const std::string & s) const
241{
242 return my_string < s;
243}
244
245bool
246String::operator>(const String & s) const
247{
248 return my_string > s.my_string;
249}
250
251bool
252String::operator<=(const String & s) const
253{
254 return my_string <= s.my_string;
255}
256
257bool
258String::operator<=(const std::string & s) const
259{
260 return my_string <= s;
261}
262
263bool
264String::operator>=(const String & s) const
265{
266 return my_string >= s.my_string;
267}
268
269bool String::operator>=(const std::string & s) const
270{
271 return my_string >= s;
272}
273
274String
275String::operator+(const char *s)
276{
277 return my_string + s;
278}
279
280String
281String::operator+(const String & s)
282{
283 return my_string + s.my_string;
284}
285
286String
287String::operator+(const std::string & s)
288{
289 return my_string + s;
290}
291
292String::operator const char *() const
293{
294 return my_string.c_str ();
295}
296
297String::operator std::string () const
298{
299 return my_string;
300}
301
302std::ostream &
303operator<<(std::ostream & s, const String & st)
304{
305 return s << st.my_string;
306}
307
308std::istream &
309operator>>(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
325std::string operator+(const std::string & s, const String & p)
326{
327 return s + p.my_string;
328}
329
330bool operator==(const std::string & s, const String & p)
331{
332 return p == s;
333}
334
335bool operator!=(const std::string & s, const String & p)
336{
337 return p != s;
338}
339
340bool operator>(const std::string & s, const String & p)
341{
342 return p <= s;
343}
344
345bool operator<(const std::string & s, const String & p)
346{
347 return p >= s;
348}
349
350bool operator<=(const std::string & s, const String & p)
351{
352 return p > s;
353}
354
355bool operator>=(const std::string & s, const String & p)
356{
357 return p < s;
358}