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

📄 test_xstring.cpp

📁 经典的string 函数库学习资料
💻 CPP
📖 第 1 页 / 共 3 页
字号:
        xstring str("Hello+World%21%22%23%24%25%26%27%28%29%a%+r%a");        xstring str2 = str.urldecode();        test(str2 == "Hello World!\"#$%&'()%a% r%a", str2);    }    {        cout << "xstring rawurlencode(void)" << endl;        xstring str("Hello World!\"#$%&'()");        xstring str2 = str.rawurlencode();        test(str2 == "Hello%20World%21%22%23%24%25%26%27%28%29", str2);    }    {        cout << "xstring rawurldecode(void)" << endl;        xstring str("Hello+World%21%22%23%24%25%26%27%28%29%a%+r%a");        xstring str2 = str.rawurldecode();        test(str2 == "Hello+World!\"#$%&'()%a%+r%a", str2);    }    {        cout << "xstring base64_encode(void)" << endl;        xstring str("This is an encoded string");        xstring str2 = str.base64_encode();        test(str2 == "VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==", str2);        cout << "xstring base64_decode(void)" << endl;        xstring str3 = str2.base64_decode();        test(str3 == str, str3);    }    {        cout << "xstring md5(void)" << endl;        xstring str;        xstring str2;        str = "";        str2 = str.md5();        test("d41d8cd98f00b204e9800998ecf8427e" == str2, str2);        str = "a";        str2 = str.md5();        test("0cc175b9c0f1b6a831c399e269772661" == str2, str2);        str = "Hello World!";        str2 = str.md5();        test("ed076287532e86365e841e92bfc50d8c" == str2, str2);        str = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";        str2 = str.md5();        test("a69d9a9991712224e6a899482474c56c" == str2, str2);    }    {        cout << "xstring sha1(void)" << endl;        xstring str;        xstring str2;        str = "";        str2 = str.sha1();        test("da39a3ee5e6b4b0d3255bfef95601890afd80709" == str2, str2);        str = "a";        str2 = str.sha1();        test("86f7e437faa5a7fce15d1ddcb9eaeaea377667b8" == str2, str2);        str = "Hello World!";        str2 = str.sha1();        test("2ef7bde608ce5404e97d5f042f95f89f1c232871" == str2, str2);        str = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";        str2 = str.sha1();        test("319108259d86e08b35e8885e6994bb5e27e74394" == str2, str2);    }    {        xstring str;        xstring str2;        cout << "xstring basename(void)" << endl;        str = "/";        str2 = str.basename();        test("/" == str2, str2);        str = "///";        str2 = str.basename();        test("/" == str2, str2);        str = "////..//////";        str2 = str.basename();        test(".." == str2, str2);        str = "/home/neo/work/xstring/xstring.cpp";        str2 = str.basename(".cpp");        test("xstring" == str2, str2);        cout << "xstring dirname(void)" << endl;        str = "/";        str2 = str.dirname();        test("/" == str2, str2);        str = "//";        str2 = str.dirname();        test("/" == str2, str2);        str = "a//";        str2 = str.dirname();        test("." == str2, str2);        str = "a////b";        str2 = str.dirname();        test("a" == str2, str2);    }    {        cout << "xstring str_rot13()" << endl;        xstring str;        xstring str2;        str = "abcdefghijklmnopqrstuvwxyz";        str2 = str.str_rot13();        test("nopqrstuvwxyzabcdefghijklm" == str2, str2);        str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";        str2 = str.str_rot13();        test("NOPQRSTUVWXYZABCDEFGHIJKLM" == str2, str2);        str = "Hello World!";        str2 = str.str_rot13();        str2 = str2.str_rot13();        test(str == str2, str2);    }    {        cout << "unsigned int crc32()" << endl;        xstring str;        unsigned int crc;        str = "The quick brown fox jumped over the lazy dog.";        crc = str.crc32();        test(2191738434U == crc);    }    {        cout << "xstring strrev()" << endl;        xstring str;        xstring str2;        str = "Hello World!";        str2 = str.strrev();        test("!dlroW olleH" == str2, str2);    }    {        cout << "xstring safesubstr()" << endl;        xstring str("abcdef");        xstring rest;        rest = str.safesubstr(1);        test(rest == "bcdef", rest);        rest = str.safesubstr(1, 3);        test(rest == "bcd", rest);        rest = str.safesubstr(0, 4);        test(rest == "abcd", rest);        rest = str.safesubstr(0, 8);        test(rest == "abcdef", rest);        rest = str.safesubstr(-1);        test(rest == "f", rest);        rest = str.safesubstr(-2);        test(rest == "ef", rest);        rest = str.safesubstr(-3, 1);        test(rest == "d", rest);        rest = str.safesubstr(0, -1);        test(rest == "abcde", rest);        rest = str.safesubstr(2, -1);        test(rest == "cde", rest);        rest = str.safesubstr(4, -4);        test(rest == "", rest);        rest = str.safesubstr(-3, -1);        test(rest == "de", rest);    }    {        cout << "xstring explode()" << endl;        xstring str = "a|b|c|d|e";        vector<xstring> pieces = str.explode("|");        test(5 == pieces.size());        test("a" == pieces[0], pieces[0]);        test("b" == pieces[1], pieces[1]);        test("c" == pieces[2], pieces[2]);        test("d" == pieces[3], pieces[3]);        test("e" == pieces[4], pieces[4]);    }    {        cout << "xstring explode()" << endl;        xstring str = "first|%%|second|%%|third|%%|fourth|%%|fifth";        vector<xstring> pieces = str.explode("|%%|", 3);        test(3 == pieces.size());        test("first" == pieces[0], pieces[0]);        test("second" == pieces[1], pieces[1]);        test("third|%%|fourth|%%|fifth" == pieces[2], pieces[2]);    }    {        cout << "xstring quotemeta()" << endl;        xstring str = "Hello . \\ + * ? [ ^ ] ( $ ) World!";        xstring str2 = str.quotemeta();        test("Hello \\. \\\\ \\+ \\* \\? \\[ \\^ \\] \\( \\$ \\) World!" == str2, str2);    }    {        cout << "xstring str_pad()" << endl;        xstring str = "Alien";        xstring str2;        str2 = str.str_pad(10);        test("Alien     " == str2, str2);        str2 = str.str_pad(10, "-=", PAD_LEFT);        test("-=-=-Alien" == str2, str2);        str2 = str.str_pad(10, "_", PAD_BOTH);        test("__Alien___" == str2, str2);        str2 = str.str_pad(6, "_");        test("Alien_" == str2, str2);        str2 = str.str_pad(2);        test("Alien" == str2, str2);        str2 = str.str_pad(20, "123", PAD_BOTH);        test("1231231Alien12312312" == str2, str2);    }    {        cout << "xstring crypt()" << endl;        xstring str;        xstring str2;        str = "";        str2 = str.crypt("");        test("$1$$qRPK7m23GJusamGpoGLby/" == str2, str2);        str = "Hello World!";        str2 = str.crypt("");        test("$1$$2LjJ93nOgUrdnGH/a1Tx.." == str2, str2);        str = "Hello World!";        str2 = str.crypt("a");        test("$1$a$3dXM6t.vR/wFkxthyFpc/1" == str2, str2);        str = "Hello World!";        str2 = str.crypt("abc");        test("$1$abc$SptKg1gOZG9bzToEJRLqT/" == str2, str2);        str = "napoleon";        str2 = str.crypt("2");        test("$1$2$c8RH9qgZ1OkIkJXLaNHVj1" == str2, str2);    }    {        cout << "bool isEmail()" << endl;        xstring str;        bool result;        str = "gongjie@xilu.com";        result = str.isEmail();        test(result == true, str);        str = "neo@mamiyami.com";        result = str.isEmail();        test(result == true, str);        str = "gongjie@21cn.com";        result = str.isEmail();        test(result == true, str);        str = "gongjie@xilu.com.a";        result = str.isEmail();        test(result == false, str);        str = "gongjie.xilu.com";        result = str.isEmail();        test(result == false, str);    }    {        cout << "bool ereg()" << endl;        xstring str;        vector<xstring> result;        str = "gongjie@xilu.com";        str.ereg("([0-9a-zA-Z]+)@([0-9a-zA-Z]+)\\.([0-9a-zA-Z]+)", result);        test(4 == result.size());        test(result[0] == "gongjie@xilu.com", result[0]);        test(result[1] == "gongjie", result[1]);        test(result[2] == "xilu", result[2]);        test(result[3] == "com", result[3]);    }    {        cout << "bool ereg()" << endl;        xstring str;        vector<xstring> result;        str = "This is a test!";        str.ereg("([0-9a-zA-Z]+)", result);        test(2 == result.size());        test(result[0] == "This", result[0]);        test(result[1] == "This", result[1]);    }    {        cout << "bool ereg()" << endl;        xstring str;        vector<xstring> result;        str = "http://www.xilu.com/";        str.ereg("[a-zA-Z]+://[^<>[:space:]]+[[:alnum:]/]", result);        test(1 == result.size());        test(result[0] == "http://www.xilu.com/", result[0]);    }    {        cout << "xstring ereg_replace()" << endl;        xstring str;        xstring str2;        str = "http://www.xilu.com/";        str2 = str.ereg_replace("[a-zA-Z]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\\0\">\\0</a>");        test(str2 == "<a href=\"http://www.xilu.com/\">http://www.xilu.com/</a>", str2);    }    {        cout << "swap()" << endl;        xstring str1 = "xstring1";        xstring str2 = "xstring2";        swap(str1, str2);        test(str1 == "xstring2", str1);        test(str2 == "xstring1", str2);    }    {        cout << "getWord()" << endl;        xstring str1 = "abcd|%%|efg|%%|hijk";        xstring str2 = "|%%|";        xstring str3;        str3 = str1.getWord(str2);        test("efg|%%|hijk" == str1, str1);        test("|%%|" == str2, str2);        test("abcd" == str3, str3);        str3 = str1.getWord(str2);        test("hijk" == str1, str1);        test("|%%|" == str2, str2);        test("efg" == str3, str3);        str3 = str1.getWord(str2);        test("" == str1, str1);        test("|%%|" == str2, str2);        test("hijk" == str3, str3);    }    {        cout << "bool isGB2312()" << endl;        xstring str1 = "abcd|%%|efg|%%|hijk";        xstring str2 = "泥偶测试";        test(str1.isGB2312(), str1);        test(str2.isGB2312(), str2);    }    {        cout << "bool ereg()" << endl;        try        {            xstring str1 = "";            xstring str2 = "";            bool result = str1.ereg(str2);            test(result == true);        }        catch (exception& e)        {            test(false, e.what());        }        catch (...)        {            test(false, "Unknown exception!");        }    }    {        // Bug report by grace.        cout << "bool ereg()" << endl;        xstring date = "2003-12-10";        vector<xstring> regs;        bool result = date.ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", regs);        test(result == true);        test(4 == regs.size());        test(regs[0] == "2003-12-10", regs[0]);        test(regs[1] == "2003", regs[1]);        test(regs[2] == "12", regs[2]);        test(regs[3] == "10", regs[3]);    }    {        // Bug report by grace.        cout << "bool ereg()" << endl;        xstring xmlrequest = "2003-10-10<MD12</MDN><SPCode>12345678</SPCode> \n\r <ProductCode>1234567890</ProductCode>\n\r <TransactionID>1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcd</TransactionID>\n\r<EffectiveDate>2003-07-07 10:00:00</EffectiveDate>";        xstring regexp = "^.*<MDN>(.*)</MDN>.*<ProductCode>(.*)</ProductCode>.*<TransactionID>(.*)</TransactionID>.*<EffectiveDate>(.*)</EffectiveDate>";        vector<xstring> regs;        bool result = xmlrequest.ereg(regexp, regs);        test(result == false);    }    return 0;}

⌨️ 快捷键说明

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