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.1 $
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.1 2002-07-25 08:01:27 arjen
27 First checkin, AXE release 0.2
29 *****************************/
31 static const char RCSID[] = "$Id: string.cpp,v 1.1 2002-07-25 08:01:27 arjen Exp $";
37 // Constructors and destructors for the String class
39 String::String() // Create an empty String
46 String::String(char c) // Create a String from a char
55 String::String(const char *s) // Create a String from a char *
58 p->s = new char[strlen(s)+1];
63 String::String(const String& x) // Create a String from another String
69 String::String(const substring &x)
73 p->s = new char[x.len+1];
74 strncpy(p->s, x.str->p->s+x.start, x.len);
75 p->s[x.len] = '\0'; // strncpy does not add the \0
89 * Assignment operators must handle cleanup of their left-hand operand.
92 String& String::operator=(const char c)
109 String& String::operator=(const char *s)
119 p->s = new char[strlen(s)+1];
125 /* Make sure that assignment of an object to itself works correctly: */
127 String& String::operator=(const String& x)
140 /* Numerical conversion */
142 String::String(int i)
145 p->s = new char[15]; // A little longer than needed...
146 sprintf(p->s, "%d", i);
150 String::String(long i)
153 p->s = new char[15]; // A little longer than needed...
154 sprintf(p->s, "%ld", i);
158 String::String(unsigned long i)
161 p->s = new char[15]; // A little longer than needed...
162 sprintf(p->s, "%lu", i);
166 String::String(double d)
169 p->s = new char[25]; // A little longer than needed...
170 sprintf(p->s, "%.3f", d);
174 /* String concatenation */
176 String& String::operator+=(const String& x)
178 char *s = new char[strlen(p->s) + strlen(x.p->s) + 1];
196 String& String::operator+=(const char * str)
198 char *s = new char[strlen(p->s) + strlen(str) + 1];
216 String operator+(const String& x, const String& y)
224 String operator+(const String& x, const char * y)
232 String operator+(const char * x, const String& y)
240 /* Shift operators */
243 String operator<<(const String &x, int n)
251 String & String::operator<<=(int n)
253 // Make sure we are the only one being shifted.
257 char *s = new char[strlen(p->s) + 1];
266 // Shift left means we really have to copy all characters.
268 int len = strlen(p->s);
273 // Shift more than we have: the String becomes empty.
278 for (i = 0; i <= len - n; i++)
287 String operator>>(const String &x, int n)
295 String & String::operator>>=(int n)
297 // Make sure we are the only one being shifted.
301 char *s = new char[strlen(p->s) + 1];
310 // Shift right is simple: just put the '\0' n places back.
313 int len = strlen(p->s);
317 // Shift more than we have: the String becomes empty.
322 p->s[len - n] = '\0';
328 /* Substring selection and assignment */
330 substring String::operator()(int start, int len)
340 String& substring::operator=(const String &x)
342 char *s = new char[strlen(x.p->s) + ~*str - len + 1];
344 strncpy(s, str->p->s, start);
347 strcat(s, str->p->s+start+len);
352 /* Input and output */
354 ostream& operator<<(ostream& s, const String& x)
361 istream& operator>>(istream& s, String& x)
372 while (buf[i] != '\n');
379 * The subscript operator is provided for access to individual characters
382 char& String::operator[](int i)
387 String String::upper(void)
392 up.p->s = new char[strlen(p->s)+1];
394 for(i=0; p->s[i]; i++)
396 up.p->s[i] = toupper(p->s[i]);
403 String String::lower(void)
408 low.p->s = new char[strlen(p->s)+1];
410 for(i=0; p->s[i]; i++)
412 low.p->s[i] = tolower(p->s[i]);
419 // Find the first occurance of 'c'
421 int String::index(char c)
425 found = strchr(p->s, c);
432 // Find the last occurance of 'c'
434 int String::rindex(char c)
438 found = strrchr(p->s, c);
445 /* In: see if I am part of x, return -1 if not found */
447 int String::in(String & x)
451 match = strstr(x.p->s, p->s);
453 return match - x.p->s;