⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xstring.cpp

📁 经典的string 函数库学习资料
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//----------------------------------------------------------------------------    /**    @return 一个新字符串,在数据库查询中需要转义的字符串前面增加反斜线。    这些字符是单引号(')、双引号(")、反斜线(@\)、和NUL(空子节)。    @code  * xstring str = "Is your name O'reilly?";  * cout << str.addslashes();   // 输出 Is your name O\'reilly?    @endcode    @see stripslashes() htmlspecialchars()     */    xstring xstring::addslashes(void) const    {        size_type size = this->size();        xstring str;        str.reserve(size * 2);  // 保留足够的内存        const_iterator  pos;        const_iterator  end = this->end();        for (pos = this->begin(); pos < end; ++pos)        {            switch (*pos)            {            case '\0' : // 0x00                str += "\\0";                continue;            case '\"' : // 0x22                str += "\\\"";                continue;            case '\'' : // 0x27                str += "\\'";                continue;            case '\\' : // 0x5c                str += "\\\\";                continue;            default :                str += *pos;                continue;            }        }        return str;    }   // xstring::addslashes()    /**    @return 去掉反斜线的字符串。    @\'变成',两个反斜线(@\@\)变成一个反斜线(@\)。    @code  * xstring str = "Is your name O\'reilly?";  * cout << str.stripslashes(); // 输出 Is your name O'reilly?    @endcode    @see addslashes()     */    xstring xstring::stripslashes(void) const    {        size_type size = this->size();        xstring str;        str.reserve(size);  // 保留足够的内存        const_iterator  pos;        const_iterator  end = this->end();        for (pos = this->begin(); pos < end; ++pos)        {            if (*pos == '\\' && pos + 1 < end)            {                switch (*(pos + 1))                {                case '0' :                    ++pos;                    str += '\0';                    continue;                case '"' :                case '\'' :                case '\\' :                    ++pos;                    break;                }            }            str += *pos;        }        return str;    }   // xstring::stripslashes()//----------------------------------------------------------------------------    /**    @param search   寻找的字符串    @param replace  替换的字符串    @param count    如果指定了这个参数,那么最多只进行count次替换    @return 返回字符串自身的引用    @code  * xstring bodytag = "<body text='%body%'>";  * bodytag.str_replace("%body%", "black");  * cout << bodytag;    // 输出 <body text='black'>  * xstring phrase  = "You should eat fruits, vegetables, and fiber every day.";  * phrase.str_replace("fruits", "pizza").str_replace("vegetables", "beer").str_replace("fiber", "ice cream");  * cout << phrase;     // 输出 You should eat pizza, beer, and ice cream every day.    @endcode    @note 该函数适用于二进制对象!    @see str_ireplace()     */    xstring& xstring::str_replace(        const xstring&  search,        const xstring&  replace,        size_type       count    )    {        xstring str;        size_type size = this->size();        size_type search_size  = search.size();        size_type idx;        size_type replace_count = 0;        const char* data = this->data();        const char* search_data = search.data();        for (idx = 0; idx < size; ++idx)        {            if (idx + search_size <= size &&                strncmp(data + idx, search_data, search_size) == 0)            {                str += replace;                idx += search_size - 1;                if (++replace_count == count)   // 计算替换次数                {                    ++idx;                    str.append(data + idx, size - idx);                    break;                }            }            else            {                str += *(data + idx);            }        }        this->swap(str);        return *this;    }   // xstring::str_replace()    /**    @param search   寻找的字符串,不区分大小写    @param replace  替换的字符串    @param count    如果指定了这个参数,那么最多只进行count次替换    @return 返回字符串自身的引用    @code  * xstring bodytag = "<body text='%BODY%'>";  * bodytag.str_replace("%body%", "black");  * cout << bodytag;    // 输出 <body text='black'>    @endcode    @see str_replace()     */    xstring& xstring::str_ireplace(        const xstring&  search,        const xstring&  replace,        size_type       count    )    {        xstring str;        size_type size = this->size();        size_type search_size  = search.size();        size_type idx;        size_type replace_count = 0;        const char* data = this->data();        const char* search_data = search.data();        for (idx = 0; idx < size; ++idx)        {            if (idx + search_size <= size &&                strncasecmp(data + idx, search_data, search_size) == 0)            {                str += replace;                idx += search_size - 1;                if (++replace_count == count)   // 计算替换次数                {                    ++idx;                    str.append(data + idx, size - idx);                    break;                }            }            else            {                str += *(data + idx);            }        }        this->swap(str);        return *this;    }   // xstring::str_ireplace()//----------------------------------------------------------------------------    /**    这个函数用来防止用户输入的文本中包含的HTML代码弄乱页面,在论坛或者留言版系    统中,这很有用。可选的参数quote_style用来说明如何转换单引号和双引号。    在默认模式@link x::QUOTE_STYLE ENT_COMPAT @endlink时,只转换双引号而不转换单引号。    如果设置@link x::QUOTE_STYLE ENT_QUOTES @endlink,单引号和双引号都被转换。    如果设置@link x::QUOTE_STYLE ENT_NOQUOTES @endlink,单引号和双引号都不被转换。    进行下面这些转换:    @li “@&”(ampersand)变成“@&amp;”    @li “"”(双引号)变成“@&quot;” 当ENT_NOQUOTES未设置时    @li “'”(单引号)变成“@&#039;” 仅当ENT_QUOTES设置时    @li “@<”(小于号)变成“@&lt;”    @li “@>”(大于号)变成“@&gt;”    @code  * xstring str = "<a href='test'>Test</a>";  * cout << str.htmlspecialchars(ENT_QUOTES);  * // 输出 &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;    @endcode    @see nl2br()     */    xstring xstring::htmlspecialchars(QUOTE_STYLE quote_style) const    {        size_type size = this->size();        xstring str;        str.reserve(size * 3);  // 保留适当数量的内存,仍有可能发生内存搬移        const_iterator  pos;        const_iterator  end = this->end();        for (pos = this->begin(); pos < end; ++pos)        {            switch (*pos)            {            case '&' :                str += "&amp;";                continue;            case '<' :                str += "&lt;";                continue;            case '>' :                str += "&gt;";                continue;            case '\"' :                switch (quote_style)                {                case ENT_COMPAT :                case ENT_QUOTES :                    str += "&quot;";                    continue;                default : ; // 什么也不做                }                break;            case '\'' :                switch (quote_style)                {                case ENT_QUOTES :                    str += "&#039;";                    continue;                default : ; // 什么也不做                }                break;            }            str += *pos;        }        return str;    }   // xstring::htmlspecialchars()    /**    返回一个新字符串,在原始字符串的每个换行字符前面插入"<br />"    @code  * xstring str = "foo isn't\n bar";  * cout << str.nl2br();    @endcode    将会输出:    @code  * foo isn't<br />  *  bar    @endcode    @see htmlspecialchars()     */    xstring xstring::nl2br(void) const    {        size_type size = this->size();        xstring str;        str.reserve(size * 2);  // 保留适当数量的内存,仍有可能发生内存搬移        const_iterator  pos;        const_iterator  end = this->end();        for (pos = this->begin(); pos < end; ++pos)        {            switch (*pos)            {            case '\r' : // 0x13                // 处理 \r\n 换行                if (pos + 1 < end && *(pos + 1) == '\n')                {                    ++pos;                }                // 故意没有 break;            case '\n' : // 0x10                str += "<br />\n";                continue;            }            str += *pos;        }        return str;    }   // xstring::nl2br()//----------------------------------------------------------------------------    /**    这个函数去掉原始字符串开始处的空白字符,返回这个新字符串的拷贝。    ltrim()会去掉下面这些字符:    @li “ ”(ASCII 32 0x20),普通的空格    @li “@\t”(ASCII 9 0x09),TAB    @li “@\n”(ASCII 10 0x0A),新的一行(换行)    @li “@\r”(ASCII 13 0x0D),回车字符    @li “@\0”(ASCII 0 0x00),NULL字符    @li “0x0B”(ASCII 11 0x0B),垂直的TAB    @code  * xstring text = "\t\tThese are a few words :) ...  ";  * xstring trimmed = text.ltrim();  * // trimmed = "These are a few words :) ...  ";    @endcode    @see rtrim() trim()     */    xstring xstring::ltrim(void) const    {        const_iterator  pos;        const_iterator  end = this->end();        for (pos = this->begin(); pos < end; ++pos)        {            switch (*pos)            {            case ' ' :  // 0x20            case '\t' : // 0x09            case '\n' : // 0x10            case '\r' : // 0x13            case '\0' : // 0x00            case 0x0b : // 0x0b                continue;            }            return xstring(pos, end);        }        return "";    }    /**    这个函数去掉原始字符串结尾处的空白字符,返回这个新字符串的拷贝。    rtrim()会去掉下面这些字符:    @li “ ”(ASCII 32 0x20),普通的空格    @li “@\t”(ASCII 9 0x09),TAB    @li “@\n”(ASCII 10 0x0A),新的一行(换行)    @li “@\r”(ASCII 13 0x0D),回车字符    @li “@\0”(ASCII 0 0x00),NULL字符    @li “0x0B”(ASCII 11 0x0B),垂直的TAB    @code  * xstring text = "\t\tThese are a few words :) ...  ";  * xstring trimmed = text.rtrim();  * // trimmed = "\t\tThese are a few words :) ...";    @endcode    @see ltrim() trim()     */    xstring xstring::rtrim(void) const    {        const_iterator  pos;        const_iterator  begin = this->begin();        for (pos = this->end(); pos > begin; --pos)        {            switch (*(pos - 1))            {            case ' ' :  // 0x20            case '\t' : // 0x09            case '\n' : // 0x10            case '\r' : // 0x13            case '\0' : // 0x00            case 0x0b : // 0x0b                continue;            }            return xstring(begin, pos);        }        return "";    }    /**    这个函数去掉原始字符串开始处的和结尾处的空白字符。    @return 新字符串的拷贝。    trim()会去掉下面这些字符:    @li “ ”(ASCII 32 0x20),普通的空格    @li “@\t”(ASCII 9 0x09),TAB    @li “@\n”(ASCII 10 0x0A),新的一行(换行)    @li “@\r”(ASCII 13 0x0D),回车字符    @li “@\0”(ASCII 0 0x00),NULL字符    @li “0x0B”(ASCII 11 0x0B),垂直的TAB    @code  * xstring text = "\t\tThese are a few words :) ...  ";  * xstring trimmed = text.trim();  * // trimmed = "These are a few words :) ...";    @endcode    @see ltrim() rtrim()     */    xstring xstring::trim(void) const    {        return (*this).ltrim().rtrim();    }//----------------------------------------------------------------------------    /**    @return 返回一个新的字符串,其中所有的非字母数字字符,除了-_.之外,都会被    替换成一个百分号(%)跟着两个十六进制数字。空格被编码成加号(+)。    这个编码方式和从WWW表单提交数据时的编码方式是一样的。

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -