3 string.cc - implement String
5 (c) 1997 Han-Wen Nienhuys & Jan Nieuwenhuizen
16 #include "libc-extension.hh"
17 #include "string-convert.hh"
20 void* mymemmove (void* dest, void const* src, size_t n);
21 #define memmove mymemmove
24 // return array, alloced with new.
26 String::copy_byte_p() const
28 Byte const* src = strh_.byte_C();
29 Byte* dest = new Byte[strh_.length_i() + 1];
30 memcpy (dest, src, strh_.length_i() + 1);
34 String::print_on (ostream& os) const
36 if (!strh_.is_binary_bo())
39 for (int i = 0; i < length_i(); i++)
40 os << (Byte)(*this)[ i ];
44 copying, constructing.
47 String::operator = (String const&source)
54 String::String (Rational r)
56 *this = String_convert::rational_str (r);
59 String::String (double f, char const* fmt)
61 *this= String_convert::double_str (f,fmt);
64 String::String (char c, int n)
66 *this = String_convert::char_str (c,n);
71 String_convert::int_str
73 String::String (int i, char const * format)
75 *this = String_convert::int_str (i,format);
78 String::String (bool b)
80 *this = (char const*) (b ? "true" : "false");
83 String::String (char const* source)
89 String::String (Byte const* byte_l, int length_i)
91 strh_.set (byte_l, length_i);
95 String::append (String s)
97 strh_.append (s.byte_C(), s.length_i());
100 String::operator +=(String s)
106 String::prepend (String s)
113 String::length_i() const
115 return strh_.length_i();
119 String::byte_C() const
121 return strh_.byte_C();
133 return strh_.byte_l();
143 String::empty_b () const
148 Do a signed comparison, analogous to memcmp;
151 String::compare_i (String const& s1, String const& s2)
153 Byte const* p1 = s1.byte_C();
154 Byte const* p2 = s2.byte_C();
158 int i1 = s1.length_i();
159 int i2 = s2.length_i();
161 int result= memcmp (p1, p2, i1 <? i2);
162 return result ? result : i1-i2;
167 String::index_last_i (char const c) const
172 char const* me = strh_.ch_C();
173 char const* p = memrchr ((Byte*)me, length_i(), c);
180 String::index_last_i (char const* string) const // UGK!
182 assert (false); // broken
183 int length = strlen (string); // ugrh
184 if (!length_i() || !length)
187 int next_i = index_i (string);
195 next_i = right_str (length_i() - index_i - length).index_i (string );
200 /** find a character.
203 the index of the leftmost character #c# (0 <= return < length_i()),
206 ? should return length_i()?, as in string.left_str (index_i (delimiter))
209 String::index_i (char c) const
211 char const* me = strh_.ch_C();
212 char const* p = (char const *) memchr (me,c, length_i());
222 1 index of leftmost occurrence of #searchfor#
225 String::index_i (String searchfor) const
227 char const* me = strh_.ch_C();
229 char const* p = (char const *)
230 memmem (me, length_i(), searchfor.ch_C(), searchfor.length_i ());
238 /** find chars of a set.
241 the index of the leftmost occurance of an element of #set#
244 String::index_any_i (String set) const
250 void const * me_l = (void const *) strh_.ch_C();
251 for (int i=0; i < set.length_i(); i++)
253 char * found=(char*) memchr (me_l, set[i], n );
263 String::left_str (int n) const
273 retval.strh_.trunc (n);
278 String::right_str (int n) const
286 return String (strh_.byte_C() + length_i() - n, n);
291 String::nomid_str (int index_i, int n) const
303 right_str (length_i() - index_i - n) ;
307 String::cut (int index_i, int n) const
315 if (!length_i() || (index_i < 0) || (index_i >= length_i () ) || (n < 1 ) )
318 if ((n > length_i()) || (index_i + n > length_i () ) )
319 n = length_i() - index_i;
321 return String (byte_C() + index_i, n);
325 String::upper_str() const
334 char *s = (char*)strh_.byte_l();
335 strnupr (s ,length_i());
341 char* s = strh_.ch_l();
342 strnlwr (s,length_i());
347 String::lower_str() const
354 String::reversed_str() const
357 strrev (str.byte_l(), str.length_i ());
362 String::value_i() const
364 return String_convert::dec2_i (*this);
368 String::value_f() const
370 return String_convert::dec2_f (*this);