3 FILE : string.hh -- declare String
5 Rehacked by HWN 3/nov/95
7 introduced Class String_handle
16 #include "arithmetic-operator.hh"
18 #include "string-handle.hh"
20 /* Libg++ also has a String class. Programming errors can lead to
21 confusion about which String is in use. Uncomment the following if you have
22 unexplained crashes after mucking with String
25 // #define String FlowerString
29 Intuitive string class. provides
32 ref counting through #String_handle#
34 conversion from bool, int, double, char* , char.
36 to be moved to String_convert:
37 conversion to int, upcase, downcase
43 indexing (index_i, index_any_i, last_index_i)
46 cutting (left_str, right_str, mid_str)
52 signed comparison (<, >, ==, etc)
55 No operator[] is provided, since this would be enormously slow. If needed,
56 convert to char const* .
65 bool null_terminated ();
69 /** init to empty string. This is needed because other
70 constructors are provided.*/
74 String (char const* source);
75 String (Byte const* byte_C, int length_i);
77 /// return "new"-ed copy of contents
78 Byte* copy_byte_p () const;
79 char* copy_ch_p() const;
81 char const* ch_C () const;
82 Byte const* byte_C () const;
86 String &operator =(String const & source);
89 void operator += (char const* s) { strh_ += s; }
90 void operator += (String s);
92 bool empty_b () const;
94 /** is the string empty?
96 Ugh-ugh-thank-you-cygnus. W32 barfs on this
98 operator bool () const;
103 void append (String);
104 void prepend (String);
107 Return a char. UNSAFE because it may change strlen () result
109 char &operator [](int n);
110 char operator [](int n) const;
112 /// return n leftmost chars
113 String left_str (int n) const;
115 /// return n rightmost chars
116 String right_str (int n) const;
118 /// return uppercase of *this
119 String upper_str () const;
121 /// return lowercase of *this
122 String lower_str () const;
124 /// return the "esrever" of *this
125 String reversed_str () const;
127 /// return a piece starting at index_i (first char = index_i 0), length n
128 String cut_str (int index_i, int n) const;
130 /// cut out a middle piece, return remainder
131 String nomid_str (int index_i, int n) const;
133 /// signed comparison, analogous to memcmp;
134 static int compare_i (String const & s1,const String& s2);
136 /// index of rightmost c
137 int index_last_i (char c) const;
139 /// index of rightmost element of string (???)
140 int index_last_i (char const* string) const;
142 /// index of leftmost #c#
143 int index_i (char c) const;
145 /// index of leftmost occurance of STRING
146 int index_i (String) const;
149 int index_any_i (String) const;
154 /// provide Stream output
155 void print_on (ostream& os) const;
157 /// the length of the string
158 int length_i () const;
160 /// convert to an integer
161 int value_i () const;
163 /// convert to a double
164 double value_f () const;
168 better to clutter global namespace, than suffer *ugh, ugh, ugh*
169 implicit conversions.
171 it might be cool to have no type-checking at all in a language,
172 but once there is, having this silently circumvented is a nightmare.
174 whenever implicit conversions seem necessary (e.g. operator << ()),
175 use Scalar as the generic type iso String.
178 /// for completeness (=handy)
179 inline String to_str (String s) { return s; }
181 String to_str (char c, int n = 1);
182 String to_str (int i, char const* format = 0);
183 String to_str (double f , char const* format = 0);
184 String to_str (bool b);
185 String to_str (char const* format, ... );
188 technically incorrect, but lets keep it here: this is a
189 catch all place for this stuff.
192 #include "international.hh"
195 #include "compare.hh"
196 INSTANTIATE_COMPARE(String const &, String::compare_i);
198 #ifdef STRING_UTILS_INLINED
200 #define INLINE inline
202 #include "string.icc"
203 /* we should be resetting INLINE. oh well. */
207 // because char const* also has an operator ==, this is for safety:
208 inline bool operator==(String s1, char const* s2)
210 return s1 == String (s2);
212 inline bool operator==(char const* s1, String s2)
214 return String (s1)==s2;
216 inline bool operator!=(String s1, char const* s2 ) {
217 return s1!=String (s2);
219 inline bool operator!=(char const* s1,String s2) {
220 return String (s2) !=s1;
223 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
226 operator << (ostream& os, String d)