a5eee1a3471be403066d6b395d589858ed98d62d
[AXE.git] / src / String.h
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 $
9 **
10 **  DESCRIPTION      :  Character String class definition
11 **
12 **  EXPORTED OBJECTS : class String
13 **  LOCAL    OBJECTS : class substring
14 **  MODULES  USED    :
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
21 **      MODIFICATIONS   : 
22 **************************************************************************/
23
24 /*****************************
25    $Log: String.h,v $
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.
28
29    Revision 1.3  2002/11/03 13:18:57  arjen
30    New functions - String::escape() and String::unescape()
31
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
35
36    Revision 1.1  2002/07/25 08:01:26  arjen
37    First checkin, AXE release 0.2
38
39 *****************************/
40
41 // static const char RCSID[] = "$Id: String.h,v 1.4 2007-05-04 13:56:05 arjen Exp $";
42
43 #ifndef STRING_H
44 #define STRING_H
45
46 #include <stdlib.h>
47 #include <iostream>
48 #include <string.h>
49
50 #include <regex.h>
51
52 //  Forward declarations.
53 class substring;
54 class regex;
55
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.
68 **                   ==, !=, <,
69 **                   >, <=, >= : String comparison.
70 **  METHODS        :
71 **
72 **  DESCRIPTION    : The String class counts the references to a String to
73 **                   minimize copying and uses standard C++ character strings
74 **                   as constants.
75 **
76 **  RELATIONS      : substring
77 **  SEE ALSO       :
78 **  LAST MODIFIED  : Aug 27, 1999
79 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
80 */
81
82 class String
83 {
84    friend class substring;
85    friend class regex;
86
87    struct srep
88    {
89       char  *s;    // pointer to data;
90       int    n;    // reference count
91    } *p;
92
93 public:
94
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)
100
101    String& operator=(const char);
102    String& operator=(const char *);
103    String& operator=(const String &);
104    ~String();
105
106    /*  Numerical conversion */
107
108    String(long);             //  String x(12);  x = "12"
109    String(unsigned long);    //  String x(12);  x = "12"
110    String(int);              //  String x(12);  x = "12"
111    operator long()
112    {
113       return strtol(p->s, 0, 0);
114    }
115
116    operator unsigned long()
117    {
118       return strtoul(p->s, 0, 0);
119    }
120
121    long dec(void)
122    {
123       return strtol(p->s, 0, 10);
124    }
125
126    long oct(void)
127    {
128       return strtol(p->s, 0, 8);
129    }
130
131    long hex(void) 
132    {
133       return strtol(p->s, 0, 16);
134    }
135
136    String(double);
137    operator double()
138    {
139       return strtod(p->s, 0);
140    }
141
142    /*     */
143
144    char& operator[](int i);    //  Individual character access
145
146    int operator~() const      //  Length of the String
147    {
148       return strlen(p->s);
149    }
150
151    operator char*()
152    {
153       if (p->s && p->s[0])
154          return p->s;
155       else
156          return 0;
157    }
158    operator const char*() const
159    {
160       if (p->s && p->s[0])
161          return p->s;
162       else
163          return 0;
164    }
165
166    operator bool()   //  Test for empty String
167    {
168       return p->s != 0 && p->s[0] != '\0';
169    }
170
171    /*
172     *   String concatenation.
173     *   char and char* are handled by constructors.
174     */
175
176    String& operator+=(const String&);
177    String& operator+=(const char *);
178    friend String operator+(const String&, const String&);
179    friend String operator+(const String&, const char *);
180    friend String operator+(const char *,  const String&);
181
182    /*
183     *  Shifting characters out
184     *            "abcdefgh" <<= 3  = "defgh"
185     *            "abcdefgh" >>= 3  = "abcde"
186     */
187
188    friend String operator<<(const String &x, int n);
189    String & operator<<=(int n);
190
191    friend String operator>>(const String &x, int n);
192    String & operator>>=(int n);
193
194    /*
195     *   Substring selection
196     *
197     *   A substring can be selected by a start index and a length or
198     *   by matching a regular expression.
199     *
200     *   Selecting a substring by regular expression, combined with
201     *   the lvalue semantics of the substring class is a particularly
202     *   powerful concept. Possible uses are for example:
203     *
204     *   (assuming String S, M; regex R;)
205     *   M = S(R);             ->  Returns matching part of S into M.
206     *   S(R) = "replacement"; -> replace matching part of S.
207     *   S(R) = "";            -> Removes matching part from S.
208     *   S(R) == S;            -> true if and only if all of S matches R exactly.
209     */
210
211    substring operator()(int start, int len);
212    substring operator()(const regex &r);
213
214    /*
215     *   Input and output
216     */
217
218    friend std::ostream& operator<<(std::ostream &, const String &);
219    friend std::istream& operator>>(std::istream &, String &);
220
221    /*
222     *   String comparison tests
223     */
224
225    friend int operator==(const String& x, const String& y)
226    {
227       return strcmp(x.p->s, y.p->s) == 0;
228    }
229
230    friend int operator!=(const String& x, const String& y)
231    {
232       return strcmp(x.p->s, y.p->s) != 0;
233    }
234
235    friend int operator<=(const String& x, const String& y)
236    {
237       return strcmp(x.p->s, y.p->s) <= 0;
238    }
239
240    friend int operator>=(const String& x, const String& y)
241    {
242       return strcmp(x.p->s, y.p->s) >= 0;
243    }
244
245    friend int operator<(const String& x, const String& y)
246    {
247       return strcmp(x.p->s, y.p->s) < 0;
248    }
249
250    friend int operator>(const String& x, const String& y)
251    {
252       return strcmp(x.p->s, y.p->s) > 0;
253    }
254
255    friend int operator==(const String& x, const char * y)
256    {
257       return strcmp(x.p->s, y) == 0;
258    }
259
260    friend int operator!=(const String& x, const char * y)
261    {
262       return strcmp(x.p->s, y) != 0;
263    }
264
265    friend int operator<=(const String& x, const char * y)
266    {
267       return strcmp(x.p->s, y) <= 0;
268    }
269
270    friend int operator>=(const String& x, const char * y)
271    {
272       return strcmp(x.p->s, y) >= 0;
273    }
274
275    friend int operator<(const String& x, const char * y)
276    {
277       return strcmp(x.p->s, y) < 0;
278    }
279
280    friend int operator>(const String& x, const char * y)
281    {
282       return strcmp(x.p->s, y) > 0;
283    }
284
285    /*
286     *  Modifiers
287     */
288
289    String upper();   //  Convert to upper case (ASCII)
290    String lower();   //  Convert to lower case (ASCII)
291    String escape();   //  Insert backslashes to escape special characters
292    String unescape(); //  Remove backslashes from escape codes.
293
294    /*
295     *  Character searching and Pattern matching
296     */
297
298    int index(char c);
299    int rindex(char c);
300
301    int in(String & x);
302
303    //  Regular expression pattern matching
304  
305    friend bool operator == (const String &s, const regex &r);
306    friend bool operator == (const regex &r, const String &s);
307 };
308    /*
309     *   Other operators to think about...
310     *
311     *
312     *   Numeric conversion with strtod and strtol and vice-versa,
313     *   so we can do:
314     *                 String nr = 123  //  nr = "123"
315     *                 float x = 2.32; String f = x;  // f = "2.32"
316     *                             f = "3.145";
317     *                         int i = f;           // i = 3
318     *
319     *   String matching:
320     *       int index(String&, start=0)  // Find position of substring
321     *
322     *
323     *  Literature:  Bjarne Stroustrup, Section 6.9
324     *               DDJ October 1991, pg 24
325     */
326
327
328 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
329 **  NAME           : substring - Determine part of a String object.
330 **  MEMBERS        : str   : The String object of which this is a substring
331 **                   start : index of the first substring character.
332 **                   len   : Number of characters in the substring
333 **  OPERATORS      : =     : Assignment of a String or char *
334 **  METHODS        :
335 **
336 **  DESCRIPTION    : This implements substring selection from a String object
337 **                   with l-value semantics.
338 **
339 **  RELATIONS      : String
340 **  SEE ALSO       :
341 **  LAST MODIFIED  :
342 **+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
343 */
344
345 class substring
346 {
347    friend class String;
348
349    String     *str;   //  Where I am a substring of.
350    int  start, len;
351
352 public:
353    String& operator=(const String &); 
354    String& operator=(char *s)
355    {
356       *this = String(s);
357       return *str;
358    }
359 };
360
361 /*  Regular expression matching */
362
363 class regex
364 {
365    friend class String;
366
367    regex_t    expression;
368    String     original;
369
370 public:
371
372    regex(const String & reg);
373    regex(const char * reg);
374    regex(const regex & reg);
375    ~regex();
376
377    friend bool operator == (const String &s, const regex &r);
378    friend bool operator == (const regex &r, const String &s);
379 };
380
381 #endif  /* STRING_H */