1 // String.C -*- C++ -*-
2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (C) 2002,2005 Clinton Ebadi
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
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.
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.
34 String::String(const char *s)
39 String::String(const std::string & s)
44 String::String(const String & s)
46 my_string = s.my_string;
49 String::String(long i)
51 std::ostringstream temp;
53 my_string = temp.str ();
56 String::String(char c)
58 my_string = std::string (1, c);
67 String::operator=(const char *s)
75 String::operator=(const String & s)
77 my_string = s.my_string;
83 String::operator=(const std::string & s)
91 String::length() const
93 return my_string.length ();
99 return my_string.find (c);
103 String::fill (char c)
105 my_string.replace (0,
106 my_string.length () - 1,
114 std::string temp = my_string;
116 temp.resize (n, ' ');
122 String::subString (int start, int end)
124 if (end < start) return "";
126 return my_string.substr (start, (end - start) + 1);
130 String::substr (int s, int e)
132 return subString (s, e);
136 String::subString (int start)
138 return subString (start, my_string.length () - 1);
142 String::substr (int s)
144 return subString (s);
150 return Utils::to_lower (my_string);
156 return Utils::to_upper (my_string);
162 return Utils::trim_str (my_string);
166 String::indexOf (char c)
168 std::string::size_type pos = my_string.find (c);
170 if (pos == std::string::npos)
177 String::operator[] (int i_)
181 if (i < 0 || my_string.length () < i) {
182 std::cerr << "String index out of range\n";
189 String::operator[](int i_) const
193 if (i < 0 || my_string.length () < i) {
194 std::cerr << "String index out of range\n";
201 String::operator==(const char *s) const
203 return my_string == s;
207 String::operator==(const String & s) const
209 return my_string == s.my_string;
213 String::operator==(const std::string & s) const
215 return my_string == s;
219 String::operator!=(const char *s) const
221 return my_string != s;
225 String::operator!=(const String & s) const
227 return my_string != s.my_string;
231 String::operator!=(const std::string & s) const
233 return my_string != s;
237 String::operator<(const String & s) const
239 return my_string < s.my_string;
243 String::operator<(const std::string & s) const
245 return my_string < s;
249 String::operator>(const String & s) const
251 return my_string > s.my_string;
255 String::operator<=(const String & s) const
257 return my_string <= s.my_string;
261 String::operator<=(const std::string & s) const
263 return my_string <= s;
267 String::operator>=(const String & s) const
269 return my_string >= s.my_string;
272 bool String::operator>=(const std::string & s) const
274 return my_string >= s;
278 String::operator+(const char *s)
280 return my_string + s;
284 String::operator+(const String & s)
286 return my_string + s.my_string;
290 String::operator+(const std::string & s)
292 return my_string + s;
295 String::operator const char *() const
297 return my_string.c_str ();
300 String::operator std::string () const
306 operator<<(std::ostream & s, const String & st)
308 return s << st.my_string;
312 operator>>(std::istream & s, String & st)
314 // The original version of string grabbed an entire line with
315 // operator>> so this version has to too (otherwise I'd just replace
316 // String with std::string everywhere because it would be just as
321 std::getline (s, temp);
328 std::string operator+(const std::string & s, const String & p)
330 return s + p.my_string;
333 bool operator==(const std::string & s, const String & p)
338 bool operator!=(const std::string & s, const String & p)
343 bool operator>(const std::string & s, const String & p)
348 bool operator<(const std::string & s, const String & p)
353 bool operator<=(const std::string & s, const String & p)
358 bool operator>=(const std::string & s, const String & p)