3 string.cc - implement String
5 (c) 1997--2004 Han-Wen Nienhuys & Jan Nieuwenhuizen
9 #ifndef _GNU_SOURCE // we want memmem
22 #include "libc-extension.hh"
23 #include "string-convert.hh"
26 void* mymemmove (void* dest, void const* src, size_t n);
27 #define memmove mymemmove
30 // return array, alloced with new.
32 String::get_copy_byte () const
34 Byte const* src = strh_.to_bytes ();
35 Byte* dest = new Byte[strh_.length () + 1];
36 memcpy (dest, src, strh_.length () + 1);
41 String::get_copy_str0 () const
43 return (char*)get_copy_byte ();
48 copying, constructing.
51 String::operator = (String const&source)
58 String::String (Byte const* byte, int len_i)
60 strh_.set (byte, len_i);
68 to_string (char c, int n)
70 return String_convert::char_string (c, n);
74 to_string (double f, char const* format)
76 return String_convert::double_string (f, format);
80 to_string (int i, char const * format)
82 return String_convert::int_string (i, format);
88 return String_convert::bool_string (b);
93 return String_convert::long_string (b);
97 to_string (char const* format, ... )
100 va_start (args, format);
101 String str = String_convert::vform_string (format, args);
108 String::append (String s)
110 strh_.append (s.to_bytes (), s.length ());
113 String::operator += (String s)
119 String::prepend (String s)
126 String::length () const
128 return strh_.length ();
132 String::to_bytes () const
134 return strh_.to_bytes ();
138 String::to_str0 () const
140 return strh_.to_str0 ();
146 return strh_.get_bytes ();
152 return strh_.get_str0 ();
156 String::is_empty () const
161 Do a signed comparison, analogous to memcmp;
164 String::compare (String const& s1, String const& s2)
166 Byte const* p1 = s1.to_bytes ();
167 Byte const* p2 = s2.to_bytes ();
172 don't forget the terminating '\0'
174 int f = (s1.length () <? s2.length ());
175 int cmp_length = 1+ f;
176 int i = memcmp (p1, p2, cmp_length);
182 String::index_last (char const c) const
187 char const* me = strh_.to_str0 ();
188 char const* p = (char const*)memrchr ((Byte*)me, length (), c);
195 String::index_last (char const* string) const // UGK!
197 assert (false); // broken
198 int len = strlen (string); // ugrh
199 if (!length () || !len)
202 int next_i = index (string);
210 next_i = right_string (length () - index_i - len).index (string );
215 /** find a character.
218 the index of the leftmost character #c# (0 <= return < length ()),
221 ? should return length ()?, as in string.left_string (index (delimiter))
224 String::index (char c) const
226 char const* me = strh_.to_str0 ();
227 char const* p = (char const *) memchr (me,c, length ());
237 1 index of leftmost occurrence of #searchfor#
240 String::index (String searchfor) const
242 char const* me = strh_.to_str0 ();
244 char const* p = (char const *)
245 memmem (me, length (), searchfor.to_str0 (), searchfor.length ());
253 /** find chars of a set.
257 the index of the leftmost occurance of an element of #set#. -1 if
263 String::index_any (String set) const
269 void const * me = (void const *) strh_.to_str0 ();
270 for (int i=0; i < set.length (); i++)
272 char * found= (char*) memchr (me, set[i], n );
275 return found - (char const*)me;
282 String::left_string (int n) const
292 retval.strh_.trunc (n);
297 String::right_string (int n) const
305 return String (strh_.to_bytes () + length () - n, n);
310 String::nomid_string (int index_i, int n) const
321 left_string (index_i) +
322 right_string (length () - index_i - n) ;
326 String::cut_string (int index_i, int n) const
334 if (!length () || (index_i < 0) || (index_i >= length () ) || (n < 1 ) )
337 if ((n > length ()) || (index_i + n > length () ) )
338 n = length () - index_i;
340 return String (to_bytes () + index_i, n);
344 String::upper_string () const
353 char *s = (char*)strh_.get_bytes ();
354 strnupr (s ,length ());
360 char* s = strh_.get_str0 ();
361 strnlwr (s,length ());
366 String::lower_string () const
373 String::reversed_string () const
376 strrev (str.get_bytes (), str.length ());
381 String::to_int () const
383 return String_convert::dec2int (*this);
387 String::to_double () const
389 return String_convert::dec2double (*this);
392 #ifdef STREAM_SUPPORT
394 operator << (ostream& os, String d)
402 String::print_on (ostream& os) const
404 if (!strh_.is_binary_bo ())
407 for (int i = 0; i < length (); i++)
408 os << (Byte) (*this)[ i ];