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 Do a signed comparison, analogous to memcmp;
146 String::compare_i(String const& s1, String const& s2 )
148 Byte const* p1 = s1.byte_C();
149 Byte const* p2 = s2.byte_C();
153 int i1 = s1.length_i();
154 int i2 = s2.length_i();
156 int result= memcmp( p1, p2, i1 <? i2 );
157 return result ? result : i1-i2;
162 String::index_last_i( char const c ) const
167 char const* me = strh_.ch_C();
168 char const* p = memrchr(me, length_i(), c );
175 String::index_last_i( char const* string ) const // UGK!
177 assert(false); // broken
178 int length = strlen( string ); // ugrh
179 if ( !length_i() || !length )
182 int next_i = index_i( string );
187 while( next_i >= 0 ) {
189 next_i = right_str( length_i() - index_i - length ).index_i( string );
194 /** find a character.
197 the index of the leftmost character #c# (0 <= return < length_i()),
200 ? should return length_i()?, as in string.left_str(index_i(delimiter))
203 String::index_i(char c ) const
205 char const* me = strh_.ch_C();
206 char const* p = (char const *) memchr( me,c, length_i());
216 index of leftmost occurrence of #searchfor#
219 String::index_i( String searchfor ) const
221 char const* me = strh_.ch_C();
222 char const* p = (char const *) memmem(
223 me, length_i(), searchfor.ch_C(), searchfor.length_i());
231 /** find chars of a set.
234 the index of the leftmost occurance of an element of #set#
237 String::index_any_i( String set ) const
243 void const * me_l = (void const *) strh_.ch_C();
244 for (int i=0; i < set.length_i(); i++) {
245 char * found=(char*) memchr(me_l, set[i], n );
254 String::left_str( int n ) const
264 retval.strh_.trunc(n);
269 String::right_str( int n ) const
277 return String( strh_.byte_C() + length_i() - n, n );
282 String::nomid_str( int index_i, int n ) const
292 left_str( index_i ) +
293 right_str( length_i() - index_i - n ) ;
297 proposal: change to "cut()"
300 String::mid_str( int index_i, int n ) const
307 if ( !length_i() || ( index_i < 0 ) || ( index_i >= length_i() ) || ( n < 1 ) )
310 if ( ( n > length_i() ) || ( index_i + n > length_i() ) )
311 n = length_i() - index_i;
313 return String( byte_C() + index_i, n );
317 String::upper_str() const
326 char *s = (char*)strh_.byte_l();
327 strnupr( s ,length_i());
333 char* s = strh_.ch_l();
334 strnlwr(s,length_i());
339 String::lower_str() const
346 String::reversed_str() const
349 strrev( str.byte_l(), str.length_i() );
354 String::value_i() const
356 return String_convert::dec2_i( *this );
360 String::value_f() const
362 return String_convert::dec2_f( *this );