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.
32 String::String(const char *s)
36 p->s = new char[len + 1];
40 String::String(const std::string & s)
43 // We do this instead of just s.length () because there might be a
44 // \0 in the string before the end (e.g. this is a message from the
46 const char* temp_str = s.c_str ();
47 len = strlen (temp_str);
48 p->s = new char[len + 1];
49 std::strcpy (p->s, temp_str);
52 String::String(const String & s)
59 String::String(long i)
61 std::ostringstream temp;
65 len = strlen(temp.str().c_str ());
66 p->s = new char[len + 1];
67 strcpy(p->s, temp.str().c_str ());
70 String::String(char c)
88 String::operator=(const char *s)
98 p->s = new char[len + 1];
105 String::operator=(const String & s)
107 s.p->n++; // protection contre st = st
118 String::operator=(const std::string & s)
128 p->s = new char[len + 1];
129 strcpy(p->s, s.c_str ());
135 String::length() const
143 if (!strchr(p->s, c))
146 return (int)(strchr(p->s, c) - p->s);
152 for (char * s = p->s; *s; s++)
162 return subString(0, n-1);
164 char *temp = new char[n+1];
167 for (int i = l; i < n; i++)
178 String::subString(int debut, int fin)
180 if (fin < debut) return "";
182 char * temp = new char[fin-debut+2];
183 strncpy(temp, p->s + debut, fin - debut + 1);
184 temp[fin - debut + 1] = '\0';
191 String::substr (int s, int e)
193 return subString (s, e);
197 String::subString(int debut)
199 return subString(debut, len - 1);
203 String::substr (int s)
205 return subString (s);
211 char *temp = new char[len + 1];
213 for (int i = 0; i < len + 1; i++)
214 if (isupper(p->s[i]))
215 temp[i] = tolower(p->s[i]);
227 char *temp = new char[len + 1];
229 for (int i = 0; i < len + 1; i++)
230 if (islower(p->s[i]))
231 temp[i] = toupper(p->s[i]);
243 int i = 0, j = len - 1;
245 while (i < j && (p->s[i] == ' ' || p->s[i] == '\t' || p->s[i] == '\r'))
248 while (j > 0 && (p->s[j] == ' ' || p->s[j] == '\t' || p->s[j] == '\r'))
251 return subString(i, j);
255 String::indexOf(char c)
257 char *s = std::strchr(p->s, c);
265 String::operator[](int i)
267 if (i < 0 || len < i) {
268 std::cerr << "String index out of range\n";
275 String::operator[](int i) const
277 if (i < 0 || len < i) {
278 std::cerr << "String index out of range\n";
285 String::operator==(const char *s) const
287 return std::strcmp(p->s, s) == 0;
291 String::operator==(const String & s) const
293 return (p == s.p) || (std::strcmp(p->s, s.p->s) == 0);
297 String::operator==(const std::string & s) const
299 return (p->s == s.c_str ()) || (std::strcmp (p->s, s.c_str()) == 0);
303 String::operator!=(const char *s) const
305 return std::strcmp(p->s, s) != 0;
309 String::operator!=(const String & s) const
311 return std::strcmp(p->s, s.p->s) != 0;
315 String::operator!=(const std::string & s) const
317 return ! (*this == s);
321 String::operator<(const String & s) const
323 return std::strcmp(p->s, s.p->s) < 0;
327 String::operator<(const std::string & s) const
329 return std::strcmp(p->s, s.c_str ()) < 0;
333 String::operator>(const String & s) const
335 return std::strcmp(p->s, s.p->s) > 0;
339 String::operator<=(const String & s) const
341 return std::strcmp(p->s, s.p->s) <= 0;
345 String::operator<=(const std::string & s) const
347 return (*this < s) || (*this == s);
351 String::operator>=(const String & s) const
353 return std::strcmp(p->s, s.p->s) >= 0;
356 bool String::operator>=(const std::string & s) const
358 return ! (*this < s);
362 String::operator+(const char *s)
364 char *temp = new char[len + std::strlen(s) + 1];
366 std::strcpy(temp, p->s);
367 std::strcat(temp, s);
376 String::operator+(const String & s)
378 char * temp = new char[len + s.len + 1];
380 std::strcpy(temp, p->s);
381 std::strcat(temp, s.p->s);
390 String::operator+(const std::string & s)
392 char * temp = new char[len + s.length () + 1];
393 std::strcpy (temp, p->s);
394 std::strcat (temp, s.c_str ());
402 String::operator const char *() const
407 String::operator std::string () const
409 return std::string (p->s);
413 operator<<(std::ostream & s, const String & st)
419 operator>>(std::istream & s, String & st)
423 st.p = new String::srep;
431 s.getline (buf, 2048, '\n');
434 st.len = strlen(buf);
435 st.p->s = new char[st.len + 1];
436 strcpy(st.p->s, buf);
441 std::string operator+(const std::string & s, const String & p)
443 std::string temp = s;
448 bool operator==(const std::string & s, const String & p)
453 bool operator!=(const std::string & s, const String & p)
458 bool operator>(const std::string & s, const String & p)
463 bool operator<(const std::string & s, const String & p)
468 bool operator<=(const std::string & s, const String & p)
473 bool operator>=(const std::string & s, const String & p)