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

📄 xstring.cpp

📁 经典的string 函数库学习资料
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/** * @file xstring.cpp * @brief 字符串的包装,函数的实现 * @author 泥偶 * @since  2003-08-29 * @date   2004-08-28 */extern "C"{    #include <sys/types.h>    #include <regex.h>}#include <algorithm>#include <cctype>#include <cstring>#include "ScopeGuard.hpp"#include "xstring.hpp"namespace x{//----------------------------------------------------------------------------    /**    @code  * xstring str = "Mary Had A Little Lamb and She LOVED It So";  * str = str.toLowerCase();  * cout << str;    // 输出 mary had a little lamb and she loved it so    @endcode    @see toUpperCase()     */    xstring xstring::toLowerCase(void) const    {        return xstring::toFoo<int(*)(int)>(&std::tolower);    }    /**    @code  * xstring str = "Mary Had A Little Lamb and She LOVED It So";  * str = str.toUpperCase();  * cout << str;    // 输出 MARY HAD A LITTLE LAMB AND SHE LOVED IT SO    @endcode    @see toLowerCase()     */    xstring xstring::toUpperCase(void) const    {        return xstring::toFoo<int(*)(int)>(&std::toupper);    }//----------------------------------------------------------------------------    // 判断字符串是否只包括数字或字母    /**    @see isAlpha() isAscii() isBlank() isCntrl() isDigit() isGraph() isLower() isPrint() isPunct() isSpace() isUpper() isXdigit()     */    bool xstring::isAlnum(void) const    {        return this->isFoo<int(*)(int)>(&std::isalnum);    }    // 判断字符串是否只包括字母    /**    @see isAlnum() isAscii() isBlank() isCntrl() isDigit() isGraph() isLower() isPrint() isPunct() isSpace() isUpper() isXdigit()     */    bool xstring::isAlpha(void) const    {        return this->isFoo<int(*)(int)>(&std::isalpha);    }    // 判断字符串是否只包括ASCII字符    /**    @see isAlnum() isAlpha() isBlank() isCntrl() isDigit() isGraph() isLower() isPrint() isPunct() isSpace() isUpper() isXdigit()     */    bool xstring::isAscii(void) const    {        return this->isFoo<int(*)(int)>(&::isascii);    }    // 判断字符串是否只包括空白字符    /**    @see isAlnum() isAlpha() isAscii() isCntrl() isDigit() isGraph() isLower() isPrint() isPunct() isSpace() isUpper() isXdigit()     */    bool xstring::isBlank(void) const    {        const_iterator  pos;        const_iterator  end = this->end();        for (pos = this->begin(); pos < end; ++pos)        {            if (*pos != ' ' && *pos != '\t')            {                return false;            }        }        return true;    }    // 判断字符串是否只包括控制字符    /**    @see isAlnum() isAlpha() isAscii() isBlank() isDigit() isGraph() isLower() isPrint() isPunct() isSpace() isUpper() isXdigit()     */    bool xstring::isCntrl(void) const    {        return this->isFoo<int(*)(int)>(&std::iscntrl);    }    // 判断字符串是否只包括数字    /**    @see isAlnum() isAlpha() isAscii() isBlank() isCntrl() isGraph() isLower()    isPrint() isPunct() isSpace() isUpper() isXdigit()     */    bool xstring::isDigit(void) const    {        return this->isFoo<int(*)(int)>(&std::isdigit);    }    // 判断字符串是否只包括可打印字符,不包括空格    /**    @see isAlnum() isAlpha() isAscii() isBlank() isCntrl() isDigit() isLower()    isPrint() isPunct() isSpace() isUpper() isXdigit()     */    bool xstring::isGraph(void) const    {        return this->isFoo<int(*)(int)>(&std::isgraph);    }    // 判断字符串是否只包括小写字母    /**    @see isAlnum() isAlpha() isAscii() isBlank() isCntrl() isDigit() isGraph()    isPrint() isPunct() isSpace() isUpper() isXdigit()     */    bool xstring::isLower(void) const    {        return this->isFoo<int(*)(int)>(&std::islower);    }    // 判断字符串是否只包括可打印字符,包括空格    /**    @see isAlnum() isAlpha() isAscii() isBlank() isCntrl() isDigit() isGraph()    isLower() isPunct() isSpace() isUpper() isXdigit()     */    bool xstring::isPrint(void) const    {        return this->isFoo<int(*)(int)>(&std::isprint);    }    // 判断字符串是否只包括除了空格和数字、字母之外的可打印字符    /**    @see isAlnum() isAlpha() isAscii() isBlank() isCntrl() isDigit() isGraph()    isLower() isPrint() isSpace() isUpper() isXdigit()     */    bool xstring::isPunct(void) const    {        return this->isFoo<int(*)(int)>(&std::ispunct);    }    // 判断字符串是否只包括空白字符    /**    @see isAlnum() isAlpha() isAscii() isBlank() isCntrl() isDigit() isGraph()    isLower() isPrint() isPunct() isUpper() isXdigit()     */    bool xstring::isSpace(void) const    {        return this->isFoo<int(*)(int)>(&std::isspace);    }    // 判断字符串是否只包括大写字母    /**    @see isAlnum() isAlpha() isAscii() isBlank() isCntrl() isDigit() isGraph()    isLower() isPrint() isPunct() isSpace() isXdigit()     */    bool xstring::isUpper(void) const    {        return this->isFoo<int(*)(int)>(&std::isupper);    }    // 判断字符串是否只包括16进制数字    /**    @see isAlnum() isAlpha() isAscii() isBlank() isCntrl() isDigit() isGraph()    isLower() isPrint() isPunct() isSpace() isUpper()     */    bool xstring::isXdigit(void) const    {        return this->isFoo<int(*)(int)>(&std::isxdigit);    }//----------------------------------------------------------------------------// JavaScript手册上面的函数// HTML方法    xstring xstring::anchor(const xstring& name) const    {        return "<a name=\"" + name + "\">" + *this + "</a>";    }    xstring xstring::big(void) const    {        return "<big>" + *this + "</big>";    }    xstring xstring::blink(void) const    {        return "<blink>" + *this + "</blink>";    }    xstring xstring::bold(void) const    {        return "<b>" + *this + "</b>";    }    xstring xstring::fixed(void) const    {        return "<tt>" + *this + "</tt>";    }    xstring xstring::fontcolor(const xstring& color) const    {        return "<font color=\"" + color + "\">" + *this + "</font>";    }    xstring xstring::fontsize(unsigned int size) const    {        return "<font size=\"" + xstring(size) + "\">" + *this + "</font>";    }    xstring xstring::italic(void) const    {        return "<i>" + *this + "</i>";    }    xstring xstring::link(const xstring& url) const    {        return "<a href=\"" + url + "\">" + *this + "</a>";    }    xstring xstring::small(void) const    {        return "<small>" + *this + "</small>";    }    xstring xstring::strike(void) const    {        return "<strike>" + *this + "</strike>";    }    xstring xstring::sub(void) const    {        return "<sub>" + *this + "</sub>";    }    xstring xstring::sup(void) const    {        return "<sup>" + *this + "</sup>";    }    // 泥偶增加的    xstring xstring::heading(unsigned int level) const    {        return "<h" + xstring(level) + ">" + *this            + "</h" + xstring(level) + ">";    }    xstring xstring::underline(void) const    {        return "<u>" + *this + "</u>";    }    xstring xstring::span(const xstring& style) const    {        return "<span style=\"" + style + "\">" + *this + "</span>";    }//----------------------------------------------------------------------------    /**    @retval 0 表示两端字符串相等    @retval <0 表示*this小于str(按字典次序)    @retval >0 表示*this大于str(按字典次序)    以strncasecmp()为比较准则。    @see std::string::compare() std::comparison()     */    int xstring::compareIgnoreCase(const xstring& str) const    {        size_type size  = this->size();        size_type osize = str.size();        size_type len   = std::min(size, osize);        int r = strncasecmp(this->data(), str.data(), len);        if (!r)        {            r = size - osize;        }        return r;    }    /**    比较动作和 compareIgnoreCase(str) 相同。     */    int xstring::compareIgnoreCase(        size_type       pos,        size_type       n,        const xstring&  str    ) const    {        size_type size  = this->size();        size_type osize = str.size();        if (pos > size)        {            /// @exception std::out_of_range 如果pos > size(),抛出异常            throw std::out_of_range("xstring::compareIgnoreCase");        }        size_type rsize = std::min(size - pos, n);        size_type len   = std::min(rsize, osize);        int r = strncasecmp(this->data() + pos, str.data(), len);        if (!r)        {            r = rsize - osize;        }        return r;    }    /**    @see compareIgnoreCase(const xstring& str)     */    int xstring::compareIgnoreCase(        size_type       pos1,        size_type       n1,        const xstring&  str,        size_type       pos2,        size_type       n2    ) const    {        size_type size  = this->size();        size_type osize = str.size();        if (pos1 > size || pos2 > osize)        {            /// @exception std::out_of_range 如果pos1 > size(),抛出异常            /// @exception std::out_of_range 如果pos2 > str.size(),抛出异常            throw std::out_of_range("xstring::compareIgnoreCase");        }        size_type rsize = std::min(size - pos1, n1);        size_type rosize = std::min(osize - pos2, n2);        size_type len   = std::min(rsize, rosize);        int r = strncasecmp(this->data() + pos1, str.data() + pos2, len);        if (!r)        {            r = rsize - rosize;        }        return r;    }    /**    @see compareIgnoreCase(const xstring& str)     */    int xstring::compareIgnoreCase(const char* s) const    {        size_type size  = this->size();        size_type osize = strlen(s);        size_type len   = std::min(size, osize);        int r = strncasecmp(this->data(), s, len);        if (!r)        {            r = size - osize;        }        return r;    }    /**    @see compareIgnoreCase(const xstring& str)     */    int xstring::compareIgnoreCase(        size_type   pos,        size_type   n1,        const char* s    ) const    {        size_type size = this->size();        if (pos > size)        {            /// @exception std::out_of_range 如果pos > size(),抛出异常            throw std::out_of_range("xstring::compareIgnoreCase");        }        size_type osize = strlen(s);        size_type rsize = std::min(size - pos, n1);        size_type len   = std::min(rsize, osize);        int r = strncasecmp(this->data() + pos, s, len);        if (!r)        {            r = rsize - osize;        }        return r;    }    /**    @see compareIgnoreCase(const xstring& str)     */    int xstring::compareIgnoreCase(        size_type   pos,        size_type   n1,        const char* s,        size_type   n2    ) const    {        size_type size = this->size();        if (pos > size)        {            /// @exception std::out_of_range 如果pos > size(),抛出异常            throw std::out_of_range("xstring::compareIgnoreCase");        }        size_type osize = std::min(strlen(s), n2);        size_type rsize = std::min(size - pos, n1);        size_type len   = std::min(rsize, osize);        int r = strncasecmp(this->data() + pos, s, len);        if (!r)        {            r = rsize - osize;        }        return r;    }

⌨️ 快捷键说明

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