2 FILE : string.hh -- declare String
4 Rehacked by HWN 3/nov/95
6 introduced Class String_handle
12 // too darn slow with gcc3
22 #include "arithmetic-operator.hh"
23 #include "string-handle.hh"
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_string, right_string, mid_string)
50 signed comparison (<, >, ==, etc)
53 No operator[] is provided, since this would be enormously slow. If needed,
54 convert to char const* .
62 bool null_terminated ();
66 /** init to empty string. This is needed because other
67 constructors are provided.*/
71 String (char const *source);
72 String (Byte const *byte, int length_i);
74 /// return "new"-ed copy of contents
75 Byte *get_copy_byte () const;
76 char *get_copy_str0 () const;
78 char const *to_str0 () const;
79 Byte const *to_bytes () const;
83 String &operator = (String const &source);
86 void operator += (char const *s) { strh_ += s; }
87 void operator += (String s);
89 bool is_empty () const;
92 void prepend (String);
95 Return a char. UNSAFE because it may change strlen () result
97 char &operator [] (int n);
98 char operator [] (int n) const;
100 /// return n leftmost chars
101 String left_string (int n) const;
103 /// return n rightmost chars
104 String right_string (int n) const;
106 /// return the "esrever" of *this
109 /// return a piece starting at index (first char = index_i 0), length n
110 String cut_string (int index_i, int n) const;
112 /// cut out a middle piece, return remainder
113 String nomid_string (int index_i, int n) const;
115 /// signed comparison, analogous to memcmp;
116 static int compare (String const &s1, const String &s2);
118 /// index of rightmost c
119 int index_last (char c) const;
121 /// index of rightmost element of string (???)
122 int index_last (char const *string) const;
124 int index (char c) const;
126 /// index of leftmost occurance of STRING
127 int index (String) const;
129 int index_any (String) const;
134 #ifdef STREAM_SUPPORT
135 /// provide Stream output
136 void print_on (ostream &os) const;
139 /// the length of the string
142 /// convert to an integer
145 /// convert to a double
146 double to_double () const;
148 String substitute (String find, String replace);
149 String substitute (char find, char replace);
153 better to clutter global namespace, than suffer *ugh, ugh, ugh*
154 implicit conversions.
156 it might be cool to have no type-checking at all in a language,
157 but once there is, having this silently circumvented is a nightmare.
159 whenever implicit conversions seem necessary (e.g. operator << ()),
160 use Scalar as the generic type iso String.
163 /// for completeness (=handy)
164 String to_string (String 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"
178 #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 bool operator == (String s1, char const *s2);
192 bool operator == (char const *s1, String s2);
193 bool operator != (String s1, char const *s2);
194 bool operator != (char const *s1, String s2);
196 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
197 #ifdef STREAM_SUPPORT
198 ostream &operator << (ostream &os, String d);