3 FILE : string.hh -- declare String
5 Rehacked by HWN 3/nov/95
7 introduced Class String_handle
14 // too darn slow with gcc3
23 #include "arithmetic-operator.hh"
24 #include "string-handle.hh"
28 Intuitive string class. provides
31 ref counting through #String_handle#
33 conversion from bool, int, double, char* , char.
35 to be moved to String_convert:
36 conversion to int, upcase, downcase
42 indexing (index_i, index_any_i, last_index_i)
45 cutting (left_string, right_string, mid_string)
51 signed comparison (<, >, ==, etc)
54 No operator[] is provided, since this would be enormously slow. If needed,
55 convert to char const* .
64 bool null_terminated ();
68 /** init to empty string. This is needed because other
69 constructors are provided.*/
73 String (char const* source);
74 String (Byte const* byte, int length_i);
76 /// return "new"-ed copy of contents
77 Byte* get_copy_byte () const;
78 char* get_copy_str0 () const;
80 char const* to_str0 () const;
81 Byte const* to_bytes () const;
85 String &operator = (String const & source);
88 void operator += (char const* s) { strh_ += s; }
89 void operator += (String s);
91 bool is_empty () const;
94 void prepend (String);
97 Return a char. UNSAFE because it may change strlen () result
99 char &operator [] (int n);
100 char operator [] (int n) const;
102 /// return n leftmost chars
103 String left_string (int n) const;
105 /// return n rightmost chars
106 String right_string (int n) const;
108 /// return uppercase of *this
109 String upper_string () const;
111 /// return lowercase of *this
112 String lower_string () const;
114 /// return the "esrever" of *this
115 String reversed_string () const;
117 /// return a piece starting at index (first char = index_i 0), length n
118 String cut_string (int index_i, int n) const;
120 /// cut out a middle piece, return remainder
121 String nomid_string (int index_i, int n) const;
123 /// signed comparison, analogous to memcmp;
124 static int compare (String const & s1,const String& s2);
126 /// index of rightmost c
127 int index_last (char c) const;
129 /// index of rightmost element of string (???)
130 int index_last (char const* string) const;
132 int index (char c) const;
134 /// index of leftmost occurance of STRING
135 int index (String) const;
138 int index_any (String) const;
143 #ifdef STREAM_SUPPORT
144 /// provide Stream output
145 void print_on (ostream& os) const;
148 /// the length of the string
151 /// convert to an integer
154 /// convert to a double
155 double to_double () const;
159 better to clutter global namespace, than suffer *ugh, ugh, ugh*
160 implicit conversions.
162 it might be cool to have no type-checking at all in a language,
163 but once there is, having this silently circumvented is a nightmare.
165 whenever implicit conversions seem necessary (e.g. operator << ()),
166 use Scalar as the generic type iso String.
169 /// for completeness (=handy)
170 inline String to_string (String s) { return s; }
172 String to_string (char c, int n = 1);
173 String to_string (int i, char const* format = 0);
174 String to_string (double f, char const* format = 0);
175 String to_string (long b);
176 String to_string (bool b);
177 String to_string (char const* format, ... );
180 technically incorrect, but lets keep it here: this is a
181 catch all place for this stuff.
184 #include "international.hh"
187 #include "compare.hh"
188 INSTANTIATE_COMPARE (String const &, String::compare);
190 #ifdef STRING_UTILS_INLINED
192 #define INLINE inline
194 #include "string.icc"
195 /* we should be resetting INLINE. oh well. */
199 // because char const* also has an operator ==, this is for safety:
200 inline bool operator== (String s1, char const* s2)
202 return s1 == String (s2);
204 inline bool operator== (char const* s1, String s2)
206 return String (s1)==s2;
208 inline bool operator!= (String s1, char const* s2 ) {
209 return s1!=String (s2);
211 inline bool operator!= (char const* s1,String s2) {
212 return String (s2) !=s1;
215 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
216 #ifdef STREAM_SUPPORT
217 ostream &operator << (ostream& os, String d);