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(int); // String x(12); x = "12"
109 String(long); // String x(12); x = "12"
110 String(unsigned long); // String x(12); x = "12"
114 return strtol(p->s, 0, 0);
119 return strtol(p->s, 0, 0);
122 operator unsigned long()
124 return strtoul(p->s, 0, 0);
129 return strtol(p->s, 0, 10);
134 return strtol(p->s, 0, 8);
139 return strtol(p->s, 0, 16);
145 return strtod(p->s, 0);
149 char& operator[](size_t i); // Individual character access
151 int operator~() const // Length of the String
153 return p->s ? strlen(p->s) : 0;
163 operator const char*() const
171 operator bool() // Test for empty String
173 return p->s != 0 && p->s[0] != '\0';
177 * String concatenation.
178 * char and char* are handled by constructors.
181 String& operator+=(const String&);
182 String& operator+=(const char *);
183 friend String operator+(const String&, const String&);
184 friend String operator+(const String&, const char *);
185 friend String operator+(const char *, const String&);
188 * Shifting characters out
189 * "abcdefgh" <<= 3 = "defgh"
190 * "abcdefgh" >>= 3 = "abcde"
193 friend String operator<<(const String &x, int n);
194 String & operator<<=(int n);
196 friend String operator>>(const String &x, int n);
197 String & operator>>=(int n);
200 * Substring selection
202 * A substring can be selected by a start index and a length or
203 * by matching a regular expression.
205 * Selecting a substring by regular expression, combined with
206 * the lvalue semantics of the substring class is a particularly
207 * powerful concept. Possible uses are for example:
209 * (assuming String S, M; regex R;)
210 * M = S(R); -> Returns matching part of S into M.
211 * S(R) = "replacement"; -> replace matching part of S.
212 * S(R) = ""; -> Removes matching part from S.
213 * S(R) == S; -> true if and only if all of S matches R exactly.
216 substring operator()(int start, int len);
217 substring operator()(const regex &r);
223 friend std::ostream& operator<<(std::ostream &, const String &);
224 // Read one line and remove the trailing line feed.
225 friend std::istream& operator>>(std::istream &, String &);
228 * String comparison tests
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 String& y)
248 return strcmp(x.p->s, y.p->s) >= 0;
251 friend int operator<(const String& x, const String& y)
253 return strcmp(x.p->s, y.p->s) < 0;
256 friend int operator>(const String& x, const String& y)
258 return strcmp(x.p->s, y.p->s) > 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;
276 friend int operator>=(const String& x, const char * y)
278 return strcmp(x.p->s, y) >= 0;
281 friend int operator<(const String& x, const char * y)
283 return strcmp(x.p->s, y) < 0;
286 friend int operator>(const String& x, const char * y)
288 return strcmp(x.p->s, y) > 0;
295 String upper(); // Convert to upper case (ASCII)
296 String lower(); // Convert to lower case (ASCII)
297 String escape(); // Insert backslashes to escape special characters
298 String unescape(); // Remove backslashes from escape codes.
301 * Character searching and Pattern matching
309 // Regular expression pattern matching
311 friend bool operator == (const String &s, const regex &r);
312 friend bool operator == (const regex &r, const String &s);
313 // TODO: The != operator
316 * Other operators to think about...
319 * Numeric conversion with strtod and strtol and vice-versa,
321 * String nr = 123 // nr = "123"
322 * float x = 2.32; String f = x; // f = "2.32"
324 * int i = f; // i = 3
327 * int index(String&, start=0) // Find position of substring
330 * Literature: Bjarne Stroustrup, Section 6.9
331 * DDJ October 1991, pg 24
335 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
336 ** NAME : substring - Determine part of a String object.
337 ** MEMBERS : str : The String object of which this is a substring
338 ** start : index of the first substring character.
339 ** len : Number of characters in the substring
340 ** OPERATORS : = : Assignment of a String or char *
343 ** DESCRIPTION : This implements substring selection from a String object
344 ** with l-value semantics.
346 ** RELATIONS : String
349 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
356 String *str; // Where I am a substring of.
360 String& operator=(const String &);
361 String& operator=(const char *s)
368 /* Regular expression matching */
379 regex(const String & reg);
380 regex(const char * reg);
381 regex(const regex & reg);
384 regex& operator=(const regex &);
386 friend bool operator == (const String &s, const regex &r);
387 friend bool operator == (const regex &r, const String &s);
390 class StringException
396 StringException(String m)
407 #endif /* STRING_H */