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 "arithmetic-operator.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_string, right_string, mid_string)
49 signed comparison (<, >, ==, etc)
52 No operator[] is provided, since this would be enormously slow. If needed,
53 convert to char const* .
61 bool null_terminated ();
65 /** init to empty string. This is needed because other
66 constructors are provided.*/
70 String (char const *source);
71 String (Byte const *byte, int length_i);
73 /// return "new"-ed copy of contents
74 Byte *get_copy_byte () const;
75 char *get_copy_str0 () const;
77 char const *to_str0 () const;
78 Byte const *to_bytes () const;
82 String &operator = (String const &source);
85 void operator += (char const *s) { strh_ += s; }
86 void operator += (String s);
88 bool is_empty () const;
91 void prepend (String);
94 Return a char. UNSAFE because it may change strlen () result
96 char &operator [] (int n);
97 char operator [] (int n) const;
99 /// return n leftmost chars
100 String left_string (int n) const;
102 /// return n rightmost chars
103 String right_string (int n) const;
105 /// return the "esrever" of *this
108 /// return a piece starting at index (first char = index_i 0), length n
109 String cut_string (int index_i, int n) const;
111 /// cut out a middle piece, return remainder
112 String nomid_string (int index_i, int n) const;
114 /// signed comparison, analogous to memcmp;
115 static int compare (String const &s1, const String &s2);
117 /// index of rightmost c
118 int index_last (char c) const;
120 /// index of rightmost element of string (???)
121 int index_last (char const *string) const;
123 int index (char c) const;
125 /// index of leftmost occurance of STRING
126 int index (String) const;
128 int index_any (String) const;
133 #ifdef STREAM_SUPPORT
134 /// provide Stream output
135 void print_on (ostream &os) const;
138 /// the length of the string
141 /// convert to an integer
144 /// convert to a double
145 double to_double () const;
147 void substitute (String find, String replace);
148 void substitute (char find, char replace);
152 better to clutter global namespace, than suffer *ugh, ugh, ugh*
153 implicit conversions.
155 it might be cool to have no type-checking at all in a language,
156 but once there is, having this silently circumvented is a nightmare.
158 whenever implicit conversions seem necessary (e.g. operator << ()),
159 use Scalar as the generic type iso String.
162 /// for completeness (=handy)
163 inline String to_string (String s) { return s; }
165 String to_string (char c, int n = 1);
166 String to_string (int i, char const *format = 0);
167 String to_string (double f, char const *format = 0);
168 String to_string (long b);
169 String to_string (bool b);
170 String to_string (char const *format, ...);
173 technically incorrect, but lets keep it here: this is a
174 catch all place for this stuff.
177 #include "international.hh"
179 #include "compare.hh"
180 INSTANTIATE_COMPARE (String const &, String::compare);
182 #ifdef STRING_UTILS_INLINED
184 #define INLINE inline
186 #include "string.icc"
187 /* we should be resetting INLINE. oh well. */
190 // because char const* also has an operator ==, this is for safety:
191 inline bool operator == (String s1, char const *s2)
193 return s1 == String (s2);
195 inline bool operator == (char const *s1, String s2)
197 return String (s1) == s2;
199 inline bool operator != (String s1, char const *s2)
201 return s1 != String (s2);
203 inline bool operator != (char const *s1, String s2)
205 return String (s2) != s1;
208 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
209 #ifdef STREAM_SUPPORT
210 ostream &operator << (ostream &os, String d);