From 59cea3a2c49618ec7351028334d468a9204f4027 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 12 May 2006 05:28:28 +0000 Subject: Added the StaticString class, which is a slightly modified version of the old sstring class, who's name is nonsense and now contains code specific to squirrelmud. Anyway, StaticString is more lightweight than the std::string class, but much less versitile. It really is just like a static string, but all nice and classey. Great for use with the HashTable. --- src/staticstring.cpp | 184 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/staticstring.h | 48 ++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 src/staticstring.cpp create mode 100644 src/staticstring.h (limited to 'src') diff --git a/src/staticstring.cpp b/src/staticstring.cpp new file mode 100644 index 0000000..7543178 --- /dev/null +++ b/src/staticstring.cpp @@ -0,0 +1,184 @@ +#include "staticstring.h" + +#include +#include +#include + +StaticString::StaticString() +{ + lpStr = NULL; + nLen = 0; +} + +StaticString::StaticString( int nLength ) +{ + lpStr = new char[nLength+1]; + nLen = nLength; + memset( lpStr, 0, nLength+1 ); +} + +StaticString::StaticString( const char *lpNewStr, int nNewLen ) +{ + lpStr = NULL; + nLen = 0; + setString( lpNewStr, nNewLen ); +} + +StaticString::StaticString( StaticString &xSrcStr, int nNewLen ) +{ + lpStr = NULL; + nLen = 0; + setString( xSrcStr, nNewLen ); +} + +StaticString::~StaticString() +{ + if( lpStr != NULL ) delete[] lpStr; +} + +char *StaticString::getString() +{ + return lpStr; +} + +int StaticString::getLength() +{ + return nLen; +} + +int StaticString::setLength( int nNewLength ) +{ + char *lpNewStr = new char[nNewLength+1]; + if( lpStr != NULL ) + { + strncpy( lpNewStr, lpStr, nNewLength ); + } + lpNewStr[nNewLength] = '\0'; + if( lpStr ) + { + delete[] lpStr; + } + lpStr = lpNewStr; + nLen = nNewLength; +} + +void StaticString::setString( const char *lpNewStr, int nNewLen ) +{ + if( lpStr ) + { + delete[] lpStr; + lpStr = NULL; + nLen = 0; + } + if( nNewLen < 0 ) + { + if( lpNewStr == NULL ) return; + nLen = strlen( lpNewStr ); + lpStr = new char[nLen+1]; + strcpy( lpStr, lpNewStr ); + } + else + { + nLen = nNewLen; + lpStr = new char[nLen+1]; + memset( lpStr, 0, nLen+1 ); + if( lpNewStr ) + strncpy( lpStr, lpNewStr, nNewLen ); + } +} + +void StaticString::setString( StaticString &sNewStr, int nNewLen ) +{ + if( lpStr ) + { + delete[] lpStr; + lpStr = NULL; + nLen = 0; + } + if( nNewLen < 0 ) + { + if( sNewStr.lpStr == NULL ) return; + nLen = sNewStr.nLen; + lpStr = new char[nLen+1]; + strcpy( lpStr, sNewStr.lpStr ); + } + else + { + nLen = nNewLen; + lpStr = new char[nLen+1]; + memset( lpStr, 0, nLen+1 ); + if( sNewStr.lpStr ) + strncpy( lpStr, sNewStr.lpStr, nNewLen ); + } +} + +StaticString &StaticString::operator=( StaticString &lpOtherStr ) +{ + setString( lpOtherStr ); + + return *this; +} + +StaticString &StaticString::operator=( std::string &lpOtherStr ) +{ + setString( lpOtherStr.c_str() ); + + return *this; +} + +StaticString &StaticString::operator=( const char *lpNewStr ) +{ + setString( lpNewStr ); + + return *this; +} + +StaticString::operator const char *() +{ + return lpStr; +} + +char StaticString::getAt( int nIndex ) +{ + if( nIndex < 0 || nIndex >= nLen ) + return '\0'; + + return lpStr[nIndex]; +} + +void StaticString::setAt( int nIndex, char cVal ) +{ + if( nIndex < 0 || nIndex >= nLen ) + return; + + lpStr[nIndex] = cVal; +} + +char &StaticString::operator[]( int nIndex ) +{ + if( nIndex < 0 || nIndex >= nLen ) + return lpStr[0]; + + return lpStr[nIndex]; +} + +StaticString::operator int() +{ + return nLen; +} + +char *StaticString::operator+( int nAmnt ) +{ + return lpStr + nAmnt; +} + +char *StaticString::operator-( int nAmnt ) +{ + return lpStr - nAmnt; +} + +void StaticString::clear() +{ + memset( lpStr, 0, nLen+1 ); +} + diff --git a/src/staticstring.h b/src/staticstring.h new file mode 100644 index 0000000..88cae70 --- /dev/null +++ b/src/staticstring.h @@ -0,0 +1,48 @@ +#ifndef STATIC_STRING_H +#define STATIC_STRING_H + +#include + +/** + * Simple string managing class. Allows for dynamically allocated string data + * along with some minor caching to speed things up when accessing your + * string data. Really designed for making copies of strings easy and safe, + * and making accessing meta-info like length fast and reliable as well. + *@author Mike Buland + */ +class StaticString +{ +public: + StaticString(); + StaticString( const char *lpNewStr, int nNewLen=-1 ); + StaticString( StaticString &xSrcStr, int nNewLen=-1 ); + StaticString( int nLength ); + ~StaticString(); + + char *getString(); + int getLength(); + int setLength( int nNewLength ); + + void setString( const char *lpNewStr, int nNewLen=-1 ); + void setString( StaticString &sNewStr, int nNewLen=-1 ); + + char getAt( int nIndex ); + void setAt( int nIndex, char cVal ); + + class StaticString &operator=( class StaticString &lpOtherStr ); + class StaticString &operator=( std::string &lpOtherStr ); + class StaticString &operator=( const char *lpNewStr ); + operator const char *(); + char &operator[]( int nIndex ); + operator int(); + char *operator+( int nAmnt ); + char *operator-( int nAmnt ); + + void clear(); + +private: + char *lpStr; + int nLen; +}; + +#endif -- cgit v1.2.3