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_l();
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_.null_terminated_b())
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, const char * 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_l(), 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_l() const
121 return strh_.byte_c_l();
125 String::ch_c_l() const
127 return strh_.ch_c_l();
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_l();
149 Byte const* p2 = s2.byte_c_l();
153 int i1 = s1.length_i();
154 int i2 = s2.length_i();
157 int result= memcmp( p1, p2, i );
158 return result ? result : i1-i2;
163 String::index_last_i( char const c ) const
168 char const* me = strh_.ch_c_l();
169 char const* p = memrchr(me, length_i(), c );
176 String::index_last_i( char const* string ) const // UGK!
178 assert(false); // broken
179 int length = strlen( string ); // ugrh
180 if ( !length_i() || !length )
183 int next_i = index_i( string );
188 while( next_i >= 0 ) {
190 next_i = right_str( length_i() - index_i - length ).index_i( string );
195 /** find a character.
198 the index of the leftmost character #c# (0 <= return < length_i()),
201 ? should return length_i()?, as in string.left_str(index_i(delimiter))
204 String::index_i(char c ) const
206 char const* me = strh_.ch_c_l();
207 char const* p = (char const *) memchr( me,c, length_i());
217 index of leftmost occurrence of #searchfor#
220 String::index_i( String searchfor ) const
222 char const* me = strh_.ch_c_l();
223 char const* p = (char const *) memmem(me, length_i(), searchfor.ch_c_l(),
224 searchfor.length_i());
232 /** find chars of a set.
235 the index of the leftmost occurance of an element of #set#
238 String::index_any_i( String set ) const
244 const void * me_l = (const void*) strh_.ch_c_l();
245 for (int i=0; i < set.length_i(); i++) {
246 char * found=(char*) memchr(me_l, set[i], n );
255 String::left_str( int n ) const
265 retval.strh_.trunc(n);
270 String::right_str( int n ) const
278 return String( strh_.byte_c_l() + length_i() - n, n );
283 String::nomid_str( int index_i, int n ) const
293 left_str( index_i ) +
294 right_str( length_i() - index_i - n ) ;
298 proposal: change to "cut()"
301 String::mid_str( int index_i, int n ) const
303 if ( !length_i() || ( index_i < 0 ) || ( index_i >= length_i() ) || ( n < 1 ) )
306 if ( ( n > length_i() ) || ( index_i + n > length_i() ) )
307 n = length_i() - index_i;
309 return String( byte_c_l() + index_i, n );
313 String::upper_str() const
322 char *s = (char*)strh_.byte_l();
323 strnupr( s ,length_i());
329 char* s = strh_.ch_l();
330 strnlwr(s,length_i());
335 String::lower_str() const
342 String::reversed_str() const
345 strrev( str.byte_l(), str.length_i() );
350 String::value_i() const
352 return String_convert::dec2_i( *this );
356 String::value_f() const
358 return String_convert::dec2_f( *this );