3 string.cc - implement String
5 (c) 1997--1999 Han-Wen Nienhuys & Jan Nieuwenhuizen
9 #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::copy_byte_p () const
33 Byte const* src = strh_.byte_C ();
34 Byte* dest = new Byte[strh_.length_i () + 1];
35 memcpy (dest, src, strh_.length_i () + 1);
40 String::copy_ch_p () const
42 return (char*)copy_byte_p ();
46 String::print_on (ostream& os) const
48 if (!strh_.is_binary_bo ())
51 for (int i = 0; i < length_i (); i++)
52 os << (Byte) (*this)[ i ];
56 copying, constructing.
59 String::operator = (String const&source)
65 String::String (char const* source)
71 String::String (Byte const* byte_l, int length_i)
73 strh_.set (byte_l, length_i);
81 to_str (char c, int n)
83 return String_convert::char_str (c, n);
87 to_str (double f, char const* format)
89 return String_convert::double_str (f, format);
93 to_str (int i, char const * format)
95 return String_convert::int_str (i, format);
101 return String_convert::bool_str (b);
106 return String_convert::long_str (b);
110 to_str (char const* format, ... )
113 va_start (args, format);
114 String str = String_convert::vform_str (format, args);
121 String::append (String s)
123 strh_.append (s.byte_C (), s.length_i ());
126 String::operator += (String s)
132 String::prepend (String s)
139 String::length_i () const
141 return strh_.length_i ();
145 String::byte_C () const
147 return strh_.byte_C ();
151 String::ch_C () const
153 return strh_.ch_C ();
159 return strh_.byte_l ();
165 return strh_.ch_l ();
169 String::empty_b () const
174 Do a signed comparison, analogous to memcmp;
177 String::compare_i (String const& s1, String const& s2)
179 Byte const* p1 = s1.byte_C ();
180 Byte const* p2 = s2.byte_C ();
185 don't forget the terminating '\0'
187 int f = (s1.length_i () <? s2.length_i ());
188 int cmp_length = 1+ f;
189 int i = memcmp (p1, p2, cmp_length);
195 String::index_last_i (char const c) const
200 char const* me = strh_.ch_C ();
201 char const* p = (char const*)memrchr ( (Byte*)me, length_i (), c);
208 String::index_last_i (char const* string) const // UGK!
210 assert (false); // broken
211 int length = strlen (string); // ugrh
212 if (!length_i () || !length)
215 int next_i = index_i (string);
223 next_i = right_str (length_i () - index_i - length).index_i (string );
228 /** find a character.
231 the index of the leftmost character #c# (0 <= return < length_i ()),
234 ? should return length_i ()?, as in string.left_str (index_i (delimiter))
237 String::index_i (char c) const
239 char const* me = strh_.ch_C ();
240 char const* p = (char const *) memchr (me,c, length_i ());
250 1 index of leftmost occurrence of #searchfor#
253 String::index_i (String searchfor) const
255 char const* me = strh_.ch_C ();
257 char const* p = (char const *)
258 memmem (me, length_i (), searchfor.ch_C (), searchfor.length_i ());
266 /** find chars of a set.
270 the index of the leftmost occurance of an element of #set#. -1 if
276 String::index_any_i (String set) const
282 void const * me_l = (void const *) strh_.ch_C ();
283 for (int i=0; i < set.length_i (); i++)
285 char * found= (char*) memchr (me_l, set[i], n );
288 return found - (char const*)me_l;
295 String::left_str (int n) const
297 if (n >= length_i ())
305 retval.strh_.trunc (n);
310 String::right_str (int n) const
318 return String (strh_.byte_C () + length_i () - n, n);
323 String::nomid_str (int index_i, int n) const
335 right_str (length_i () - index_i - n) ;
339 String::cut_str (int index_i, int n) const
347 if (!length_i () || (index_i < 0) || (index_i >= length_i () ) || (n < 1 ) )
350 if ( (n > length_i ()) || (index_i + n > length_i () ) )
351 n = length_i () - index_i;
353 return String (byte_C () + index_i, n);
357 String::upper_str () const
366 char *s = (char*)strh_.byte_l ();
367 strnupr (s ,length_i ());
373 char* s = strh_.ch_l ();
374 strnlwr (s,length_i ());
379 String::lower_str () const
386 String::reversed_str () const
389 strrev (str.byte_l (), str.length_i ());
394 String::value_i () const
396 return String_convert::dec2_i (*this);
400 String::value_f () const
402 return String_convert::dec2_f (*this);
406 operator << (ostream& os, String d)