Remove String->const char* conversion operator in favor of c_str method
[clinton/bobotpp.git] / source / String.C
CommitLineData
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
30String::String()
31{
c1dcc495 32}
33
34String::String(const char *s)
35{
005c31fb 36 my_string = s;
c1dcc495 37}
38
39String::String(const std::string & s)
40{
005c31fb 41 my_string = s;
c1dcc495 42}
43
44String::String(const String & s)
45{
005c31fb 46 my_string = s.my_string;
c1dcc495 47}
48
49String::String(long i)
50{
51 std::ostringstream temp;
52 temp << i;
005c31fb 53 my_string = temp.str ();
c1dcc495 54}
55
56String::String(char c)
57{
005c31fb 58 my_string = std::string (1, c);
c1dcc495 59}
60
61String::~String()
62{
005c31fb 63
c1dcc495 64}
65
66String &
67String::operator=(const char *s)
68{
005c31fb 69 my_string = s;
70
c1dcc495 71 return *this;
72}
73
74String &
75String::operator=(const String & s)
76{
005c31fb 77 my_string = s.my_string;
78
c1dcc495 79 return *this;
80}
81
82String &
83String::operator=(const std::string & s)
84{
005c31fb 85 my_string = s;
c1dcc495 86
87 return *this;
88}
89
90int
91String::length() const
92{
005c31fb 93 return my_string.length ();
c1dcc495 94}
95
96int
97String::find(char c)
98{
005c31fb 99 return my_string.find (c);
c1dcc495 100}
101
102void
005c31fb 103String::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
111String
005c31fb 112String::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
121String
005c31fb 122String::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
129String
130String::substr (int s, int e)
131{
132 return subString (s, e);
133}
134
135String
005c31fb 136String::subString (int start)
c1dcc495 137{
005c31fb 138 return subString (start, my_string.length () - 1);
c1dcc495 139}
140
141String
142String::substr (int s)
143{
144 return subString (s);
145}
146
147String
148String::toLower()
149{
005c31fb 150 return Utils::to_lower (my_string);
c1dcc495 151}
152
153String
154String::toUpper()
155{
005c31fb 156 return Utils::to_upper (my_string);
c1dcc495 157}
158
159String
160String::trim()
161{
005c31fb 162 return Utils::trim_str (my_string);
c1dcc495 163}
164
165int
005c31fb 166String::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
176char &
cf8ea873 177String::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
188const char &
cf8ea873 189String::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
200bool
201String::operator==(const char *s) const
202{
005c31fb 203 return my_string == s;
c1dcc495 204}
205
206bool
207String::operator==(const String & s) const
208{
005c31fb 209 return my_string == s.my_string;
c1dcc495 210}
211
212bool
213String::operator==(const std::string & s) const
214{
005c31fb 215 return my_string == s;
c1dcc495 216}
217
218bool
219String::operator!=(const char *s) const
220{
005c31fb 221 return my_string != s;
c1dcc495 222}
223
224bool
225String::operator!=(const String & s) const
226{
005c31fb 227 return my_string != s.my_string;
c1dcc495 228}
229
230bool
231String::operator!=(const std::string & s) const
232{
005c31fb 233 return my_string != s;
c1dcc495 234}
235
236bool
237String::operator<(const String & s) const
238{
005c31fb 239 return my_string < s.my_string;
c1dcc495 240}
241
242bool
243String::operator<(const std::string & s) const
244{
005c31fb 245 return my_string < s;
c1dcc495 246}
247
248bool
249String::operator>(const String & s) const
250{
005c31fb 251 return my_string > s.my_string;
c1dcc495 252}
253
254bool
255String::operator<=(const String & s) const
256{
005c31fb 257 return my_string <= s.my_string;
c1dcc495 258}
259
260bool
261String::operator<=(const std::string & s) const
262{
005c31fb 263 return my_string <= s;
c1dcc495 264}
265
266bool
267String::operator>=(const String & s) const
268{
005c31fb 269 return my_string >= s.my_string;
c1dcc495 270}
271
272bool String::operator>=(const std::string & s) const
273{
005c31fb 274 return my_string >= s;
c1dcc495 275}
276
277String
278String::operator+(const char *s)
279{
005c31fb 280 return my_string + s;
c1dcc495 281}
282
283String
284String::operator+(const String & s)
285{
005c31fb 286 return my_string + s.my_string;
c1dcc495 287}
288
289String
290String::operator+(const std::string & s)
291{
005c31fb 292 return my_string + s;
c1dcc495 293}
294
815a1816 295const char*
296String::c_str () const
297 {
298 return my_string.c_str ();
299 }
c1dcc495 300
301String::operator std::string () const
302{
005c31fb 303 return my_string;
c1dcc495 304}
305
306std::ostream &
307operator<<(std::ostream & s, const String & st)
308{
005c31fb 309 return s << st.my_string;
c1dcc495 310}
311
312std::istream &
313operator>>(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
329std::string operator+(const std::string & s, const String & p)
330{
005c31fb 331 return s + p.my_string;
c1dcc495 332}
333
334bool operator==(const std::string & s, const String & p)
335{
336 return p == s;
337}
338
339bool operator!=(const std::string & s, const String & p)
340{
341 return p != s;
342}
343
344bool operator>(const std::string & s, const String & p)
345{
346 return p <= s;
347}
348
349bool operator<(const std::string & s, const String & p)
350{
351 return p >= s;
352}
353
354bool operator<=(const std::string & s, const String & p)
355{
356 return p > s;
357}
358
359bool operator>=(const std::string & s, const String & p)
360{
361 return p < s;
362}