1 /**************************************************************************
2 ** (c) Copyright 1997, Andromeda Technology & Automation
3 ***************************************************************************
4 ** MODULE INFORMATION *
5 ***********************
6 ** FILE NAME : string.cpp
7 ** SYSTEM NAME : Andromeda X-Windows Encapsulation
8 ** VERSION NUMBER : $Revision: 1.2 $
10 ** DESCRIPTION : String class implementation.
15 ***************************************************************************
16 ** ADMINISTRATIVE INFORMATION *
17 ********************************
18 ** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
19 ** CREATION DATE : Nov 17, 1997
20 ** LAST UPDATE : Aug 26, 1999
22 **************************************************************************/
24 /*****************************
26 Revision 1.2 2002-09-28 06:42:11 arjen
27 A few small bug fixes.
29 Revision 1.1 2002/07/25 08:01:27 arjen
30 First checkin, AXE release 0.2
32 *****************************/
34 static const char RCSID[] = "$Id: string.cpp,v 1.2 2002-09-28 06:42:11 arjen Exp $";
40 // Constructors and destructors for the String class
42 String::String() // Create an empty String
49 String::String(char c) // Create a String from a char
58 String::String(const char *s) // Create a String from a char *
61 p->s = new char[strlen(s)+1];
66 String::String(const String& x) // Create a String from another String
72 String::String(const substring &x)
76 p->s = new char[x.len+1];
77 strncpy(p->s, x.str->p->s+x.start, x.len);
78 p->s[x.len] = '\0'; // strncpy does not add the \0
92 * Assignment operators must handle cleanup of their left-hand operand.
95 String& String::operator=(const char c)
112 String& String::operator=(const char *s)
122 p->s = new char[strlen(s)+1];
128 /* Make sure that assignment of an object to itself works correctly: */
130 String& String::operator=(const String& x)
143 /* Numerical conversion */
145 String::String(int i)
148 p->s = new char[15]; // A little longer than needed...
149 sprintf(p->s, "%d", i);
153 String::String(long i)
156 p->s = new char[15]; // A little longer than needed...
157 sprintf(p->s, "%ld", i);
161 String::String(unsigned long i)
164 p->s = new char[15]; // A little longer than needed...
165 sprintf(p->s, "%lu", i);
169 String::String(double d)
172 p->s = new char[25]; // A little longer than needed...
173 sprintf(p->s, "%.3f", d);
177 /* String concatenation */
179 String& String::operator+=(const String& x)
181 char *s = new char[strlen(p->s) + strlen(x.p->s) + 1];
199 String& String::operator+=(const char * str)
201 char *s = new char[strlen(p->s) + strlen(str) + 1];
219 String operator+(const String& x, const String& y)
227 String operator+(const String& x, const char * y)
235 String operator+(const char * x, const String& y)
243 /* Shift operators */
246 String operator<<(const String &x, int n)
254 String & String::operator<<=(int n)
256 // Make sure we are the only one being shifted.
260 char *s = new char[strlen(p->s) + 1];
269 // Shift left means we really have to copy all characters.
271 int len = strlen(p->s);
276 // Shift more than we have: the String becomes empty.
281 for (i = 0; i <= len - n; i++)
290 String operator>>(const String &x, int n)
298 String & String::operator>>=(int n)
300 // Make sure we are the only one being shifted.
304 char *s = new char[strlen(p->s) + 1];
313 // Shift right is simple: just put the '\0' n places back.
316 int len = strlen(p->s);
320 // Shift more than we have: the String becomes empty.
325 p->s[len - n] = '\0';
331 /* Substring selection and assignment */
333 substring String::operator()(int start, int len)
343 String& substring::operator=(const String &x)
345 char *s = new char[strlen(x.p->s) + ~*str - len + 1];
347 strncpy(s, str->p->s, start);
350 strcat(s, str->p->s+start+len);
355 /* Input and output */
357 std::ostream& operator<<(std::ostream& s, const String& x)
364 std::istream& operator>>(std::istream& s, String& x)
375 while (s && buf[i] != '\n');
385 * The subscript operator is provided for access to individual characters
388 char& String::operator[](int i)
393 String String::upper(void)
398 up.p->s = new char[strlen(p->s)+1];
400 for(i=0; p->s[i]; i++)
402 up.p->s[i] = toupper(p->s[i]);
409 String String::lower(void)
414 low.p->s = new char[strlen(p->s)+1];
416 for(i=0; p->s[i]; i++)
418 low.p->s[i] = tolower(p->s[i]);
425 // Find the first occurance of 'c'
427 int String::index(char c)
431 found = strchr(p->s, c);
438 // Find the last occurance of 'c'
440 int String::rindex(char c)
444 found = strrchr(p->s, c);
451 /* In: see if I am part of x, return -1 if not found */
453 int String::in(String & x)
457 match = strstr(x.p->s, p->s);
459 return match - x.p->s;