FastString大功告成!

原创 2004年06月18日 10:32:00

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;

FastStrings

  • 2012年03月13日 17:52
  • 37KB
  • 下载

【Android】各种小知识点,不间断更新

提高应用启动速度splash页面瞬间响应App启动时闪屏页的白屏或者黑屏现象(取决于你所选取的Theme),无论怎么优化Application和启动页的onCreate都无法消除,毕竟解析界面是需要一...
  • gitzzp
  • gitzzp
  • 2016年06月15日 19:37
  • 779

java慎用String.substring(int start, int end)

1:问题的抛出         今天在安卓项目中使用后台线程操作一个大文件,分块读取文件中的所有内容,每次操作加载一个小块进行解析,解析到指定的文本内容之后会加载并常驻内存中,即使所有我解析到的文本内...
  • u011421480
  • u011421480
  • 2014年02月11日 12:11
  • 2066

FastString大功告成!

FastString类大功告成,比string快30%,小数据处理快3倍以上,提供丰富的字符串处理方法。 主要用于一些性能要求较高的应用,大部分是一行数据处理的场合,如服务器协议处理、邮件协议处理和邮...
  • javenstudio
  • javenstudio
  • 2004年06月18日 10:32
  • 2347

PostgreSQL substring()函数

substring(string [from int] [for int])text抽取子字串 substring('Thomas' from 2 for 3)homsubstring(string ...
  • stail111
  • stail111
  • 2010年06月22日 10:39
  • 10623

Splay大功告成

写的时候很纠结,磕磕绊绊的,断断续续的搞了好几天,其实前几天就写完了的,不过没a过题,正确性没经过历史的检验。今天无聊在杭电找了道Splay的入门题a了下,情况竟然出奇的顺利,除了因为内存泄露超内存了...
  • SwordHoly
  • SwordHoly
  • 2010年09月07日 13:13
  • 700

生日礼物-大功告成

制作这张贺卡让我找回了久违的快乐,那种在很小的时候常常感觉到的创造的快乐,希望kitty猫和坏小子能把快乐带到妹妹身边,永远不要退色:)正面:信封,从正面的洞里漏出来:展开之后:打开贺卡之后看到的信封...
  • zhangxuecheng1985
  • zhangxuecheng1985
  • 2008年09月17日 01:13
  • 124

MySQL Document quick search

Optimize table http://dev.mysql.com/doc/refman/5.7/en/optimize-table.html Flush Tables http://nili...
  • pandoraliu
  • pandoraliu
  • 2013年10月29日 17:42
  • 472

fastboot命令分析

====================================================================== [cpp] view plaincopyprint?...
  • zzobin
  • zzobin
  • 2012年06月25日 09:17
  • 6332

怀孕计划大功告成

六月份计划,七月份实施,八月份怀孕计划大功告成~~ 好孕~~ 机会真是垂青于有准备的人。确实如此。此计划耗费巨大的脑力跟心智,才短短的两个月备孕时间,感觉如此漫长和辛劳,尤其是检查排卵期,每天都要...
  • linlin_juejue
  • linlin_juejue
  • 2011年08月18日 08:20
  • 264
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:FastString大功告成!
举报原因:
原因补充:

(最多只允许输入30个字)