shared: add _nm_utils_escape_plain() API

This commit is contained in:
Thomas Haller
2019-03-20 14:24:04 +01:00
parent e26e0fdffd
commit da5895951a
2 changed files with 40 additions and 12 deletions

View File

@@ -962,6 +962,15 @@ comp_l:
/*****************************************************************************/ /*****************************************************************************/
static void
_char_lookup_table_init (guint8 lookup[static 256],
const char *candidates)
{
memset (lookup, 0, 256);
while (candidates[0] != '\0')
lookup[(guint8) ((candidates++)[0])] = 1;
}
/** /**
* nm_utils_strsplit_set: * nm_utils_strsplit_set:
* @str: the string to split. * @str: the string to split.
@@ -1007,9 +1016,8 @@ nm_utils_strsplit_set (const char *str, const char *delimiters, gboolean allow_e
/* initialize lookup table for delimiter */ /* initialize lookup table for delimiter */
if (!delimiters) if (!delimiters)
delimiters = " \t\n"; delimiters = " \t\n";
memset (delimiters_table, 0, sizeof (delimiters_table));
for (i = 0; delimiters[i]; i++) _char_lookup_table_init (delimiters_table, delimiters);
delimiters_table[(guint8) delimiters[i]] = 1;
#define _is_delimiter(ch, delimiters_table, allow_esc, esc) \ #define _is_delimiter(ch, delimiters_table, allow_esc, esc) \
((delimiters_table)[(guint8) (ch)] != 0 && (!allow_esc || !esc)) ((delimiters_table)[(guint8) (ch)] != 0 && (!allow_esc || !esc))
@@ -2422,21 +2430,26 @@ _nm_utils_user_data_unpack (gpointer user_data, int nargs, ...)
/*****************************************************************************/ /*****************************************************************************/
#define _CH_LOOKUP(ch_lookup, ch) (!!((ch_lookup)[(guint8) (ch)]))
const char * const char *
_nm_utils_escape_spaces (const char *str, char **to_free) _nm_utils_escape_plain (const char *str, const char *candidates, char **to_free)
{ {
const char *ptr = str; const char *ptr = str;
char *ret, *r; char *ret, *r;
guint8 ch_lookup[256];
*to_free = NULL; *to_free = NULL;
if (!str) if (!str)
return NULL; return NULL;
_char_lookup_table_init (ch_lookup, candidates ?: NM_ASCII_SPACES);
while (TRUE) { while (TRUE) {
if (!*ptr) if (!*ptr)
return str; return str;
if (g_ascii_isspace (*ptr)) if (_CH_LOOKUP (ch_lookup, *ptr))
break; break;
ptr++; ptr++;
} }
@@ -2446,7 +2459,7 @@ _nm_utils_escape_spaces (const char *str, char **to_free)
r = ret; r = ret;
*to_free = ret; *to_free = ret;
while (*ptr) { while (*ptr) {
if (g_ascii_isspace (*ptr)) if (_CH_LOOKUP (ch_lookup, *ptr))
*r++ = '\\'; *r++ = '\\';
*r++ = *ptr++; *r++ = *ptr++;
} }
@@ -2456,23 +2469,26 @@ _nm_utils_escape_spaces (const char *str, char **to_free)
} }
char * char *
_nm_utils_unescape_spaces (char *str, gboolean do_strip) _nm_utils_unescape_plain (char *str, const char *candidates, gboolean do_strip)
{ {
gsize i = 0; gsize i = 0;
gsize j = 0; gsize j = 0;
gsize preserve_space_at = 0; gsize preserve_space_at = 0;
guint8 ch_lookup[256];
if (!str) if (!str)
return NULL; return NULL;
_char_lookup_table_init (ch_lookup, candidates ?: NM_ASCII_SPACES);
if (do_strip) { if (do_strip) {
while (str[i] && g_ascii_isspace (str[i])) while (str[i] && _CH_LOOKUP (ch_lookup, str[i]))
i++; i++;
} }
for (; str[i]; i++) { for (; str[i]; i++) {
if ( str[i] == '\\' if ( str[i] == '\\'
&& g_ascii_isspace (str[i+1])) { && _CH_LOOKUP (ch_lookup, str[i+1])) {
preserve_space_at = j; preserve_space_at = j;
i++; i++;
} }
@@ -2482,7 +2498,7 @@ _nm_utils_unescape_spaces (char *str, gboolean do_strip)
if (do_strip && j > 0) { if (do_strip && j > 0) {
while ( --j > preserve_space_at while ( --j > preserve_space_at
&& g_ascii_isspace (str[j])) && _CH_LOOKUP (ch_lookup, str[j]))
str[j] = '\0'; str[j] = '\0';
} }

View File

@@ -1084,8 +1084,20 @@ void _nm_utils_user_data_unpack (gpointer user_data, int nargs, ...);
/*****************************************************************************/ /*****************************************************************************/
const char *_nm_utils_escape_spaces (const char *str, char **to_free); const char *_nm_utils_escape_plain (const char *str, const char *candidates, char **to_free);
char *_nm_utils_unescape_spaces (char *str, gboolean do_strip); char *_nm_utils_unescape_plain (char *str, const char *candidates, gboolean do_strip);
static inline const char *
_nm_utils_escape_spaces (const char *str, char **to_free)
{
return _nm_utils_escape_plain (str, NM_ASCII_SPACES, to_free);
}
static inline char *
_nm_utils_unescape_spaces (char *str, gboolean do_strip)
{
return _nm_utils_unescape_plain (str, NM_ASCII_SPACES, do_strip);
}
/*****************************************************************************/ /*****************************************************************************/