3 FILE : string.hh -- declare String
5 Rehacked by HWN 3/nov/95
7 introduced Class String_handle
15 #include <iostream.h> /* gcc 3.0 */
20 #include "arithmetic-operator.hh"
21 #include "flower-proto.hh"
22 #include "string-handle.hh"
26 Intuitive string class. provides
29 ref counting through #String_handle#
31 conversion from bool, int, double, char* , char.
33 to be moved to String_convert:
34 conversion to int, upcase, downcase
40 indexing (index_i, index_any_i, last_index_i)
43 cutting (left_str, right_str, mid_str)
49 signed comparison (<, >, ==, etc)
52 No operator[] is provided, since this would be enormously slow. If needed,
53 convert to char const* .
62 bool null_terminated ();
66 /** init to empty string. This is needed because other
67 constructors are provided.*/
71 String (char const* source);
72 String (Byte const* byte_C, int length_i);
74 /// return "new"-ed copy of contents
75 Byte* copy_byte_p () const;
76 char* copy_ch_p () const;
78 char const* ch_C () const;
79 Byte const* byte_C () const;
83 String &operator = (String const & source);
86 void operator += (char const* s) { strh_ += s; }
87 void operator += (String s);
89 bool empty_b () const;
92 void prepend (String);
95 Return a char. UNSAFE because it may change strlen () result
97 char &operator [] (int n);
98 char operator [] (int n) const;
100 /// return n leftmost chars
101 String left_str (int n) const;
103 /// return n rightmost chars
104 String right_str (int n) const;
106 /// return uppercase of *this
107 String upper_str () const;
109 /// return lowercase of *this
110 String lower_str () const;
112 /// return the "esrever" of *this
113 String reversed_str () const;
115 /// return a piece starting at index_i (first char = index_i 0), length n
116 String cut_str (int index_i, int n) const;
118 /// cut out a middle piece, return remainder
119 String nomid_str (int index_i, int n) const;
121 /// signed comparison, analogous to memcmp;
122 static int compare_i (String const & s1,const String& s2);
124 /// index of rightmost c
125 int index_last_i (char c) const;
127 /// index of rightmost element of string (???)
128 int index_last_i (char const* string) const;
130 int index_i (char c) const;
132 /// index of leftmost occurance of STRING
133 int index_i (String) const;
136 int index_any_i (String) const;
141 /// provide Stream output
142 void print_on (ostream& os) const;
144 /// the length of the string
145 int length_i () const;
147 /// convert to an integer
148 int value_i () const;
150 /// convert to a double
151 double value_f () const;
155 better to clutter global namespace, than suffer *ugh, ugh, ugh*
156 implicit conversions.
158 it might be cool to have no type-checking at all in a language,
159 but once there is, having this silently circumvented is a nightmare.
161 whenever implicit conversions seem necessary (e.g. operator << ()),
162 use Scalar as the generic type iso String.
165 /// for completeness (=handy)
166 inline String to_str (String s) { return s; }
168 String to_str (char c, int n = 1);
169 String to_str (int i, char const* format = 0);
170 String to_str (double f , char const* format = 0);
171 String to_str (long b);
172 String to_str (bool b);
173 String to_str (char const* format, ... );
176 technically incorrect, but lets keep it here: this is a
177 catch all place for this stuff.
180 #include "international.hh"
183 #include "compare.hh"
184 INSTANTIATE_COMPARE (String const &, String::compare_i);
186 #ifdef STRING_UTILS_INLINED
188 #define INLINE inline
190 #include "string.icc"
191 /* we should be resetting INLINE. oh well. */
195 // because char const* also has an operator ==, this is for safety:
196 inline bool operator== (String s1, char const* s2)
198 return s1 == String (s2);
200 inline bool operator== (char const* s1, String s2)
202 return String (s1)==s2;
204 inline bool operator!= (String s1, char const* s2 ) {
205 return s1!=String (s2);
207 inline bool operator!= (char const* s1,String s2) {
208 return String (s2) !=s1;
211 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
213 ostream &operator << (ostream& os, String d);