From 54a9e72250ee06f7b13201c4290d2d70726d666f Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Thu, 21 Oct 2010 17:33:32 +0400 Subject: [PATCH] Some optimization of loops in translation functions. Signed-off-by: Andrew Borodin --- lib/search/glob.c | 39 ++++++++++----------------------------- lib/search/hex.c | 28 ++++++++++++++++++---------- lib/search/normal.c | 15 +++++++-------- 3 files changed, 35 insertions(+), 47 deletions(-) diff --git a/lib/search/glob.c b/lib/search/glob.c index 6e9907600..62ae712b8 100644 --- a/lib/search/glob.c +++ b/lib/search/glob.c @@ -52,55 +52,38 @@ mc_search__glob_translate_to_regex (const GString * astr) { const char *str = astr->str; GString *buff; - gsize loop = 0; + gsize loop; gboolean inside_group = FALSE; buff = g_string_sized_new (32); - while (loop < astr->len) - { + for (loop = 0; loop < astr->len; loop++) switch (str[loop]) { case '*': if (!strutils_is_char_escaped (str, &(str[loop]))) - { - g_string_append (buff, (inside_group) ? ".*" : "(.*)"); - loop++; - continue; - } + g_string_append (buff, inside_group ? ".*" : "(.*)"); break; case '?': if (!strutils_is_char_escaped (str, &(str[loop]))) - { - g_string_append (buff, (inside_group) ? "." : "(.)"); - loop++; - continue; - } + g_string_append (buff, inside_group ? "." : "(.)"); break; case ',': if (!strutils_is_char_escaped (str, &(str[loop]))) - { - g_string_append (buff, "|"); - loop++; - continue; - } + g_string_append_c (buff, '|'); break; case '{': if (!strutils_is_char_escaped (str, &(str[loop]))) { - g_string_append (buff, "("); + g_string_append_c (buff, '('); inside_group = TRUE; - loop++; - continue; } break; case '}': if (!strutils_is_char_escaped (str, &(str[loop]))) { - g_string_append (buff, ")"); + g_string_append_c (buff, ')'); inside_group = FALSE; - loop++; - continue; } break; case '+': @@ -110,13 +93,11 @@ mc_search__glob_translate_to_regex (const GString * astr) case ')': case '^': g_string_append_c (buff, '\\'); + /* fall through */ + default: g_string_append_c (buff, str[loop]); - loop++; - continue; + break; } - g_string_append_c (buff, str[loop]); - loop++; - } return buff; } diff --git a/lib/search/hex.c b/lib/search/hex.c index d1d52febe..9bcc89d8e 100644 --- a/lib/search/hex.c +++ b/lib/search/hex.c @@ -53,7 +53,7 @@ mc_search__hex_translate_to_regex (const GString * astr) { const char *str = astr->str; GString *buff; - gchar *tmp_str, *tmp_str2; + gchar *tmp_str; gsize loop = 0; int val, ptr; @@ -61,33 +61,41 @@ mc_search__hex_translate_to_regex (const GString * astr) tmp_str = g_strndup (str, astr->len); g_strchug (tmp_str); /* trim leadind whitespaces */ - while (loop < astr->len) { - if (sscanf (tmp_str + loop, "%i%n", &val, &ptr)) { - if (val < -128 || val > 255) { + while (loop < astr->len) + { + if (sscanf (tmp_str + loop, "%i%n", &val, &ptr)) + { + gchar *tmp_str2; + + if (val < -128 || val > 255) + { loop++; continue; } + tmp_str2 = g_strdup_printf ("\\x%02X", (unsigned char) val); g_string_append (buff, tmp_str2); g_free (tmp_str2); loop += ptr; - continue; } - - if (*(tmp_str + loop) == '"') { + else if (*(tmp_str + loop) == '"') + { gsize loop2 = 0; + loop++; - while (loop + loop2 < astr->len) { + while (loop + loop2 < astr->len) + { if (*(tmp_str + loop + loop2) == '"' && !strutils_is_char_escaped (tmp_str, tmp_str + loop + loop2)) break; loop2++; } + g_string_append_len (buff, tmp_str + loop, loop2 - 1); loop += loop2; - continue; } - loop++; + else + loop++; } g_free (tmp_str); diff --git a/lib/search/normal.c b/lib/search/normal.c index 94f4f8937..222f896b5 100644 --- a/lib/search/normal.c +++ b/lib/search/normal.c @@ -50,12 +50,13 @@ mc_search__normal_translate_to_regex (const GString * astr) { const char *str = astr->str; GString *buff; - gsize loop = 0; + gsize loop; buff = g_string_sized_new (32); - while (loop < astr->len) { - switch (str[loop]) { + for (loop = 0; loop < astr->len; loop++) + switch (str[loop]) + { case '*': case '?': case ',': @@ -73,13 +74,11 @@ mc_search__normal_translate_to_regex (const GString * astr) case '-': case '|': g_string_append_c (buff, '\\'); + /* fall through */ + default: g_string_append_c (buff, str[loop]); - loop++; - continue; + break; } - g_string_append_c (buff, str[loop]); - loop++; - } return buff; }