2 string.cc - implement String
4 (c) 1997--2005 Han-Wen Nienhuys & Jan Nieuwenhuizen
7 #ifndef _GNU_SOURCE // we want memmem
20 #include "libc-extension.hh"
21 #include "string-convert.hh"
24 void *mymemmove (void *dest, void const *src, size_t n);
25 #define memmove mymemmove
28 // return array, alloced with new.
30 String::get_copy_byte () const
32 Byte const *src = strh_.to_bytes ();
33 Byte *dest = new Byte[strh_.length () + 1];
34 memcpy (dest, src, strh_.length () + 1);
39 String::get_copy_str0 () const
41 return (char *)get_copy_byte ();
46 copying, constructing.
49 String::operator = (String const &source)
55 String::String (Byte const *byte, int len_i)
57 strh_.set (byte, len_i);
65 to_string (char c, int n)
67 return String_convert::char_string (c, n);
71 to_string (double f, char const *format)
73 return String_convert::double_string (f, format);
77 to_string (int i, char const *format)
79 return String_convert::int_string (i, format);
85 return String_convert::bool_string (b);
90 return String_convert::long_string (b);
94 to_string (char const *format, ...)
97 va_start (args, format);
98 String str = String_convert::vform_string (format, args);
105 String::append (String s)
107 strh_.append (s.to_bytes (), s.length ());
110 String::operator += (String s)
116 String::prepend (String s)
123 String::length () const
125 return strh_.length ();
129 String::to_bytes () const
131 return strh_.to_bytes ();
135 String::to_str0 () const
137 return strh_.to_str0 ();
143 return strh_.get_bytes ();
149 return strh_.get_str0 ();
153 String::is_empty () const
158 Do a signed comparison, analogous to memcmp;
161 String::compare (String const &s1, String const &s2)
163 Byte const *p1 = s1.to_bytes ();
164 Byte const *p2 = s2.to_bytes ();
169 don't forget the terminating '\0'
171 int f = min (s1.length (), s2.length ());
172 int cmp_length = 1+ f;
173 int i = memcmp (p1, p2, cmp_length);
179 String::index_last (char const c) const
184 char const *me = strh_.to_str0 ();
185 char const *p = (char const *)memrchr ((Byte *)me, length (), c);
192 String::index_last (char const *string) const // UGK!
194 assert (false); // broken
195 int len = strlen (string); // ugrh
196 if (!length () || !len)
199 int next_i = index (string);
207 next_i = right_string (length () - index_i - len).index (string);
212 /** find a character.
215 the index of the leftmost character #c# (0 <= return < length ()),
218 ? should return length ()?, as in string.left_string (index (delimiter))
221 String::index (char c) const
223 char const *me = strh_.to_str0 ();
224 char const *p = (char const *) memchr (me, c, length ());
234 index of leftmost occurrence of #searchfor#
237 String::index (String searchfor) const
239 char const *me = strh_.to_str0 ();
241 char const *p = (char const *)
242 memmem (me, length (), searchfor.to_str0 (), searchfor.length ());
250 /** find chars of a set.
254 the index of the leftmost occurance of an element of #set#. -1 if
258 String::index_any (String set) const
264 void const *me = (void const *) strh_.to_str0 ();
265 for (int i = 0; i < set.length (); i++)
267 char *found = (char *) memchr (me, set[i], n);
270 return found - (char const *)me;
277 String::left_string (int n) const
287 retval.strh_.trunc (n);
292 String::right_string (int n) const
300 return String (strh_.to_bytes () + length () - n, n);
304 String::nomid_string (int index_i, int n) const
314 return left_string (index_i) + right_string (length () - index_i - n);
318 String::cut_string (int index_i, int n) const
326 if (!length () || (index_i < 0) || (index_i >= length ()) || (n < 1))
329 if ((n > length ()) || (index_i + n > length ()))
330 n = length () - index_i;
332 return String (to_bytes () + index_i, n);
339 strnupr (get_str0 (), length ());
345 strnlwr (get_str0 (), length ());
351 memrev (get_bytes (), length ());
355 String::to_int () const
357 return String_convert::dec2int (*this);
361 String::to_double () const
363 return String_convert::dec2double (*this);
366 #ifdef STREAM_SUPPORT
368 operator << (ostream &os, String d)
375 String::print_on (ostream &os) const
377 if (!strh_.is_binary_bo ())
380 for (int i = 0; i < length (); i++)
381 os << (Byte) (*this)[ i ];
386 String::substitute (String find, String replace)
388 int n = replace.length ();
389 for (int i = index (find); i > - 1; i = index (find))
390 *this = left_string (i) + find + right_string (length () - i - n);
394 String::substitute (char find, char replace)
396 for (int i = index (find); i > - 1; i = index (find))
397 (*this)[i] = replace;