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

📄 test_xstring.cpp

📁 经典的string 函数库学习资料
💻 CPP
📖 第 1 页 / 共 3 页
字号:
        test(v == 1);        v = xstring("-1").to<double>();        test(v == -1);    }    {        cout << "to<string>(void)" << endl;        string str;        str = xstring("0").to<string>();        test(str == "0", str);        str = xstring("1").to<string>();        test(str == "1", str);        str = xstring("-1").to<string>();        test(str == "-1", str);    }    {        cout << "to<xstring>(void)" << endl;        xstring str;        str = xstring("0").to<xstring>();        test(str == "0", str);        str = xstring("1").to<xstring>();        test(str == "1", str);        str = xstring("-1").to<xstring>();        test(str == "-1", str);    }    {        xstring str("Hello World!");        xstring str2;        cout << "xstring toLowerCase(void)" << endl;        str2 = str.toLowerCase();        test(str2 == "hello world!", str2);        cout << "xstring toUpperCase(void)" << endl;        str2 = str.toUpperCase();        test(str2 == "HELLO WORLD!", str2);    }    {        xstring str;        bool result;        cout << "bool isAlnum(void)" << endl;        str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";        result = str.isAlnum();        test(result == true, str);        str = "\x80";        result = str.isAlnum();        test(result == false, str);        cout << "bool isAlpha(void)" << endl;        str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";        result = str.isAlpha();        test(result == true, str);        str = "!@#$%^&*()[]{}\\|;:'\",./<>?`~-=_+";        result = str.isAlpha();        test(result == false, str);        cout << "bool isAscii(void)" << endl;        str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\|;:'\",./<>?`~-=_+ ";        result = str.isAscii();        test(result == true, str);        str = "\x80";        result = str.isAscii();        test(result == false, str);        cout << "bool isBlank(void)" << endl;        str = " \t \t";        result = str.isBlank();        test(result == true, str);        str = "z";        result = str.isBlank();        test(result == false, str);        cout << "bool isCntrl(void)" << endl;        str = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x013\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";        result = str.isCntrl();        test(result == true, str);        str = "z";        result = str.isCntrl();        test(result == false, str);        cout << "bool isDigit(void)" << endl;        str = "1234567890";        result = str.isDigit();        test(result == true, str);        str = "z";        result = str.isDigit();        test(result == false, str);        cout << "bool isGraph(void)" << endl;        str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\|;:'\",./<>?`~-=_+";        result = str.isGraph();        test(result == true, str);        str = " ";        result = str.isGraph();        test(result == false, str);        cout << "bool isLower(void)" << endl;        str = "abcdefghijklmnopqrstuvwxyz";        result = str.isLower();        test(result == true, str);        str = "abcdefghijklmnopqrstuvwxyz1";        result = str.isLower();        test(result == false, str);        cout << "bool isPrint(void)" << endl;        str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\|;:'\",./<>?`~-=_+ ";        result = str.isPrint();        test(result == true, str);        str = "\x01";        result = str.isPrint();        test(result == false, str);        cout << "bool isPunct(void)" << endl;        str = "!@#$%^&*()[]{}\\|;:'\",./<>?`~-=_+";        result = str.isPunct();        test(result == true, str);        str = "!@#$%^&*()[]{}\\|;:'\",./<>?`~-=_+a";        result = str.isPunct();        test(result == false, str);        cout << "bool isSpace(void)" << endl;        str = " \f\n\r\t\v";        result = str.isSpace();        test(result == true, str);        str = " \f\n\r\t\vz";        result = str.isSpace();        test(result == false, str);        cout << "bool isUpper(void)" << endl;        str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";        result = str.isUpper();        test(result == true, str);        str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1";        result = str.isUpper();        test(result == false, str);        cout << "bool isXdigit(void)" << endl;        str = "1234567890abcdefABCDEF";        result = str.isXdigit();        test(result == true, str);        str = "1234567890abcdefABCDEFz";        result = str.isXdigit();        test(result == false, str);    }    {        xstring str("Hello World!");        xstring str2;        cout << "xstring anchor(const xstring& name)" << endl;        str2 = str.anchor("section1");        test(str2 == "<a name=\"section1\">Hello World!</a>", str2);        cout << "xstring big(void)" << endl;        str2 = str.big();        test(str2 == "<big>Hello World!</big>", str2);        cout << "xstring blink(void)" << endl;        str2 = str.blink();        test(str2 == "<blink>Hello World!</blink>", str2);        cout << "xstring bold(void)" << endl;        str2 = str.bold();        test(str2 == "<b>Hello World!</b>", str2);        cout << "xstring fixed(void)" << endl;        str2 = str.fixed();        test(str2 == "<tt>Hello World!</tt>", str2);        cout << "xstring fontcolor(const xstring& color)" << endl;        str2 = str.fontcolor("blue");        test(str2 == "<font color=\"blue\">Hello World!</font>", str2);        cout << "xstring fontsize(unsigned int size)" << endl;        str2 = str.fontsize(7);        test(str2 == "<font size=\"7\">Hello World!</font>", str2);        cout << "xstring italic(void)" << endl;        str2 = str.italic();        test(str2 == "<i>Hello World!</i>", str2);        cout << "xstring link(const xstring& url)" << endl;        str2 = str.link("#section1");        test(str2 == "<a href=\"#section1\">Hello World!</a>", str2);        cout << "xstring small(void)" << endl;        str2 = str.small();        test(str2 == "<small>Hello World!</small>", str2);        cout << "xstring strike(void)" << endl;        str2 = str.strike();        test(str2 == "<strike>Hello World!</strike>", str2);        cout << "xstring sub(void)" << endl;        str2 = str.sub();        test(str2 == "<sub>Hello World!</sub>", str2);        cout << "xstring sup(void)" << endl;        str2 = str.sup();        test(str2 == "<sup>Hello World!</sup>", str2);        cout << "xstring heading(unsigned int level)" << endl;        str2 = str.heading(1);        test(str2 == "<h1>Hello World!</h1>", str2);        cout << "xstring underline(void)" << endl;        str2 = str.underline();        test(str2 == "<u>Hello World!</u>", str2);        cout << "xstring span(const xstring& style)" << endl;        str2 = str.span("font-weight: 900");        test(str2 == "<span style=\"font-weight: 900\">Hello World!</span>", str2);        cout << "str.bold().italic().strike().big()" << endl;        str2 = str.bold().italic().strike().big();        test(str2 == "<big><strike><i><b>Hello World!</b></i></strike></big>", str2);    }    {        cout << "int compareIgnoreCase(const xstring& str)" << endl;        xstring str("Hello World!");        test(str.compareIgnoreCase(xstring("HELLO WORLD!")) == 0);        test(str.compareIgnoreCase(xstring("IELLO WORLD!")) < 0);        test(str.compareIgnoreCase(xstring("AELLO WORLD!")) > 0);    }    {        cout << "int compareIgnoreCasecompareIgnoreCase(size_type pos, size_type n, const xstring& str)" << endl;        xstring str("Hello World!");        test(str.compareIgnoreCase(0, 5, xstring("HELLO")) == 0);        test(str.compareIgnoreCase(0, 5, xstring("IELLO")) < 0);        test(str.compareIgnoreCase(0, 5, xstring("AELLO")) > 0);    }    {        cout << "int compareIgnoreCase(size_type pos1, size_type n1, const xstring& str, size_type pos2, size_type n2)" << endl;        xstring str("Hello World!");        test(str.compareIgnoreCase(0, 5, xstring("HELLO WORLD!"), 0, 5) == 0);        test(str.compareIgnoreCase(0, 5, xstring("IELLO WORLD!"), 0, 5) < 0);        test(str.compareIgnoreCase(0, 5, xstring("AELLO WORLD!"), 0, 5) > 0);    }    {        cout << "int compareIgnoreCase(const char* s)" << endl;        xstring str("Hello World!");        test(str.compareIgnoreCase("HELLO WORLD!") == 0);        test(str.compareIgnoreCase("IELLO WORLD!") < 0);        test(str.compareIgnoreCase("AELLO WORLD!") > 0);    }    {        cout << "int compareIgnoreCase(size_type pos, size_type n1, const char* s)" << endl;        xstring str("Hello World!");        test(str.compareIgnoreCase(0, 5, "HELLO") == 0);        test(str.compareIgnoreCase(0, 5, "IELLO") < 0);        test(str.compareIgnoreCase(0, 5, "AELLO") > 0);    }    {        cout << "int compareIgnoreCase(size_type pos, size_type n1, const char* s, size_type n2)" << endl;        xstring str("Hello World!");        test(str.compareIgnoreCase(0, 5, "HELLO WORLD!", 5) == 0);        test(str.compareIgnoreCase(0, 5, "IELLO WORLD!", 5) < 0);        test(str.compareIgnoreCase(0, 5, "AELLO WORLD!", 5) > 0);    }    {        cout << "xstring addslashes(void)" << endl;        xstring str("\"\'Hello World!\'\"");        str += '\0';        str += '\\';        xstring str2 = str.addslashes();        test(str2 == "\\\"\\'Hello World!\\'\\\"\\0\\\\", str2);        cout << "xstring stripslashes(void)" << endl;        xstring str3 = str2.stripslashes();        test(str3 == xstring("\"\'Hello World!\'\"") + '\0' + '\\', str3);        test(xstring("\\\\\\").stripslashes() == "\\\\");    }    {        cout << "xstring ltrim(void)" << endl;        test(xstring(" \t\r\nHello World!").ltrim() == "Hello World!");        test(xstring('\0').ltrim() == "");        test(xstring(static_cast<char>(0x0b)).ltrim() == "");        test(xstring("").ltrim() == "");        cout << "xstring rtrim(void)" << endl;        test(xstring(xstring("Hello World! \t\r\n") + '\0'            + static_cast<char>(0x0b)).rtrim() == "Hello World!");        test(xstring("").rtrim() == "");        cout << "xstring trim(void)" << endl;        test(xstring(xstring(" \t\r\n \t\r\nHello World! \t\r\n") + '\0'            + static_cast<char>(0x0b)).trim() == "Hello World!");        test(xstring("").trim() == "");    }    {        cout << "xstring str_replace(const xstring& search, const xstring& replace, const size_type count = npos)" << endl;        xstring str2;        str2 = xstring("Hello World!").str_replace("o", "0");        test(str2 == "Hell0 W0rld!", str2);        str2 = xstring("Hello World!").str_replace("l", "1");        test(str2 == "He11o Wor1d!", str2);        str2 = xstring("Hello World!").str_replace("!", "!!!");        test(str2 == "Hello World!!!", str2);        str2 = xstring("Hello World!").str_replace("ll", "!!!!!");        test(str2 == "He!!!!!o World!", str2);        str2 = xstring("Hello World!").str_replace("l", "1", 1);        test(str2 == "He1lo World!", str2);        str2 = xstring("Hello World!").str_replace("l", "1", 2);        test(str2 == "He11o World!", str2);        str2 = xstring("Hello World!").str_replace("l", "1", 3);        test(str2 == "He11o Wor1d!", str2);        str2 = xstring("Hello World!").str_replace("l", "1", 4);        test(str2 == "He11o Wor1d!", str2);        str2 = xstring("Hello World!").str_replace("l", "1", 100);        test(str2 == "He11o Wor1d!", str2);        str2 = xstring("Hello World!").str_replace("Hello", "Hi", 100);        test(str2 == "Hi World!", str2);    }    {        cout << "====== str_replace() performance test =======" << endl;        struct timeval time_start;        struct timeval time_end;        gettimeofday(&time_start, NULL);        for (int i = 0; i < 10000 ; ++i)        {            xstring("!!!!!!!!!!!!!!!!!!!!!!!!!!").str_replace("!", "Hello World!");        }        gettimeofday(&time_end, NULL);        cout << time_start.tv_sec << " " << time_start.tv_usec << endl;        cout << time_end.tv_sec   << " " << time_end.tv_usec   << endl;        double time_start_d            = (double)time_start.tv_sec + (double)time_start.tv_usec / (double)1000000;        double time_end_d            = (double)time_end.tv_sec + (double)time_end.tv_usec / (double)1000000;        cout << "start time: " << time_start_d << endl;        cout << "end time:   " << time_end_d   << endl;        cout << "====== str_replace() performance test =======" << endl;    }    {        cout << "xstring str_ireplace(const xstring& search, const xstring& replace, const size_type count = npos)" << endl;        xstring str2;        str2 = xstring("Hello World!").str_ireplace("O", "0");        test(str2 == "Hell0 W0rld!", str2);        str2 = xstring("Hello World!").str_ireplace("L", "1");        test(str2 == "He11o Wor1d!", str2);        str2 = xstring("Hello World!").str_ireplace("!", "!!!");        test(str2 == "Hello World!!!", str2);        str2 = xstring("Hello World!").str_ireplace("lL", "!!!!!");        test(str2 == "He!!!!!o World!", str2);        str2 = xstring("Hello World!").str_ireplace("l", "1", 1);        test(str2 == "He1lo World!", str2);        str2 = xstring("Hello World!").str_ireplace("L", "1", 2);        test(str2 == "He11o World!", str2);        str2 = xstring("Hello World!").str_ireplace("l", "1", 3);        test(str2 == "He11o Wor1d!", str2);        str2 = xstring("Hello World!").str_ireplace("L", "1", 4);        test(str2 == "He11o Wor1d!", str2);        str2 = xstring("Hello World!").str_ireplace("l", "1", 100);        test(str2 == "He11o Wor1d!", str2);        str2 = xstring("Hello World!").str_ireplace("heLLO", "Hi", 100);        test(str2 == "Hi World!", str2);    }    {        cout << "xstring htmlspecialchars(const QUOTE_STYLE quote_style = ENT_COMPAT)" << endl;        xstring str("<Hello World!>\"'&");        xstring str2;        str2 = str.htmlspecialchars();        test(str2 == "&lt;Hello World!&gt;&quot;'&amp;" ,str2);        str2 = str.htmlspecialchars(ENT_COMPAT);        test(str2 == "&lt;Hello World!&gt;&quot;'&amp;" ,str2);        str2 = str.htmlspecialchars(ENT_QUOTES);        test(str2 == "&lt;Hello World!&gt;&quot;&#039;&amp;" ,str2);        str2 = str.htmlspecialchars(ENT_NOQUOTES);        test(str2 == "&lt;Hello World!&gt;\"'&amp;" ,str2);    }    {        cout << "xstring nl2br(void)" << endl;        xstring str("Hello World!\rHello World!\nHello World!\r\nHello World!\n\r");        xstring str2 = str.nl2br();        test(str2 == "Hello World!<br />\nHello World!<br />\nHello World!<br />\nHello World!<br />\n<br />\n", str2);    }    {        cout << "xstring urlencode(void)" << endl;        xstring str("Hello World!\"#$%&'()");        xstring str2 = str.urlencode();        test(str2 == "Hello+World%21%22%23%24%25%26%27%28%29", str2);    }    {        cout << "xstring urldecode(void)" << endl;

⌨️ 快捷键说明

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