From ab9dc9f6d4a425c28900c5ed4332df3e9aacc9fe Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 27 Mar 2020 10:10:27 +0100 Subject: [PATCH] shared: refactor initializing character lookup tables for strsplit --- shared/nm-glib-aux/nm-shared-utils.c | 29 ++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c index 4ad2e43606..fffd761b7a 100644 --- a/shared/nm-glib-aux/nm-shared-utils.c +++ b/shared/nm-glib-aux/nm-shared-utils.c @@ -1496,13 +1496,28 @@ comp_l: /*****************************************************************************/ +static void +_char_lookup_table_set_one (guint8 lookup[static 256], + char ch) +{ + lookup[(guint8) ch] = 1; +} + +static void +_char_lookup_table_set_all (guint8 lookup[static 256], + const char *candidates) +{ + while (candidates[0] != '\0') + _char_lookup_table_set_one (lookup, (candidates++)[0]); +} + 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; + if (candidates) + _char_lookup_table_set_all (lookup, candidates); } static gboolean @@ -1717,11 +1732,9 @@ done2: /* We no longer need ch_lookup for its original purpose. Modify it, so it * can detect the delimiters, '\\', and (optionally) whitespaces. */ - ch_lookup[((guint8) '\\')] = 1; - if (f_strstrip) { - for (i = 0; NM_ASCII_SPACES[i]; i++) - ch_lookup[((guint8) (NM_ASCII_SPACES[i]))] = 1; - } + _char_lookup_table_set_one (ch_lookup, '\\'); + if (f_strstrip) + _char_lookup_table_set_all (ch_lookup, NM_ASCII_SPACES); for (i_token = 0; ptr[i_token]; i_token++) { s = (char *) ptr[i_token]; @@ -1767,7 +1780,7 @@ nm_utils_escaped_tokens_escape (const char *str, _char_lookup_table_init (ch_lookup, delimiters); /* also mark '\\' as requiring escaping. */ - ch_lookup[((guint8) '\\')] = 1; + _char_lookup_table_set_one (ch_lookup, '\\'); n_escapes = 0; for (i = 0; str[i] != '\0'; i++) {