1 /****************************************************************************
2 PROJECT: FlowerSoft C++ library
5 Rehacked by HWN 3/nov/95
7 introduced Class String_handle
19 static char* strlwr( char* s
)
25 *p
= tolower( *p
); /* a macro on some compilers */
31 static char* strupr( char* s
)
37 *p
= toupper( *p
); /* a macro on some compilers */
43 String::String(Rational r
)
45 char * n
= Itoa(r
.numerator()); // LEAK????
48 if (r
.denominator() != 1) {
49 char * d
= Itoa(r
.denominator());
50 *this += String( '/' ) + String(d
);
57 // return array, alloced with new.
59 String::copy_array() const
61 const char *src
= data
;
62 char *dest
= new char[data
.len() + 1];
68 String::printOn(ostream
& os
) const
70 os
<< (const char*) data
;
73 String::String (bool b
)
75 *this = (const char *) (b
? "true" : "false");
77 String::String( const char* source
)
84 String::operator +=(String s
)
86 *this += (const char *) s
;
95 String::String(char c
, int n
)
98 assert(n
>= 0 && n
<= 80); // what the fuck is 80?
99 //min(max( n, 0 ), 80);
106 String::String(int i
)
108 char digits
[ 81 ]; // who the FUCK is 80???
110 sprintf(digits
, "%d", i
); // assume radix 10
114 String::String( const int i
, const int n
, const char c
)
122 data
= String( fillChar
, n
- v
.len() ) + String( v
);
123 // String convd to const char *
134 // signed comparison, analogous to strcmp;
136 String::compare(const String
& s1
,const String
&s2
)
138 const char * p1
=s1
.cptr();
139 const char * p2
= s2
.cptr();
143 return strcmp(p1
,p2
);
148 String::lastPos( const char c
) const
150 const char *me
= data
;
154 const char* p
= strrchr(me
, c
);
162 String::lastPos( const char* string
) const
165 int length
= strlen( string
);
166 if ( len() && length
)
168 int nextpos
= this->pos( string
);
172 nextpos
= right( len() - pos
- length
+ 1 ).pos( string
);
179 // return 0 if not found.
181 // ? should return len()?, as in string.left(pos(delimiter))
183 String::pos(char c
) const
185 const char *me
= data
;
189 const char* p
= strchr( me
, c
);
196 // find searchfor. (what if this == "" && searchfor == "") ???
198 String::pos( const char* searchfor
) const
200 const char *me
= data
;
202 if ( len() && searchfor
)
204 const char* p
= strstr(me
, searchfor
);
211 // find chars of a set.
213 String::posAny( const char* string
) const
216 const char *s
= (const char *)data
;
217 if ( len() && string
)
219 const char* p
= strpbrk( s
, string
);
227 String::left( int n
) const
237 retval
.data
.trunc(n
);
244 String::right( int n
) const
253 const char *src
= (const char *)data
+ len() - n
;
261 String::nomid( const int pos
, const int n
) const
270 return String( String( left( pos
- 1 ) ) + right( len() - pos
- n
+ 1 ));
275 String::mid( int pos
, int n
) const
280 // pos 1 == data->string[ 0 ];
281 // pos 0 allowed for convenience
282 if ( !len() || ( pos
< 0 ) || ( pos
> len() ) && ( n
< 1 ) )
285 retval
= ((const char *) data
) + pos
-1;
286 if (n
> retval
.len())
288 retval
.data
.trunc(n
);
297 char *s
= data
.array_for_modify();
304 String
String::lower()
306 char *s
= data
.array_for_modify();
311 String::String (double f
, const char *fmt
)
313 /* worst case would be printing HUGE (or 1/HUGE), which is approx
314 2e318, this number would have approx 318 zero's in its string.
316 1024 is a safe length for the buffer
321 sprintf(buf
, "%f", f
);
328 String::value() const
332 int conv
= sscanf(data
, "%ld", &l
);
339 String::fvalue() const
343 int conv
= sscanf(data
, "%lf", &d
);
350 String
quoteString( String msg
, String quote
)
352 return msg
+ " `" + quote
+ "' ";
356 char *strrev(char *s
)
360 char *q
= s
+ strlen(s
) - 1;
372 String::reversed() const
375 char *s
= retval
.data
.array_for_modify();