FastString大功告成!

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

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

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

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值