diff options
-rw-r--r-- | Ivy/DataTypes.h | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/Ivy/DataTypes.h b/Ivy/DataTypes.h index ea17c37..190a7a6 100644 --- a/Ivy/DataTypes.h +++ b/Ivy/DataTypes.h @@ -16,14 +16,15 @@ namespace ivy class string {
public:
+ static const size_t npos = size_t(-1);
string(size_t len = 0)
{ ptr = allocBuffer( len ); };
string ( const string& s )
{ ptr = 0; copy( s.ptr, s.size ); }
- string( const char * s, int len = -1 )
+ string( const char * s, size_t len = npos )
{
ptr = 0;
- if ( len < 0 )
+ if ( len == npos )
len = strlen(s) ;
copy( s, len );
}
@@ -34,17 +35,17 @@ public: { return size; }
bool empty() const
{ return size==0; }
- void copy( const char * s, int len )
+ void copy( const char * s, size_t len )
{
if ( ptr ) delete [] ptr;
ptr = allocBuffer( len );
strncpy( ptr, s, len );
}
- void erase(int start=0, int len = -1)
+ void erase(size_t start=0, size_t len = npos)
{
char *newptr;
size_t real_len;
- real_len = len < 0 ? size - start : len;
+ real_len = len == npos ? size - start : len;
if ( real_len > size ) real_len = size;
newptr = allocBuffer( size - real_len );
strncpy( newptr, ptr , start );
@@ -52,14 +53,14 @@ public: delete ptr;
ptr = newptr;
}
- void append( const char *s , int len = -1 )
+ void append( const char *s , size_t len = npos )
{
insert ( size, s , len );
}
- void insert(int index, const char * s, int len = -1)
+ void insert(size_t index, const char * s, size_t len = npos)
{
char *newptr;
- if ( len < 0 ) len = strlen(s) ;
+ if ( len == npos ) len = strlen(s) ;
newptr = allocBuffer( size + len );
strncpy( newptr , ptr, index );
strncpy( newptr+index , s, len ) ;
@@ -67,12 +68,12 @@ public: delete ptr;
ptr = newptr;
}
- string substr( int start, int len = -1 )
+ string substr( size_t start, size_t len = npos )
{ return string ( &ptr[start], len ); }
- int find_first_of( const char* strCharSet )
- { char *fnd = strpbrk( ptr, strCharSet ); if ( fnd ) return fnd - ptr; else return -1; }
- int rfind( char c )
- { char *fnd = strrchr( ptr, c); if ( fnd ) return fnd - ptr; else return -1; }
+ size_t find_first_of( const char* strCharSet )
+ { char *fnd = strpbrk( ptr, strCharSet ); if ( fnd ) return (size_t)(fnd - ptr); else return npos; }
+ size_t rfind( char c )
+ { char *fnd = strrchr( ptr, c); if ( fnd ) return (size_t)(fnd - ptr); else return npos; }
// friend string operator + (const string& left, const char* right);
// friend string operator + (const char* left, const string& right);
friend string operator + (const string& left, const string& right)
@@ -114,7 +115,7 @@ private: char *ptr;
size_t size;
// always allocate one extra character for '\0' termination
- char *allocBuffer(int len)
+ char *allocBuffer(size_t len)
{
char *newptr;
size = len;
@@ -295,7 +296,7 @@ public: void push_back( const T& value )
{
T* newdata = new T[len+1];
- for ( unsigned int i = 0; i < len ; i ++ )
+ for ( size_t i = 0; i < len ; i ++ )
newdata[i] = data[i];
newdata[len++] = value;
if ( data ) delete [] data;
|