3 string.cc - implement String
5 (c) 1997--1998 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);
105 to_str (char const* format, ... )
108 va_start (args, format);
109 String str = String_convert::vform_str (format, args);
116 String::append (String s)
118 strh_.append (s.byte_C (), s.length_i ());
121 String::operator += (String s)
127 String::prepend (String s)
134 String::length_i () const
136 return strh_.length_i ();
140 String::byte_C () const
142 return strh_.byte_C ();
146 String::ch_C () const
148 return strh_.ch_C ();
154 return strh_.byte_l ();
160 return strh_.ch_l ();
164 String::empty_b () const
169 Do a signed comparison, analogous to memcmp;
172 String::compare_i (String const& s1, String const& s2)
174 Byte const* p1 = s1.byte_C ();
175 Byte const* p2 = s2.byte_C ();
180 don't forget the terminating '\0'
182 int f = (s1.length_i () <? s2.length_i ());
183 int cmp_length = 1+ f;
184 int i = memcmp (p1, p2, cmp_length);
190 String::index_last_i (char const c) const
195 char const* me = strh_.ch_C ();
196 char const* p = (char const*)memrchr ( (Byte*)me, length_i (), c);
203 String::index_last_i (char const* string) const // UGK!
205 assert (false); // broken
206 int length = strlen (string); // ugrh
207 if (!length_i () || !length)
210 int next_i = index_i (string);
218 next_i = right_str (length_i () - index_i - length).index_i (string );
223 /** find a character.
226 the index of the leftmost character #c# (0 <= return < length_i ()),
229 ? should return length_i ()?, as in string.left_str (index_i (delimiter))
232 String::index_i (char c) const
234 char const* me = strh_.ch_C ();
235 char const* p = (char const *) memchr (me,c, length_i ());
245 1 index of leftmost occurrence of #searchfor#
248 String::index_i (String searchfor) const
250 char const* me = strh_.ch_C ();
252 char const* p = (char const *)
253 memmem (me, length_i (), searchfor.ch_C (), searchfor.length_i ());
261 /** find chars of a set.
265 the index of the leftmost occurance of an element of #set#. -1 if
271 String::index_any_i (String set) const
277 void const * me_l = (void const *) strh_.ch_C ();
278 for (int i=0; i < set.length_i (); i++)
280 char * found= (char*) memchr (me_l, set[i], n );
290 String::left_str (int n) const
292 if (n >= length_i ())
300 retval.strh_.trunc (n);
305 String::right_str (int n) const
313 return String (strh_.byte_C () + length_i () - n, n);
318 String::nomid_str (int index_i, int n) const
330 right_str (length_i () - index_i - n) ;
334 String::cut_str (int index_i, int n) const
342 if (!length_i () || (index_i < 0) || (index_i >= length_i () ) || (n < 1 ) )
345 if ( (n > length_i ()) || (index_i + n > length_i () ) )
346 n = length_i () - index_i;
348 return String (byte_C () + index_i, n);
352 String::upper_str () const
361 char *s = (char*)strh_.byte_l ();
362 strnupr (s ,length_i ());
368 char* s = strh_.ch_l ();
369 strnlwr (s,length_i ());
374 String::lower_str () const
381 String::reversed_str () const
384 strrev (str.byte_l (), str.length_i ());
389 String::value_i () const
391 return String_convert::dec2_i (*this);
395 String::value_f () const
397 return String_convert::dec2_f (*this);