string: add spdk_str_chomp() function
Add a helper function to remove trailing newlines. Change-Id: I8b1a2bf3d70ef17e0bc7e74429ac955c68cb6bcf Signed-off-by: Daniel Verkamp <daniel.verkamp@intel.com> Reviewed-on: https://review.gerrithub.io/370592 Reviewed-by: Ben Walker <benjamin.walker@intel.com> Tested-by: SPDK Automated Test System <sys_sgsw@intel.com>
This commit is contained in:
parent
2019ebf43e
commit
16c362a8de
@ -89,6 +89,16 @@ char *spdk_strsepq(char **stringp, const char *delim);
|
|||||||
*/
|
*/
|
||||||
char *spdk_str_trim(char *s);
|
char *spdk_str_trim(char *s);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove trailing newlines from the end of a string in place.
|
||||||
|
*
|
||||||
|
* Any sequence of trailing \r and \n characters is removed from the end of the string.
|
||||||
|
*
|
||||||
|
* \param s String to remove newline from.
|
||||||
|
* \return Number of characters removed.
|
||||||
|
*/
|
||||||
|
size_t spdk_str_chomp(char *s);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copy a string into a fixed-size buffer, padding extra bytes with a specific character.
|
* Copy a string into a fixed-size buffer, padding extra bytes with a specific character.
|
||||||
*
|
*
|
||||||
|
@ -307,3 +307,22 @@ spdk_parse_ip_addr(char *ip, char **host, char **port)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t
|
||||||
|
spdk_str_chomp(char *s)
|
||||||
|
{
|
||||||
|
size_t len = strlen(s);
|
||||||
|
size_t removed = 0;
|
||||||
|
|
||||||
|
while (len > 0) {
|
||||||
|
if (s[len - 1] != '\r' && s[len - 1] != '\n') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
s[len - 1] = '\0';
|
||||||
|
len--;
|
||||||
|
removed++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
@ -95,6 +95,47 @@ test_parse_ip_addr(void)
|
|||||||
CU_ASSERT_EQUAL(port, NULL);
|
CU_ASSERT_EQUAL(port, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_str_chomp(void)
|
||||||
|
{
|
||||||
|
char s[1024];
|
||||||
|
|
||||||
|
/* One \n newline */
|
||||||
|
snprintf(s, sizeof(s), "%s", "hello world\n");
|
||||||
|
CU_ASSERT(spdk_str_chomp(s) == 1);
|
||||||
|
CU_ASSERT(strcmp(s, "hello world") == 0);
|
||||||
|
|
||||||
|
/* One \r\n newline */
|
||||||
|
snprintf(s, sizeof(s), "%s", "hello world\r\n");
|
||||||
|
CU_ASSERT(spdk_str_chomp(s) == 2);
|
||||||
|
CU_ASSERT(strcmp(s, "hello world") == 0);
|
||||||
|
|
||||||
|
/* No newlines */
|
||||||
|
snprintf(s, sizeof(s), "%s", "hello world");
|
||||||
|
CU_ASSERT(spdk_str_chomp(s) == 0);
|
||||||
|
CU_ASSERT(strcmp(s, "hello world") == 0);
|
||||||
|
|
||||||
|
/* Two newlines */
|
||||||
|
snprintf(s, sizeof(s), "%s", "hello world\n\n");
|
||||||
|
CU_ASSERT(spdk_str_chomp(s) == 2);
|
||||||
|
CU_ASSERT(strcmp(s, "hello world") == 0);
|
||||||
|
|
||||||
|
/* Empty string */
|
||||||
|
snprintf(s, sizeof(s), "%s", "");
|
||||||
|
CU_ASSERT(spdk_str_chomp(s) == 0);
|
||||||
|
CU_ASSERT(strcmp(s, "") == 0);
|
||||||
|
|
||||||
|
/* One-character string with only \n */
|
||||||
|
snprintf(s, sizeof(s), "%s", "\n");
|
||||||
|
CU_ASSERT(spdk_str_chomp(s) == 1);
|
||||||
|
CU_ASSERT(strcmp(s, "") == 0);
|
||||||
|
|
||||||
|
/* One-character string without a newline */
|
||||||
|
snprintf(s, sizeof(s), "%s", "a");
|
||||||
|
CU_ASSERT(spdk_str_chomp(s) == 0);
|
||||||
|
CU_ASSERT(strcmp(s, "a") == 0);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
@ -112,7 +153,8 @@ main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
CU_add_test(suite, "test_parse_ip_addr", test_parse_ip_addr) == NULL) {
|
CU_add_test(suite, "test_parse_ip_addr", test_parse_ip_addr) == NULL ||
|
||||||
|
CU_add_test(suite, "test_str_chomp", test_str_chomp) == NULL) {
|
||||||
CU_cleanup_registry();
|
CU_cleanup_registry();
|
||||||
return CU_get_error();
|
return CU_get_error();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user