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