mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-02-04 14:10:36 +01:00
shared: add space escape functions
This commit is contained in:
parent
da3b72d842
commit
e0bbaf6a39
3 changed files with 109 additions and 0 deletions
|
|
@ -7074,6 +7074,53 @@ test_ethtool_offload (void)
|
|||
g_assert_cmpstr (d->optname, ==, NM_ETHTOOL_OPTNAME_FEATURE_RXHASH);
|
||||
}
|
||||
|
||||
static void
|
||||
test_nm_utils_escape_spaces (void)
|
||||
{
|
||||
char *to_free;
|
||||
|
||||
g_assert_cmpstr (_nm_utils_escape_spaces (NULL, &to_free), ==, NULL);
|
||||
g_free (to_free);
|
||||
|
||||
g_assert_cmpstr (_nm_utils_escape_spaces ("", &to_free), ==, "");
|
||||
g_free (to_free);
|
||||
|
||||
g_assert_cmpstr (_nm_utils_escape_spaces (" ", &to_free), ==, "\\ ");
|
||||
g_free (to_free);
|
||||
|
||||
g_assert_cmpstr (_nm_utils_escape_spaces ("\t ", &to_free), ==, "\\\t\\ ");
|
||||
g_free (to_free);
|
||||
|
||||
g_assert_cmpstr (_nm_utils_escape_spaces ("abc", &to_free), ==, "abc");
|
||||
g_free (to_free);
|
||||
|
||||
g_assert_cmpstr (_nm_utils_escape_spaces ("abc def", &to_free), ==, "abc\\ def");
|
||||
g_free (to_free);
|
||||
|
||||
g_assert_cmpstr (_nm_utils_escape_spaces ("abc\tdef", &to_free), ==, "abc\\\tdef");
|
||||
g_free (to_free);
|
||||
}
|
||||
|
||||
static void
|
||||
test_nm_utils_unescape_spaces (void)
|
||||
{
|
||||
#define CHECK_STR(in, out) \
|
||||
G_STMT_START { \
|
||||
gs_free char *str = g_strdup (in); \
|
||||
\
|
||||
g_assert_cmpstr (_nm_utils_unescape_spaces (str), ==, out); \
|
||||
} G_STMT_END
|
||||
|
||||
CHECK_STR ("\\a", "\\a");
|
||||
CHECK_STR ("foobar", "foobar");
|
||||
CHECK_STR ("foo bar", "foo bar");
|
||||
CHECK_STR ("foo\\ bar", "foo bar");
|
||||
CHECK_STR ("foo\\", "foo\\");
|
||||
CHECK_STR ("\\\\\t", "\\\t");
|
||||
|
||||
#undef CHECK_STR
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
NMTST_DEFINE ();
|
||||
|
|
@ -7224,6 +7271,8 @@ int main (int argc, char **argv)
|
|||
g_test_add_func ("/core/general/_nm_utils_dns_option_find_idx", test_nm_utils_dns_option_find_idx);
|
||||
g_test_add_func ("/core/general/_nm_utils_validate_json", test_nm_utils_check_valid_json);
|
||||
g_test_add_func ("/core/general/_nm_utils_team_config_equal", test_nm_utils_team_config_equal);
|
||||
g_test_add_func ("/core/general/_nm_utils_escape_spaces", test_nm_utils_escape_spaces);
|
||||
g_test_add_func ("/core/general/_nm_utils_unescape_spaces", test_nm_utils_unescape_spaces);
|
||||
g_test_add_func ("/core/general/test_nm_utils_enum", test_nm_utils_enum);
|
||||
g_test_add_func ("/core/general/nm-set-out", test_nm_set_out);
|
||||
g_test_add_func ("/core/general/route_attributes/parse", test_route_attributes_parse);
|
||||
|
|
|
|||
|
|
@ -1702,3 +1702,60 @@ _nm_utils_user_data_unpack (gpointer user_data, int nargs, ...)
|
|||
|
||||
g_slice_free1 (((gsize) nargs) * sizeof (gconstpointer), user_data);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
#define IS_SPACE(c) NM_IN_SET ((c), ' ', '\t')
|
||||
|
||||
const char *
|
||||
_nm_utils_escape_spaces (const char *str, char **to_free)
|
||||
{
|
||||
const char *ptr = str;
|
||||
char *ret, *r;
|
||||
|
||||
*to_free = NULL;
|
||||
|
||||
if (!str)
|
||||
return NULL;
|
||||
|
||||
while (TRUE) {
|
||||
if (!*ptr)
|
||||
return str;
|
||||
if (IS_SPACE (*ptr))
|
||||
break;
|
||||
ptr++;
|
||||
}
|
||||
|
||||
ptr = str;
|
||||
ret = g_new (char, strlen (str) * 2 + 1);
|
||||
r = ret;
|
||||
*to_free = ret;
|
||||
while (*ptr) {
|
||||
if (IS_SPACE (*ptr))
|
||||
*r++ = '\\';
|
||||
*r++ = *ptr++;
|
||||
}
|
||||
*r = '\0';
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *
|
||||
_nm_utils_unescape_spaces (char *str)
|
||||
{
|
||||
guint i, j = 0;
|
||||
|
||||
if (!str)
|
||||
return NULL;
|
||||
|
||||
for (i = 0; str[i]; i++) {
|
||||
if (str[i] == '\\' && IS_SPACE (str[i+1]))
|
||||
i++;
|
||||
str[j++] = str[i];
|
||||
}
|
||||
str[j] = '\0';
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
#undef IS_SPACE
|
||||
|
|
|
|||
|
|
@ -718,4 +718,7 @@ void _nm_utils_user_data_unpack (gpointer user_data, int nargs, ...);
|
|||
|
||||
/*****************************************************************************/
|
||||
|
||||
const char *_nm_utils_escape_spaces (const char *str, char **to_free);
|
||||
char *_nm_utils_unescape_spaces (char *str);
|
||||
|
||||
#endif /* __NM_SHARED_UTILS_H__ */
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue