3 FILE : string.hh -- implement String inline helper classes,
4 and declare stringclass.
7 Rehacked by HWN 3/nov/95
9 introduced Class String_handle
20 #include "stringutil.hh"
22 /** the smart string class.
24 Intuitive string class. provides
26 ref counting through #String_handle#
28 conversion from bool, int, double, char *, char.
30 conversion to int, upcase, downcase
35 indexing (pos, posAny, lastPos)
37 cutting (left, right, mid)
41 signed comparison (<, >, ==, etc)
43 No operator[] is provided, since this would be enormously slow. If needed,
44 convert to const char *.
49 String_handle data
; // should derive String from String_handle?
52 /** init to "". needed because other constructors are provided.*/
56 String( const char* source
);
59 String( char c
, int n
= 1 );
67 /// String s( 3.14, 6, '#' );
68 String ( double f
, const char *fmt
=0);
69 String( int i
, int n
, char c
= ' ' );
71 /// return a "new"-ed copy of contents
72 char *copy_array() const; // return a "new"-ed copy of contents
74 const char *cptr() const;
75 const char *ptr() { return ((const String
*)this)->cptr(); }
77 /// return the data. Don't use for writing the data.
78 operator const char *() const { return cptr(); }
80 String
operator =( const String
& source
) { data
= source
.data
; return *this; }
83 void operator += (const char *s
) { data
+= s
; }
84 void operator += (String s
);
86 char operator []( int n
) const { return data
[n
]; }
88 /// return n leftmost chars
89 String
left( int n
) const;
91 /// return n rightmost chars
92 String
right( int n
) const;
94 /// convert this to upcase
97 /// convert this to downcase
98 String
lower(); // & ??
100 /// return the "esrever" of *this
101 String
reversed() const;
104 /// return a piece starting at pos (first char = pos 1), length n
105 String
mid(int pos
, int n
) const;
107 /// cut out a middle piece, return remainder
108 String
nomid(int pos
, int n
) const;
110 /// signed comparison, analogous to strcmp;
111 static int compare(const String
& s1
,const String
& s2
);
113 /// index of rightmost c
114 int lastPos( char c
) const;
116 /// index of rightmost element of string
117 int lastPos( const char* string
) const;
119 /** index of leftmost c.
121 0 if not found, else index + 1
123 int pos(char c
) const;
124 int pos(const char* string
) const;
125 int posAny(const char* string
) const;
128 /// provide Stream output
129 void printOn(ostream
& os
) const;
131 /// convert to an integer
134 /// convert to a double
135 double fvalue() const;
137 /// the length of the string
142 #include "compare.hh"
144 instantiate_compare(const String
&, String::compare
);
146 // because const char* also has an operator ==, this is for safety:
147 inline bool operator==(String s1
, const char *s2
){
148 return s1
== String(s2
);
150 inline bool operator==(const char *s1
, String s2
)
152 return String(s1
)==s2
;
154 inline bool operator!=(String s1
, const char *s2
) {
155 return s1
!=String(s2
);
157 inline bool operator!=(const char *s1
,String s2
) {
158 return String(s2
) !=s1
;
163 operator + (String s1
, String s2
)
170 operator << ( ostream
& os
, String d
)
177 String
quoteString(String message
, String quote
);