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 $";
53 // Forward declarations.
58 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
59 ** NAME : String - Character String class.
60 ** MEMBERS : p -> s : Pointer to the actual String data.
61 ** p -> n : Number of references to this String.
62 ** OPERATORS : = : Assign a String, char *, char to a String.
63 ** +, += : concatenate Strings
64 ** ~ : Length of a String
65 ** [] : Individual character access.
66 ** ! : Empty String test.
67 ** () : Substring selection.
68 ** ostream << : String to output stream.
69 ** istream << : String from input stream.
71 ** >, <=, >= : String comparison.
74 ** DESCRIPTION : The String class counts the references to a String to
75 ** minimize copying and uses standard C++ character strings
78 ** RELATIONS : substring
80 ** LAST MODIFIED : Aug 27, 1999
81 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
86 friend class substring;
91 char *s; // pointer to data;
92 int n; // reference count
97 String(const char *); // String x = "abc"
98 String(char); // String x = 'a'
99 String(); // String x;
100 String(const String &); // String x = String ...
101 String(const substring &); // String x = String(s, l)
103 String& operator=(const char);
104 String& operator=(const char *);
105 String& operator=(const String &);
108 /* Numerical conversion */
110 String(int); // String x(12); x = "12"
111 String(long); // String x(12); x = "12"
112 String(unsigned long); // String x(12); x = "12"
116 return strtol(p->s, 0, 0);
121 return strtol(p->s, 0, 0);
124 operator unsigned long()
126 return strtoul(p->s, 0, 0);
131 return strtol(p->s, 0, 10);
136 return strtol(p->s, 0, 8);
141 return strtol(p->s, 0, 16);
147 return strtod(p->s, 0);
151 char& operator[](size_t i); // Individual character access
153 int operator~() const // Length of the String
155 return p->s ? strlen(p->s) : 0;
165 operator const char*() const
173 operator bool() // Test for empty String
175 return p->s != 0 && p->s[0] != '\0';
179 * String concatenation.
180 * char and char* are handled by constructors.
183 String& operator+=(const String&);
184 String& operator+=(const char *);
185 friend String operator+(const String&, const String&);
186 friend String operator+(const String&, const char *);
187 friend String operator+(const char *, const String&);
190 * Shifting characters out
191 * "abcdefgh" <<= 3 = "defgh"
192 * "abcdefgh" >>= 3 = "abcde"
195 friend String operator<<(const String &x, int n);
196 String & operator<<=(int n);
198 friend String operator>>(const String &x, int n);
199 String & operator>>=(int n);
202 * Substring selection
204 * A substring can be selected by a start index and a length or
205 * by matching a regular expression.
207 * Selecting a substring by regular expression, combined with
208 * the lvalue semantics of the substring class is a particularly
209 * powerful concept. Possible uses are for example:
211 * (assuming String S, M; regex R;)
212 * M = S(R); -> Returns matching part of S into M.
213 * S(R) = "replacement"; -> replace matching part of S.
214 * S(R) = ""; -> Removes matching part from S.
215 * S(R) == S; -> true if and only if all of S matches R exactly.
218 substring operator()(int start, int len);
219 substring operator()(const regex &r);
225 friend std::ostream& operator<<(std::ostream &, const String &);
226 // Read one line and remove the trailing line feed.
227 friend std::istream& operator>>(std::istream &, String &);
230 * String comparison tests
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 String& y)
260 return strcmp(x.p->s, y.p->s) > 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;
288 friend int operator>(const String& x, const char * y)
290 return strcmp(x.p->s, y) > 0;
297 String upper(); // Convert to upper case (ASCII)
298 String lower(); // Convert to lower case (ASCII)
299 String escape(); // Insert backslashes to escape special characters
300 String unescape(); // Remove backslashes from escape codes.
303 * Character searching and Pattern matching
311 // Regular expression pattern matching
313 friend bool operator == (const String &s, const regex &r);
314 friend bool operator == (const regex &r, const String &s);
315 friend bool operator != (const String &s, const regex &r);
316 friend bool operator != (const regex &r, const String &s);
318 SuperString split(const String &separator);
322 * Other operators to think about...
325 * Numeric conversion with strtod and strtol and vice-versa,
327 * String nr = 123 // nr = "123"
328 * float x = 2.32; String f = x; // f = "2.32"
330 * int i = f; // i = 3
333 * int index(String&, start=0) // Find position of substring
336 * Literature: Bjarne Stroustrup, Section 6.9
337 * DDJ October 1991, pg 24
341 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
342 ** NAME : substring - Determine part of a String object.
343 ** MEMBERS : str : The String object of which this is a substring
344 ** start : index of the first substring character.
345 ** len : Number of characters in the substring
346 ** OPERATORS : = : Assignment of a String or char *
349 ** DESCRIPTION : This implements substring selection from a String object
350 ** with l-value semantics.
352 ** RELATIONS : String
355 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
362 String *str; // Where I am a substring of.
366 String& operator=(const String &);
367 String& operator=(const char *s)
374 /* Regular expression matching */
385 regex(const String & reg);
386 regex(const char * reg);
387 regex(const regex & reg);
390 regex& operator=(const regex &);
392 friend bool operator == (const String &s, const regex &r);
393 friend bool operator == (const regex &r, const String &s);
394 friend bool operator != (const String &s, const regex &r);
395 friend bool operator != (const regex &r, const String &s);
400 std::vector<String> _ss;
406 SuperString(const String &s)
408 std::vector<String> __ss(1, s);
412 SuperString(const int n)
414 std::vector<String> __ss(n);
418 operator bool() // Test for empty SuperString
420 return _ss.size() != 0;
423 int operator~() const // Length of the SuperString
428 String& operator[](size_t i) // Individual String access
433 // Adding Strings and SuperStrings
435 friend SuperString operator+(const SuperString&, const String&);
436 friend SuperString operator+(const String&, const SuperString&);
437 friend SuperString operator+(const SuperString&, const SuperString&);
438 SuperString& operator+=(const String&);
441 * SuperString comparison tests
444 friend int operator==(const SuperString& x, const SuperString& y)
446 return x._ss == y._ss;
449 friend int operator!=(const SuperString& x, const SuperString& y)
451 return x._ss != y._ss;
454 friend int operator<=(const SuperString& x, const SuperString& y)
456 return x._ss <= y._ss;
459 friend int operator>=(const SuperString& x, const SuperString& y)
461 return x._ss >= y._ss;
464 friend int operator<(const SuperString& x, const SuperString& y)
466 return x._ss < y._ss;
469 friend int operator>(const SuperString& x, const SuperString& y)
471 return x._ss > y._ss;
474 String join(const String &separator);
482 class StringException
488 StringException(String m)
499 #define ERROR_MSG_SIZE 200
507 RegexException(int errcode, regex_t *preg)
509 char errbuf[ERROR_MSG_SIZE];
511 regerror(errcode, preg, errbuf, ERROR_MSG_SIZE);
521 #endif /* STRING_H */