VirtualBox

Changeset 101343 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Oct 4, 2023 7:30:37 PM (16 months ago)
Author:
vboxsync
Message:

IPRT/ministring: Added C-style endsWith methods.

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

Legend:

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

    r98103 r101343  
    10381038}
    10391039
     1040bool RTCString::endsWith(const char *a_pszSuffix, size_t a_cchSuffix) const RT_NOEXCEPT
     1041{
     1042    Assert(RTStrNLen(a_pszSuffix, a_cchSuffix) == a_cchSuffix);
     1043    return a_cchSuffix >  0
     1044        && a_cchSuffix <= length()
     1045        && ::memcmp(&m_psz[length() - a_cchSuffix], a_pszSuffix, a_cchSuffix) == 0;
     1046}
     1047
     1048bool RTCString::endsWith(const char *a_pszSuffix) const RT_NOEXCEPT
     1049{
     1050    return endsWith(a_pszSuffix, strlen(a_pszSuffix));
     1051}
     1052
     1053bool RTCString::endsWithI(const char *a_pszSuffix, size_t a_cchSuffix) const RT_NOEXCEPT
     1054{
     1055    Assert(RTStrNLen(a_pszSuffix, a_cchSuffix) == a_cchSuffix);
     1056    return a_cchSuffix >  0
     1057        && a_cchSuffix <= length()
     1058        && ::RTStrNICmp(&m_psz[length() - a_cchSuffix], a_pszSuffix, a_cchSuffix) == 0;
     1059}
     1060
     1061bool RTCString::endsWithI(const char *a_pszSuffix) const RT_NOEXCEPT
     1062{
     1063    return endsWithI(a_pszSuffix, strlen(a_pszSuffix));
     1064}
     1065
    10401066bool RTCString::startsWith(const RTCString &that, CaseSensitivity cs /*= CaseSensitive*/) const RT_NOEXCEPT
    10411067{
  • trunk/src/VBox/Runtime/testcase/tstIprtMiniString.cpp

    r98103 r101343  
    192192
    193193    /* printf */
    194     RTCString StrFmt;
    195     CHECK(StrFmt.printf("%s-%s-%d", "abc", "def", 42).equals("abc-def-42"));
    196     test1Hlp1("abc-42-def", "%s-%d-%s", "abc", 42, "def");
    197     test1Hlp1("", "");
    198     test1Hlp1("1", "1");
    199     test1Hlp1("foobar", "%s", "foobar");
     194    {
     195        RTCString StrFmt;
     196        CHECK(StrFmt.printf("%s-%s-%d", "abc", "def", 42).equals("abc-def-42"));
     197        test1Hlp1("abc-42-def", "%s-%d-%s", "abc", 42, "def");
     198        test1Hlp1("", "");
     199        test1Hlp1("1", "1");
     200        test1Hlp1("foobar", "%s", "foobar");
     201    }
    200202
    201203    /* substring constructors */
    202     RTCString SubStr1("", (size_t)0);
    203     CHECK_EQUAL(SubStr1, "");
    204 
    205     RTCString SubStr2("abcdef", 2);
    206     CHECK_EQUAL(SubStr2, "ab");
    207 
    208     RTCString SubStr3("abcdef", 1);
    209     CHECK_EQUAL(SubStr3, "a");
    210 
    211     RTCString SubStr4("abcdef", 6);
    212     CHECK_EQUAL(SubStr4, "abcdef");
    213 
    214     RTCString SubStr5("abcdef", 7);
    215     CHECK_EQUAL(SubStr5, "abcdef");
    216 
    217 
    218     RTCString SubStrBase("abcdef");
    219 
    220     RTCString SubStr10(SubStrBase, 0);
    221     CHECK_EQUAL(SubStr10, "abcdef");
    222 
    223     RTCString SubStr11(SubStrBase, 1);
    224     CHECK_EQUAL(SubStr11, "bcdef");
    225 
    226     RTCString SubStr12(SubStrBase, 1, 1);
    227     CHECK_EQUAL(SubStr12, "b");
    228 
    229     RTCString SubStr13(SubStrBase, 2, 3);
    230     CHECK_EQUAL(SubStr13, "cde");
    231 
    232     RTCString SubStr14(SubStrBase, 2, 4);
    233     CHECK_EQUAL(SubStr14, "cdef");
    234 
    235     RTCString SubStr15(SubStrBase, 2, 5);
    236     CHECK_EQUAL(SubStr15, "cdef");
     204    {
     205        RTCString SubStr1("", (size_t)0);
     206        CHECK_EQUAL(SubStr1, "");
     207
     208        RTCString SubStr2("abcdef", 2);
     209        CHECK_EQUAL(SubStr2, "ab");
     210
     211        RTCString SubStr3("abcdef", 1);
     212        CHECK_EQUAL(SubStr3, "a");
     213
     214        RTCString SubStr4("abcdef", 6);
     215        CHECK_EQUAL(SubStr4, "abcdef");
     216
     217        RTCString SubStr5("abcdef", 7);
     218        CHECK_EQUAL(SubStr5, "abcdef");
     219
     220        RTCString SubStrBase("abcdef");
     221
     222        RTCString SubStr10(SubStrBase, 0);
     223        CHECK_EQUAL(SubStr10, "abcdef");
     224
     225        RTCString SubStr11(SubStrBase, 1);
     226        CHECK_EQUAL(SubStr11, "bcdef");
     227
     228        RTCString SubStr12(SubStrBase, 1, 1);
     229        CHECK_EQUAL(SubStr12, "b");
     230
     231        RTCString SubStr13(SubStrBase, 2, 3);
     232        CHECK_EQUAL(SubStr13, "cde");
     233
     234        RTCString SubStr14(SubStrBase, 2, 4);
     235        CHECK_EQUAL(SubStr14, "cdef");
     236
     237        RTCString SubStr15(SubStrBase, 2, 5);
     238        CHECK_EQUAL(SubStr15, "cdef");
     239    }
    237240
    238241    /* substr() and substrCP() functions */
     
    398401    CHECK_EQUAL(StrTruncate2, "");
    399402
     403    /* endsWith */
     404    RTCString const strEmpty;
     405    strTest = "qwerty";
     406    CHECK(strTest.endsWith(strTest));
     407    CHECK(strTest.endsWith("qwerty"));
     408    CHECK(strTest.endsWith("werty"));
     409    CHECK(strTest.endsWith("erty"));
     410    CHECK(strTest.endsWith("rty"));
     411    CHECK(strTest.endsWith("ty"));
     412    CHECK(strTest.endsWith("y"));
     413    CHECK(!strTest.endsWith(""));
     414    CHECK(strTest.endsWithI("qwerty"));
     415    CHECK(strTest.endsWithI("werty"));
     416    CHECK(strTest.endsWithI("erty"));
     417    CHECK(strTest.endsWithI("rty"));
     418    CHECK(strTest.endsWithI("ty"));
     419    CHECK(strTest.endsWithI("y"));
     420    CHECK(!strTest.endsWithI(""));
     421    CHECK(!strTest.endsWith(strEmpty));
     422
     423    /* startsWith */
     424    CHECK(strTest.startsWith(strTest));
     425    CHECK(strTest.startsWith("qwerty"));
     426    CHECK(strTest.startsWith("qwert"));
     427    CHECK(strTest.startsWith("qwer"));
     428    CHECK(strTest.startsWith("qwe"));
     429    CHECK(strTest.startsWith("qw"));
     430    CHECK(strTest.startsWith("q"));
     431    CHECK(!strTest.startsWith(""));
     432    CHECK(!strTest.startsWith(strEmpty));
     433
    400434#undef CHECK
    401435#undef CHECK_DUMP
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