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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
31 String::String(const char *s)
36 String::String(const std::string & s)
41 String::String(const String & s)
43 my_string = s.my_string;
46 String::String(long i)
48 std::ostringstream temp;
50 my_string = temp.str ();
53 String::String(char c)
55 my_string = std::string (1, c);
64 String::operator=(const char *s)
72 String::operator=(const String & s)
74 my_string = s.my_string;
80 String::operator=(const std::string & s)
88 String::length() const
90 return my_string.length ();
96 return my_string.find (c);
100 String::fill (char c)
102 my_string.replace (0,
103 my_string.length () - 1,
111 std::string temp = my_string;
113 temp.resize (n, ' ');
119 String::subString (int start, int end)
121 if (end < start) return "";
123 return my_string.substr (start, (end - start) + 1);
127 String::substr (int s, int e)
129 return subString (s, e);
133 String::subString (int start)
135 return subString (start, my_string.length () - 1);
139 String::substr (int s)
141 return subString (s);
147 return Utils::to_lower (my_string);
153 return Utils::to_upper (my_string);
159 return Utils::trim_str (my_string);
163 String::indexOf (char c)
165 std::string::size_type pos = my_string.find (c);
167 if (pos == std::string::npos)
174 String::operator[] (int i_)
178 if (i < 0 || my_string.length () < i) {
179 std::cerr << "String index out of range\n";
186 String::operator[](int i_) const
190 if (i < 0 || my_string.length () < i) {
191 std::cerr << "String index out of range\n";
198 String::operator==(const char *s) const
200 return my_string == s;
204 String::operator==(const String & s) const
206 return my_string == s.my_string;
210 String::operator==(const std::string & s) const
212 return my_string == s;
216 String::operator!=(const char *s) const
218 return my_string != s;
222 String::operator!=(const String & s) const
224 return my_string != s.my_string;
228 String::operator!=(const std::string & s) const
230 return my_string != s;
234 String::operator<(const String & s) const
236 return my_string < s.my_string;
240 String::operator<(const std::string & s) const
242 return my_string < s;
246 String::operator>(const String & s) const
248 return my_string > s.my_string;
252 String::operator<=(const String & s) const
254 return my_string <= s.my_string;
258 String::operator<=(const std::string & s) const
260 return my_string <= s;
264 String::operator>=(const String & s) const
266 return my_string >= s.my_string;
269 bool String::operator>=(const std::string & s) const
271 return my_string >= s;
275 String::operator+(const char *s)
277 return my_string + s;
281 String::operator+(const String & s)
283 return my_string + s.my_string;
287 String::operator+(const std::string & s)
289 return my_string + s;
292 String::operator const char *() const
294 return my_string.c_str ();
297 String::operator std::string () const
303 operator<<(std::ostream & s, const String & st)
305 return s << st.my_string;
309 operator>>(std::istream & s, String & st)
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
318 std::getline (s, temp);
325 std::string operator+(const std::string & s, const String & p)
327 return s + p.my_string;
330 bool operator==(const std::string & s, const String & p)
335 bool operator!=(const std::string & s, const String & p)
340 bool operator>(const std::string & s, const String & p)
345 bool operator<(const std::string & s, const String & p)
350 bool operator<=(const std::string & s, const String & p)
355 bool operator>=(const std::string & s, const String & p)