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;
95 void prepend (String);
98 Return a char. UNSAFE because it may change strlen () result
100 char &operator [](int n);
101 char operator [](int n) const;
103 /// return n leftmost chars
104 String left_str (int n) const;
106 /// return n rightmost chars
107 String right_str (int n) const;
109 /// return uppercase of *this
110 String upper_str () const;
112 /// return lowercase of *this
113 String lower_str () const;
115 /// return the "esrever" of *this
116 String reversed_str () const;
118 /// return a piece starting at index_i (first char = index_i 0), length n
119 String cut_str (int index_i, int n) const;
121 /// cut out a middle piece, return remainder
122 String nomid_str (int index_i, int n) const;
124 /// signed comparison, analogous to memcmp;
125 static int compare_i (String const & s1,const String& s2);
127 /// index of rightmost c
128 int index_last_i (char c) const;
130 /// index of rightmost element of string (???)
131 int index_last_i (char const* string) const;
133 int index_i (char c) const;
135 /// index of leftmost occurance of STRING
136 int index_i (String) const;
139 int index_any_i (String) const;
144 /// provide Stream output
145 void print_on (ostream& os) const;
147 /// the length of the string
148 int length_i () const;
150 /// convert to an integer
151 int value_i () const;
153 /// convert to a double
154 double value_f () const;
158 better to clutter global namespace, than suffer *ugh, ugh, ugh*
159 implicit conversions.
161 it might be cool to have no type-checking at all in a language,
162 but once there is, having this silently circumvented is a nightmare.
164 whenever implicit conversions seem necessary (e.g. operator << ()),
165 use Scalar as the generic type iso String.
168 /// for completeness (=handy)
169 inline String to_str (String s) { return s; }
171 String to_str (char c, int n = 1);
172 String to_str (int i, char const* format = 0);
173 String to_str (double f , char const* format = 0);
174 String to_str (long b);
175 String to_str (bool b);
176 String to_str (char const* format, ... );
179 technically incorrect, but lets keep it here: this is a
180 catch all place for this stuff.
183 #include "international.hh"
186 #include "compare.hh"
187 INSTANTIATE_COMPARE(String const &, String::compare_i);
189 #ifdef STRING_UTILS_INLINED
191 #define INLINE inline
193 #include "string.icc"
194 /* we should be resetting INLINE. oh well. */
198 // because char const* also has an operator ==, this is for safety:
199 inline bool operator==(String s1, char const* s2)
201 return s1 == String (s2);
203 inline bool operator==(char const* s1, String s2)
205 return String (s1)==s2;
207 inline bool operator!=(String s1, char const* s2 ) {
208 return s1!=String (s2);
210 inline bool operator!=(char const* s1,String s2) {
211 return String (s2) !=s1;
214 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
217 operator << (ostream& os, String d)