-/****************************************************************************
- PROJECT: FlowerSoft C++ library
- FILE : string.cc
-
- Rehacked by HWN 3/nov/95
- removed String &
- introduced Class String_handle
---*/
-
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <ctype.h>
-#include <assert.h>
-//#include "globals.hh"
+/*
+ string.cc - implement String
+
+ (c) 1997--2006 Han-Wen Nienhuys & Jan Nieuwenhuizen
+*/
+
+#ifndef _GNU_SOURCE // we want memmem
+#define _GNU_SOURCE
+#endif
+
#include "string.hh"
-static char* strlwr( char* s )
-{
- char* p = s;
+#include <cstdlib>
+#include <cstdio>
+#include <cassert>
+#include <cstring>
- while( *p )
- {
- *p = tolower( *p ); /* a macro on some compilers */
- p++;
- }
- return s;
-}
+#include <iostream>
+using namespace std;
-static char* strupr( char* s )
-{
- char* p = s;
+#include "libc-extension.hh"
+#include "string-convert.hh"
- while( *p )
- {
- *p = toupper( *p ); /* a macro on some compilers */
- p++;
- }
- return s;
-}
+#ifdef STRING_DEBUG
+void *mymemmove (void *dest, void const *src, size_t n);
+#define memmove mymemmove
+#endif
// return array, alloced with new.
-char *
-String::copy_array() const
+Byte *
+String::get_copy_byte () const
{
- const char *src = data;
- char *dest = new char[data.len() + 1];
- strcpy(dest, src);
- return dest;
+ Byte const *src = strh_.to_bytes ();
+ Byte *dest = new Byte[strh_.length () + 1];
+ memcpy (dest, src, strh_.length () + 1);
+ return dest;
}
-void
-String::printOn(ostream& os) const
+char *
+String::get_copy_str0 () const
{
- os << (const char*) data;
+ return (char *)get_copy_byte ();
}
-String::String (bool b)
+\f
+/*
+ copying, constructing.
+*/
+String &
+String::operator = (String const &source)
{
- *this = (const char *) (b ? "true" : "false");
-}
-String::String( const char* source )
-{
- assert(source);
- data = source;
+ strh_ = source.strh_;
+ return *this;
}
-void
-String::operator +=(String s)
+String::String (Byte const *byte, int len_i)
{
- *this += (const char *) s;
+ strh_.set (byte, len_i);
}
-int
-String::len() const
+/**
+ @see
+ String_convert::
+*/
+String
+to_string (char c, int n)
{
- return data.len();
+ return String_convert::char_string (c, n);
}
-String::String(char c, int n)
+String
+to_string (double f, char const *format)
{
- int l = n;
- assert(n >= 0 && n <= 80); // what the fuck is 80?
-//min(max( n, 0 ), 80);
- char s[81];
- memset(s, c, l);
- s[l] = 0;
- data = s;
+ return String_convert::double_string (f, format);
}
-String::String(int i)
+String
+to_string (int i, char const *format)
{
- char digits[ 81 ]; // who the FUCK is 80???
- digits[ 0 ] = '\0';
- sprintf(digits, "%d", i ); // assume radix 10
- data = digits;
+ return String_convert::int_string (i, format);
}
-String::String( const int i, const int n, const char c )
+String
+to_string (bool b)
{
- char fillChar = c;
- if ( fillChar)
- fillChar = '0';
-
- String v( i );
-
- data = String( fillChar, n - v.len() ) + String( v );
- // String convd to const char *
+ return String_convert::bool_string (b);
+}
+String
+to_string (long b)
+{
+ return String_convert::long_string (b);
}
- const char*
-String::ptr() const
+String
+to_string (char const *format, ...)
{
- return data;
+ va_list args;
+ va_start (args, format);
+ String str = String_convert::vform_string (format, args);
+ va_end (args);
+ return str;
}
+\f
+void
+String::append (String s)
+{
+ strh_.append (s.to_bytes (), s.length ());
+}
+void
+String::operator += (String s)
+{
+ append (s);
+}
+void
+String::prepend (String s)
+{
+ s += *this;
+ *this = s;
+}
-#ifdef CENTRAL_OBJECT // everything derived from Sortable object
-// comparisons.
int
-String::operator ==( const Sortable& test ) const
+String::length () const
{
- const String *s = (const String *) &test;
- return *this == *s;
+ return strh_.length ();
}
-int
-String::operator &&(const Object& test) const
+Byte const *
+String::to_bytes () const
{
- const String *s = (const String *) &test;
-
- int i = min( len(), s->len() );
- return ( i > 0 ) ?
- ( !strncmp( data, s->data, i ) ) : 0;
+ return strh_.to_bytes ();
}
-int
-String::operator >( const Sortable& test ) const
+Byte *
+String::get_bytes ()
{
- const String *s = (const String *) &test;
- return strcmp( data, s->data ) > 0;
+ return strh_.get_bytes ();
}
-#endif
-// signed comparison, analogous to strcmp;
-int
-String::compare( const char* test ) const
+char *
+String::get_str0 ()
{
- if (test == (const char *) data)
- return 0;
+ return strh_.get_str0 ();
+}
- return strcmp(data, test);
+bool
+String::is_empty () const
+{
+ return !length ();
}
+/**
+ Do a signed comparison, analogous to memcmp;
+*/
+int
+String::compare (String const &s1, String const &s2)
+{
+ Byte const *p1 = s1.to_bytes ();
+ Byte const *p2 = s2.to_bytes ();
+ if (p1 == p2)
+ return 0;
+ /*
+ 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::lastPos( const char c ) const
+String::index_last (char const c) const
{
- const char *me = data;
- int pos = 0;
- if ( len() )
- {
- const char* p = strrchr(me, c );
- if ( p )
- pos = p - me + 1;
- }
- return pos;
+ if (!length ())
+ return -1;
+
+ char const *me = strh_.to_str0 ();
+ char const *p = (char const *)memrchr ((Byte *)me, length (), c);
+ if (p)
+ return p - me;
+ return -1;
}
int
-String::lastPos( const char* string ) const
-{
- int pos = 0;
- int length = strlen( string );
- if ( len() && length )
- {
- int nextpos = this->pos( string );
- while( nextpos )
- {
- pos += nextpos;
- nextpos = right( len() - pos - length + 1 ).pos( string );
- }
- }
- return pos;
-}
-
-// find c
-// return 0 if not found.
-
-// ? should return len()?, as in string.left(pos(delimiter))
-int
-String::pos(char c ) const
-{
- const char *me = data;
- int pos = 0;
- if ( len() )
- {
- const char* p = strchr( me, c );
- if ( p )
- pos = p - me + 1;
- }
- return pos;
+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::pos( const char* searchfor ) const
+String::index (char c) const
{
- const char *me = data;
- int pos = 0;
- if ( len() && searchfor)
- {
- const char* p = strstr(me, searchfor);
- if ( p )
- pos = p - me + 1;
- }
- return pos;
+ 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::posAny( const char* string ) const
+String::index (String searchfor) const
{
- int pos = 0;
- const char *s = (const char *)data;
- if ( len() && string )
- {
- const char* p = strpbrk( s, string );
- if ( p )
- pos = p - s + 1;
- }
- return pos;
+ char const *me = strh_.to_str0 ();
+
+ char const *p
+ = (char const *) memmem (me, length (),
+ searchfor.to_str0 (), searchfor.length ());
+
+ if (p)
+ return p - me;
+
+ 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( int n ) const
+String::left_string (int n) const
{
- if (n >= len())
- return *this;
+ if (n >= length ())
+ return *this;
- String retval;
- if (n < 1)
- return retval;
-
- retval = *this;
- retval.data.trunc(n);
+ String retval;
+ if (n < 1)
return retval;
-}
+ retval = *this;
+ retval.strh_.trunc (n);
+ return retval;
+}
-// n rightmst chars
String
-String::right( int n ) const
-{
- if (n > len())
- return *this;
-
- String retval;
- if ( n < 1)
- return retval;
-
- const char *src = (const char *)data + len() - n;
- retval += src;
+String::right_string (int n) const
+{
+ if (n > length ())
+ return *this;
- return retval;
-}
+ if (n < 1)
+ return "";
+ return String (strh_.to_bytes () + length () - n, n);
+}
String
-String::nomid( const int pos, const int n ) const
+String::nomid_string (int index_i, int n) const
{
- String retval;
-
- if ( pos < 1 )
- return String("");
- if ( pos > len())
- return *this;
-
- return String( String( left( pos - 1 ) ) + right( len() - pos - n + 1 ));
-}
+ 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( int pos, int n ) const
+String::cut_string (int index_i, int n) const
{
- String retval;
+ if (index_i < 0)
+ {
+ n += index_i;
+ index_i = 0;
+ }
- // HWN. This SUX:
- // pos 1 == data->string[ 0 ];
- // pos 0 allowed for convenience
- if ( !len() || ( pos < 0 ) || ( pos > len() ) && ( n < 1 ) )
- return retval;
+ if (!length () || (index_i < 0) || (index_i >= length ()) || (n < 1))
+ return String ();
- retval = ((const char *) data) + pos -1;
- if (n > retval.len())
- n =retval.len();
- retval.data.trunc(n);
- return retval;
-}
+ if ((n > length ()) || (index_i + n > length ()))
+ n = length () - index_i;
+ return String (to_bytes () + index_i, n);
+}
+\f
-// to uppercase
-String
-String::upper()
+void
+String::to_upper ()
{
- char *s = data.array_for_modify();
- strupr(s );
- return *this;
+ strnupr (get_str0 (), length ());
}
-
-// to lowercase
-String String::lower()
+void
+String::to_lower ()
{
- char *s = data.array_for_modify();
- strlwr(s);
- return *this;
+ strnlwr (get_str0 (), length ());
}
-String::String (double f, const char *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 ());
}
-long
-String::value() const
+int
+String::to_int () const
{
- long l =0;
- if (len()) {
- int conv = sscanf(data, "%ld", &l);
- assert(conv);
- }
- return l;
+ return String_convert::dec2int (*this);
}
double
-String::fvalue() const
+String::to_double () const
{
- double d =0;
- if (len()) {
- int conv = sscanf(data, "%lf", &d);
- assert(conv);
- }
- return d;
+ return String_convert::dec2double (*this);
}
-
-String quoteString( String msg, String quote)
+#ifdef STREAM_SUPPORT
+ostream &
+operator << (ostream &os, String d)
{
- return msg + " `" + quote + "' ";
+ d.print_on (os);
+ return os;
}
-
-char *strrev(char *s)
+void
+String::print_on (ostream &os) const
{
- char c;
- char *p = s;
- char *q = s + strlen(s) - 1;
-
- while (q > p) {
- c = *p;
- *p++ = *q;
- *q-- = c;
- }
- return s;
+ 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() const
+String
+String::substitute (char find, char replace)
{
- String retval=*this;
- char *s = retval.data.array_for_modify();
- strrev(s);
- return retval;
-}
-bool
-String::to_bool() const
-{
- if (!len())
- return false;
- if (*this == "0")
- return false;
- String u (*this);
- u.upper();
- if (u== "FALSE")
- return false;
- return true;
+ for (int i = index (find); i > - 1; i = index (find))
+ (*this)[i] = replace;
+ return *this;
}