FastString类大功告成,比string快30%,小数据处理快3倍以上,提供丰富的字符串处理方法。
主要用于一些性能要求较高的应用,大部分是一行数据处理的场合,如服务器协议处理、邮件协议处理和邮件解析处理等。
/** * @class FastString * * @brief This class provides a wrapper facade for C strings. * * This class uses an included engine to allocate memory, * that stored one line string buffer in chars arrary and bigger * string in malloc buffer. * NOTE: if an instance of this class is constructed from or * assigned an empty string (with first element of '/0'), then it * is not allocated new space. Instead, its internal * representation is set equal to a global empty string. * CAUTION: in cases when FastString is constructed from a * provided buffer with the release parameter set to 0, * FastString is not guaranteed to be '/0' terminated. * * @author Naven */
template <typename CHAR, size_t BUFSIZE = 256> class FastString_Base{protected: CHAR m_psBufLine[BUFSIZE]; CHAR *m_psBuf; CHAR *m_psRep; size_t m_nLen; size_t m_nBufLen;
private: void init(); void allocate(const size_t size, int reallocate = 0);
public: FastString_Base<CHAR, BUFSIZE>(); FastString_Base<CHAR, BUFSIZE>(const FastString_Base<CHAR, BUFSIZE> &s); FastString_Base<CHAR, BUFSIZE>(const CHAR *str); FastString_Base<CHAR, BUFSIZE>(const CHAR *str, const size_t len); FastString_Base<CHAR, BUFSIZE>(const size_t len, const CHAR c); ~FastString_Base<CHAR, BUFSIZE>();
void set(const CHAR *str, size_t len); FastString_Base<CHAR, BUFSIZE> substring(const size_t offset, size_t length = -1) const; void substring(FastString_Base<CHAR, BUFSIZE> &s, const size_t offset, size_t length = -1) const; FastString_Base<CHAR, BUFSIZE>& append(const CHAR *str, size_t len); int startsWith(const CHAR *prefix, size_t len = 0, size_t offset = 0, int ignorecase = 0) const; int equals(const CHAR *str, size_t len = 0, int ignorecase = 0) const; FastString_Base<CHAR, BUFSIZE>& toLowerCase(); FastString_Base<CHAR, BUFSIZE>& toUpperCase(); FastString_Base<CHAR, BUFSIZE>& replace(const FastString_Base<CHAR, BUFSIZE> &name, const FastString_Base<CHAR, BUFSIZE> &value, const int ignorecase = 0); FastString_Base<CHAR, BUFSIZE>& replaces(FastString_Base<CHAR, BUFSIZE> *pNames, FastString_Base<CHAR, BUFSIZE> *pValues, int count, const int ignorecase = 0); CHAR *detach(); CHAR *clone(); void resize(size_t len, CHAR c); void resize(size_t len); void release(); void clear();
size_t length() const; size_t size() const; size_t capacity() const; const CHAR *c_str() const; CHAR *rep() const; //CHAR operator [] (size_t i) const; CHAR charAt(const size_t i) const; operator CHAR * () const; operator const CHAR * () const; operator void * () const; operator const void * () const; FastString_Base<CHAR, BUFSIZE>& replaceIgnoreCase(const FastString_Base<CHAR, BUFSIZE> &name, const FastString_Base<CHAR, BUFSIZE> &value); FastString_Base<CHAR, BUFSIZE>& replacesIgnoreCase(FastString_Base<CHAR, BUFSIZE> *pNames, FastString_Base<CHAR, BUFSIZE> *pValues, int count); FastString_Base<CHAR, BUFSIZE> substr(const size_t offset, size_t length = -1) const; FastString_Base<CHAR, BUFSIZE> &operator = (const FastString_Base<CHAR, BUFSIZE> &s); FastString_Base<CHAR, BUFSIZE> &operator = (const CHAR *str); FastString_Base<CHAR, BUFSIZE>& append(const FastString_Base<CHAR, BUFSIZE> &s); FastString_Base<CHAR, BUFSIZE>& append(const CHAR *str); FastString_Base<CHAR, BUFSIZE>& append(const CHAR c); FastString_Base<CHAR, BUFSIZE>& append(const int n); FastString_Base<CHAR, BUFSIZE>& append(const unsigned int n); FastString_Base<CHAR, BUFSIZE>& append(const float f); int endsWith(const CHAR *suffix, size_t len = 0) const; int endsWithIgnoreCase(const CHAR *suffix, size_t len = 0) const; int startsWithIgnoreCase(const CHAR *prefix, size_t len = 0, size_t offset = 0) const; int equalsIgnoreCase(const CHAR *str, size_t len = 0) const; int equals(const FastString_Base<CHAR, BUFSIZE> &s) const; int equalsIgnoreCase(const FastString_Base<CHAR, BUFSIZE> &s) const; int getline(int pos, FastString_Base<CHAR, BUFSIZE> &s, const CHAR *sep = NULL); int empty() const;
FastString_Base<CHAR, BUFSIZE>& trimRight(size_t offset); FastString_Base<CHAR, BUFSIZE>& trimLeft(size_t offset); FastString_Base<CHAR, BUFSIZE>& removeChar(const CHAR c); FastString_Base<CHAR, BUFSIZE>& removeChars(const CHAR *chrs); FastString_Base<CHAR, BUFSIZE>& replaceChars(const CHAR *chrs, const CHAR chr); FastString_Base<CHAR, BUFSIZE>& rtrimChars(const CHAR *chrs); FastString_Base<CHAR, BUFSIZE>& ltrimChars(const CHAR *chrs); FastString_Base<CHAR, BUFSIZE>& trimChars(const CHAR *chrs); FastString_Base<CHAR, BUFSIZE>& trimRight(); FastString_Base<CHAR, BUFSIZE>& trimLeft(); FastString_Base<CHAR, BUFSIZE>& rtrim(); FastString_Base<CHAR, BUFSIZE>& ltrim(); FastString_Base<CHAR, BUFSIZE>& trim(); size_t extractValueByName( const CHAR *substr, const CHAR *div1, const CHAR *div2, FastString_Base<CHAR, BUFSIZE> &value, const CHAR *vdiv, const int count = 1); size_t extractIntegerByName( const CHAR *substr, long *value, const long defval = 0); size_t extractValueByName( const CHAR *substr, FastString_Base<CHAR, BUFSIZE> &value, const int count = 1); size_t extractValueByIndex( FastString_Base<CHAR, BUFSIZE> &value, const CHAR tag, const int index); int indexOf( CHAR target[], size_t targetCount, size_t fromIndex, const int ignorecase = 0); int indexOf( CHAR target[], size_t fromIndex = 0); int indexOfIgnoreCase( CHAR target[], size_t targetCount, size_t fromIndex); int indexOfIgnoreCase( CHAR target[], size_t fromIndex = 0); int lastIndexOf(CHAR target[], size_t targetCount, size_t fromIndex, const int ignorecase = 0); int lastIndexOf(CHAR target[], size_t fromIndex = 0); int lastIndexOfIgnoreCase( CHAR target[], size_t targetCount, size_t fromIndex); int lastIndexOfIgnoreCase( CHAR target[], size_t fromIndex = 0);
public: static CHAR* removechars(CHAR *s, const CHAR *chrs); static CHAR* replacechars(CHAR *s, const CHAR *chrs, const CHAR chr); static CHAR* rtrimchars(CHAR *s, const CHAR *chrs); static CHAR* ltrimchars(CHAR *s, const CHAR *chrs); static CHAR* trimchars(CHAR *s, const CHAR *chrs); static CHAR* rtrim(CHAR *s); static CHAR* ltrim(CHAR *s); static CHAR* trim(CHAR *s); static CHAR* nextline(CHAR *sbuf, const int size, CHAR **pptr, CHAR **ppchr, CHAR *chr); static size_t extractValueByName( const CHAR *str, const CHAR *substr, const CHAR *div1, const CHAR *div2, CHAR *buf, const size_t size, const CHAR *vdiv, const int count = 1); static size_t extractIntegerByName( const CHAR *str, const CHAR *substr, long *value, const long defval = 0); static size_t extractValueByName( const CHAR *str, const CHAR *substr, CHAR *value, const size_t size, const int count=1); static size_t extractValueByIndex( const CHAR *pstr, const size_t len, CHAR *value, const size_t valsize, const CHAR tag, const int index); static int findIndexOf( CHAR source[], size_t sourceCount, CHAR target[], size_t targetCount, size_t fromIndex = 0, const int ignorecase = 0); static int findIndexOf( CHAR source[], CHAR target[], size_t fromIndex = 0); static int findIndexOfIgnoreCase( CHAR source[], size_t sourceCount, CHAR target[], size_t targetCount, size_t fromIndex = 0); static int findIndexOfIgnoreCase( CHAR source[], CHAR target[], size_t fromIndex = 0); static int findLastIndexOf( CHAR source[], size_t sourceCount, CHAR target[], size_t targetCount, size_t fromIndex = 0, const int ignorecase = 0); static int findLastIndexOf( CHAR source[], CHAR target[], size_t fromIndex = 0); static int findLastIndexOfIgnoreCase( CHAR source[], size_t sourceCount, CHAR target[], size_t targetCount, size_t fromIndex = 0); static int findLastIndexOfIgnoreCase( CHAR source[], CHAR target[], size_t fromIndex = 0); };
typedef class FastString_Base<char, 256> FastString; typedef class FastString_Base<char, 65> ShortString;
typedef class FastArray_Base<FastString> FastStringArray; typedef class FastArray_Base<ShortString> ShortStringArray;
typedef class FastArray_Iterator<FastString> FastStringArrayIterator; typedef class FastArray_Iterator<ShortString> ShortStringArrayIterator;