3 FILE : string.hh -- implement String inline helpclasses,
4 and declare stringclass.
7 Rehacked by HWN 3/nov/95
9 introduced Class String_handle
19 //#include "globals.hh"
20 #include "stringutil.hh"
24 #include "sortable.hh"
25 #define String__mpp String : public Sortable
27 #define String__mpp String
30 /// the smart string class.
34 String_handle data
; // should derive String from String_handle?
38 #ifdef CENTRAL_OBJECT // everything derived from Sortable object
39 virtual int operator ==( const Sortable
& test
) const;
40 virtual int operator &&( const Object
& test
) const;
41 virtual int operator >( const Sortable
& test
) const;
46 /** needed because other constructors are provided.*/
49 String( const char* source
);
52 String( char c
, int n
= 1 );
60 /// String s( 3.14, 6, '#' );
61 String ( double f
, const char *fmt
=0);
62 String( int i
, int n
, char c
= ' ' );
64 /// return a "new"-ed copy of contents
65 char *copy_array() const; // return a "new"-ed copy of contents
67 /// return the data. Don't use for writing the data.
68 virtual operator const char *() const; // virtual???
70 String
operator =( const String
& source
) { data
= source
.data
; return *this; }
73 void operator += (const char *s
) { data
+= s
; }
74 void operator += (String s
);
76 char operator []( int n
) const { return data
[n
]; }
78 /// return n leftmost chars
79 String
left( int n
) const;
81 /// return n rightmost chars
82 String
right( int n
) const;
84 /// convert this to upcase
87 /// convert this to downcase
88 String
lower(); // & ??
90 /// return the "esrever" of *this
91 String
reversed() const;
94 /// return a piece starting at pos (first char = pos 1), length n
95 String
mid(int pos
, int n
) const;
97 /// cut out a middle piece, return remainder
98 String
nomid(int pos
, int n
) const;
100 /// signed comparison, analogous to strcmp;
101 int compare( const char* s
) const;
103 /// index of rightmost c
104 int lastPos( char c
) const;
105 /// index of rightmost element of string
106 int lastPos( const char* string
) const;
108 /// index of leftmost c
109 int pos(char c
) const;
112 0 if not found, else index + 1
114 int pos(const char* string
) const;
115 int posAny(const char* string
) const;
118 /// provide Stream output
119 void printOn(ostream
& os
) const;
121 /// convert to an integer
124 /// convert to a double
125 double fvalue() const;
127 /// the length of the string
132 Intuitive string class. provides
134 ref counting thru #String_handle#
136 conversion from bool, int, double, char *, char.
138 conversion to int, upcase, downcase
143 indexing (pos, posAny, lastPos)
145 cutting (left, right, mid)
149 signed comparison (<, >, ==, etc)
151 No operator[] is provided, since this would be enormously slow. If needed,
152 convert to const char *.
156 // because const char* also has an operator ==, this is for safety:
157 inline bool operator==(String s1
, String s2
){ return !(s1
.compare(s2
));}
158 inline bool operator==(String s1
, const char *s2
){ return !(s1
.compare(s2
));}
159 inline bool operator==(const char *s1
, String s2
){ return !(s2
.compare(s1
));}
160 inline bool operator!=(String s1
, const char *s2
) { return s1
.compare(s2
);}
161 inline bool operator!=(const char *s1
,String s2
) { return s2
.compare(s1
);}
162 inline bool operator!=(String s1
, String s2
) { return s1
.compare(s2
);}
165 operator + (String s1
, String s2
)
172 operator << ( ostream
& os
, String d
)
179 String
quoteString(String message
, String quote
);