VirtualBox

Changeset 30318 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Jun 21, 2010 7:49:28 AM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
62876
Message:

iprt/cpp/ministring.h: Added a append variant taking a 'const char *' argument to avoid the unnecessary duplication of constant strings. (tstUtf8.cpp: Dropped some of the lisping.)

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/string/ministring.cpp

    r28800 r30318  
    5050        memcpy(m_psz + lenThis, that.m_psz, lenThat);
    5151        m_psz[lenThis + lenThat] = '\0';
     52        m_cbLength = cbBoth - 1;
     53    }
     54    return *this;
     55}
     56
     57MiniString &MiniString::append(const char *pszThat)
     58{
     59    size_t cchThat = strlen(pszThat);
     60    if (cchThat)
     61    {
     62        size_t cchThis = length();
     63        size_t cbBoth = cchThis + cchThat + 1;
     64
     65        reserve(cbBoth);
     66            // calls realloc(cbBoth) and sets m_cbAllocated; may throw bad_alloc.
     67#ifndef RT_EXCEPTIONS_ENABLED
     68        AssertRelease(capacity() >= cbBoth);
     69#endif
     70
     71        memcpy(m_psz + cchThis, pszThat, cchThat);
     72        m_psz[cbBoth - 1] = '\0';
    5273        m_cbLength = cbBoth - 1;
    5374    }
  • trunk/src/VBox/Runtime/testcase/tstUtf8.cpp

    r28800 r30318  
    948948
    949949    iprt::MiniString empty;
    950     CHECK( (empty.length() == 0) );
    951     CHECK( (empty.capacity() == 0) );
     950    CHECK(empty.length() == 0);
     951    CHECK(empty.capacity() == 0);
    952952
    953953    iprt::MiniString sixbytes("12345");
    954     CHECK( (sixbytes.length() == 5) );
    955     CHECK( (sixbytes.capacity() == 6) );
    956 
    957     sixbytes.append("678");
    958     CHECK( (sixbytes.length() == 8) );
    959     CHECK( (sixbytes.capacity() == 9) );
     954    CHECK(sixbytes.length() == 5);
     955    CHECK(sixbytes.capacity() == 6);
     956
     957    sixbytes.append(iprt::MiniString("678"));
     958    CHECK(sixbytes.length() == 8);
     959    CHECK(sixbytes.capacity() == 9);
     960
     961    sixbytes.append("9a");
     962    CHECK(sixbytes.length() == 10);
     963    CHECK(sixbytes.capacity() == 11);
    960964
    961965    char *psz = sixbytes.mutableRaw();
    962         // 12345678
     966        // 123456789a
    963967        //       ^
    964968        // 0123456
    965969    psz[6] = '\0';
    966970    sixbytes.jolt();
    967     CHECK( (sixbytes.length() == 6) );
    968     CHECK( (sixbytes.capacity() == 7) );
     971    CHECK(sixbytes.length() == 6);
     972    CHECK(sixbytes.capacity() == 7);
    969973
    970974    iprt::MiniString morebytes("tobereplaced");
     
    972976    morebytes.append(sixbytes);
    973977
    974     CHECK_DUMP( (morebytes == "newstring 123456"), morebytes.c_str() );
     978    CHECK_DUMP(morebytes == "newstring 123456", morebytes.c_str());
    975979
    976980    iprt::MiniString third(morebytes);
    977981    third.reserve(100 * 1024);      // 100 KB
    978     CHECK_DUMP( (third == "newstring 123456"), morebytes.c_str() );
    979     CHECK( (third.capacity() == 100 * 1024) );
    980     CHECK( (third.length() == morebytes.length()) );        // must not have changed
     982    CHECK_DUMP(third == "newstring 123456", morebytes.c_str() );
     983    CHECK(third.capacity() == 100 * 1024);
     984    CHECK(third.length() == morebytes.length());          // must not have changed
    981985
    982986    iprt::MiniString copy1(morebytes);
    983987    iprt::MiniString copy2 = morebytes;
    984     CHECK( (copy1 == copy2) );
     988    CHECK(copy1 == copy2);
    985989
    986990    copy1 = NULL;
    987     CHECK( (copy1.length() == 0) );
     991    CHECK(copy1.length() == 0);
    988992
    989993    copy1 = "";
    990     CHECK( (copy1.length() == 0) );
    991 
    992     CHECK( (iprt::MiniString("abc") < iprt::MiniString("def")) );
    993     CHECK( (iprt::MiniString("abc") != iprt::MiniString("def")) );
    994     CHECK_DUMP_I( (iprt::MiniString("def") > iprt::MiniString("abc")) );
     994    CHECK(copy1.length() == 0);
     995
     996    CHECK(iprt::MiniString("abc") <  iprt::MiniString("def"));
     997    CHECK(iprt::MiniString("abc") != iprt::MiniString("def"));
     998    CHECK_DUMP_I(iprt::MiniString("def") > iprt::MiniString("abc"));
     999    CHECK(iprt::MiniString("abc") == iprt::MiniString("abc"));
    9951000
    9961001    copy2.setNull();
    997     for (int i = 0;
    998          i < 100;
    999          ++i)
     1002    for (int i = 0; i < 100; ++i)
    10001003    {
    10011004        copy2.reserve(50);      // should be ignored after 50 loops
    10021005        copy2.append("1");
    10031006    }
    1004     CHECK( (copy2.length() == 100) );
     1007    CHECK(copy2.length() == 100);
    10051008
    10061009    copy2.setNull();
    1007     for (int i = 0;
    1008          i < 100;
    1009          ++i)
     1010    for (int i = 0; i < 100; ++i)
    10101011    {
    10111012        copy2.reserve(50);      // should be ignored after 50 loops
    10121013        copy2.append('1');
    10131014    }
    1014     CHECK( (copy2.length() == 100) );
     1015    CHECK(copy2.length() == 100);
    10151016
    10161017#undef CHECK
     1018#undef CHECK_DUMP
     1019#undef CHECK_DUMP_I
    10171020}
    10181021
     
    10991102    /* Test Latin1 -> Utf16 */
    11001103    const char *pszLat1 = "\x01\x20\x40\x80\x81";
    1101     RTTEST_CHECK(hTest, (RTLatin1CalcUtf16Len(pszLat1) == 5));
     1104    RTTEST_CHECK(hTest, RTLatin1CalcUtf16Len(pszLat1) == 5);
    11021105    rc = RTLatin1CalcUtf16LenEx(pszLat1, 3, &cchActual);
    11031106    RTTEST_CHECK_RC_OK(hTest, rc);
    11041107    if (RT_SUCCESS(rc))
    1105         RTTEST_CHECK(hTest, (cchActual == 3));
     1108        RTTEST_CHECK(hTest, cchActual == 3);
    11061109    rc = RTLatin1CalcUtf16LenEx(pszLat1, RTSTR_MAX, &cchActual);
    11071110    RTTEST_CHECK_RC_OK(hTest, rc);
    11081111    if (RT_SUCCESS(rc))
    1109         RTTEST_CHECK(hTest, (cchActual == 5));
     1112        RTTEST_CHECK(hTest, cchActual == 5);
    11101113    RTUTF16 *pwc = NULL;
    11111114    RTUTF16 wc[6];
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette