3 string.cc - implement String
5 (c) 1997 Han-Wen Nienhuys & Jan Nieuwenhuizen
9 #ifndef _GNU_SOURCE // we want memmem
20 #include "libc-extension.hh"
21 #include "string-convert.hh"
24 void* mymemmove (void* dest, void const* src, size_t n);
25 #define memmove mymemmove
28 // return array, alloced with new.
30 String::copy_byte_p() const
32 Byte const* src = strh_.byte_C();
33 Byte* dest = new Byte[strh_.length_i() + 1];
34 memcpy (dest, src, strh_.length_i() + 1);
38 String::print_on (ostream& os) const
40 if (!strh_.is_binary_bo())
43 for (int i = 0; i < length_i(); i++)
44 os << (Byte)(*this)[ i ];
48 copying, constructing.
51 String::operator = (String const&source)
57 String::String (double f, char const* fmt)
59 *this= String_convert::double_str (f,fmt);
62 String::String (char c, int n)
64 *this = String_convert::char_str (c,n);
69 String_convert::int_str
71 String::String (int i, char const * format)
73 *this = String_convert::int_str (i,format);
76 String::String (bool b)
78 *this = (char const*) (b ? "true" : "false");
81 String::String (char const* source)
87 String::String (Byte const* byte_l, int length_i)
89 strh_.set (byte_l, length_i);
93 String::append (String s)
95 strh_.append (s.byte_C(), s.length_i());
98 String::operator +=(String s)
104 String::prepend (String s)
111 String::length_i() const
113 return strh_.length_i();
117 String::byte_C() const
119 return strh_.byte_C();
131 return strh_.byte_l();
141 String::empty_b () const
146 Do a signed comparison, analogous to memcmp;
149 String::compare_i (String const& s1, String const& s2)
151 Byte const* p1 = s1.byte_C();
152 Byte const* p2 = s2.byte_C();
157 don't forget the terminating '\0'
159 int f = (s1.length_i() <? s2.length_i());
160 int cmp_length = 1+ f;
161 return memcmp (p1, p2, cmp_length);
166 String::index_last_i (char const c) const
171 char const* me = strh_.ch_C();
172 char const* p = memrchr ((Byte*)me, length_i(), c);
179 String::index_last_i (char const* string) const // UGK!
181 assert (false); // broken
182 int length = strlen (string); // ugrh
183 if (!length_i() || !length)
186 int next_i = index_i (string);
194 next_i = right_str (length_i() - index_i - length).index_i (string );
199 /** find a character.
202 the index of the leftmost character #c# (0 <= return < length_i()),
205 ? should return length_i()?, as in string.left_str (index_i (delimiter))
208 String::index_i (char c) const
210 char const* me = strh_.ch_C();
211 char const* p = (char const *) memchr (me,c, length_i());
221 1 index of leftmost occurrence of #searchfor#
224 String::index_i (String searchfor) const
226 char const* me = strh_.ch_C();
228 char const* p = (char const *)
229 memmem (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 String::cut (int index_i, int n) const
314 if (!length_i() || (index_i < 0) || (index_i >= length_i () ) || (n < 1 ) )
317 if ((n > length_i()) || (index_i + n > length_i () ) )
318 n = length_i() - index_i;
320 return String (byte_C() + index_i, n);
324 String::upper_str() const
333 char *s = (char*)strh_.byte_l();
334 strnupr (s ,length_i());
340 char* s = strh_.ch_l();
341 strnlwr (s,length_i());
346 String::lower_str() const
353 String::reversed_str() const
356 strrev (str.byte_l(), str.length_i ());
361 String::value_i() const
363 return String_convert::dec2_i (*this);
367 String::value_f() const
369 return String_convert::dec2_f (*this);