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 $";
54 // 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)
103 String(const std::string &); // String x = std::string("abc")
105 String& operator=(const char);
106 String& operator=(const char *);
107 String& operator=(const String &);
108 String& operator=(const std::string &);
111 /* Numerical conversion */
113 String(int); // String x(12); x = "12"
114 String(long); // String x(12); x = "12"
115 String(unsigned long); // String x(12); x = "12"
119 return strtol(p->s, 0, 0);
124 return strtol(p->s, 0, 0);
127 operator unsigned long()
129 return strtoul(p->s, 0, 0);
134 return strtol(p->s, 0, 10);
139 return strtol(p->s, 0, 8);
144 return strtol(p->s, 0, 16);
150 return strtod(p->s, 0);
154 char& operator[](size_t i); // Individual character access
156 int operator~() const // Length of the String
158 return p->s ? strlen(p->s) : 0;
161 int size() const // Length of the String
163 return p->s ? strlen(p->s) : 0;
173 operator const char*() const
181 std::string std_string()
184 return std::string(p->s);
186 return std::string("");
189 operator bool() // Test for empty String
191 return p->s != 0 && p->s[0] != '\0';
195 * String concatenation.
196 * char and char* are handled by constructors.
199 String& operator+=(const String&);
200 String& operator+=(const char *);
201 friend String operator+(const String&, const String&);
202 friend String operator+(const String&, const char *);
203 friend String operator+(const char *, const String&);
206 * Shifting characters out
207 * "abcdefgh" <<= 3 = "defgh"
208 * "abcdefgh" >>= 3 = "abcde"
211 friend String operator<<(const String &x, int n);
212 String & operator<<=(int n);
214 friend String operator>>(const String &x, int n);
215 String & operator>>=(int n);
218 * Substring selection
220 * A substring can be selected by a start index and a length or
221 * by matching a regular expression.
223 * Selecting a substring by regular expression, combined with
224 * the lvalue semantics of the substring class is a particularly
225 * powerful concept. Possible uses are for example:
227 * (assuming String S, M; regex R;)
228 * M = S(R); -> Returns matching part of S into M.
229 * S(R) = "replacement"; -> replace matching part of S.
230 * S(R) = ""; -> Removes matching part from S.
231 * S(R) == S; -> true if and only if all of S matches R exactly.
234 substring operator()(int start, int len);
235 substring operator()(const regex &r);
241 friend std::ostream& operator<<(std::ostream &, const String &);
242 // Read one line and remove the trailing line feed.
243 friend std::istream& operator>>(std::istream &, String &);
246 * String comparison tests
249 friend int operator==(const String& x, const String& y)
251 return strcmp(x.p->s, y.p->s) == 0;
254 friend int operator!=(const String& x, const String& y)
256 return strcmp(x.p->s, y.p->s) != 0;
259 friend int operator<=(const String& x, const String& y)
261 return strcmp(x.p->s, y.p->s) <= 0;
264 friend int operator>=(const String& x, const String& y)
266 return strcmp(x.p->s, y.p->s) >= 0;
269 friend int operator<(const String& x, const String& y)
271 return strcmp(x.p->s, y.p->s) < 0;
274 friend int operator>(const String& x, const String& y)
276 return strcmp(x.p->s, y.p->s) > 0;
279 friend int operator==(const String& x, const char * y)
281 return strcmp(x.p->s, y) == 0;
284 friend int operator!=(const String& x, const char * y)
286 return strcmp(x.p->s, y) != 0;
289 friend int operator<=(const String& x, const char * y)
291 return strcmp(x.p->s, y) <= 0;
294 friend int operator>=(const String& x, const char * y)
296 return strcmp(x.p->s, y) >= 0;
299 friend int operator<(const String& x, const char * y)
301 return strcmp(x.p->s, y) < 0;
304 friend int operator>(const String& x, const char * y)
306 return strcmp(x.p->s, y) > 0;
313 String upper(); // Convert to upper case (ASCII)
314 String lower(); // Convert to lower case (ASCII)
315 String escape(); // Insert backslashes to escape special characters
316 String unescape(); // Remove backslashes from escape codes.
319 * Character and String searching
325 int index(const char * x);
326 int index(String & x)
328 return index(x.p->s);
331 int rindex(const char * x);
332 int rindex(String & x)
334 return rindex(x.p->s);
342 // Regular expression pattern matching
344 friend bool operator == (const String &s, const regex &r);
345 friend bool operator == (const regex &r, const String &s);
346 friend bool operator != (const String &s, const regex &r);
347 friend bool operator != (const regex &r, const String &s);
349 SuperString split(const String &separator);
350 SuperString tokenize(const String &delimiters);
354 * Other operators to think about...
357 * Numeric conversion with strtod and strtol and vice-versa,
359 * String nr = 123 // nr = "123"
360 * float x = 2.32; String f = x; // f = "2.32"
362 * int i = f; // i = 3
365 * int index(String&, start=0) // Find position of substring
368 * Literature: Bjarne Stroustrup, Section 6.9
369 * DDJ October 1991, pg 24
373 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
374 ** NAME : substring - Determine part of a String object.
375 ** MEMBERS : str : The String object of which this is a substring
376 ** start : index of the first substring character.
377 ** len : Number of characters in the substring
378 ** OPERATORS : = : Assignment of a String or char *
381 ** DESCRIPTION : This implements substring selection from a String object
382 ** with l-value semantics.
384 ** RELATIONS : String
387 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
394 String *str; // Where I am a substring of.
398 String& operator=(const String &);
399 String& operator=(const char *s)
406 /* Regular expression matching */
417 regex(const String & reg);
418 regex(const char * reg);
419 regex(const regex & reg);
422 regex& operator=(const regex &);
424 friend bool operator == (const String &s, const regex &r);
425 friend bool operator == (const regex &r, const String &s);
426 friend bool operator != (const String &s, const regex &r);
427 friend bool operator != (const regex &r, const String &s);
432 std::vector<String> _ss;
438 SuperString(const String &s)
440 std::vector<String> __ss(1, s);
444 SuperString(const int n)
446 std::vector<String> __ss(n);
450 operator bool() // Test for empty SuperString
452 return _ss.size() != 0;
455 int operator~() const // Length of the SuperString
460 String& operator[](size_t i) // Individual String access
465 // Inserting and erasing Strings
467 void insert(int position, const String &s)
469 _ss.insert(_ss.begin() + position, s);
472 void erase(int position)
474 _ss.erase(_ss.begin() + position);
477 // Adding Strings and SuperStrings
479 friend SuperString operator+(const SuperString&, const String&);
480 friend SuperString operator+(const String&, const SuperString&);
481 friend SuperString operator+(const SuperString&, const SuperString&);
482 SuperString& operator+=(const String&);
485 * SuperString comparison tests
488 friend int operator==(const SuperString& x, const SuperString& y)
490 return x._ss == y._ss;
493 friend int operator!=(const SuperString& x, const SuperString& y)
495 return x._ss != y._ss;
498 friend int operator<=(const SuperString& x, const SuperString& y)
500 return x._ss <= y._ss;
503 friend int operator>=(const SuperString& x, const SuperString& y)
505 return x._ss >= y._ss;
508 friend int operator<(const SuperString& x, const SuperString& y)
510 return x._ss < y._ss;
513 friend int operator>(const SuperString& x, const SuperString& y)
515 return x._ss > y._ss;
518 String join(const String &separator);
526 class StringException
532 StringException(String m)
543 #define ERROR_MSG_SIZE 200
551 RegexException(int errcode, regex_t *preg)
553 char errbuf[ERROR_MSG_SIZE];
555 regerror(errcode, preg, errbuf, ERROR_MSG_SIZE);
565 #endif /* STRING_H */