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.2 $
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.2 2002-09-28 06:45:51 arjen
27 New feature: subtring selection by regular expression.
28 Bugfix: use the std: namespace for STL classes istream and ostream
30 Revision 1.1 2002/07/25 08:01:26 arjen
31 First checkin, AXE release 0.2
33 *****************************/
35 // static const char RCSID[] = "$Id: String.h,v 1.2 2002-09-28 06:45:51 arjen Exp $";
46 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
47 ** NAME : String - Character String class.
48 ** MEMBERS : p -> s : Pointer to the actual String data.
49 ** p -> n : Number of references to this String.
50 ** OPERATORS : = : Assign a String, char *, char to a String.
51 ** +, += : concatenate Strings
52 ** ~ : Length of a String
53 ** [] : Individual character access.
54 ** ! : Empty String test.
55 ** () : Substring selection.
56 ** ostream << : String to output stream.
57 ** istream << : String from input stream.
59 ** >, <=, >= : String comparison.
62 ** DESCRIPTION : The String class counts the references to a String to
63 ** minimize copying and uses standard C++ character strings
66 ** RELATIONS : substring
68 ** LAST MODIFIED : Aug 27, 1999
69 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
75 friend class substring;
80 char *s; // pointer to data;
81 int n; // reference count
86 String(const char *); // String x = "abc"
87 String(char); // String x = 'a'
88 String(); // String x;
89 String(const String &); // String x = String ...
90 String(const substring &); // String x = String(s, l)
92 String& operator=(const char);
93 String& operator=(const char *);
94 String& operator=(const String &);
97 /* Numerical conversion */
99 String(long); // String x(12); x = "12"
100 String(unsigned long); // String x(12); x = "12"
101 String(int); // String x(12); x = "12"
104 return strtol(p->s, 0, 0);
107 operator unsigned long()
109 return strtoul(p->s, 0, 0);
114 return strtol(p->s, 0, 10);
119 return strtol(p->s, 0, 8);
124 return strtol(p->s, 0, 16);
130 return strtod(p->s, 0);
135 char& operator[](int i); // Individual character access
137 int operator~() const // Length of the String
149 operator const char*() const
157 operator bool() // Test for empty String
159 return p->s != 0 && p->s[0] != '\0';
163 * String concatenation.
164 * char and char* are handled by constructors.
167 String& operator+=(const String&);
168 String& operator+=(const char *);
169 friend String operator+(const String&, const String&);
170 friend String operator+(const String&, const char *);
171 friend String operator+(const char *, const String&);
174 * Shifting characters out
175 * "abcdefgh" <<= 3 = "defgh"
176 * "abcdefgh" >>= 3 = "abcde"
179 friend String operator<<(const String &x, int n);
180 String & operator<<=(int n);
182 friend String operator>>(const String &x, int n);
183 String & operator>>=(int n);
186 * Substring selection
188 * A substring can be selected by a start index and a length or
189 * by matching a regular expression.
191 * Selecting a substring by regular expression, combined with
192 * the lvalue semantics of the substring class is a particularly
193 * powerful concept. Possible uses are for example:
195 * (assuming String S, M; regex R;)
196 * M = S(R); -> Returns matching part of S into M.
197 * S(R) = "replacement"; -> replace matching part of S.
198 * S(R) = ""; -> Removes matching part from S.
199 * S(R) == S; -> true if and only if all of S matches R exactly.
202 substring operator()(int start, int len);
203 substring operator()(const regex &r);
209 friend std::ostream& operator<<(std::ostream &, const String &);
210 friend std::istream& operator>>(std::istream &, String &);
213 * String comparison tests
216 friend int operator==(const String& x, const String& y)
218 return strcmp(x.p->s, y.p->s) == 0;
221 friend int operator!=(const String& x, const String& y)
223 return strcmp(x.p->s, y.p->s) != 0;
226 friend int operator<=(const String& x, const String& y)
228 return strcmp(x.p->s, y.p->s) <= 0;
231 friend int operator>=(const String& x, const String& y)
233 return strcmp(x.p->s, y.p->s) >= 0;
236 friend int operator<(const String& x, const String& y)
238 return strcmp(x.p->s, y.p->s) < 0;
241 friend int operator>(const String& x, const String& y)
243 return strcmp(x.p->s, y.p->s) > 0;
246 friend int operator==(const String& x, const char * y)
248 return strcmp(x.p->s, y) == 0;
251 friend int operator!=(const String& x, const char * y)
253 return strcmp(x.p->s, y) != 0;
256 friend int operator<=(const String& x, const char * y)
258 return strcmp(x.p->s, y) <= 0;
261 friend int operator>=(const String& x, const char * y)
263 return strcmp(x.p->s, y) >= 0;
266 friend int operator<(const String& x, const char * y)
268 return strcmp(x.p->s, y) < 0;
271 friend int operator>(const String& x, const char * y)
273 return strcmp(x.p->s, y) > 0;
284 * Character searching and Pattern matching
292 // Regular expression pattern matching
294 friend bool operator == (const String &s, const regex &r);
295 friend bool operator == (const regex &r, const String &s);
298 * Other operators to think about...
301 * Numeric conversion with strtod and strtol and vice-versa,
303 * String nr = 123 // nr = "123"
304 * float x = 2.32; String f = x; // f = "2.32"
306 * int i = f; // i = 3
309 * int index(String&, start=0) // Find position of substring
312 * Literature: Bjarne Stroustrup, Section 6.9
313 * DDJ October 1991, pg 24
317 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
318 ** NAME : substring - Determine part of a String object.
319 ** MEMBERS : str : The String object of which this is a substring
320 ** start : index of the first substring character.
321 ** len : Number of characters in the substring
322 ** OPERATORS : = : Assignment of a String or char *
325 ** DESCRIPTION : This implements substring selection from a String object
326 ** with l-value semantics.
328 ** RELATIONS : String
331 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
338 String *str; // Where I am a substring of.
342 String& operator=(const String &);
343 String& operator=(char *s)
350 /* Regular expression matching */
360 regex(const String & reg);
361 regex(const char * reg);
364 friend bool operator == (const String &s, const regex &r);
365 friend bool operator == (const regex &r, const String &s);
368 #endif /* STRING_H */