VirtualBox

Changeset 85345 in vbox


Ignore:
Timestamp:
Jul 15, 2020 8:03:03 AM (5 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
139330
Message:

IPRT: Follow-up fixes for r139114 (RTStrSplit + testcases).

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

Legend:

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

    r85154 r85345  
    4646
    4747    /* Determine the number of paths in buffer first. */
    48     size_t       cch    = cbStrings - 1;
    49     char const  *pszTmp = pcszStrings;
     48    size_t      cch     = cbStrings - 1;
     49    char const *pcszTmp = pcszStrings;
     50    const char *pcszEnd = RTStrEnd(pcszTmp, RTSTR_MAX);
     51    char const *pcszNext;
    5052    const size_t cchSep = strlen(pcszSeparator);
     53          size_t cchNext;
    5154    while (cch > 0)
    5255    {
    53         char const *pszNext = RTStrStr(pszTmp, pcszSeparator);
    54         if (!pszNext)
     56        pcszNext = RTStrStr(pcszTmp, pcszSeparator);
     57        if (!pcszNext)
    5558            break;
    56         const size_t cchNext = pszNext - pszTmp;
     59        cchNext = pcszNext - pcszTmp;
    5760        if (cchNext + cchSep > cch)
    5861            break;
    59         pszTmp += cchNext + cchSep;
    60         cch    -= cchNext + cchSep;
     62        pcszNext += cchSep;
     63        pcszTmp  += cchNext + cchSep;
     64        cch      -= cchNext + cchSep;
    6165        if (cchNext)
    6266            ++cStrings;
    6367    }
    6468
     69    if (pcszTmp != pcszEnd) /* Do we need to take a trailing string without separator into account? */
     70        cStrings++;
     71
    6572    if (!cStrings)
    6673    {
    67         *pcStrings = 0;
     74        *ppapszStrings = NULL;
     75        *pcStrings     = 0;
    6876        return VINF_SUCCESS;
    6977    }
     
    7684
    7785    cch    = cbStrings - 1;
    78     pszTmp = pcszStrings;
     86    pcszTmp = pcszStrings;
    7987
    80     for (size_t i = 0; i < 3;)
     88    for (size_t i = 0; i < cStrings;)
    8189    {
    82         char const *pszNext = RTStrStr(pszTmp, pcszSeparator);
    83         if (!pszNext)
    84             break;
    85         const size_t cchNext = pszNext - pszTmp;
    86         if (cchNext + cchSep > cch)
    87             break;
     90        pcszNext = RTStrStr(pcszTmp, pcszSeparator);
     91        if (!pcszNext)
     92            pcszNext = pcszEnd;
     93        cchNext = pcszNext - pcszTmp;
    8894        if (cchNext)
    8995        {
    90             papszStrings[i] = RTStrDupN(pszTmp, cchNext);
     96            papszStrings[i] = RTStrDupN(pcszTmp, cchNext);
    9197            if (!papszStrings[i])
    9298            {
     
    96102            i++;
    97103        }
    98         pszTmp += cchNext + cchSep;
    99         cch    -= cchNext + cchSep;
     104        pcszTmp += cchNext + cchSep;
     105        cch     -= cchNext + cchSep;
    100106    }
    101107
  • trunk/src/VBox/Runtime/testcase/tstRTStrSplit.cpp

    r85155 r85345  
    5555    RTTEST_CHECK_RC(hTest, RTStrSplit(szEmpty, sizeof(szEmpty), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
    5656    RTTEST_CHECK(hTest, cStrings == 0);
    57     RTTEST_CHECK(hTest, papszStrings == NULL);
    5857
    5958    /* No separator given. */
    6059    const char szNoSep[] = "foo";
    6160    RTTEST_CHECK_RC(hTest, RTStrSplit(szNoSep, sizeof(szNoSep), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
    62     RTTEST_CHECK(hTest, cStrings == 0);
    63     RTTEST_CHECK(hTest, papszStrings == NULL);
     61    RTTEST_CHECK(hTest, cStrings == 1);
     62    RTTEST_CHECK(hTest, RTStrICmp(papszStrings[0], "foo") == 0);
    6463
    6564    /* Single string w/ separator. */
     
    6968    RTTEST_CHECK(hTest, papszStrings && RTStrICmp(papszStrings[0], "foo") == 0);
    7069
    71     /* Multiple strings w/ separators. */
    72     const char szWithSep2[] = "foo\r\nbar\r\n";
     70    /* Multiple strings w/ separator. */
     71    const char szWithSep2[] = "foo\r\nbar";
    7372    RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep2, sizeof(szWithSep2), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
    7473    RTTEST_CHECK(hTest, cStrings == 2);
     
    8887
    8988    /* Multiple strings w/ two consequtive separators. */
    90     const char szWithSep4[] = "foo\r\nbar\r\n\r\nbaz\r\n";
     89    const char szWithSep4[] = "foo\r\nbar\r\n\r\nbaz";
    9190    RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep4, sizeof(szWithSep4), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
    9291    RTTEST_CHECK(hTest, cStrings == 3);
     
    9796                        && RTStrICmp(papszStrings[2], "baz") == 0);
    9897
     98    /* Multiple strings w/ trailing separators. */
     99    const char szWithSep5[] = "foo\r\nbar\r\n\r\nbaz\r\n\r\n";
     100    RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep5, sizeof(szWithSep5), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
     101    RTTEST_CHECK(hTest, cStrings == 3);
     102    RTTEST_CHECK(hTest,    cStrings == 3
     103                        && papszStrings
     104                        && RTStrICmp(papszStrings[0], "foo") == 0
     105                        && RTStrICmp(papszStrings[1], "bar") == 0
     106                        && RTStrICmp(papszStrings[2], "baz") == 0);
    99107    /*
    100108     * Summary.
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