1 /**************************************************************************
2 ** (c) Copyright 1997, Andromeda Technology & Automation
3 ***************************************************************************
4 ** MODULE INFORMATION *
5 ***********************
6 ** FILE NAME : String.h
7 ** SYSTEM NAME : Andromeda X-Windows Encapsulation
8 ** VERSION NUMBER : $Revision: 1.1 $
10 ** DESCRIPTION : Character String class definition
12 ** EXPORTED OBJECTS : class String
13 ** LOCAL OBJECTS : class substring
15 ***************************************************************************
16 ** ADMINISTRATIVE INFORMATION *
17 ********************************
18 ** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
19 ** CREATION DATE : Nov 17, 1995
20 ** LAST UPDATE : Feb 23, 2001
22 **************************************************************************/
24 /*****************************
26 Revision 1.1 2002-07-25 08:01:26 arjen
27 First checkin, AXE release 0.2
29 *****************************/
31 // static const char RCSID[] = "$Id: String.h,v 1.1 2002-07-25 08:01:26 arjen Exp $";
42 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
43 ** NAME : String - Character String class.
44 ** MEMBERS : p -> s : Pointer to the actual String data.
45 ** p -> n : Number of references to this String.
46 ** OPERATORS : = : Assign a String, char *, char to a String.
47 ** +, += : concatenate Strings
48 ** ~ : Length of a String
49 ** [] : Individual character access.
50 ** ! : Empty String test.
51 ** () : Substring selection.
52 ** ostream << : String to output stream.
53 ** istream << : String from input stream.
55 ** >, <=, >= : String comparison.
58 ** DESCRIPTION : The String class counts the references to a String to
59 ** minimize copying and uses standard C++ character strings
62 ** RELATIONS : substring
64 ** LAST MODIFIED : Aug 27, 1999
65 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
71 friend class substring;
76 char *s; // pointer to data;
77 int n; // reference count
82 String(const char *); // String x = "abc"
83 String(char); // String x = 'a'
84 String(); // String x;
85 String(const String &); // String x = String ...
86 String(const substring &); // String x = String(s, l)
88 String& operator=(const char);
89 String& operator=(const char *);
90 String& operator=(const String &);
93 /* Numerical conversion */
95 String(long); // String x(12); x = "12"
96 String(unsigned long); // String x(12); x = "12"
97 String(int); // String x(12); x = "12"
100 return strtol(p->s, 0, 0);
103 operator unsigned long()
105 return strtoul(p->s, 0, 0);
110 return strtol(p->s, 0, 10);
115 return strtol(p->s, 0, 8);
120 return strtol(p->s, 0, 16);
126 return strtod(p->s, 0);
131 char& operator[](int i); // Individual character access
133 int operator~() const // Length of the String
145 operator const char*() const
153 operator bool() // Test for empty String
155 return p->s != 0 && p->s[0] != '\0';
159 * String concatenation.
160 * char and char* are handled by constructors.
163 String& operator+=(const String&);
164 String& operator+=(const char *);
165 friend String operator+(const String&, const String&);
166 friend String operator+(const String&, const char *);
167 friend String operator+(const char *, const String&);
170 * Shifting characters out
171 * "abcdefgh" <<= 3 = "defgh"
172 * "abcdefgh" >>= 3 = "abcde"
175 friend String operator<<(const String &x, int n);
176 String & operator<<=(int n);
178 friend String operator>>(const String &x, int n);
179 String & operator>>=(int n);
182 * Substring selection
185 substring operator()(int start, int len);
191 friend ostream& operator<<(ostream &, const String &);
192 friend istream& operator>>(istream &, String &);
195 * String comparison tests
198 friend int operator==(const String& x, const String& y)
200 return strcmp(x.p->s, y.p->s) == 0;
203 friend int operator!=(const String& x, const String& y)
205 return strcmp(x.p->s, y.p->s) != 0;
208 friend int operator<=(const String& x, const String& y)
210 return strcmp(x.p->s, y.p->s) <= 0;
213 friend int operator>=(const String& x, const String& y)
215 return strcmp(x.p->s, y.p->s) >= 0;
218 friend int operator<(const String& x, const String& y)
220 return strcmp(x.p->s, y.p->s) < 0;
223 friend int operator>(const String& x, const String& y)
225 return strcmp(x.p->s, y.p->s) > 0;
228 friend int operator==(const String& x, const char * y)
230 return strcmp(x.p->s, y) == 0;
233 friend int operator!=(const String& x, const char * y)
235 return strcmp(x.p->s, y) != 0;
238 friend int operator<=(const String& x, const char * y)
240 return strcmp(x.p->s, y) <= 0;
243 friend int operator>=(const String& x, const char * y)
245 return strcmp(x.p->s, y) >= 0;
248 friend int operator<(const String& x, const char * y)
250 return strcmp(x.p->s, y) < 0;
253 friend int operator>(const String& x, const char * y)
255 return strcmp(x.p->s, y) > 0;
266 * Character searching and Pattern matching
274 // Regular expression pattern matching
276 friend bool operator == (const String &s, const regex &r);
277 friend bool operator == (const regex &r, const String &s);
280 * Other operators to think about...
283 * Numeric conversion with strtod and strtol and vice-versa,
285 * String nr = 123 // nr = "123"
286 * float x = 2.32; String f = x; // f = "2.32"
288 * int i = f; // i = 3
291 * int index(String&, start=0) // Find position of substring
294 * Literature: Bjarne Stroustrup, Section 6.9
295 * DDJ October 1991, pg 24
299 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
300 ** NAME : substring - Determine part of a String object.
301 ** MEMBERS : str : The String object of which this is a substring
302 ** start : index of the first substring character.
303 ** len : Number of characters in the substring
304 ** OPERATORS : = : Assignment of a String or char *
307 ** DESCRIPTION : This implements substring selection from a String object
308 ** with l-value semantics.
310 ** RELATIONS : String
313 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
320 String *str; // Where I am a substring of.
324 String& operator=(const String &);
325 String& operator=(char *s)
332 /* Regular expression matching */
340 regex(const String & reg);
341 regex(const char * reg);
344 friend bool operator == (const String &s, const regex &r);
345 friend bool operator == (const regex &r, const String &s);
348 #endif /* STRING_H */