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.4 $
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 : Nov 03, 2002
22 **************************************************************************/
24 /*****************************
26 Revision 1.4 2003-03-29 07:18:54 arjen
27 String constructor and assignment from char * are more robust fro NULL pointers.
29 Revision 1.3 2002/11/03 13:18:57 arjen
30 New functions - String::escape() and String::unescape()
32 Revision 1.2 2002/09/28 06:42:11 arjen
33 A few small bug fixes.
35 Revision 1.1 2002/07/25 08:01:27 arjen
36 First checkin, AXE release 0.2
38 *****************************/
40 static const char RCSID[] = "$Id: string.cpp,v 1.4 2003-03-29 07:18:54 arjen Exp $";
46 // Constructors and destructors for the String class
48 String::String() // Create an empty String
55 String::String(char c) // Create a String from a char
64 String::String(const char *s) // Create a String from a char *
73 p->s = new char[strlen(s)+1];
79 String::String(const String& x) // Create a String from another String
85 String::String(const substring &x)
89 p->s = new char[x.len+1];
90 strncpy(p->s, x.str->p->s+x.start, x.len);
91 p->s[x.len] = '\0'; // strncpy does not add the \0
105 * Assignment operators must handle cleanup of their left-hand operand.
108 String& String::operator=(const char c)
125 String& String::operator=(const char *s)
137 p->s = new char[strlen(s)+1];
144 /* Make sure that assignment of an object to itself works correctly: */
146 String& String::operator=(const String& x)
159 /* Numerical conversion */
161 String::String(int i)
164 p->s = new char[15]; // A little longer than needed...
165 sprintf(p->s, "%d", i);
169 String::String(long i)
172 p->s = new char[15]; // A little longer than needed...
173 sprintf(p->s, "%ld", i);
177 String::String(unsigned long i)
180 p->s = new char[15]; // A little longer than needed...
181 sprintf(p->s, "%lu", i);
185 String::String(double d)
188 p->s = new char[25]; // A little longer than needed...
189 sprintf(p->s, "%.3f", d);
193 /* String concatenation */
195 String& String::operator+=(const String& x)
197 char *s = new char[strlen(p->s) + strlen(x.p->s) + 1];
215 String& String::operator+=(const char * str)
217 char *s = new char[strlen(p->s) + strlen(str) + 1];
235 String operator+(const String& x, const String& y)
243 String operator+(const String& x, const char * y)
251 String operator+(const char * x, const String& y)
259 /* Shift operators */
262 String operator<<(const String &x, int n)
270 String & String::operator<<=(int n)
272 // Make sure we are the only one being shifted.
276 char *s = new char[strlen(p->s) + 1];
285 // Shift left means we really have to copy all characters.
287 int len = strlen(p->s);
292 // Shift more than we have: the String becomes empty.
297 for (i = 0; i <= len - n; i++)
306 String operator>>(const String &x, int n)
314 String & String::operator>>=(int n)
316 // Make sure we are the only one being shifted.
320 char *s = new char[strlen(p->s) + 1];
329 // Shift right is simple: just put the '\0' n places back.
332 int len = strlen(p->s);
336 // Shift more than we have: the String becomes empty.
341 p->s[len - n] = '\0';
347 /* Substring selection and assignment */
349 substring String::operator()(int start, int len)
359 String& substring::operator=(const String &x)
361 char *s = new char[strlen(x.p->s) + ~*str - len + 1];
363 strncpy(s, str->p->s, start);
366 strcat(s, str->p->s+start+len);
371 /* Input and output */
373 std::ostream& operator<<(std::ostream& s, const String& x)
380 std::istream& operator>>(std::istream& s, String& x)
391 while (s && buf[i] != '\n');
401 * The subscript operator is provided for access to individual characters
404 char& String::operator[](int i)
409 String String::upper()
414 up.p->s = new char[strlen(p->s)+1];
416 for(i=0; p->s[i]; i++)
418 up.p->s[i] = toupper(p->s[i]);
425 String String::lower()
430 low.p->s = new char[strlen(p->s)+1];
432 for(i=0; p->s[i]; i++)
434 low.p->s[i] = tolower(p->s[i]);
441 String String::escape()
443 const int BUFSIZE = 500;
445 char buffer[BUFSIZE];
447 int i; // Index in buffer[]
448 int j; // Index in *this
451 for (j = 0; p->s[j] != '\0'; j++)
512 if (p->s[j] > '\x20' && p->s[j] < '\x7F')
514 buffer[i++] = p->s[j];
520 // Turn into hexadecimal representation
524 nibble = (p->s[j] >> 4) & 0x0f;
525 buffer[i++] = nibble < 10 ? nibble + '0' : nibble - 10 + 'A';
526 nibble = p->s[j] & 0x0f;
527 buffer[i++] = nibble < 10 ? nibble + '0' : nibble - 10 + 'A';
539 String String::unescape()
544 unescaped.p->s = new char[strlen(p->s)+1];
605 while (*s >= '0' && *s <= '7')
616 s++; // Skip the initial 'x'
620 *d += *s > '9' ? toupper(*s) - 'A' + 10 : *s - '0';
643 // Find the first occurance of 'c'
645 int String::index(char c)
649 found = strchr(p->s, c);
656 // Find the last occurance of 'c'
658 int String::rindex(char c)
662 found = strrchr(p->s, c);
669 /* In: see if I am part of x, return -1 if not found */
671 int String::in(String & x)
675 match = strstr(x.p->s, p->s);
677 return match - x.p->s;