3 FILE : string.hh -- declare String
5 Rehacked by HWN 3/nov/95
7 introduced Class String_handle
14 #include "arithmetic-operator.hh"
16 #include "string-handle.hh"
18 /* Libg++ also has a String class. Programming errors can lead to
19 confusion about which String is in use. Uncomment the following if you have
20 unexplained crashes after mucking with String
23 // #define String FlowerString
27 Intuitive string class. provides
30 ref counting through #String_handle#
32 conversion from bool, int, double, char* , char.
34 to be moved to String_convert:
35 conversion to int, upcase, downcase
41 indexing (index_i, index_any_i, last_index_i)
44 cutting (left_str, right_str, mid_str)
50 signed comparison (<, >, ==, etc)
53 No operator[] is provided, since this would be enormously slow. If needed,
54 convert to char const* .
63 bool null_terminated ();
67 /** init to empty string. This is needed because other
68 constructors are provided.*/
72 String (char const* source
);
73 String (Byte
const* byte_C
, int length_i
);
75 /// return "new"-ed copy of contents
76 Byte
* copy_byte_p () const;
77 char* copy_ch_p() const;
79 char const* ch_C () const;
80 Byte
const* byte_C () const;
84 String
&operator =(String
const & source
);
87 void operator += (char const* s
) { strh_
+= s
; }
88 void operator += (String s
);
90 bool empty_b () const;
93 void prepend (String
);
96 Return a char. UNSAFE because it may change strlen () result
98 char &operator [](int n
);
99 char operator [](int n
) const;
101 /// return n leftmost chars
102 String
left_str (int n
) const;
104 /// return n rightmost chars
105 String
right_str (int n
) const;
107 /// return uppercase of *this
108 String
upper_str () const;
110 /// return lowercase of *this
111 String
lower_str () const;
113 /// return the "esrever" of *this
114 String
reversed_str () const;
116 /// return a piece starting at index_i (first char = index_i 0), length n
117 String
cut_str (int index_i
, int n
) const;
119 /// cut out a middle piece, return remainder
120 String
nomid_str (int index_i
, int n
) const;
122 /// signed comparison, analogous to memcmp;
123 static int compare_i (String
const & s1
,const String
& s2
);
125 /// index of rightmost c
126 int index_last_i (char c
) const;
128 /// index of rightmost element of string (???)
129 int index_last_i (char const* string
) const;
131 int index_i (char c
) const;
133 /// index of leftmost occurance of STRING
134 int index_i (String
) const;
137 int index_any_i (String
) const;
142 /// provide Stream output
143 void print_on (ostream
& os
) const;
145 /// the length of the string
146 int length_i () const;
148 /// convert to an integer
149 int value_i () const;
151 /// convert to a double
152 double value_f () const;
156 better to clutter global namespace, than suffer *ugh, ugh, ugh*
157 implicit conversions.
159 it might be cool to have no type-checking at all in a language,
160 but once there is, having this silently circumvented is a nightmare.
162 whenever implicit conversions seem necessary (e.g. operator << ()),
163 use Scalar as the generic type iso String.
166 /// for completeness (=handy)
167 inline String
to_str (String s
) { return s
; }
169 String
to_str (char c
, int n
= 1);
170 String
to_str (int i
, char const* format
= 0);
171 String
to_str (double f
, char const* format
= 0);
172 String
to_str (long b
);
173 String
to_str (bool b
);
174 String
to_str (char const* format
, ... );
177 technically incorrect, but lets keep it here: this is a
178 catch all place for this stuff.
181 #include "international.hh"
184 #include "compare.hh"
185 INSTANTIATE_COMPARE(String
const &, String::compare_i
);
187 #ifdef STRING_UTILS_INLINED
189 #define INLINE inline
191 #include "string.icc"
192 /* we should be resetting INLINE. oh well. */
196 // because char const* also has an operator ==, this is for safety:
197 inline bool operator==(String s1
, char const* s2
)
199 return s1
== String (s2
);
201 inline bool operator==(char const* s1
, String s2
)
203 return String (s1
)==s2
;
205 inline bool operator!=(String s1
, char const* s2
) {
206 return s1
!=String (s2
);
208 inline bool operator!=(char const* s1
,String s2
) {
209 return String (s2
) !=s1
;
212 IMPLEMENT_ARITHMETIC_OPERATOR (String
, +);
214 ostream
&operator << (ostream
& os
, String d
);