VirtualBox

Ignore:
Timestamp:
Jan 14, 2011 2:16:45 PM (14 years ago)
Author:
vboxsync
Message:

IPRT: fix rare crash in MiniString::substr(); rename substr() to substrCP() and add a substr that operates on bytes, not codepoints; more to come

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/testcase/tstIprtMiniString.cpp

    r33862 r35567  
    211211    CHECK_EQUAL(SubStr15, "cdef");
    212212
     213    /* substr() and substrCP() functions */
     214    iprt::MiniString strTest("");
     215    CHECK_EQUAL(strTest.substr(0), "");
     216    CHECK_EQUAL(strTest.substrCP(0), "");
     217    CHECK_EQUAL(strTest.substr(1), "");
     218    CHECK_EQUAL(strTest.substrCP(1), "");
     219
     220    /* now let's have some non-ASCII to chew on */
     221    strTest = "abcdefßäbcdef";
     222            // 13 codepoints, but 15 bytes (excluding null terminator);
     223            // "ß" and "ä" consume two bytes each
     224    CHECK_EQUAL(strTest.substr(0),   strTest.c_str());
     225    CHECK_EQUAL(strTest.substrCP(0), strTest.c_str());
     226
     227    CHECK_EQUAL(strTest.substr(2),   "cdefßäbcdef");
     228    CHECK_EQUAL(strTest.substrCP(2), "cdefßäbcdef");
     229
     230    CHECK_EQUAL(strTest.substr(2, 2),   "cd");
     231    CHECK_EQUAL(strTest.substrCP(2, 2), "cd");
     232
     233    CHECK_EQUAL(strTest.substr(6),   "ßäbcdef");
     234    CHECK_EQUAL(strTest.substrCP(6), "ßäbcdef");
     235
     236    CHECK_EQUAL(strTest.substr(6, 2),   "ß");           // UTF-8 "ß" consumes two bytes
     237    CHECK_EQUAL(strTest.substrCP(6, 1), "ß");
     238
     239    CHECK_EQUAL(strTest.substr(8),   "äbcdef");         // UTF-8 "ß" consumes two bytes
     240    CHECK_EQUAL(strTest.substrCP(7), "äbcdef");
     241
     242    CHECK_EQUAL(strTest.substr(8, 3),   "äb");          // UTF-8 "ä" consumes two bytes
     243    CHECK_EQUAL(strTest.substrCP(7, 2), "äb");
     244
     245    CHECK_EQUAL(strTest.substr(14, 1),   "f");
     246    CHECK_EQUAL(strTest.substrCP(12, 1), "f");
     247
     248    CHECK_EQUAL(strTest.substr(15, 1),   "");
     249    CHECK_EQUAL(strTest.substrCP(13, 1), "");
     250
     251    CHECK_EQUAL(strTest.substr(16, 1),   "");
     252    CHECK_EQUAL(strTest.substrCP(15, 1), "");
    213253
    214254    /* special constructor and assignment arguments */
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