ICU-1043 strtok_r should work with strings ending with delimiters.

X-SVN-Rev: 5245
This commit is contained in:
George Rhoten 2001-07-16 22:57:39 +00:00
parent ec3de1fc13
commit 1f063c715d
2 changed files with 32 additions and 7 deletions

View File

@ -258,15 +258,23 @@ u_strtok_r(UChar *src,
nonDelimIdx = u_strspn(tokSource, delim); nonDelimIdx = u_strspn(tokSource, delim);
tokSource = &tokSource[nonDelimIdx]; tokSource = &tokSource[nonDelimIdx];
nextToken = u_strpbrk(tokSource, delim); if (*tokSource) {
if (nextToken != NULL) { nextToken = u_strpbrk(tokSource, delim);
*(nextToken++) = 0; if (nextToken != NULL) {
*saveState = nextToken; /* Create a token */
return tokSource; *(nextToken++) = 0;
*saveState = nextToken;
return tokSource;
}
else if (saveState && *saveState) {
/* Return the last token */
*saveState = NULL;
return tokSource;
}
} }
else if (saveState && *saveState) { else {
/* No tokens were found. Only delimiters were left. */
*saveState = NULL; *saveState = NULL;
return tokSource;
} }
return NULL; return NULL;
} }

View File

@ -989,6 +989,23 @@ static void TestStringFunctions()
if (state != NULL) { if (state != NULL) {
log_err("State should be NULL for empty string\n"); log_err("State should be NULL for empty string\n");
} }
u_uastrcpy(currTokenBuf, ", ,");
if (u_strtok_r(currTokenBuf, delimBuf, &state) != NULL) {
log_err("Didn't get NULL for a string of delimiters\n");
}
if (state != NULL) {
log_err("State should be NULL for a string of delimiters\n");
}
u_uastrcpy(currTokenBuf, "q, ,");
if (u_strtok_r(currTokenBuf, delimBuf, &state) == NULL) {
log_err("Got NULL for a string that does not begin with delimiters\n");
}
if (u_strtok_r(NULL, delimBuf, &state) != NULL) {
log_err("Didn't get NULL for a string that ends in delimiters\n");
}
if (state != NULL) {
log_err("State should be NULL for empty string\n");
}
} }
/* test u_strcmpCodePointOrder() */ /* test u_strcmpCodePointOrder() */