2 string.cc - implement String
4 (c) 1997--2006 Han-Wen Nienhuys & Jan Nieuwenhuizen
7 #ifndef _GNU_SOURCE // we want memmem
21 #include "libc-extension.hh"
22 #include "string-convert.hh"
25 void *mymemmove (void *dest, void const *src, size_t n);
26 #define memmove mymemmove
29 // return array, alloced with new.
31 String::get_copy_byte () const
33 Byte const *src = strh_.to_bytes ();
34 Byte *dest = new Byte[strh_.length () + 1];
35 memcpy (dest, src, strh_.length () + 1);
40 String::get_copy_str0 () const
42 return (char *)get_copy_byte ();
47 copying, constructing.
50 String::operator = (String const &source)
56 String::String (Byte const *byte, int len_i)
58 strh_.set (byte, len_i);
66 to_string (char c, int n)
68 return String_convert::char_string (c, n);
72 to_string (double f, char const *format)
74 return String_convert::double_string (f, format);
78 to_string (int i, char const *format)
80 return String_convert::int_string (i, format);
86 return String_convert::bool_string (b);
91 return String_convert::long_string (b);
95 to_string (char const *format, ...)
98 va_start (args, format);
99 String str = String_convert::vform_string (format, args);
106 String::append (String s)
108 strh_.append (s.to_bytes (), s.length ());
111 String::operator += (String s)
117 String::prepend (String s)
124 String::length () const
126 return strh_.length ();
130 String::to_bytes () const
132 return strh_.to_bytes ();
138 return strh_.get_bytes ();
144 return strh_.get_str0 ();
148 String::is_empty () const
153 Do a signed comparison, analogous to memcmp;
156 String::compare (String const &s1, String const &s2)
158 Byte const *p1 = s1.to_bytes ();
159 Byte const *p2 = s2.to_bytes ();
164 don't forget the terminating '\0'
166 int f = min (s1.length (), s2.length ());
167 int cmp_length = 1+ f;
168 int i = memcmp (p1, p2, cmp_length);
174 String::index_last (char const c) const
179 char const *me = strh_.to_str0 ();
180 char const *p = (char const *)memrchr ((Byte *)me, length (), c);
187 String::index_last (char const *string) const // UGK!
189 assert (false); // broken
190 int len = strlen (string); // ugrh
191 if (!length () || !len)
194 int next_i = index (string);
202 next_i = right_string (length () - index_i - len).index (string);
207 /** find a character.
210 the index of the leftmost character #c# (0 <= return < length ()),
213 ? should return length ()?, as in string.left_string (index (delimiter))
216 String::index (char c) const
218 char const *me = strh_.to_str0 ();
219 char const *p = (char const *) memchr (me, c, length ());
229 index of leftmost occurrence of #searchfor#
232 String::index (String searchfor) const
234 char const *me = strh_.to_str0 ();
237 = (char const *) memmem (me, length (),
238 searchfor.to_str0 (), searchfor.length ());
246 /** find chars of a set.
250 the index of the leftmost occurance of an element of #set#. -1 if
254 String::index_any (String set) const
260 void const *me = (void const *) strh_.to_str0 ();
261 for (int i = 0; i < set.length (); i++)
263 char *found = (char *) memchr (me, set[i], n);
265 return found - (char const *)me;
271 String::left_string (int n) const
281 retval.strh_.trunc (n);
286 String::right_string (int n) const
294 return String (strh_.to_bytes () + length () - n, n);
298 String::nomid_string (int index_i, int n) const
308 return left_string (index_i) + right_string (length () - index_i - n);
312 String::cut_string (int index_i, int n) const
320 if (!length () || (index_i < 0) || (index_i >= length ()) || (n < 1))
323 if ((n > length ()) || (index_i + n > length ()))
324 n = length () - index_i;
326 return String (to_bytes () + index_i, n);
333 strnupr (get_str0 (), length ());
339 strnlwr (get_str0 (), length ());
345 memrev (get_bytes (), length ());
349 String::to_int () const
351 return String_convert::dec2int (*this);
355 String::to_double () const
357 return String_convert::dec2double (*this);
360 #ifdef STREAM_SUPPORT
362 operator << (ostream &os, String d)
369 String::print_on (ostream &os) const
371 if (!strh_.is_binary_bo ())
374 for (int i = 0; i < length (); i++)
375 os << (Byte) (*this)[ i ];
380 String::substitute (String find, String replace)
382 int n = find.length ();
383 int m = replace.length ();
384 for (int i = index (find), j = 0; i > -1;
385 i = right_string (length () - j).index (find))
387 *this = left_string (i + j)
389 + right_string (length () - j - i - n);
396 String::substitute (char find, char replace)
398 for (int i = index (find); i > - 1; i = index (find))
399 (*this)[i] = replace;