1 // String.C -*- C++ -*-
2 // Copyright (c) 1997, 1998 Etienne BERNARD
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
31 String::String(const char *s)
35 p->s = new char[len + 1];
39 String::String(const std::string & s)
43 p->s = new char[len + 1];
44 std::strcpy (p->s, s.c_str ());
47 String::String(const String & s)
54 String::String(long i)
56 std::ostringstream temp;
60 len = strlen(temp.str().c_str ());
61 p->s = new char[len + 1];
62 strcpy(p->s, temp.str().c_str ());
65 String::String(char c)
83 String::operator=(const char *s)
93 p->s = new char[len + 1];
100 String::operator=(const String & s)
102 s.p->n++; // protection contre st = st
113 String::operator=(const std::string & s)
123 p->s = new char[len + 1];
124 strcpy(p->s, s.c_str ());
130 String::length() const
138 if (!strchr(p->s, c))
141 return (int)(strchr(p->s, c) - p->s);
147 for (char * s = p->s; *s; s++)
157 return subString(0, n-1);
159 char *temp = new char[n+1];
162 for (int i = l; i < n; i++)
173 String::subString(int debut, int fin)
175 if (fin < debut) return "";
177 char * temp = new char[fin-debut+2];
178 strncpy(temp, p->s + debut, fin - debut + 1);
179 temp[fin - debut + 1] = '\0';
186 String::subString(int debut)
188 return subString(debut, len - 1);
194 char *temp = new char[len + 1];
196 for (int i = 0; i < len + 1; i++)
197 if (isupper(p->s[i]))
198 temp[i] = tolower(p->s[i]);
210 char *temp = new char[len + 1];
212 for (int i = 0; i < len + 1; i++)
213 if (islower(p->s[i]))
214 temp[i] = toupper(p->s[i]);
226 int i = 0, j = len - 1;
228 while (i < j && (p->s[i] == ' ' || p->s[i] == '\t' || p->s[i] == '\r'))
231 while (j > 0 && (p->s[j] == ' ' || p->s[j] == '\t' || p->s[i] == '\r'))
234 return subString(i, j);
238 String::indexOf(char c)
240 char *s = std::strchr(p->s, c);
248 String::operator[](int i)
250 if (i < 0 || len < i) {
251 std::cerr << "String index out of range\n";
258 String::operator[](int i) const
260 if (i < 0 || len < i) {
261 std::cerr << "String index out of range\n";
268 String::operator==(const char *s) const
270 return std::strcmp(p->s, s) == 0;
274 String::operator==(const String & s) const
276 return (p == s.p) || (std::strcmp(p->s, s.p->s) == 0);
280 String::operator==(const std::string & s) const
282 return (p->s == s.c_str ()) || (std::strcmp (p->s, s.c_str()) == 0);
286 String::operator!=(const char *s) const
288 return std::strcmp(p->s, s) != 0;
292 String::operator!=(const String & s) const
294 return std::strcmp(p->s, s.p->s) != 0;
298 String::operator!=(const std::string & s) const
300 return ! (*this == s);
304 String::operator<(const String & s) const
306 return std::strcmp(p->s, s.p->s) < 0;
310 String::operator<(const std::string & s) const
312 return std::strcmp(p->s, s.c_str ()) < 0;
316 String::operator>(const String & s) const
318 return std::strcmp(p->s, s.p->s) > 0;
322 String::operator<=(const String & s) const
324 return std::strcmp(p->s, s.p->s) <= 0;
328 String::operator<=(const std::string & s) const
330 return (*this < s) || (*this == s);
334 String::operator>=(const String & s) const
336 return std::strcmp(p->s, s.p->s) >= 0;
339 bool String::operator>=(const std::string & s) const
341 return ! (*this < s);
345 String::operator+(const char *s)
347 char *temp = new char[len + std::strlen(s) + 1];
349 std::strcpy(temp, p->s);
350 std::strcat(temp, s);
359 String::operator+(const String & s)
361 char * temp = new char[len + s.len + 1];
363 std::strcpy(temp, p->s);
364 std::strcat(temp, s.p->s);
373 String::operator+(const std::string & s)
375 char * temp = new char[len + s.length () + 1];
376 std::strcpy (temp, p->s);
377 std::strcat (temp, s.c_str ());
385 String::operator const char *() const
390 String::operator std::string () const
392 return std::string (p->s);
396 operator<<(std::ostream & s, const String & st)
402 operator>>(std::istream & s, String & st)
406 st.p = new String::srep;
414 s.getline (buf, 2048, '\n');
417 st.len = strlen(buf);
418 st.p->s = new char[st.len + 1];
419 strcpy(st.p->s, buf);
424 std::string operator+(const std::string & s, const String & p)
426 std::string temp = s;
431 bool operator==(const std::string & s, const String & p)
436 bool operator!=(const std::string & s, const String & p)
441 bool operator>(const std::string & s, const String & p)
446 bool operator<(const std::string & s, const String & p)
451 bool operator<=(const std::string & s, const String & p)
456 bool operator>=(const std::string & s, const String & p)