Changeset 85345 in vbox
- Timestamp:
- Jul 15, 2020 8:03:03 AM (5 years ago)
- svn:sync-xref-src-repo-rev:
- 139330
- Location:
- trunk/src/VBox/Runtime
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/string/RTStrSplit.cpp
r85154 r85345 46 46 47 47 /* 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; 50 52 const size_t cchSep = strlen(pcszSeparator); 53 size_t cchNext; 51 54 while (cch > 0) 52 55 { 53 char const *pszNext = RTStrStr(pszTmp, pcszSeparator);54 if (!p szNext)56 pcszNext = RTStrStr(pcszTmp, pcszSeparator); 57 if (!pcszNext) 55 58 break; 56 c onst size_t cchNext = pszNext - pszTmp;59 cchNext = pcszNext - pcszTmp; 57 60 if (cchNext + cchSep > cch) 58 61 break; 59 pszTmp += cchNext + cchSep; 60 cch -= cchNext + cchSep; 62 pcszNext += cchSep; 63 pcszTmp += cchNext + cchSep; 64 cch -= cchNext + cchSep; 61 65 if (cchNext) 62 66 ++cStrings; 63 67 } 64 68 69 if (pcszTmp != pcszEnd) /* Do we need to take a trailing string without separator into account? */ 70 cStrings++; 71 65 72 if (!cStrings) 66 73 { 67 *pcStrings = 0; 74 *ppapszStrings = NULL; 75 *pcStrings = 0; 68 76 return VINF_SUCCESS; 69 77 } … … 76 84 77 85 cch = cbStrings - 1; 78 p szTmp = pcszStrings;86 pcszTmp = pcszStrings; 79 87 80 for (size_t i = 0; i < 3;)88 for (size_t i = 0; i < cStrings;) 81 89 { 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; 88 94 if (cchNext) 89 95 { 90 papszStrings[i] = RTStrDupN(p szTmp, cchNext);96 papszStrings[i] = RTStrDupN(pcszTmp, cchNext); 91 97 if (!papszStrings[i]) 92 98 { … … 96 102 i++; 97 103 } 98 p szTmp += cchNext + cchSep;99 cch -= cchNext + cchSep;104 pcszTmp += cchNext + cchSep; 105 cch -= cchNext + cchSep; 100 106 } 101 107 -
trunk/src/VBox/Runtime/testcase/tstRTStrSplit.cpp
r85155 r85345 55 55 RTTEST_CHECK_RC(hTest, RTStrSplit(szEmpty, sizeof(szEmpty), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS); 56 56 RTTEST_CHECK(hTest, cStrings == 0); 57 RTTEST_CHECK(hTest, papszStrings == NULL);58 57 59 58 /* No separator given. */ 60 59 const char szNoSep[] = "foo"; 61 60 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); 64 63 65 64 /* Single string w/ separator. */ … … 69 68 RTTEST_CHECK(hTest, papszStrings && RTStrICmp(papszStrings[0], "foo") == 0); 70 69 71 /* Multiple strings w/ separator s. */72 const char szWithSep2[] = "foo\r\nbar \r\n";70 /* Multiple strings w/ separator. */ 71 const char szWithSep2[] = "foo\r\nbar"; 73 72 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep2, sizeof(szWithSep2), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS); 74 73 RTTEST_CHECK(hTest, cStrings == 2); … … 88 87 89 88 /* 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"; 91 90 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep4, sizeof(szWithSep4), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS); 92 91 RTTEST_CHECK(hTest, cStrings == 3); … … 97 96 && RTStrICmp(papszStrings[2], "baz") == 0); 98 97 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); 99 107 /* 100 108 * Summary.
Note:
See TracChangeset
for help on using the changeset viewer.