From 16c362a8de70efb246b69b38c820f974dd7c2965 Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Thu, 20 Jul 2017 09:24:57 -0700 Subject: [PATCH] string: add spdk_str_chomp() function Add a helper function to remove trailing newlines. Change-Id: I8b1a2bf3d70ef17e0bc7e74429ac955c68cb6bcf Signed-off-by: Daniel Verkamp Reviewed-on: https://review.gerrithub.io/370592 Reviewed-by: Ben Walker Tested-by: SPDK Automated Test System --- include/spdk/string.h | 10 ++++++ lib/util/string.c | 19 +++++++++++ test/unit/lib/util/string.c/string_ut.c | 44 ++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/include/spdk/string.h b/include/spdk/string.h index b0ee0ad0c..5db46c534 100644 --- a/include/spdk/string.h +++ b/include/spdk/string.h @@ -89,6 +89,16 @@ char *spdk_strsepq(char **stringp, const char *delim); */ 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. * diff --git a/lib/util/string.c b/lib/util/string.c index d9aaad80c..471cf50d6 100644 --- a/lib/util/string.c +++ b/lib/util/string.c @@ -307,3 +307,22 @@ spdk_parse_ip_addr(char *ip, char **host, char **port) 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; +} diff --git a/test/unit/lib/util/string.c/string_ut.c b/test/unit/lib/util/string.c/string_ut.c index d0baec870..6a70a9c35 100644 --- a/test/unit/lib/util/string.c/string_ut.c +++ b/test/unit/lib/util/string.c/string_ut.c @@ -95,6 +95,47 @@ test_parse_ip_addr(void) 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 main(int argc, char **argv) { @@ -112,7 +153,8 @@ main(int argc, char **argv) } 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(); return CU_get_error(); }