2 FILE : string.hh -- declare String
4 Rehacked by HWN 3/nov/95
6 introduced Class String_handle
12 // too darn slow with gcc3
21 #include "std-string.hh"
24 #include "arithmetic-operator.hh"
25 #include "string-handle.hh"
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_string, right_string, mid_string)
52 signed comparison (<, >, ==, etc)
55 No operator[] is provided, since this would be enormously slow. If needed,
56 convert to char const* .
64 String (Std_string const &);
65 operator Std_string () const;
66 #endif /* STD_STRING */
68 /* std::string interface */
69 char const *c_str () const;
71 int String::find (String &s, int pos=0) const;
72 int find (char c) const;
73 int rfind (char c) const;
74 String replace (int pos, int n, String str);
76 String (String const &, int pos, int n=-1);
77 String (int n, char c);
82 bool null_terminated ();
86 /** init to empty string. This is needed because other
87 constructors are provided.*/
91 String (char const *source);
92 String (Byte const *byte, int length_i);
94 /// return "new"-ed copy of contents
95 Byte *get_copy_byte () const;
96 char *get_copy_str0 () const;
98 Byte const *to_bytes () const;
102 String &operator = (String const &source);
105 void operator += (char const *s) { strh_ += s; }
106 void operator += (String s);
108 void append (String);
109 void prepend (String);
112 Return a char. UNSAFE because it may change strlen () result
114 char &operator [] (int n);
115 char operator [] (int n) const;
117 /// return n leftmost chars
118 String left_string (int n) const;
120 /// return n rightmost chars
121 String right_string (int n) const;
123 /// return the "esrever" of *this
126 /// return a piece starting at index (first char = index_i 0), length n
127 String cut_string (int index_i, int n) const;
129 /// cut out a middle piece, return remainder
130 String nomid_string (int index_i, int n) const;
132 /// signed comparison, analogous to memcmp;
133 static int compare (String const &s1, const String &s2);
135 /// index of rightmost element of string (???)
136 int index_last (char const *string) const;
138 int index_last (char c) const;
140 int index (char c) const;
142 /// index of leftmost occurance of STRING
143 int index (String) const;
145 int index_any (String) const;
150 #ifdef STREAM_SUPPORT
151 /// provide Stream output
152 void print_on (ostream &os) const;
155 /// the length of the string
158 /// convert to an integer
161 /// convert to a double
162 double to_double () const;
164 String substitute (String find, String replace);
165 String substitute (char find, char replace);
169 better to clutter global namespace, than suffer *ugh, ugh, ugh*
170 implicit conversions.
172 it might be cool to have no type-checking at all in a language,
173 but once there is, having this silently circumvented is a nightmare.
175 whenever implicit conversions seem necessary (e.g. operator << ()),
176 use Scalar as the generic type iso String.
179 /// for completeness (=handy)
180 String to_string (String s);
181 String to_string (char c, int n = 1);
182 String to_string (int i, char const *format = 0);
183 String to_string (double f, char const *format = 0);
184 String to_string (long b);
185 String to_string (bool b);
186 String to_string (char const *format, ...);
189 technically incorrect, but lets keep it here: this is a
190 catch all place for this stuff.
193 #include "international.hh"
194 #include "compare.hh"
196 INSTANTIATE_COMPARE (String const &, String::compare);
198 #ifdef STRING_UTILS_INLINED
200 #define INLINE inline
202 #include "string.icc"
203 /* we should be resetting INLINE. oh well. */
206 // because char const* also has an operator ==, this is for safety:
207 bool operator == (String s1, char const *s2);
208 bool operator == (char const *s1, String s2);
209 bool operator != (String s1, char const *s2);
210 bool operator != (char const *s1, String s2);
212 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
213 #ifdef STREAM_SUPPORT
214 ostream &operator << (ostream &os, String d);