Commit 16c362a8 authored by Daniel Verkamp's avatar Daniel Verkamp Committed by Ben Walker
Browse files

string: add spdk_str_chomp() function



Add a helper function to remove trailing newlines.

Change-Id: I8b1a2bf3d70ef17e0bc7e74429ac955c68cb6bcf
Signed-off-by: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
Reviewed-on: https://review.gerrithub.io/370592


Reviewed-by: default avatarBen Walker <benjamin.walker@intel.com>
Tested-by: default avatarSPDK Automated Test System <sys_sgsw@intel.com>
parent 2019ebf4
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -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.
 *
+19 −0
Original line number Diff line number Diff line
@@ -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;
}
+43 −1
Original line number Diff line number Diff line
@@ -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();
	}