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

📄 string.cpp

📁 Pegasus is an open-source implementationof the DMTF CIM and WBEM standards. It is designed to be por
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================//// Author: Mike Brasher (mbrasher@bmc.com)//// Modified By:////%/////////////////////////////////////////////////////////////////////////////#include <Pegasus/Common/PegasusAssert.h>#include <cstring>#include <Pegasus/Common/String.h>#include <Pegasus/Common/Exception.h>#include <Pegasus/Common/CommonUTF.h>#include <stdcxx/stream/strstream>PEGASUS_USING_PEGASUS;PEGASUS_USING_STD;static char * verbose;int test(int argc, char** argv){    verbose = getenv("PEGASUS_TEST_VERBOSE");    String s1 = "Hello World";    String s2 = s1;    String s3(s2);    PEGASUS_TEST_ASSERT(String::equal(s1, s3));    // Test append characters to String    String s4 = "Hello";    s4.append(Char16(0x0000));    s4.append(Char16(0x1234));    s4.append(Char16(0x5678));    s4.append(Char16(0x9cde));    s4.append(Char16(0xffff));    {#ifdef HAVE_SSTREAM        stringstream os;#endif#ifdef HAVE_STRSTREAM        ostrstream os;#endif        os << s4;#ifdef HAVE_STRSTREAM        os.put('\0');#endif#ifndef PEGASUS_HAS_ICU        const char EXPECTED[] = "Hello\\x0000\\x1234\\x5678\\x9CDE\\xFFFF";#else        CString cstr = s4.getCString();        const char * EXPECTED = (const char *)cstr;#endif#ifdef HAVE_SSTREAM    string os_str = os.str();    const char* tmp = os_str.c_str();#endif#ifdef HAVE_STRSTREAM        char *tmp = os.str();#endif        PEGASUS_TEST_ASSERT(strcmp(EXPECTED, tmp) == 0);#ifdef PEGASUS_PLATFORM_AIX_RS_IBMCXX        os.freeze(false);#else#ifdef HAVE_STRSTREAM        delete tmp;#endif#endif    }    {        // Test getCString        const char STR0[] = "one two three four";        String s = STR0;        PEGASUS_TEST_ASSERT(strcmp(s.getCString(), STR0) == 0);    }    {        // Test remove        String s = "abcdefg";        s.remove(3, 3);        PEGASUS_TEST_ASSERT(String::equal(s, "abcg"));        PEGASUS_TEST_ASSERT(s.size() == 4);        s = "abcdefg";        s.remove(3, 4);        PEGASUS_TEST_ASSERT(String::equal(s, "abc"));        PEGASUS_TEST_ASSERT(s.size() == 3);        s = "abcdefg";        s.remove(3);        PEGASUS_TEST_ASSERT(String::equal(s, "abc"));        PEGASUS_TEST_ASSERT(s.size() == 3);        s = "abc";        s.remove(3);        PEGASUS_TEST_ASSERT(String::equal(s, "abc"));        PEGASUS_TEST_ASSERT(s.size() == 3);        s = "abc";        s.remove(0);        PEGASUS_TEST_ASSERT(String::equal(s, ""));        PEGASUS_TEST_ASSERT(s.size() == 0);        s = "abc";        s.remove(0, 1);        PEGASUS_TEST_ASSERT(String::equal(s, "bc"));        PEGASUS_TEST_ASSERT(s.size() == 2);        String t1 = "HELLO";        String t2 = t1;        t2.toLower();        PEGASUS_TEST_ASSERT(String::equal(t1, "HELLO"));        PEGASUS_TEST_ASSERT(String::equal(t2, "hello"));    }    {        // another test of the append method        String t1 = "one";        t1.append(" two");        PEGASUS_TEST_ASSERT(String::equal(t1, "one two"));        t1.append(' ');        t1.append('t');        t1.append('h');        t1.append('r');        t1.append("ee");        PEGASUS_TEST_ASSERT(String::equal(t1,"one two three"));        // used as example in Doc.        String test = "abc";        test.append("def");        PEGASUS_TEST_ASSERT(test == "abcdef");    }    // Test of the different overload operators    {        // Test the == overload operator        String t1 = "one";        String t2 = "one";        PEGASUS_TEST_ASSERT(t1 == "one");        PEGASUS_TEST_ASSERT("one" == t1);        PEGASUS_TEST_ASSERT(t1 == t2);        PEGASUS_TEST_ASSERT(t2 == t1);        PEGASUS_TEST_ASSERT(String("one") == "one");        const char STR0[] = "one two three four";        String s = STR0;        CString tmp = s.getCString();        PEGASUS_TEST_ASSERT(tmp == s);        PEGASUS_TEST_ASSERT(s == tmp);    }    {        // Tests of the + Overload operator        String t1 = "abc";        String t2 = t1 + t1;        PEGASUS_TEST_ASSERT(t2 == "abcabc");        t1 = "abc";        t2 = t1 + "def";        PEGASUS_TEST_ASSERT(t2 == "abcdef");        t1 = "ghi";        PEGASUS_TEST_ASSERT(t1 == "ghi");        // ATTN: the following fails because there        // is no single character overload operator        // KS: Apr 2001        // t2 = t1 + 'k' + 'l' + 'm' + "nop";        t2 = t1 + "k" + "l" + "m" + "nop";        PEGASUS_TEST_ASSERT(t2 == "ghiklmnop");        PEGASUS_TEST_ASSERT(String::equal(t2,"ghiklmnop"));        // add tests for != operator.        t1 = "abc";        PEGASUS_TEST_ASSERT(t1 != "ghi");        PEGASUS_TEST_ASSERT(t1 != t2);        // add tests for other compare operators        // Operater <        t1 = "ab";        t2 = "cde";        PEGASUS_TEST_ASSERT(t1 < t2);        PEGASUS_TEST_ASSERT(t1 <= t2);        PEGASUS_TEST_ASSERT(t2 > t1);        PEGASUS_TEST_ASSERT(t2 >=t1);        PEGASUS_TEST_ASSERT(String::compare(t1,t2) < 0);        PEGASUS_TEST_ASSERT(String::compare(t2,t1) > 0);        PEGASUS_TEST_ASSERT(String::compare(t1, t2, 1) < 0);        PEGASUS_TEST_ASSERT(String::compare(t2, t1, 1) > 0);        PEGASUS_TEST_ASSERT(String::compare(t1, t2, 10) < 0);        PEGASUS_TEST_ASSERT(String::compare(t2, t1, 10) > 0);        PEGASUS_TEST_ASSERT(String::compare(t1, t2, 0) == 0);        t2 = t1;        PEGASUS_TEST_ASSERT(t1 <= t2);        PEGASUS_TEST_ASSERT(t1 >= t2);        PEGASUS_TEST_ASSERT(String::compare(t1, t2) == 0);        PEGASUS_TEST_ASSERT(String::compare(t1, t2, 0) == 0);        PEGASUS_TEST_ASSERT(String::compare(t1, t2, 1) == 0);        PEGASUS_TEST_ASSERT(String::compare(t1, t2, 10) == 0);        // Tests for compare with same length        t1 = "abc";        t2 = "def";        PEGASUS_TEST_ASSERT(t1 < t2);        PEGASUS_TEST_ASSERT(String::compare(t1, t2) < 0);        PEGASUS_TEST_ASSERT(String::compare(t2, t1) > 0);        PEGASUS_TEST_ASSERT(String::compare(t1, t2, 10) < 0);        PEGASUS_TEST_ASSERT(String::compare(t1, t2, 0) == 0);              t1 = "abc";        t2 = "ABC";        PEGASUS_TEST_ASSERT(String::equalNoCase(t1,t2));        PEGASUS_TEST_ASSERT(!String::equal(t1,t2));        PEGASUS_TEST_ASSERT(String::compareNoCase(t1,t2) == 0);        t1.toUpper();        t2.toLower();        PEGASUS_TEST_ASSERT(String::equal(t1, "ABC"));        PEGASUS_TEST_ASSERT(String::equal(t2, "abc"));        t1 = "1000";        t2 = "1001";        PEGASUS_TEST_ASSERT(String::compareNoCase(t1,t2) < 0);        PEGASUS_TEST_ASSERT(String::compare(t1, t2) < 0);        PEGASUS_TEST_ASSERT(String::compare(t1, t2, 3) == 0);        PEGASUS_TEST_ASSERT(String::compare(t1, t2, 4) < 0);#ifdef PEGASUS_HAS_ICU        //        // Strings used to test non-ascii case mappings        // Tests context sensitve mappings (eg. greek)        // Tests expansion after mapping (eg german)        //        // Lower case german and greek        // --latin small letter sharp s (german) (2 of these to cause        //  ICU overflow error inside of String)        // --greek small letter sigma (followed by another letter)        // --latin small a        // --greek small letter sigma (NOT followed by another letter)        const Char16 lowermap[] = {                                   0xdf,                                   0xdf,                                   0x3c3,                                   'a',                                   0x3c2,                                   0x00};        String degkLow(lowermap);        // Needed because the german char does not round trip        // after an uppercase followed by lower case.        // --latin small letters 's' 's' (4 of these due to expansion)        // --greek small letter sigma (followed by another letter)        // --latin small a        // --greek small letter sigma (NOT followed by another letter)        const Char16 lowermap2[] = {                                   's', 's', 's', 's',                                   0x3c3,                                   'a',                                   0x3c2,                                   0x00};         String degkLow2(lowermap2);                // Upper case greek and german        // latin cap letter sharp s (german) (4 of these due to expansion)        // greek cap letter sigma (followed by another letter)        // latin cap A        // greek cap letter sigma (NOT followed by another letter)        const Char16 uppermap[] = {                                  'S', 'S', 'S', 'S',                                   0x3a3,                                   'A',                                   0x3a3,                                   0x00};         String degkUp(uppermap);        PEGASUS_TEST_ASSERT(String::compareNoCase(degkLow,degkUp) == 0);        // does a binary compare, so lower > upper        PEGASUS_TEST_ASSERT(String::compare(degkLow,degkUp) > 0);        PEGASUS_TEST_ASSERT(String::equalNoCase(degkLow,degkUp));        String mapTest(degkLow);        mapTest.toUpper();        PEGASUS_TEST_ASSERT(String::equal(mapTest, degkUp));        // Note that the German char does not round trip        mapTest.toLower();        PEGASUS_TEST_ASSERT(String::equal(mapTest, degkLow2));#endif    }    {        // Test of the [] operator        String t1 = "abc";        Char16 c = t1[1];        // note c is Char16        PEGASUS_TEST_ASSERT(c == 'b');        //ATTN: test for outofbounds exception        try        {            c = t1[200];        }        catch (IndexOutOfBoundsException&)        {        PEGASUS_TEST_ASSERT(true);        }    }    {        // Test the find function        String t1 = "abcdef";        String t2 = "cde";        String t3 = "xyz";        String t4 = "abc";        String t5 = "abd";        String t6 = "defg";        PEGASUS_TEST_ASSERT(t1.find('c') == 2);        PEGASUS_TEST_ASSERT(t1.find(t2)==2);        PEGASUS_TEST_ASSERT(t1.find(t3)==PEG_NOT_FOUND);        PEGASUS_TEST_ASSERT(t1.find(t4)==0);        PEGASUS_TEST_ASSERT(t1.find(t5)==PEG_NOT_FOUND);        PEGASUS_TEST_ASSERT(t1.find(t6)==PEG_NOT_FOUND);        PEGASUS_TEST_ASSERT(t1.find("cde")==2);        PEGASUS_TEST_ASSERT(t1.find("def")==3);        PEGASUS_TEST_ASSERT(t1.find("xyz")==PEG_NOT_FOUND);        PEGASUS_TEST_ASSERT(t1.find("a") ==0);        // test for the case where string        // partly occurs and then later        // completely occurs        String s = "this is an apple";        PEGASUS_TEST_ASSERT(s.find("apple")==11);        PEGASUS_TEST_ASSERT(s.find("appld")==PEG_NOT_FOUND);        PEGASUS_TEST_ASSERT(s.find("this")==0);        PEGASUS_TEST_ASSERT(s.find("t")==0);        PEGASUS_TEST_ASSERT(s.find("e")==15);        s = "a";        PEGASUS_TEST_ASSERT(s.find("b")==PEG_NOT_FOUND);        PEGASUS_TEST_ASSERT(s.find("a")==0);        PEGASUS_TEST_ASSERT(s.find(s)==0);        s = "aaaapple";        PEGASUS_TEST_ASSERT(s.find("apple")==3);        // 20020715-RK This method was removed from the String class        //{        //    String nameSpace = "a#b#c";        //    nameSpace.translate('#', '/');        //    PEGASUS_TEST_ASSERT(nameSpace == "a/b/c");        //}    }    {        //        // Test String unicode enablement        //        char utf8chr[]    = {                              '\xCE', '\x99', '\xCE', '\xBF', '\xCF', '\x8D',                              '\xCE', '\xBD', '\xCE', '\xB9', '\xCE', '\xBA',                              '\xCE', '\xBF', '\xCE', '\xBD', '\xCF', '\x84',                              '\0'                            }; // utf8 string with mutliple byte characters        Char16 utf16chr[] = {                              0x0399,0x03BF,0x03CD,0x03BD,0x03B9,                              0x03BA,0x03BF,0x03BD,0x03C4,0x00                            };  // utf16 representation of the utf8 string        String utf16string(utf16chr);        String utf8string(utf8chr);        String utf16merge(utf8string.getChar16Data());        CString temp = utf8string.getCString();        CString temp2 = utf16string.getCString();        const char*  tmp = (const char *)temp;

⌨️ 快捷键说明

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