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 char const *to_str0 () const;
99 Byte const *to_bytes () const;
103 String &operator = (String const &source);
106 void operator += (char const *s) { strh_ += s; }
107 void operator += (String s);
109 bool is_empty () const;
111 void append (String);
112 void prepend (String);
115 Return a char. UNSAFE because it may change strlen () result
117 char &operator [] (int n);
118 char operator [] (int n) const;
120 /// return n leftmost chars
121 String left_string (int n) const;
123 /// return n rightmost chars
124 String right_string (int n) const;
126 /// return the "esrever" of *this
129 /// return a piece starting at index (first char = index_i 0), length n
130 String cut_string (int index_i, int n) const;
132 /// cut out a middle piece, return remainder
133 String nomid_string (int index_i, int n) const;
135 /// signed comparison, analogous to memcmp;
136 static int compare (String const &s1, const String &s2);
138 /// index of rightmost c
139 int index_last (char c) const;
141 /// index of rightmost element of string (???)
142 int index_last (char const *string) const;
144 int index (char c) const;
146 /// index of leftmost occurance of STRING
147 int index (String) const;
149 int index_any (String) const;
154 #ifdef STREAM_SUPPORT
155 /// provide Stream output
156 void print_on (ostream &os) const;
159 /// the length of the string
162 /// convert to an integer
165 /// convert to a double
166 double to_double () const;
168 String substitute (String find, String replace);
169 String substitute (char find, char replace);
173 better to clutter global namespace, than suffer *ugh, ugh, ugh*
174 implicit conversions.
176 it might be cool to have no type-checking at all in a language,
177 but once there is, having this silently circumvented is a nightmare.
179 whenever implicit conversions seem necessary (e.g. operator << ()),
180 use Scalar as the generic type iso String.
183 /// for completeness (=handy)
184 String to_string (String s);
185 String to_string (char c, int n = 1);
186 String to_string (int i, char const *format = 0);
187 String to_string (double f, char const *format = 0);
188 String to_string (long b);
189 String to_string (bool b);
190 String to_string (char const *format, ...);
193 technically incorrect, but lets keep it here: this is a
194 catch all place for this stuff.
197 #include "international.hh"
198 #include "compare.hh"
200 INSTANTIATE_COMPARE (String const &, String::compare);
202 #ifdef STRING_UTILS_INLINED
204 #define INLINE inline
206 #include "string.icc"
207 /* we should be resetting INLINE. oh well. */
210 // because char const* also has an operator ==, this is for safety:
211 bool operator == (String s1, char const *s2);
212 bool operator == (char const *s1, String s2);
213 bool operator != (String s1, char const *s2);
214 bool operator != (char const *s1, String s2);
216 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
217 #ifdef STREAM_SUPPORT
218 ostream &operator << (ostream &os, String d);