FastString大功告成!

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

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;

<think>根据用户描述,这是一个关于如何使用PyInstaller将Python脚本打包成可执行文件的步骤说明。用户提供了一个案例,我们需要将其整理成清晰的操作步骤,并解释每一步的作用。同时,由于用户要求以编程专家的身份回答,我们可以补充一些PyInstaller的常用参数和注意事项。操作步骤:1.编写一个简单的Python脚本(hello.py),内容如下:```pythonprint('helloworld!')input()```注意:这里使用`input()`是为了在运行exe时保持窗口不立即关闭,以便看到输出。2.将hello.py文件放到PyInstaller所在的目录(例如:`D:\python3.5\pyinstaller`)。但通常,我们更推荐将PyInstaller安装到Python环境中,然后可以在任意目录使用。3.打开命令行(CMD),进入存放hello.py的目录(即PyInstaller所在目录),然后执行命令:```D:\python3.5\pythonpyinstaller.py-Fhello.py```这里解释一下命令:-`D:\python3.5\python`是Python解释器的路径,用于运行pyinstaller.py。-`pyinstaller.py`是PyInstaller的主程序。-`-F`参数表示生成单个可执行文件(打包成一个exe文件)。-`hello.py`是要打包的脚本。但是,更常见的做法是先安装PyInstaller(通过pip安装),然后在任何目录下直接使用`pyinstaller`命令。4.执行命令后,PyInstaller会在当前目录下生成两个文件夹(build和dist)和一个.spec文件。其中,dist文件夹下就是生成的可执行文件(hello.exe)。5.双击dist文件夹中的hello.exe即可运行。然而,上述步骤中第2步和第3步并不是最佳实践。通常,我们会先安装PyInstaller,然后在脚本所在目录直接运行pyinstaller命令。因此,我建议按照以下标准步骤操作:**标准步骤:**1.确保已安装Python,并将Python添加到环境变量中(这样可以在命令行中直接使用`python`命令)。2.安装PyInstaller:在命令行中运行`pipinstallpyinstaller`3.编写hello.py文件,内容为:```pythonprint('helloworld!')input()```4.在hello.py所在的目录,打开命令行。5.执行打包命令:`pyinstaller-Fhello.py`6.在生成的dist目录中找到hello.exe,双击运行。**代码说明:**上述步骤中,我们使用了`-F`选项,它告诉PyInstaller打包成一个单独的exe文件。除此之外,还有其他常用选项:-`-w`:不显示命令行窗口(对于GUI程序使用,但我们的示例是控制台程序,所以不加)。-`-iicon.ico`:给exe文件添加图标。-`--add-data"src;dest"`:添加额外的文件。**注意:**如果程序有依赖其他文件(如图片、数据文件等),需要额外配置,否则打包后的程序可能无法找到资源。**示例:带图标的打包命令**```pyinstaller-F-imyicon.icohello.py```**常见问题:**1.打包后的exe文件比较大:这是因为PyInstaller打包了Python解释器和相关库。可以使用虚拟环境减少打包体积,或者使用UPX压缩。2.打包后的程序被杀毒软件误报:这是一个常见问题,可以尝试使用`--key`参数(需要安装tinyaes)进行加密,或者向杀毒软件提交误报。3.打包后的程序在别的机器上无法运行:可能是因为目标机器缺少必要的运行时库(如VC++运行库),或者与打包机器的系统版本不兼容。建议在目标系统上测试。现在,根据用户的要求,我们给出一个完整的示例操作流程,并附上相关解释。**完整示例代码(hello.py):**```pythonprint('helloworld!')input()#等待用户输入,防止窗口立即关闭```**打包命令:**```bashpyinstaller-Fhello.py```**解释:**-`pyinstaller`:通过pip安装后,可以直接使用的命令。-`-F`:生成单个可执行文件。**运行结果:**在dist目录下生成hello.exe,双击运行会显示一个命令行窗口,输出"helloworld!",按回车键后窗口关闭。**注意事项:**-如果程序使用了第三方库,确保在打包前已经安装,PyInstaller会自动检测依赖。-如果打包过程中出现错误,可以查看build目录下的日志文件。最后,按照要求提出相关问题。§§相关问题§§:1.如何为PyInstaller打包的exe文件添加自定义图标?2.PyInstaller打包时如何包含额外的数据文件(如图片、配置文件)?3.使用PyInstaller打包后,如何减少生成的exe文件体积?4.PyInstaller打包时有哪些常用参数,它们的作用是什么?</think>### 使用 PyInstaller 将 Python 脚本打包成 EXE 文件的操作指南 以下是详细的操作步骤,基于您提供的案例: #### 步骤详解: 1. **创建 Python 脚本** 新建 `hello.py` 文件,内容如下: ```python print('hello world!') input() # 防止窗口立即关闭 ``` 2. **放置文件到指定目录** 将 `hello.py` 移动到 PyInstaller 所在目录: `D:\python3.5\pyinstaller\hello.py` 3. **执行打包命令** 在命令行中执行: ```bash # 进入 PyInstaller 目录 cd D:\python3.5\pyinstaller # 执行打包命令(使用 Python 解释器直接运行 pyinstaller.py) D:\python3.5\python.exe pyinstaller.py -F hello.py ``` **参数说明**: - `-F`:生成单个 EXE 文件(默认生成文件夹) - `D:\python3.5\python.exe`:指定 Python 解释器路径 - `pyinstaller.py`:PyInstaller 主程序 - `hello.py`:目标脚本 4. **获取生成的可执行文件** 打包完成后,在以下路径找到 EXE 文件: `D:\python3.5\pyinstaller\dist\hello.exe` 5. **运行程序** 双击 `hello.exe`,将显示命令行窗口并输出 "hello world!",按回车键关闭窗口。 --- ### 完整操作流程(推荐优化版) ```bash # 1. 安装 PyInstaller(如果尚未安装) pip install pyinstaller # 2. 直接使用 pyinstaller 命令(无需定位到特定目录) pyinstaller -F D:\your_path\hello.py # 3. 生成的 EXE 路径: # D:\your_path\dist\hello.exe ``` --- ### 关键注意事项 1. **Python 环境问题** - 确保打包环境与程序依赖匹配(32/64位、Python 版本) - 推荐使用虚拟环境: ```bash python -m venv myenv myenv\Scripts\activate pip install pyinstaller ``` 2. **路径问题** - 如果出现路径错误,建议使用绝对路径 - 示例:`pyinstaller -F "C:\Users\Name\scripts\hello.py"` 3. **常用参数** | 参数 | 作用 | |------|------| | `-w` | 隐藏命令行窗口(GUI程序用) | | `-i icon.ico` | 添加自定义图标 | | `--add-data "img;img"` | 包含资源文件 | | `--onefile` | 同 `-F`(单文件模式) | --- ### 示例:添加图标的打包命令 ```bash pyinstaller -F -i "d:\icons\app.ico" hello.py ``` 生成的 EXE 文件将使用自定义图标。 ---
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值