2 PROJECT: FlowerSoft C++ library
5 Rehacked by HWN 3/nov/95
7 introduced class String_handle
19 void* mymemmove( void* dest, void* src, size_t n );
20 #define memmove mymemmove
30 *p = tolower( *p ); /* a macro on some compilers */
43 *p = toupper( *p ); /* a macro on some compilers */
49 String::String(Rational r)
51 char * n = Itoa(r.numerator()); // LEAK????
54 if (r.denominator() != 1) {
55 char * d = Itoa(r.denominator());
56 *this += String( '/' ) + String(d);
63 // return array, alloced with new.
65 String::copy_byte_p() const
67 Byte const* src = strh_.byte_c_l();
68 Byte* dest = new Byte[strh_.length_i() + 1];
69 memmove( dest, src, strh_.length_i() + 1 );
74 String::printOn(ostream& os) const
76 if ( length_i() == strlen( ch_c_l() ) )
79 for ( int i = 0; i < length_i(); i++ )
80 os << (Byte)(*this)[ i ];
83 String::String (bool b)
85 *this = (char const* ) (b ? "true" : "false");
87 String::String( char const* source )
93 String::String( Byte const* byte_l, int length_i )
95 // assert( !length_i || byte_l );// ugh. Storing null pointers?
96 strh_.set( byte_l, length_i );
100 String::operator +=(String s)
102 strh_.append( s.byte_c_l(), s.length_i() );
106 String::length_i() const
108 return strh_.length_i();
111 // will go away, fixed anyway
112 String::String( char c, int n )
115 char* ch_p = new char[ n ];
116 memset( ch_p, c, n );
117 strh_.set( (Byte*)ch_p, n );
121 String::String(int i)
123 char digits[ 81 ]; // who the fuck is 80???
125 sprintf(digits, "%d", i ); // assume radix 10
129 String::String( const int i, const int n, char const c )
137 String str = String( fillChar, n - v.length_i() ) + String( v );
138 strh_.set( str.byte_c_l(), str.length_i() );
142 String::byte_c_l() const
144 return strh_.byte_c_l();
148 String::ch_c_l() const
150 return strh_.ch_c_l();
156 return strh_.byte_l();
165 // signed comparison, analogous to memcmp;
167 String::compare(String const& s1, String const& s2 )
169 Byte const* p1 = s1.byte_c_l();
170 Byte const* p2 = s2.byte_c_l();
174 int i1 = s1.length_i();
175 int i2 = s2.length_i();
178 int result= memcmp( p1, p2, i );
179 return result ? result : i1-i2;
184 String::lastPos( char const c ) const
187 assert( length_i() == strlen( ch_c_l() ) );
188 char const* me = strh_.ch_c_l();
192 char const* p = strrchr(me, c );
200 String::lastPos( char const* string ) const
203 assert( length_i() == strlen( ch_c_l() ) );
205 int length = strlen( string );
206 if ( length_i() && length )
208 int nextpos = this->pos( string );
212 nextpos = right( length_i() - pos - length + 1 ).pos( string );
219 // return 0 if not found.
221 // ? should return length_i()?, as in string.left(pos(delimiter))
223 String::pos(char c ) const
226 assert( length_i() == strlen( ch_c_l() ) );
227 char const* me = strh_.ch_c_l();
231 char const* p = strchr( me, c );
238 // find searchfor. (what if this == "" && searchfor == "") ???
240 String::pos( char const* searchfor ) const
243 assert( length_i() == strlen( ch_c_l() ) );
244 char const* me = strh_.ch_c_l();
246 if ( length_i() && searchfor)
248 char const* p = strstr(me, searchfor);
255 // find chars of a set.
257 String::posAny( char const* string ) const
260 assert( length_i() == strlen( ch_c_l() ) );
262 char const* s = (char const* )strh_.ch_c_l();
263 if ( length_i() && string )
265 char const* p = strpbrk( s, string );
273 String::left( int n ) const
283 retval.strh_.trunc(n);
290 String::right( int n ) const
298 return String( strh_.byte_c_l() + length_i() - n, n );
303 String::nomid( const int pos, const int n ) const
309 if ( pos > length_i())
312 return String( String( left( pos - 1 ) ) + right( length_i() - pos - n + 1 ));
317 String::mid( int pos, int n ) const
319 // HWN. This SUX: JCN: yep, please change me + all my invocations
320 // pos 1 == strh_->string[ 0 ];
321 // pos 0 allowed for convenience
322 if ( !length_i() || ( pos < 0 ) || ( pos > length_i() ) && ( n < 1 ) )
326 if ( ( n > length_i() ) || ( pos + n - 1 > length_i() ) )
327 n = length_i() - pos + 1;
329 return String( byte_c_l() + pos -1, n );
338 assert( length_i() == strlen( ch_c_l() ) );
339 char *s = strh_.byte_l();
350 assert( length_i() == strlen( ch_c_l() ) );
351 char* s = strh_.byte_l();
356 String::String (double f, char const* fmt)
358 /* worst case would be printing HUGE (or 1/HUGE), which is approx
359 2e318, this number would have approx 318 zero's in its string.
361 1024 is a safe length for the buffer
366 sprintf(buf, "%f", f);
373 String::value() const
377 int conv = sscanf(strh_.ch_c_l(), "%ld", &l);
384 String::fvalue() const
388 int conv = sscanf(strh_.ch_c_l(), "%lf", &d);
395 String quoteString( String msg, String quote)
397 return msg + " `" + quote + "' ";
402 strrev( Byte* byte_l, int length_i )
405 Byte* left_byte_l = byte_l;
406 Byte* right_byte_l = byte_l + length_i;
408 while ( right_byte_l > left_byte_l ) {
410 *left_byte_l++ = *right_byte_l;
411 *right_byte_l-- = by;
418 String::reversed() const
421 strrev( str.byte_l(), str.length_i() );