Changeset 26482 in vbox
- Timestamp:
- Feb 14, 2010 1:38:44 AM (15 years ago)
- svn:sync-xref-src-repo-rev:
- 57651
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/string.h
r26481 r26482 258 258 259 259 /** 260 * Truncates an IPRT allocated string. 261 * 262 * @retval VINF_SUCCESS. 263 * @retval VERR_OUT_OF_RANGE if cchNew is too long. Nothing is done. 264 * 265 * @param ppsz Pointer to the string pointer. The string 266 * pointer can be NULL if @a cchNew is 0, no change 267 * is made then. If we actually reallocate the 268 * string, the string pointer might be changed by 269 * this call. (In/Out) 270 * @param cchNew The new string length (excluding the 271 * terminator). The string must be at least this 272 * long or we'll return VERR_OUT_OF_RANGE and 273 * assert on you. 274 */ 275 RTDECL(int) RTStrATruncate(char **ppsz, size_t cchNew); 276 277 /** 260 278 * Allocates memory for string storage. 261 279 * … … 302 320 * 303 321 * You should normally not have use this function, except perhaps to truncate a 304 * really long string you've got from some IPRT string API. 322 * really long string you've got from some IPRT string API, but then you should 323 * use RTStrATruncate. 305 324 * 306 325 * @returns VINF_SUCCESS. -
trunk/src/VBox/Runtime/common/string/stringalloc.cpp
r26481 r26482 249 249 RT_EXPORT_SYMBOL(RTStrAAppendExN); 250 250 251 252 RTDECL(int) RTStrATruncate(char **ppsz, size_t cchNew) 253 { 254 char *pszOld = *ppsz; 255 if (!cchNew) 256 { 257 if (pszOld && *pszOld) 258 { 259 *pszOld = '\0'; 260 char *pszNew = (char *)RTMemRealloc(pszOld, 1); 261 if (!pszNew) 262 *ppsz = pszNew; 263 } 264 } 265 else 266 { 267 AssertPtrReturn(pszOld, VERR_OUT_OF_RANGE); 268 char *pszZero = (char *)memchr(pszOld, '\0', cchNew + 63); 269 AssertReturn(!pszZero || (size_t)(pszZero - pszOld) >= cchNew, VERR_OUT_OF_RANGE); 270 pszOld[cchNew] = '\0'; 271 if (!pszZero) 272 { 273 char *pszNew = (char *)RTMemRealloc(pszOld, cchNew + 1); 274 if (pszNew) 275 *ppsz = pszNew; 276 } 277 } 278 return VINF_SUCCESS; 279 } 280 RT_EXPORT_SYMBOL(RTStrATruncate); 281 -
trunk/src/VBox/Runtime/testcase/tstRTStrAlloc.cpp
r26481 r26482 203 203 RTTESTI_CHECK(!RTStrCmp(psz, "abcdefghijklmnopqrstuvwxyz-")); 204 204 RTStrFree(psz); 205 206 /* RTStrATruncate */ 207 psz = NULL; 208 RTTESTI_CHECK_RC(RTStrATruncate(&psz, 0), VINF_SUCCESS); 209 RTTESTI_CHECK(!psz); 210 211 RTTESTI_CHECK(psz = RTStrDup("")); 212 RTTESTI_CHECK_RC(RTStrATruncate(&psz, 0), VINF_SUCCESS); 213 RTStrFree(psz); 214 215 RTTESTI_CHECK(psz = RTStrDup("1234567890")); 216 RTTESTI_CHECK_RC(RTStrATruncate(&psz, 5), VINF_SUCCESS); 217 RTTESTI_CHECK(!RTStrCmp(psz, "12345")); 218 RTStrFree(psz); 219 220 psz = NULL; 221 for (uint32_t i = 0; i < 128; i++) 222 RTTESTI_CHECK_RC_RETV(RTStrAAppend(&psz, "abcdefghijklmnopqrstuvwxyz"), VINF_SUCCESS); 223 RTTESTI_CHECK_RC(RTStrATruncate(&psz, sizeof("abcdefghijklmnopqrstuvwxyz") - 1), VINF_SUCCESS); 224 RTTESTI_CHECK(!RTStrCmp(psz, "abcdefghijklmnopqrstuvwxyz")); 225 RTTESTI_CHECK_RC(RTStrATruncate(&psz, 6), VINF_SUCCESS); 226 RTTESTI_CHECK(!RTStrCmp(psz, "abcdef")); 227 RTTESTI_CHECK_RC(RTStrATruncate(&psz, 1), VINF_SUCCESS); 228 RTTESTI_CHECK(!RTStrCmp(psz, "a")); 229 RTTESTI_CHECK_RC(RTStrATruncate(&psz, 0), VINF_SUCCESS); 230 RTTESTI_CHECK(!RTStrCmp(psz, "")); 231 RTStrFree(psz); 232 205 233 } 206 234
Note:
See TracChangeset
for help on using the changeset viewer.