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 (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();
228 char const* p = (char const *) memmem (
229 me, length_i(), searchfor.ch_C(), searchfor.length_i ());
237 /** find chars of a set.
240 the index of the leftmost occurance of an element of #set#
243 String::index_any_i (String set) const
249 void const * me_l = (void const *) strh_.ch_C();
250 for (int i=0; i < set.length_i(); i++)
252 char * found=(char*) memchr (me_l, set[i], n );
262 String::left_str (int n) const
272 retval.strh_.trunc (n);
277 String::right_str (int n) const
285 return String (strh_.byte_C() + length_i() - n, n);
290 String::nomid_str (int index_i, int n) const
302 right_str (length_i() - index_i - n) ;
306 proposal: change to "cut()"
309 String::mid_str (int index_i, int n) const
317 if (!length_i() || (index_i < 0) || (index_i >= length_i () ) || (n < 1 ) )
320 if ((n > length_i()) || (index_i + n > length_i () ) )
321 n = length_i() - index_i;
323 return String (byte_C() + index_i, n);
327 String::upper_str() const
336 char *s = (char*)strh_.byte_l();
337 strnupr (s ,length_i());
343 char* s = strh_.ch_l();
344 strnlwr (s,length_i());
349 String::lower_str() const
356 String::reversed_str() const
359 strrev (str.byte_l(), str.length_i ());
364 String::value_i() const
366 return String_convert::dec2_i (*this);
370 String::value_f() const
372 return String_convert::dec2_f (*this);