mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 17:34:18 +03:00
Add functions dealing with control characters in strings
Add virStringHasControlChars that checks if the string has any control characters other than \t\r\n, and virStringStripControlChars that removes them in-place.
This commit is contained in:
parent
e892842dfd
commit
2a530a3e50
@ -2139,6 +2139,7 @@ virStrdup;
|
||||
virStringArrayHasString;
|
||||
virStringFreeList;
|
||||
virStringFreeListCount;
|
||||
virStringHasControlChars;
|
||||
virStringIsEmpty;
|
||||
virStringJoin;
|
||||
virStringListLength;
|
||||
@ -2148,6 +2149,7 @@ virStringSortCompare;
|
||||
virStringSortRevCompare;
|
||||
virStringSplit;
|
||||
virStringSplitCount;
|
||||
virStringStripControlChars;
|
||||
virStringStripIPv6Brackets;
|
||||
virStrncpy;
|
||||
virStrndup;
|
||||
|
@ -968,3 +968,45 @@ virStringStripIPv6Brackets(char *str)
|
||||
str[len - 2] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const char control_chars[] =
|
||||
"\x01\x02\x03\x04\x05\x06\x07"
|
||||
"\x08" /* \t \n */ "\x0B\x0C" /* \r */ "\x0E\x0F"
|
||||
"\x10\x11\x12\x13\x14\x15\x16\x17"
|
||||
"\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
|
||||
|
||||
bool
|
||||
virStringHasControlChars(const char *str)
|
||||
{
|
||||
if (!str)
|
||||
return false;
|
||||
|
||||
return str[strcspn(str, control_chars)] != '\0';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* virStringStripControlChars:
|
||||
* @str: the string to strip
|
||||
*
|
||||
* Modify the string in-place to remove the control characters
|
||||
* in the interval: [0x01, 0x20)
|
||||
*/
|
||||
void
|
||||
virStringStripControlChars(char *str)
|
||||
{
|
||||
size_t len, i, j;
|
||||
|
||||
if (!str)
|
||||
return;
|
||||
|
||||
len = strlen(str);
|
||||
for (i = 0, j = 0; i < len; i++) {
|
||||
if (index(control_chars, str[i]))
|
||||
continue;
|
||||
|
||||
str[j++] = str[i];
|
||||
}
|
||||
str[j] = '\0';
|
||||
}
|
||||
|
@ -271,5 +271,7 @@ char *virStringReplace(const char *haystack,
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
|
||||
|
||||
void virStringStripIPv6Brackets(char *str);
|
||||
bool virStringHasControlChars(const char *str);
|
||||
void virStringStripControlChars(char *str);
|
||||
|
||||
#endif /* __VIR_STRING_H__ */
|
||||
|
@ -551,6 +551,29 @@ static int testStripIPv6Brackets(const void *args)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int testStripControlChars(const void *args)
|
||||
{
|
||||
const struct testStripData *data = args;
|
||||
int ret = -1;
|
||||
char *res = NULL;
|
||||
|
||||
if (VIR_STRDUP(res, data->string) < 0)
|
||||
goto cleanup;
|
||||
|
||||
virStringStripControlChars(res);
|
||||
|
||||
if (STRNEQ_NULLABLE(res, data->result)) {
|
||||
fprintf(stderr, "Returned '%s', expected '%s'\n",
|
||||
NULLSTR(res), NULLSTR(data->result));
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
cleanup:
|
||||
VIR_FREE(res);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
mymain(void)
|
||||
@ -783,6 +806,22 @@ mymain(void)
|
||||
TEST_STRIP_IPV6_BRACKETS(":hello]", ":hello]");
|
||||
TEST_STRIP_IPV6_BRACKETS(":[]:", ":[]:");
|
||||
|
||||
#define TEST_STRIP_CONTROL_CHARS(str, res) \
|
||||
do { \
|
||||
struct testStripData stripData = { \
|
||||
.string = str, \
|
||||
.result = res, \
|
||||
}; \
|
||||
if (virtTestRun("Strip control chars from " #str, \
|
||||
testStripControlChars, &stripData) < 0) \
|
||||
ret = -1; \
|
||||
} while (0)
|
||||
|
||||
TEST_STRIP_CONTROL_CHARS(NULL, NULL);
|
||||
TEST_STRIP_CONTROL_CHARS("\nhello \r hello\t", "\nhello \r hello\t");
|
||||
TEST_STRIP_CONTROL_CHARS("\x01H\x02" "E\x03L\x04L\x05O", "HELLO");
|
||||
TEST_STRIP_CONTROL_CHARS("\x01\x02\x03\x04HELL\x05O", "HELLO");
|
||||
TEST_STRIP_CONTROL_CHARS("\nhello \x01\x07hello\t", "\nhello hello\t");
|
||||
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user