/*
- PROJECT: FlowerSoft C++ library
- FILE : string.cc
+ string.cc - implement String
- Rehacked by HWN 3/nov/95
- removed String &
- introduced class String_handle
- */
+ (c) 1997--2006 Han-Wen Nienhuys & Jan Nieuwenhuizen
+*/
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <ctype.h>
-#include <assert.h>
+#ifndef _GNU_SOURCE // we want memmem
+#define _GNU_SOURCE
+#endif
#include "string.hh"
+#include <cstdlib>
+#include <cstdio>
+#include <cassert>
+#include <cstring>
+
+#include <iostream>
+using namespace std;
+
+#include "libc-extension.hh"
+#include "string-convert.hh"
+
#ifdef STRING_DEBUG
-void* mymemmove( void* dest, void const* src, size_t n );
+void *mymemmove (void *dest, void const *src, size_t n);
#define memmove mymemmove
#endif
-static char*
-strlwr( char* s )
+// return array, alloced with new.
+Byte *
+String::get_copy_byte () const
{
- char* p = s;
-
- while( *p ) {
- *p = tolower( *p ); /* a macro on some compilers */
- p++;
- }
- return s;
+ Byte const *src = strh_.to_bytes ();
+ Byte *dest = new Byte[strh_.length () + 1];
+ memcpy (dest, src, strh_.length () + 1);
+ return dest;
}
-static char*
-strupr( char* s )
+char *
+String::get_copy_str0 () const
{
- char* p = s;
-
- while( *p ) {
- *p = toupper( *p ); /* a macro on some compilers */
- p++;
- }
- return s;
+ return (char *)get_copy_byte ();
}
-String::String(Rational r)
+\f
+/*
+ copying, constructing.
+*/
+String &
+String::operator = (String const &source)
{
- char * n = Itoa(r.numerator()); // LEAK????
-
- *this = n;
- if (r.denominator() != 1) {
- char * d = Itoa(r.denominator());
- *this += String( '/' ) + String(d);
- //delete d;
- }
-/* delete n;
- */
+ strh_ = source.strh_;
+ return *this;
}
-// return array, alloced with new.
-Byte*
-String::copy_byte_p() const
+String::String (Byte const *byte, int len_i)
{
- Byte const* src = strh_.byte_c_l();
- Byte* dest = new Byte[strh_.length_i() + 1];
- memmove( dest, src, strh_.length_i() + 1 );
- return dest;
+ strh_.set (byte, len_i);
}
-void
-String::print_on(ostream& os) const
+/**
+ @see
+ String_convert::
+*/
+String
+to_string (char c, int n)
{
- if ( length_i() == strlen( ch_c_l() ) )
- os << ch_c_l();
- else
- for ( int i = 0; i < length_i(); i++ )
- os << (Byte)(*this)[ i ];
+ return String_convert::char_string (c, n);
}
-String::String (bool b)
+String
+to_string (double f, char const *format)
{
- *this = (char const* ) (b ? "true" : "false");
-}
-String::String( char const* source )
-{
- assert(source);
- strh_ = source;
+ return String_convert::double_string (f, format);
}
-String::String( Byte const* byte_l, int length_i )
-{
-// assert( !length_i || byte_l );// ugh. Storing null pointers?
- strh_.set( byte_l, length_i );
+String
+to_string (int i, char const *format)
+{
+ return String_convert::int_string (i, format);
}
-void
-String::operator +=(String s)
+String
+to_string (bool b)
{
- strh_.append( s.byte_c_l(), s.length_i() );
+ return String_convert::bool_string (b);
}
-
-int
-String::length_i() const
+String
+to_string (long b)
{
- return strh_.length_i();
+ return String_convert::long_string (b);
}
-// will go away, fixed anyway
-String::String( char c, int n )
+String
+to_string (char const *format, ...)
{
- n = n >= 0 ? n : 0;
- char* ch_p = new char[ n ];
- memset( ch_p, c, n );
- strh_.set( (Byte*)ch_p, n );
- delete ch_p;
+ va_list args;
+ va_start (args, format);
+ String str = String_convert::vform_string (format, args);
+ va_end (args);
+ return str;
}
-String::String(int i)
+\f
+void
+String::append (String s)
{
- char digits[ 81 ]; // who the fuck is 80???
- digits[ 0 ] = '\0';
- sprintf(digits, "%d", i ); // assume radix 10
- strh_ = digits;
+ strh_.append (s.to_bytes (), s.length ());
}
-
-String::String( const int i, const int n, char const c )
+void
+String::operator += (String s)
{
- char fill_ch = c;
- if ( fill_ch)
- fill_ch = '0';
-
- String v( i );
-
- String str = String( fill_ch, n - v.length_i() ) + String( v );
- strh_.set( str.byte_c_l(), str.length_i() );
+ append (s);
}
-Byte const*
-String::byte_c_l() const
+void
+String::prepend (String s)
{
- return strh_.byte_c_l();
+ s += *this;
+ *this = s;
}
-char const*
-String::ch_c_l() const
+int
+String::length () const
{
- return strh_.ch_c_l();
+ return strh_.length ();
}
-Byte*
-String::byte_l()
+Byte const *
+String::to_bytes () const
{
- return strh_.byte_l();
+ return strh_.to_bytes ();
}
-char*
-String::ch_l()
+Byte *
+String::get_bytes ()
{
- return strh_.ch_l();
+ return strh_.get_bytes ();
}
-// signed comparison, analogous to memcmp;
-int
-String::compare_i(String const& s1, String const& s2 )
+char *
+String::get_str0 ()
{
- Byte const* p1 = s1.byte_c_l();
- Byte const* p2 = s2.byte_c_l();
- if ( p1 == p2 )
- return 0;
-
- int i1 = s1.length_i();
- int i2 = s2.length_i();
- int i = i1 <? i2;
-
- int result= memcmp( p1, p2, i );
- return result ? result : i1-i2;
+ return strh_.get_str0 ();
}
-
+bool
+String::is_empty () const
+{
+ return !length ();
+}
+/**
+ Do a signed comparison, analogous to memcmp;
+*/
int
-String::index_last_i( char const c ) const
+String::compare (String const &s1, String const &s2)
{
- // not binary safe
- assert( length_i() == strlen( ch_c_l() ) );
- if ( !length_i() )
- return -1;
+ Byte const *p1 = s1.to_bytes ();
+ Byte const *p2 = s2.to_bytes ();
+ if (p1 == p2)
+ return 0;
- char const* me = strh_.ch_c_l();
- char const* p = strrchr(me, c );
- if ( p )
- return p - me;
- return -1;
+ /*
+ don't forget the terminating '\0'
+ */
+ int f = min (s1.length (), s2.length ());
+ int cmp_length = 1+ f;
+ int i = memcmp (p1, p2, cmp_length);
+ return i;
}
+\f
int
-String::index_last_i( char const* string ) const
+String::index_last (char const c) const
{
- // not binary safe
- assert( length_i() == strlen( ch_c_l() ) );
+ if (!length ())
+ return -1;
- int length = strlen( string );
- if ( !length_i() || !length )
- return -1;
-
- int next_i = index_i( string );
- if ( next_i == -1 )
- return -1;
-
- int index_i = 0;
- while( next_i >= 0 ) {
- index_i += next_i;
- next_i = right_str( length_i() - index_i - length ).index_i( string );
- }
- return index_i;
+ char const *me = strh_.to_str0 ();
+ char const *p = (char const *)memrchr ((Byte *)me, length (), c);
+ if (p)
+ return p - me;
+ return -1;
}
-// find c
-// return -1 if not found.
-
-// ? should return length_i()?, as in string.left_str(index_i(delimiter))
int
-String::index_i(char c ) const
-{
- // not binary safe
- assert( length_i() == strlen( ch_c_l() ) );
- if ( !length_i() )
- return -1;
-
- char const* me = strh_.ch_c_l();
- char const* p = strchr( me, c );
- if ( p )
- return p - me;
- return -1;
+String::index_last (char const *string) const // UGK!
+{
+ assert (false); // broken
+ int len = strlen (string); // ugrh
+ if (!length () || !len)
+ return -1;
+
+ int next_i = index (string);
+ if (next_i == -1)
+ return -1;
+
+ int index_i = 0;
+ while (next_i >= 0)
+ {
+ index_i += next_i;
+ next_i = right_string (length () - index_i - len).index (string);
+ }
+ return index_i;
}
-// find searchfor. (what if this == "" && searchfor == "") ???
+/** find a character.
+
+@return
+the index of the leftmost character #c# (0 <= return < length ()),
+or -1 if not found.
+
+? should return length ()?, as in string.left_string (index (delimiter))
+*/
int
-String::index_i( char const* searchfor ) const
+String::index (char c) const
{
- // not binary safe
- assert( length_i() == strlen( ch_c_l() ) );
- if ( !length_i() || !searchfor )
- return -1;
-
- char const* me = strh_.ch_c_l();
- char const* p = strstr(me, searchfor);
- if ( p )
- return p - me;
- return -1;
+ char const *me = strh_.to_str0 ();
+ char const *p = (char const *) memchr (me, c, length ());
+ if (p)
+ return p - me;
+ return -1;
}
-// find chars of a set.
+/**
+ find a substring.
+
+ @return
+ index of leftmost occurrence of #searchfor#
+*/
int
-String::index_any_i( char const* string ) const
+String::index (String searchfor) const
{
- // not binary safe
- assert( length_i() == strlen( ch_c_l() ) );
+ char const *me = strh_.to_str0 ();
+
+ char const *p
+ = (char const *) memmem (me, length (),
+ searchfor.to_str0 (), searchfor.length ());
+
+ if (p)
+ return p - me;
- if ( !length_i() || !string )
- return -1;
-
- char const* s = (char const* )strh_.ch_c_l();
- char const* p = strpbrk( s, string );
- if ( p )
- return p - s;
- return -1;
+ return -1;
}
+/** find chars of a set.
+
+@return
+
+the index of the leftmost occurance of an element of #set#. -1 if
+nothing is found.
+*/
+int
+String::index_any (String set) const
+{
+ int n = length ();
+ if (!n)
+ return -1;
+
+ void const *me = (void const *) strh_.to_str0 ();
+ for (int i = 0; i < set.length (); i++)
+ {
+ char *found = (char *) memchr (me, set[i], n);
+ if (found)
+ return found - (char const *)me;
+ }
+ return -1;
+}
+\f
String
-String::left_str( int n ) const
+String::left_string (int n) const
{
- if (n >= length_i())
- return *this;
+ if (n >= length ())
+ return *this;
- String retval;
- if (n < 1)
- return retval;
-
- retval = *this;
- retval.strh_.trunc(n);
+ String retval;
+ if (n < 1)
return retval;
-}
+ retval = *this;
+ retval.strh_.trunc (n);
+ return retval;
+}
-// n rightmst chars
String
-String::right_str( int n ) const
+String::right_string (int n) const
{
- if (n > length_i())
- return *this;
-
- if ( n < 1)
- String();
-
- return String( strh_.byte_c_l() + length_i() - n, n );
-}
+ if (n > length ())
+ return *this;
+
+ if (n < 1)
+ return "";
+ return String (strh_.to_bytes () + length () - n, n);
+}
String
-String::nomid_str( int index_i, int n ) const
+String::nomid_string (int index_i, int n) const
{
- if ( index_i < 0 )
- return String();
- if ( index_i >= length_i() )
- return *this;
-
- return String( String( left_str( index_i ) ) + right_str( length_i() - index_i - n ));
-}
+ if (index_i < 0)
+ {
+ n += index_i;
+ index_i = 0;
+ }
+ if (n <= 0)
+ return *this;
+ return left_string (index_i) + right_string (length () - index_i - n);
+}
String
-String::mid_str( int index_i, int n ) const
+String::cut_string (int index_i, int n) const
{
- if ( !length_i() || ( index_i < 0 ) || ( index_i >= length_i() ) || ( n < 1 ) )
- return String();
+ if (index_i < 0)
+ {
+ n += index_i;
+ index_i = 0;
+ }
- if ( ( n > length_i() ) || ( index_i + n > length_i() ) )
- n = length_i() - index_i;
+ if (!length () || (index_i < 0) || (index_i >= length ()) || (n < 1))
+ return String ();
- return String( byte_c_l() + index_i, n );
-}
+ if ((n > length ()) || (index_i + n > length ()))
+ n = length () - index_i;
+ return String (to_bytes () + index_i, n);
+}
+\f
-// return uppercase
-String
-String::upper_str() const
+void
+String::to_upper ()
{
- // not binary safe
- assert( length_i() == strlen( ch_c_l() ) );
- String str = *this;
- char *s = str.strh_.byte_l();
- strupr( s );
- return str;
+ strnupr (get_str0 (), length ());
}
-
-// return lowercase
-String
-String::lower_str() const
+void
+String::to_lower ()
{
- // not binary safe
- assert( length_i() == strlen( ch_c_l() ) );
- String str = *this;
- char* s = str.strh_.ch_l();
- strlwr(s);
- return str;
+ strnlwr (get_str0 (), length ());
}
-String::String (double f, char const* fmt)
+void
+String::reverse ()
{
- /* worst case would be printing HUGE (or 1/HUGE), which is approx
- 2e318, this number would have approx 318 zero's in its string.
-
- 1024 is a safe length for the buffer
- */
-
- char buf[1024];
- if (!fmt)
- sprintf(buf, "%f", f);
- else
- sprintf(buf, fmt,f);
- *this = buf;
+ memrev (get_bytes (), length ());
}
int
-String::value_i() const
+String::to_int () const
{
- return String_convert::dec2_i( *this );
+ return String_convert::dec2int (*this);
}
double
-String::value_f() const
+String::to_double () const
{
- return String_convert::dec2_f( *this );
+ return String_convert::dec2double (*this);
}
-
-#if 0
-String
-quoteString( String msg, String quote)
+#ifdef STREAM_SUPPORT
+ostream &
+operator << (ostream &os, String d)
{
- return msg + " `" + quote + "' ";
+ d.print_on (os);
+ return os;
}
-#endif // 0
-Byte*
-strrev( Byte* byte_l, int length_i )
+void
+String::print_on (ostream &os) const
{
- Byte byte;
- Byte* left_byte_l = byte_l;
- Byte* right_byte_l = byte_l + length_i;
-
- while ( right_byte_l > left_byte_l ) {
- byte = *left_byte_l;
- *left_byte_l++ = *right_byte_l;
- *right_byte_l-- = byte;
- }
- return byte_l;
+ if (!strh_.is_binary_bo ())
+ os << to_str0 ();
+ else
+ for (int i = 0; i < length (); i++)
+ os << (Byte) (*this)[ i ];
}
+#endif
+String
+String::substitute (String find, String replace)
+{
+ int n = find.length ();
+ int m = replace.length ();
+ for (int i = index (find), j = 0; i > -1;
+ i = right_string (length () - j).index (find))
+ {
+ *this = left_string (i + j)
+ + replace
+ + right_string (length () - j - i - n);
+ j += i + m;
+ }
+ return *this;
+}
-String
-String::reversed_str() const
+String
+String::substitute (char find, char replace)
{
- String str = *this;
- strrev( str.byte_l(), str.length_i() );
- return str;
+ for (int i = index (find); i > - 1; i = index (find))
+ (*this)[i] = replace;
+ return *this;
}