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

📄 xstring.cpp

📁 经典的string 函数库学习资料
💻 CPP
📖 第 1 页 / 共 5 页
字号:
        size_type size = this->size();        size_type end_size = end.size();        xstring str;        str.reserve((size / chunklen + 1) * end_size + size);   // 保留足够的内存        for (size_type i = 0; i < size; ++i)        {            str += (*this)[i];            if (i % chunklen == chunklen - 1)            {                str += end;            }        }        return str;    }//----------------------------------------------------------------------------    // 抄来的    // http://rtp1.slowblink.com/~libh/dox/html/Dirname_8cc-source.html    /**    给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名。如果文    件名是以suffix结束的,那这一部分也会被去掉。    @code  * xstring path = "/home/httpd/html/index.html";  * xstring file;  * file = path.basename();         // file is set to "index.html"  * file = path.basename(".html");  // file is set to "index"    @endcode    @see dirname()     */    xstring xstring::basename(const xstring& suffix) const    {        // neo patch begin 2003-10-10        size_type size   = this->size();        char* const name = new char[size + 1];        memset(name, 0, size + 1);        this->copy(name, size);        // neo patch end 2003-10-10        char* p = name;        /*        * (1) If string is // it is implementation defined whether steps (2)        *       through (5) are skipped or processed.        *        * (2) If string consists entirely of slash characters, string shall        *       be set to a single slash character.  In this case, skip steps        *       (3) through (5).        */        for (;; ++p)        {            if (!*p)            {                if (p > name)                {                    delete[] name;  // neo patch 2003-10-10                    return "/";                }                else                {                    delete[] name;  // neo patch 2003-10-10                    return "";                }            }            if (*p != '/')            {                break;            }        }        /*        * (3) If there are any trailing slash characters in string, they        *       shall be removed.        */        for (; *p; ++p)        {            continue;        }        while (*--p == '/')        {            continue;        }        *++p = '\0';        /*        * (4) If there are any slash characters remaining in string, the        *       prefix of string up to an including the last slash character        *       in string shall be removed.        */        while (--p >= name)        {            if (*p == '/')            {                break;            }        }        ++p;        /*        * (5) If the suffix operand is present, is not identical to the        *       characters remaining in string, and is identical to a suffix        *       of the characters remaining in string, the suffix suffix        *       shall be removed from string.        */        if (suffix.length() > 0)        {            const int suffixlen = suffix.length();            const int stringlen = strlen(p);            if (suffixlen < stringlen)            {                const int off = stringlen - suffixlen;                if (!strcmp(p + off, suffix.c_str()))                {                    p[off] = '\0';                }            }        }        try        {            xstring s(p);            delete[] name;            return s;        }        catch (...)        {            delete[] name;            throw;        }    }   // xstring::basename()    // 抄来的    // http://rtp1.slowblink.com/~libh/dox/html/Dirname_8cc-source.html    /**    给出一个包含有指向一个文件的全路径的字符串,本函数返回去掉文件名后的目录名。    @code  * xstring path = "/etc/passwd";  * xstring file = path.dirname();  // file is set to "/etc"    @endcode    @see basename()     */    xstring xstring::dirname(void) const    {        // neo patch begin 2003-10-10        size_type size   = this->size();        char* const name = new char[size + 1];        memset(name, 0, size + 1);        this->copy(name, size);        // neo patch end 2003-10-10        char* p = name;        /*        * (1) If string is //, skip steps (2) through (5).        * (2) If string consists entirely of slash characters, string        *       shall be set to a single slash character.  In this case,        *       skip steps (3) through (8).        */        for (;; ++p)        {            if (!*p)            {                if (p > name)                {                    delete[] name;  // neo patch 2003-10-10                    return "/";                }                else                {                    delete[] name;  // neo patch 2003-10-10                    return ".";                }            }            if (*p != '/')            {                break;            }        }        /*        * (3) If there are any trailing slash characters in string, they        *       shall be removed.        */        for (; *p; ++p);        while (*--p == '/')        {            continue;        }        *++p = '\0';        /*        * (4) If there are no slash characters remaining in string,        *       string shall be set to a single period character.  In this        *       case skip steps (5) through (8).        *        * (5) If there are any trailing nonslash characters in string,        *       they shall be removed.        */        while (--p >= name)        {            if (*p == '/')            {                break;            }        }        ++p;        if (p == name)        {            delete[] name;  // neo patch 2003-10-10            return ".";        }        /*        * (6) If the remaining string is //, it is implementation defined        *       whether steps (7) and (8) are skipped or processed.        *        * This case has already been handled, as part of steps (1) and (2).        */        /*        * (7) If there are any trailing slash characters in string, they        *       shall be removed.        */        while (--p >= name)        {            if (*p != '/')            {                break;            }        }        ++p;        /*        * (8) If the remaining string is empty, string shall be set to        *       a single slash character.        */        *p = '\0';        if(p == name)        {            delete[] name;  // neo patch 2003-10-10            return "/";        }        try        {            xstring s(name);            delete[] name;            return s;        }        catch (...)        {            delete[] name;            throw;        }    }   // xstring::dirname()//----------------------------------------------------------------------------    /**    @return 字符串的十六进制数字表示的ASCII字符串。    转换是按字节进行的,并且高位在前。     */    xstring xstring::bin2hex(void) const    {        static char hex_data[] = "0123456789abcdef";        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)        {            str += hex_data[(*pos >> 4) & 0x0F];            str += hex_data[*pos & 0x0F];        }        return str;    }    /**    @return ROT13编码过的字符串。    ROT13编码方式只是简单的把字母表中的每一个字符移动13个位置,不在字母表中的    字符不被改变。编码和解码都以同一个函数完成。    @code  * xstring str = "xstring 1.0";  * cout << str.str_rot13();    // 输出 kfgevat 1.0    @endcode     */    xstring xstring::str_rot13(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 (('A' <= *pos && *pos <= 'M') ||                ('a' <= *pos && *pos <= 'm'))            {               str += static_cast<char>((*pos) + 13);            }            else if (('N' <= *pos && *pos <= 'Z') ||                     ('n' <= *pos && *pos <= 'z'))            {               str += static_cast<char>((*pos) - 13);            }            else            {               str += *pos;            }        }        return str;    }   // xstring::str_rot13()    /**    @return 反转过的字符串    @code  * xstring str = "Hello world!";  * cout << str.strrev();    // 输出 !dlrow olleH    @endcode     */    xstring xstring::strrev(void) const    {        return xstring(this->rbegin(), this->rend());    }//----------------------------------------------------------------------------    /**    @param start    子字符串的起始位置    @param length   子字符串的最大长度    @return 由start和length参数指定的部分字符串。    如果start是非负的,返回的字符串的起始位置是原始字符串中第start个字符,这个    位置是从0开始计算的。    @code  * xstring str("abcdef");  * xstring rest;  * rest = str.safesubstr(1);       // 返回 "bcdef"  * rest = str.safesubstr(1, 3);    // 返回 "bcd"  * rest = str.safesubstr(0, 4);    // 返回 "abcd"  * rest = str.safesubstr(0, 8);    // 返回 "abcdef"    @endcode    如果start是负的,返回的字符串的起始位置是原始字符串中从右侧数的第start个字    符。    @code  * rest = str.safesubstr(-1);      // 返回 "f"  * rest = str.safesubstr(-2);      // 返回 "ef"  * rest = str.safesubstr(-3, 1);   // 返回 "d"    @endcode    如果给出了length并且是正的,返回的字符串将包含由start位置开始的最多length    个字符。如果给出了length并且是负的,那末原始中最后的length个字符将被忽略。    如果start指定的位置在在这个位置之后,那么将返回空字符串    @code  * rest = str.safesubstr(0, -1);   // 返回 "abcde"  * rest = str.safesubstr(2, -1);   // 返回 "cde"  * rest = str.safesubstr(4, -4);   // 返回 ""  * rest = str.safesubstr(-3, -1);  // 返回 "de"    @endcode    */    xstring xstring::safesubstr(        long long int start,        long long int length    ) const // throw()    {        long long int size = this->size();        if (start < -size)        {            start = 0;        }        else if (start < 0)        {            start += size;        }        else if (start > size)        {            start = size;        }        if (length < start - size)        {            length = 0;        }        else if (length < 0)        {            length += size - start;        }        else if (length > size - start)        {            length = size - start;        }        return xstring(*this, start, length);    }   // xstring::safesubstr()//----------------------------------------------------------------------------    /**    @return xstring的vector,vector中的每一个元素是被separator分开的原始字符串    中的子字符串。    如果设置了limit,返回的vector中将最多包含limit个元素,其中最后一个元素包含    原始字符串中其余的部分。    如果separator是一个空字符串(""), explode()将返回一个空的vector<xstring>    对象。    @code  * xstring pizza = "piece1 piece2 piece3 piece4 piece5 piece6";  * vector<xstring> pieces = pizza.explode(" ");  * cout << pieces[0];  // piece1  * cout << pieces[1];  // piece2  * xstring data = "foo:*:1023:1000::/home/foo:/bin/sh";

⌨️ 快捷键说明

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