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 * () cons

FastString类比string快30%,小数据处理快3倍以上,提供丰富字符串处理方法。主要用于性能要求高、一行数据处理的场合,如服务器协议处理等。文中给出了该类的详细代码实现。
2149

被折叠的 条评论
为什么被折叠?



