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.6 $
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 : Nov 02, 2002
22 **************************************************************************/
24 /*****************************
26 Revision 1.6 2010-12-30 12:04:02 arjen
27 Added regex assignment operator
29 Revision 1.4 2007-05-04 13:56:05 arjen
30 Added a copy contructor to the regex class. This prevents multiple frees in the destructor.
32 Revision 1.3 2002/11/03 13:18:57 arjen
33 New functions - String::escape() and String::unescape()
35 Revision 1.2 2002/09/28 06:45:51 arjen
36 New feature: subtring selection by regular expression.
37 Bugfix: use the std: namespace for STL classes istream and ostream
39 Revision 1.1 2002/07/25 08:01:26 arjen
40 First checkin, AXE release 0.2
42 *****************************/
44 // static const char RCSID[] = "$Id: String.h,v 1.6 2010-12-30 12:04:02 arjen Exp $";
55 // Forward declarations.
59 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
60 ** NAME : String - Character String class.
61 ** MEMBERS : p -> s : Pointer to the actual String data.
62 ** p -> n : Number of references to this String.
63 ** OPERATORS : = : Assign a String, char *, char to a String.
64 ** +, += : concatenate Strings
65 ** ~ : Length of a String
66 ** [] : Individual character access.
67 ** ! : Empty String test.
68 ** () : Substring selection.
69 ** ostream << : String to output stream.
70 ** istream << : String from input stream.
72 ** >, <=, >= : String comparison.
75 ** DESCRIPTION : The String class counts the references to a String to
76 ** minimize copying and uses standard C++ character strings
79 ** RELATIONS : substring
81 ** LAST MODIFIED : Aug 27, 1999
82 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
87 friend class substring;
92 char *s; // pointer to data;
93 int n; // reference count
98 String(const char *); // String x = "abc"
99 String(char); // String x = 'a'
100 String(); // String x;
101 String(const String &); // String x = String ...
102 String(const substring &); // String x = String(s, l)
104 String& operator=(const char);
105 String& operator=(const char *);
106 String& operator=(const String &);
109 /* Numerical conversion */
111 String(long); // String x(12); x = "12"
112 String(unsigned long); // String x(12); x = "12"
113 String(int); // String x(12); x = "12"
116 return strtol(p->s, 0, 0);
119 operator unsigned long()
121 return strtoul(p->s, 0, 0);
126 return strtol(p->s, 0, 10);
131 return strtol(p->s, 0, 8);
136 return strtol(p->s, 0, 16);
142 return strtod(p->s, 0);
147 char& operator[](int i); // Individual character access
149 int operator~() const // Length of the String
161 operator const char*() const
169 operator bool() // Test for empty String
171 return p->s != 0 && p->s[0] != '\0';
175 * String concatenation.
176 * char and char* are handled by constructors.
179 String& operator+=(const String&);
180 String& operator+=(const char *);
181 friend String operator+(const String&, const String&);
182 friend String operator+(const String&, const char *);
183 friend String operator+(const char *, const String&);
186 * Shifting characters out
187 * "abcdefgh" <<= 3 = "defgh"
188 * "abcdefgh" >>= 3 = "abcde"
191 friend String operator<<(const String &x, int n);
192 String & operator<<=(int n);
194 friend String operator>>(const String &x, int n);
195 String & operator>>=(int n);
198 * Substring selection
200 * A substring can be selected by a start index and a length or
201 * by matching a regular expression.
203 * Selecting a substring by regular expression, combined with
204 * the lvalue semantics of the substring class is a particularly
205 * powerful concept. Possible uses are for example:
207 * (assuming String S, M; regex R;)
208 * M = S(R); -> Returns matching part of S into M.
209 * S(R) = "replacement"; -> replace matching part of S.
210 * S(R) = ""; -> Removes matching part from S.
211 * S(R) == S; -> true if and only if all of S matches R exactly.
214 substring operator()(int start, int len);
215 substring operator()(const regex &r);
221 friend std::ostream& operator<<(std::ostream &, const String &);
222 friend std::istream& operator>>(std::istream &, String &);
225 * String comparison tests
228 friend int operator==(const String& x, const String& y)
230 return strcmp(x.p->s, y.p->s) == 0;
233 friend int operator!=(const String& x, const String& y)
235 return strcmp(x.p->s, y.p->s) != 0;
238 friend int operator<=(const String& x, const String& y)
240 return strcmp(x.p->s, y.p->s) <= 0;
243 friend int operator>=(const String& x, const String& y)
245 return strcmp(x.p->s, y.p->s) >= 0;
248 friend int operator<(const String& x, const String& y)
250 return strcmp(x.p->s, y.p->s) < 0;
253 friend int operator>(const String& x, const String& y)
255 return strcmp(x.p->s, y.p->s) > 0;
258 friend int operator==(const String& x, const char * y)
260 return strcmp(x.p->s, y) == 0;
263 friend int operator!=(const String& x, const char * y)
265 return strcmp(x.p->s, y) != 0;
268 friend int operator<=(const String& x, const char * y)
270 return strcmp(x.p->s, y) <= 0;
273 friend int operator>=(const String& x, const char * y)
275 return strcmp(x.p->s, y) >= 0;
278 friend int operator<(const String& x, const char * y)
280 return strcmp(x.p->s, y) < 0;
283 friend int operator>(const String& x, const char * y)
285 return strcmp(x.p->s, y) > 0;
292 String upper(); // Convert to upper case (ASCII)
293 String lower(); // Convert to lower case (ASCII)
294 String escape(); // Insert backslashes to escape special characters
295 String unescape(); // Remove backslashes from escape codes.
298 * Character searching and Pattern matching
306 // Regular expression pattern matching
308 friend bool operator == (const String &s, const regex &r);
309 friend bool operator == (const regex &r, const String &s);
312 * Other operators to think about...
315 * Numeric conversion with strtod and strtol and vice-versa,
317 * String nr = 123 // nr = "123"
318 * float x = 2.32; String f = x; // f = "2.32"
320 * int i = f; // i = 3
323 * int index(String&, start=0) // Find position of substring
326 * Literature: Bjarne Stroustrup, Section 6.9
327 * DDJ October 1991, pg 24
331 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
332 ** NAME : substring - Determine part of a String object.
333 ** MEMBERS : str : The String object of which this is a substring
334 ** start : index of the first substring character.
335 ** len : Number of characters in the substring
336 ** OPERATORS : = : Assignment of a String or char *
339 ** DESCRIPTION : This implements substring selection from a String object
340 ** with l-value semantics.
342 ** RELATIONS : String
345 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
352 String *str; // Where I am a substring of.
356 String& operator=(const String &);
357 String& operator=(char *s)
364 /* Regular expression matching */
375 regex(const String & reg);
376 regex(const char * reg);
377 regex(const regex & reg);
380 friend bool operator == (const String &s, const regex &r);
381 friend bool operator == (const regex &r, const String &s);
384 #endif /* STRING_H */