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.3 $
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.3 2002-11-03 13:18:57 arjen
27 New functions - String::escape() and String::unescape()
29 Revision 1.2 2002/09/28 06:45:51 arjen
30 New feature: subtring selection by regular expression.
31 Bugfix: use the std: namespace for STL classes istream and ostream
33 Revision 1.1 2002/07/25 08:01:26 arjen
34 First checkin, AXE release 0.2
36 *****************************/
38 // static const char RCSID[] = "$Id: String.h,v 1.3 2002-11-03 13:18:57 arjen Exp $";
49 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
50 ** NAME : String - Character String class.
51 ** MEMBERS : p -> s : Pointer to the actual String data.
52 ** p -> n : Number of references to this String.
53 ** OPERATORS : = : Assign a String, char *, char to a String.
54 ** +, += : concatenate Strings
55 ** ~ : Length of a String
56 ** [] : Individual character access.
57 ** ! : Empty String test.
58 ** () : Substring selection.
59 ** ostream << : String to output stream.
60 ** istream << : String from input stream.
62 ** >, <=, >= : String comparison.
65 ** DESCRIPTION : The String class counts the references to a String to
66 ** minimize copying and uses standard C++ character strings
69 ** RELATIONS : substring
71 ** LAST MODIFIED : Aug 27, 1999
72 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
78 friend class substring;
83 char *s; // pointer to data;
84 int n; // reference count
89 String(const char *); // String x = "abc"
90 String(char); // String x = 'a'
91 String(); // String x;
92 String(const String &); // String x = String ...
93 String(const substring &); // String x = String(s, l)
95 String& operator=(const char);
96 String& operator=(const char *);
97 String& operator=(const String &);
100 /* Numerical conversion */
102 String(long); // String x(12); x = "12"
103 String(unsigned long); // String x(12); x = "12"
104 String(int); // String x(12); x = "12"
107 return strtol(p->s, 0, 0);
110 operator unsigned long()
112 return strtoul(p->s, 0, 0);
117 return strtol(p->s, 0, 10);
122 return strtol(p->s, 0, 8);
127 return strtol(p->s, 0, 16);
133 return strtod(p->s, 0);
138 char& operator[](int i); // Individual character access
140 int operator~() const // Length of the String
152 operator const char*() const
160 operator bool() // Test for empty String
162 return p->s != 0 && p->s[0] != '\0';
166 * String concatenation.
167 * char and char* are handled by constructors.
170 String& operator+=(const String&);
171 String& operator+=(const char *);
172 friend String operator+(const String&, const String&);
173 friend String operator+(const String&, const char *);
174 friend String operator+(const char *, const String&);
177 * Shifting characters out
178 * "abcdefgh" <<= 3 = "defgh"
179 * "abcdefgh" >>= 3 = "abcde"
182 friend String operator<<(const String &x, int n);
183 String & operator<<=(int n);
185 friend String operator>>(const String &x, int n);
186 String & operator>>=(int n);
189 * Substring selection
191 * A substring can be selected by a start index and a length or
192 * by matching a regular expression.
194 * Selecting a substring by regular expression, combined with
195 * the lvalue semantics of the substring class is a particularly
196 * powerful concept. Possible uses are for example:
198 * (assuming String S, M; regex R;)
199 * M = S(R); -> Returns matching part of S into M.
200 * S(R) = "replacement"; -> replace matching part of S.
201 * S(R) = ""; -> Removes matching part from S.
202 * S(R) == S; -> true if and only if all of S matches R exactly.
205 substring operator()(int start, int len);
206 substring operator()(const regex &r);
212 friend std::ostream& operator<<(std::ostream &, const String &);
213 friend std::istream& operator>>(std::istream &, String &);
216 * String comparison tests
219 friend int operator==(const String& x, const String& y)
221 return strcmp(x.p->s, y.p->s) == 0;
224 friend int operator!=(const String& x, const String& y)
226 return strcmp(x.p->s, y.p->s) != 0;
229 friend int operator<=(const String& x, const String& y)
231 return strcmp(x.p->s, y.p->s) <= 0;
234 friend int operator>=(const String& x, const String& y)
236 return strcmp(x.p->s, y.p->s) >= 0;
239 friend int operator<(const String& x, const String& y)
241 return strcmp(x.p->s, y.p->s) < 0;
244 friend int operator>(const String& x, const String& y)
246 return strcmp(x.p->s, y.p->s) > 0;
249 friend int operator==(const String& x, const char * y)
251 return strcmp(x.p->s, y) == 0;
254 friend int operator!=(const String& x, const char * y)
256 return strcmp(x.p->s, y) != 0;
259 friend int operator<=(const String& x, const char * y)
261 return strcmp(x.p->s, y) <= 0;
264 friend int operator>=(const String& x, const char * y)
266 return strcmp(x.p->s, y) >= 0;
269 friend int operator<(const String& x, const char * y)
271 return strcmp(x.p->s, y) < 0;
274 friend int operator>(const String& x, const char * y)
276 return strcmp(x.p->s, y) > 0;
283 String upper(); // Convert to upper case (ASCII)
284 String lower(); // Convert to lower case (ASCII)
285 String escape(); // Insert backslashes to escape special characters
286 String unescape(); // Remove backslashes from escape codes.
289 * Character searching and Pattern matching
297 // Regular expression pattern matching
299 friend bool operator == (const String &s, const regex &r);
300 friend bool operator == (const regex &r, const String &s);
303 * Other operators to think about...
306 * Numeric conversion with strtod and strtol and vice-versa,
308 * String nr = 123 // nr = "123"
309 * float x = 2.32; String f = x; // f = "2.32"
311 * int i = f; // i = 3
314 * int index(String&, start=0) // Find position of substring
317 * Literature: Bjarne Stroustrup, Section 6.9
318 * DDJ October 1991, pg 24
322 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
323 ** NAME : substring - Determine part of a String object.
324 ** MEMBERS : str : The String object of which this is a substring
325 ** start : index of the first substring character.
326 ** len : Number of characters in the substring
327 ** OPERATORS : = : Assignment of a String or char *
330 ** DESCRIPTION : This implements substring selection from a String object
331 ** with l-value semantics.
333 ** RELATIONS : String
336 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
343 String *str; // Where I am a substring of.
347 String& operator=(const String &);
348 String& operator=(char *s)
355 /* Regular expression matching */
365 regex(const String & reg);
366 regex(const char * reg);
369 friend bool operator == (const String &s, const regex &r);
370 friend bool operator == (const regex &r, const String &s);
373 #endif /* STRING_H */