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.4 $
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 : Mar 31, 2010
22 **************************************************************************/
24 /*****************************
26 Revision 1.4 2007/05/04 13:56:05 arjen
27 Added a copy contructor to the regex class. This prevents multiple frees in the destructor.
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:45:51 arjen
33 New feature: subtring selection by regular expression.
34 Bugfix: use the std: namespace for STL classes istream and ostream
36 Revision 1.1 2002/07/25 08:01:26 arjen
37 First checkin, AXE release 0.2
39 *****************************/
41 // static const char RCSID[] = "$Id: String.h,v 1.4 2007/05/04 13:56:05 arjen Exp $";
52 // Forward declarations.
56 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57 ** NAME : String - Character String class.
58 ** MEMBERS : p -> s : Pointer to the actual String data.
59 ** p -> n : Number of references to this String.
60 ** OPERATORS : = : Assign a String, char *, char to a String.
61 ** +, += : concatenate Strings
62 ** ~ : Length of a String
63 ** [] : Individual character access.
64 ** ! : Empty String test.
65 ** () : Substring selection.
66 ** ostream << : String to output stream.
67 ** istream << : String from input stream.
69 ** >, <=, >= : String comparison.
72 ** DESCRIPTION : The String class counts the references to a String to
73 ** minimize copying and uses standard C++ character strings
76 ** RELATIONS : substring
78 ** LAST MODIFIED : Aug 27, 1999
79 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
84 friend class substring;
89 char *s; // pointer to data;
90 int n; // reference count
95 String(const char *); // String x = "abc"
96 String(char); // String x = 'a'
97 String(); // String x;
98 String(const String &); // String x = String ...
99 String(const substring &); // String x = String(s, l)
101 String& operator=(const char);
102 String& operator=(const char *);
103 String& operator=(const String &);
106 /* Numerical conversion */
108 String(long); // String x(12); x = "12"
109 String(unsigned long); // String x(12); x = "12"
110 String(int); // String x(12); x = "12"
113 return strtol(p->s, 0, 0);
116 operator unsigned long()
118 return strtoul(p->s, 0, 0);
123 return strtol(p->s, 0, 10);
128 return strtol(p->s, 0, 8);
133 return strtol(p->s, 0, 16);
139 return strtod(p->s, 0);
144 char& operator[](int i); // Individual character access
146 int operator~() const // Length of the String
158 operator const char*() const
166 operator bool() // Test for empty String
168 return p->s != 0 && p->s[0] != '\0';
172 * String concatenation.
173 * char and char* are handled by constructors.
176 String& operator+=(const String&);
177 String& operator+=(const char *);
178 friend String operator+(const String&, const String&);
179 friend String operator+(const String&, const char *);
180 friend String operator+(const char *, const String&);
183 * Shifting characters out
184 * "abcdefgh" <<= 3 = "defgh"
185 * "abcdefgh" >>= 3 = "abcde"
188 friend String operator<<(const String &x, int n);
189 String & operator<<=(int n);
191 friend String operator>>(const String &x, int n);
192 String & operator>>=(int n);
195 * Substring selection
197 * A substring can be selected by a start index and a length or
198 * by matching a regular expression.
200 * Selecting a substring by regular expression, combined with
201 * the lvalue semantics of the substring class is a particularly
202 * powerful concept. Possible uses are for example:
204 * (assuming String S, M; regex R;)
205 * M = S(R); -> Returns matching part of S into M.
206 * S(R) = "replacement"; -> replace matching part of S.
207 * S(R) = ""; -> Removes matching part from S.
208 * S(R) == S; -> true if and only if all of S matches R exactly.
211 substring operator()(int start, int len);
212 substring operator()(const regex &r);
218 friend std::ostream& operator<<(std::ostream &, const String &);
219 friend std::istream& operator>>(std::istream &, String &);
222 * String comparison tests
225 friend int operator==(const String& x, const String& y)
227 return strcmp(x.p->s, y.p->s) == 0;
230 friend int operator!=(const String& x, const String& y)
232 return strcmp(x.p->s, y.p->s) != 0;
235 friend int operator<=(const String& x, const String& y)
237 return strcmp(x.p->s, y.p->s) <= 0;
240 friend int operator>=(const String& x, const String& y)
242 return strcmp(x.p->s, y.p->s) >= 0;
245 friend int operator<(const String& x, const String& y)
247 return strcmp(x.p->s, y.p->s) < 0;
250 friend int operator>(const String& x, const String& y)
252 return strcmp(x.p->s, y.p->s) > 0;
255 friend int operator==(const String& x, const char * y)
257 return strcmp(x.p->s, y) == 0;
260 friend int operator!=(const String& x, const char * y)
262 return strcmp(x.p->s, y) != 0;
265 friend int operator<=(const String& x, const char * y)
267 return strcmp(x.p->s, y) <= 0;
270 friend int operator>=(const String& x, const char * y)
272 return strcmp(x.p->s, y) >= 0;
275 friend int operator<(const String& x, const char * y)
277 return strcmp(x.p->s, y) < 0;
280 friend int operator>(const String& x, const char * y)
282 return strcmp(x.p->s, y) > 0;
289 String upper(); // Convert to upper case (ASCII)
290 String lower(); // Convert to lower case (ASCII)
291 String escape(); // Insert backslashes to escape special characters
292 String unescape(); // Remove backslashes from escape codes.
295 * Character searching and Pattern matching
303 // Regular expression pattern matching
305 friend bool operator == (const String &s, const regex &r);
306 friend bool operator == (const regex &r, const String &s);
309 * Other operators to think about...
312 * Numeric conversion with strtod and strtol and vice-versa,
314 * String nr = 123 // nr = "123"
315 * float x = 2.32; String f = x; // f = "2.32"
317 * int i = f; // i = 3
320 * int index(String&, start=0) // Find position of substring
323 * Literature: Bjarne Stroustrup, Section 6.9
324 * DDJ October 1991, pg 24
328 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
329 ** NAME : substring - Determine part of a String object.
330 ** MEMBERS : str : The String object of which this is a substring
331 ** start : index of the first substring character.
332 ** len : Number of characters in the substring
333 ** OPERATORS : = : Assignment of a String or char *
336 ** DESCRIPTION : This implements substring selection from a String object
337 ** with l-value semantics.
339 ** RELATIONS : String
342 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
349 String *str; // Where I am a substring of.
353 String& operator=(const String &);
354 String& operator=(char *s)
361 /* Regular expression matching */
372 regex(const String & reg);
373 regex(const char * reg);
374 regex(const regex & reg);
377 regex& operator=(const regex &);
379 friend bool operator == (const String &s, const regex &r);
380 friend bool operator == (const regex &r, const String &s);
383 #endif /* STRING_H */