From 3934c6fb8e08914e3d45fe1d0a1091e1c449dc0b Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Fri, 22 Oct 2010 10:01:48 +0300 Subject: [PATCH 1/9] Ticket #2396 (Find File "Whole words" search bug) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit when searching in files for non english word with "Whole words" set "on" - then nothig will be found try search word: "время" and also "time" in next example: 'time' Время 'Time' 'время' Signed-off-by: Slava Zanko Signed-off-by: Andrew Borodin --- lib/search/normal.c | 9 ++++++--- lib/search/regex.c | 23 +++++++++++++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/search/normal.c b/lib/search/normal.c index c2c8ea734..fbb6790cc 100644 --- a/lib/search/normal.c +++ b/lib/search/normal.c @@ -92,9 +92,12 @@ mc_search__cond_struct_new_init_normal (const char *charset, mc_search_t * lc_mc mc_search__normal_translate_to_regex (mc_search_cond->str->str, &mc_search_cond->len); g_string_free (mc_search_cond->str, TRUE); - if (lc_mc_search->whole_words) { - g_string_prepend (tmp, "\\b"); - g_string_append (tmp, "\\b"); + if (lc_mc_search->whole_words) + { + /* NOTE: \b as word boundary doesn't allow search + * whole words with non-ASCII symbols */ + g_string_prepend (tmp, "(^|[^\\p{L}\\p{N}_])("); + g_string_append (tmp, ")([^\\p{L}\\p{N}_]|$)"); } mc_search_cond->str = tmp; diff --git a/lib/search/regex.c b/lib/search/regex.c index 40e9599a2..f19e1a2ca 100644 --- a/lib/search/regex.c +++ b/lib/search/regex.c @@ -58,7 +58,7 @@ typedef enum /*** file scope functions ************************************************************************/ static gboolean -mc_search__regex_str_append_if_special (GString * copy_to, GString * regex_str, gsize * offset) +mc_search__regex_str_append_if_special (GString * copy_to, const GString * regex_str, gsize * offset) { char *tmp_regex_str; gsize spec_chr_len; @@ -622,10 +622,25 @@ mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data, { case COND__FOUND_OK: #ifdef SEARCH_TYPE_GLIB - g_match_info_fetch_pos (lc_mc_search->regex_match_info, 0, &start_pos, &end_pos); + if (lc_mc_search->whole_words) + { + g_match_info_fetch_pos (lc_mc_search->regex_match_info, 2, &start_pos, &end_pos); + } + else + { + g_match_info_fetch_pos (lc_mc_search->regex_match_info, 0, &start_pos, &end_pos); + } #else /* SEARCH_TYPE_GLIB */ - start_pos = lc_mc_search->iovector[0]; - end_pos = lc_mc_search->iovector[1]; + if (lc_mc_search->whole_words) + { + start_pos = lc_mc_search->iovector[4]; + end_pos = lc_mc_search->iovector[5]; + } + else + { + start_pos = lc_mc_search->iovector[0]; + end_pos = lc_mc_search->iovector[1]; + } #endif /* SEARCH_TYPE_GLIB */ if (found_len) *found_len = end_pos - start_pos; From a0d69353e8df5e41745346d06983da157c1d6339 Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Thu, 21 Oct 2010 10:34:27 +0300 Subject: [PATCH 2/9] Code cleanup for avoid compiler warnings Signed-off-by: Slava Zanko --- src/charsets.c | 6 +----- src/selcodepage.c | 17 ++++++++++------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/charsets.c b/src/charsets.c index 009f9b5e7..1f281424a 100644 --- a/src/charsets.c +++ b/src/charsets.c @@ -114,9 +114,6 @@ load_codepages_list_from_file (GPtrArray **list, const char *fname) } else { - guint i; - codepage_desc *desc; - /* whether id is already present in list */ /* if yes, overwrite description */ for (i = 0; i < (*list)->len; i++) @@ -154,7 +151,6 @@ load_codepages_list_from_file (GPtrArray **list, const char *fname) void load_codepages_list (void) { - int result = -1; char *fname; /* 1: try load /usr/share/mc/mc.charsets */ @@ -195,7 +191,7 @@ get_codepage_id (const int n) int get_codepage_index (const char *id) { - int i; + size_t i; if (strcmp (id, OTHER_8BIT) == 0) return -1; if (codepages == NULL) diff --git a/src/selcodepage.c b/src/selcodepage.c index 9c1c33713..4a028d714 100644 --- a/src/selcodepage.c +++ b/src/selcodepage.c @@ -60,7 +60,8 @@ get_hotkey (int n) int select_charset (int center_y, int center_x, int current_charset, gboolean seldisplay) { - int i; + size_t i; + int listbox_result; char buffer[255]; /* Create listbox */ @@ -88,24 +89,26 @@ select_charset (int center_y, int center_x, int current_charset, gboolean seldis /* Select the default entry */ i = (seldisplay) - ? ((current_charset < 0) ? codepages->len : current_charset) - : (current_charset + 1); + ? ((current_charset < 0) ? codepages->len : (size_t) current_charset) + : ((size_t)current_charset + 1); listbox_select_entry (listbox->list, i); - i = run_listbox (listbox); + listbox_result = run_listbox (listbox); - if (i < 0) { + if (listbox_result < 0) { /* Cancel dialog */ return SELECT_CHARSET_CANCEL; } else { /* some charset has been selected */ if (seldisplay) { /* charset list is finished with "Other 8 bit" item */ - return ((guint) i >= codepages->len) ? SELECT_CHARSET_OTHER_8BIT : i; + return (listbox_result >= (int) codepages->len) + ? SELECT_CHARSET_OTHER_8BIT + : listbox_result; } else { /* charset list is began with "- < No translation >" item */ - return (i - 1); + return (listbox_result - 1); } } } From 863f1ec174869f792fa91431bf4867c8f3977f2d Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Thu, 21 Oct 2010 12:22:10 +0300 Subject: [PATCH 3/9] Fixed bit operations in mc_search_regex__process_append_str() Signed-off-by: Slava Zanko --- lib/search/regex.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/search/regex.c b/lib/search/regex.c index f19e1a2ca..2bc80fc4a 100644 --- a/lib/search/regex.c +++ b/lib/search/regex.c @@ -478,7 +478,7 @@ mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize char_len = strlen (tmp_str); if (*replace_flags & REPLACE_T_UPP_TRANSFORM_CHAR) { - *replace_flags &= !REPLACE_T_UPP_TRANSFORM_CHAR; + *replace_flags &= ~REPLACE_T_UPP_TRANSFORM_CHAR; tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len); g_string_append (dest_str, tmp_string->str); g_string_free (tmp_string, TRUE); @@ -486,7 +486,7 @@ mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize } else if (*replace_flags & REPLACE_T_LOW_TRANSFORM_CHAR) { - *replace_flags &= !REPLACE_T_LOW_TRANSFORM_CHAR; + *replace_flags &= ~REPLACE_T_LOW_TRANSFORM_CHAR; tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len); g_string_append (dest_str, tmp_string->str); g_string_free (tmp_string, TRUE); From 8d59d66188e1028f12a611de416856fa6312eb4a Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Thu, 21 Oct 2010 12:25:38 +0300 Subject: [PATCH 4/9] Removed mc_search_cond_t->len (used mc_search_cond_t->str->len instead). Signed-off-by: Slava Zanko --- lib/search/glob.c | 2 +- lib/search/hex.c | 2 +- lib/search/internal.h | 1 - lib/search/normal.c | 2 +- lib/search/search.c | 1 - 5 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/search/glob.c b/lib/search/glob.c index ad0c42506..c791a8d2f 100644 --- a/lib/search/glob.c +++ b/lib/search/glob.c @@ -163,7 +163,7 @@ mc_search__cond_struct_new_init_glob (const char *charset, mc_search_t * lc_mc_s mc_search_cond_t * mc_search_cond) { GString *tmp = - mc_search__glob_translate_to_regex (mc_search_cond->str->str, &mc_search_cond->len); + mc_search__glob_translate_to_regex (mc_search_cond->str->str, &mc_search_cond->str->len); g_string_free (mc_search_cond->str, TRUE); diff --git a/lib/search/hex.c b/lib/search/hex.c index b8d772967..bb93cd9c2 100644 --- a/lib/search/hex.c +++ b/lib/search/hex.c @@ -101,7 +101,7 @@ mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_se mc_search_cond_t * mc_search_cond) { GString *tmp = - mc_search__hex_translate_to_regex (mc_search_cond->str->str, &mc_search_cond->len); + mc_search__hex_translate_to_regex (mc_search_cond->str->str, &mc_search_cond->str->len); g_string_free (mc_search_cond->str, TRUE); mc_search_cond->str = tmp; diff --git a/lib/search/internal.h b/lib/search/internal.h index 0d69d2fab..edb5e9f72 100644 --- a/lib/search/internal.h +++ b/lib/search/internal.h @@ -27,7 +27,6 @@ typedef struct mc_search_cond_struct { GString *upper; GString *lower; mc_search_regex_t *regex_handle; - gsize len; gchar *charset; } mc_search_cond_t; diff --git a/lib/search/normal.c b/lib/search/normal.c index fbb6790cc..9a2b92904 100644 --- a/lib/search/normal.c +++ b/lib/search/normal.c @@ -89,7 +89,7 @@ mc_search__cond_struct_new_init_normal (const char *charset, mc_search_t * lc_mc mc_search_cond_t * mc_search_cond) { GString *tmp = - mc_search__normal_translate_to_regex (mc_search_cond->str->str, &mc_search_cond->len); + mc_search__normal_translate_to_regex (mc_search_cond->str->str, &mc_search_cond->str->len); g_string_free (mc_search_cond->str, TRUE); if (lc_mc_search->whole_words) diff --git a/lib/search/search.c b/lib/search/search.c index 625c5da66..f79bc02a4 100644 --- a/lib/search/search.c +++ b/lib/search/search.c @@ -64,7 +64,6 @@ mc_search__cond_struct_new (mc_search_t * lc_mc_search, const char *str, mc_search_cond = g_malloc0 (sizeof (mc_search_cond_t)); mc_search_cond->str = g_string_new_len (str, str_len); - mc_search_cond->len = str_len; mc_search_cond->charset = g_strdup (charset); switch (lc_mc_search->search_type) { From 601047b6a1125b894a95ed39b906344e3ee0d010 Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Thu, 21 Oct 2010 12:28:44 +0300 Subject: [PATCH 5/9] Avoid extra-allocation of string while prepare to regexp-search. Signed-off-by: Slava Zanko --- lib/search/regex.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/search/regex.c b/lib/search/regex.c index 2bc80fc4a..defe98b7a 100644 --- a/lib/search/regex.c +++ b/lib/search/regex.c @@ -532,8 +532,7 @@ mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_ if (!lc_mc_search->is_case_sensitive) { - tmp = g_string_new_len (mc_search_cond->str->str, mc_search_cond->str->len); - g_string_free (mc_search_cond->str, TRUE); + tmp = mc_search_cond->str; mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp->str, tmp->len); g_string_free (tmp, TRUE); } From 5cac8caabe94fe4665fd0bfb044da390f5a83a89 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Thu, 21 Oct 2010 17:07:06 +0400 Subject: [PATCH 6/9] Minor optimization of translation function arguments. Signed-off-by: Andrew Borodin --- lib/search/glob.c | 14 +++++++------- lib/search/hex.c | 15 +++++++-------- lib/search/normal.c | 16 ++++++++-------- lib/search/regex.c | 16 +++++++++------- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/lib/search/glob.c b/lib/search/glob.c index c791a8d2f..602146162 100644 --- a/lib/search/glob.c +++ b/lib/search/glob.c @@ -48,13 +48,14 @@ /*** file scope functions ************************************************************************/ static GString * -mc_search__glob_translate_to_regex (gchar * str, gsize * len) +mc_search__glob_translate_to_regex (const GString * astr) { + const char *str = astr->str; GString *buff = g_string_new (""); - gsize orig_len = *len; gsize loop = 0; gboolean inside_group = FALSE; - while (loop < orig_len) + + while (loop < astr->len) { switch (str[loop]) { @@ -114,7 +115,7 @@ mc_search__glob_translate_to_regex (gchar * str, gsize * len) g_string_append_c (buff, str[loop]); loop++; } - *len = buff->len; + return buff; } @@ -162,9 +163,9 @@ void mc_search__cond_struct_new_init_glob (const char *charset, mc_search_t * lc_mc_search, mc_search_cond_t * mc_search_cond) { - GString *tmp = - mc_search__glob_translate_to_regex (mc_search_cond->str->str, &mc_search_cond->str->len); + GString *tmp; + tmp = mc_search__glob_translate_to_regex (mc_search_cond->str); g_string_free (mc_search_cond->str, TRUE); if (lc_mc_search->is_entire_line) @@ -175,7 +176,6 @@ mc_search__cond_struct_new_init_glob (const char *charset, mc_search_t * lc_mc_s mc_search_cond->str = tmp; mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond); - } /* --------------------------------------------------------------------------------------------- */ diff --git a/lib/search/hex.c b/lib/search/hex.c index bb93cd9c2..c1531e50b 100644 --- a/lib/search/hex.c +++ b/lib/search/hex.c @@ -49,17 +49,18 @@ /*** file scope functions ************************************************************************/ static GString * -mc_search__hex_translate_to_regex (gchar * str, gsize * len) +mc_search__hex_translate_to_regex (const GString * astr) { + const char *str = astr->str; GString *buff = g_string_new (""); - gchar *tmp_str = g_strndup (str, *len); + gchar *tmp_str = g_strndup (str, astr->len); gchar *tmp_str2; gsize loop = 0; int val, ptr; g_strchug (tmp_str); /* trim leadind whitespaces */ - while (loop < *len) { + while (loop < astr->len) { if (sscanf (tmp_str + loop, "%i%n", &val, &ptr)) { if (val < -128 || val > 255) { loop++; @@ -75,7 +76,7 @@ mc_search__hex_translate_to_regex (gchar * str, gsize * len) if (*(tmp_str + loop) == '"') { gsize loop2 = 0; loop++; - while (loop + loop2 < *len) { + while (loop + loop2 < astr->len) { if (*(tmp_str + loop + loop2) == '"' && !strutils_is_char_escaped (tmp_str, tmp_str + loop + loop2)) break; @@ -90,7 +91,6 @@ mc_search__hex_translate_to_regex (gchar * str, gsize * len) g_free (tmp_str); - *len = buff->len; return buff; } @@ -100,14 +100,13 @@ void mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_search, mc_search_cond_t * mc_search_cond) { - GString *tmp = - mc_search__hex_translate_to_regex (mc_search_cond->str->str, &mc_search_cond->str->len); + GString *tmp; + tmp = mc_search__hex_translate_to_regex (mc_search_cond->str); g_string_free (mc_search_cond->str, TRUE); mc_search_cond->str = tmp; mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond); - } /* --------------------------------------------------------------------------------------------- */ diff --git a/lib/search/normal.c b/lib/search/normal.c index 9a2b92904..761567204 100644 --- a/lib/search/normal.c +++ b/lib/search/normal.c @@ -46,13 +46,13 @@ /*** file scope functions ************************************************************************/ static GString * -mc_search__normal_translate_to_regex (gchar * str, gsize * len) +mc_search__normal_translate_to_regex (const GString * astr) { + const char *str = astr->str; GString *buff = g_string_new (""); - gsize orig_len = *len; gsize loop = 0; - while (loop < orig_len) { + while (loop < astr->len) { switch (str[loop]) { case '*': case '?': @@ -78,7 +78,7 @@ mc_search__normal_translate_to_regex (gchar * str, gsize * len) g_string_append_c (buff, str[loop]); loop++; } - *len = buff->len; + return buff; } @@ -88,10 +88,11 @@ void mc_search__cond_struct_new_init_normal (const char *charset, mc_search_t * lc_mc_search, mc_search_cond_t * mc_search_cond) { - GString *tmp = - mc_search__normal_translate_to_regex (mc_search_cond->str->str, &mc_search_cond->str->len); + GString *tmp; + tmp = mc_search__normal_translate_to_regex (mc_search_cond->str); g_string_free (mc_search_cond->str, TRUE); + if (lc_mc_search->whole_words) { /* NOTE: \b as word boundary doesn't allow search @@ -99,10 +100,9 @@ mc_search__cond_struct_new_init_normal (const char *charset, mc_search_t * lc_mc g_string_prepend (tmp, "(^|[^\\p{L}\\p{N}_])("); g_string_append (tmp, ")([^\\p{L}\\p{N}_]|$)"); } + mc_search_cond->str = tmp; - mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond); - } /* --------------------------------------------------------------------------------------------- */ diff --git a/lib/search/regex.c b/lib/search/regex.c index defe98b7a..fe928599c 100644 --- a/lib/search/regex.c +++ b/lib/search/regex.c @@ -194,12 +194,13 @@ mc_search__cond_struct_new_regex_accum_append (const char *charset, GString * st /* --------------------------------------------------------------------------------------------- */ static GString * -mc_search__cond_struct_new_regex_ci_str (const char *charset, const char *str, gsize str_len) +mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString *astr) { + const char *str = astr->str; GString *accumulator, *spec_char, *ret_str; gsize loop; GString *tmp; - tmp = g_string_new_len (str, str_len); + tmp = g_string_new_len (str, astr->len); ret_str = g_string_new (""); @@ -207,7 +208,7 @@ mc_search__cond_struct_new_regex_ci_str (const char *charset, const char *str, g spec_char = g_string_new (""); loop = 0; - while (loop <= str_len) + while (loop <= astr->len) { if (mc_search__regex_str_append_if_special (spec_char, tmp, &loop)) { @@ -221,13 +222,13 @@ mc_search__cond_struct_new_regex_ci_str (const char *charset, const char *str, g { mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator); - while (loop < str_len && !(tmp->str[loop] == ']' + while (loop < astr->len && !(tmp->str[loop] == ']' && !strutils_is_char_escaped (tmp->str, &(tmp->str[loop])))) { g_string_append_c (ret_str, tmp->str[loop]); loop++; - } + g_string_append_c (ret_str, tmp->str[loop]); loop++; continue; @@ -522,7 +523,6 @@ void mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_search, mc_search_cond_t * mc_search_cond) { - GString *tmp = NULL; #ifdef SEARCH_TYPE_GLIB GError *error = NULL; #else /* SEARCH_TYPE_GLIB */ @@ -532,8 +532,10 @@ mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_ if (!lc_mc_search->is_case_sensitive) { + GString *tmp; + tmp = mc_search_cond->str; - mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp->str, tmp->len); + mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp); g_string_free (tmp, TRUE); } #ifdef SEARCH_TYPE_GLIB From 69cebb9984066f11dc56d6c87b89d7089f677585 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Thu, 21 Oct 2010 17:09:07 +0400 Subject: [PATCH 7/9] mc_search__cond_struct_new_regex_ci_str(): get rid of extra string duplication. Signed-off-by: Andrew Borodin Signed-off-by: Slava Zanko --- lib/search/regex.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/search/regex.c b/lib/search/regex.c index fe928599c..2e40f6035 100644 --- a/lib/search/regex.c +++ b/lib/search/regex.c @@ -196,12 +196,8 @@ mc_search__cond_struct_new_regex_accum_append (const char *charset, GString * st static GString * mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString *astr) { - const char *str = astr->str; GString *accumulator, *spec_char, *ret_str; gsize loop; - GString *tmp; - tmp = g_string_new_len (str, astr->len); - ret_str = g_string_new (""); accumulator = g_string_new (""); @@ -210,7 +206,7 @@ mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString *ast while (loop <= astr->len) { - if (mc_search__regex_str_append_if_special (spec_char, tmp, &loop)) + if (mc_search__regex_str_append_if_special (spec_char, astr, &loop)) { mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator); g_string_append_len (ret_str, spec_char->str, spec_char->len); @@ -218,32 +214,32 @@ mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString *ast continue; } - if (tmp->str[loop] == '[' && !strutils_is_char_escaped (tmp->str, &(tmp->str[loop]))) + if (astr->str[loop] == '[' && !strutils_is_char_escaped (astr->str, &(astr->str[loop]))) { mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator); - while (loop < astr->len && !(tmp->str[loop] == ']' - && !strutils_is_char_escaped (tmp->str, &(tmp->str[loop])))) + while (loop < astr->len && !(astr->str[loop] == ']' + && !strutils_is_char_escaped (astr->str, &(astr->str[loop])))) { - g_string_append_c (ret_str, tmp->str[loop]); + g_string_append_c (ret_str, astr->str[loop]); loop++; } - g_string_append_c (ret_str, tmp->str[loop]); + g_string_append_c (ret_str, astr->str[loop]); loop++; continue; } /* TODO: handle [ and ] */ - g_string_append_c (accumulator, tmp->str[loop]); + g_string_append_c (accumulator, astr->str[loop]); loop++; } mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator); g_string_free (accumulator, TRUE); g_string_free (spec_char, TRUE); - g_string_free (tmp, TRUE); + return ret_str; } From b6fd832a8a55990a0e672af51151cf436b347868 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Thu, 21 Oct 2010 17:18:18 +0400 Subject: [PATCH 8/9] Create strings with small preallocated sizes. Signed-off-by: Andrew Borodin --- lib/search/glob.c | 9 +++++++-- lib/search/hex.c | 7 ++++--- lib/search/normal.c | 4 +++- lib/search/regex.c | 11 ++++++----- lib/search/search.c | 8 ++++---- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/lib/search/glob.c b/lib/search/glob.c index 602146162..6e9907600 100644 --- a/lib/search/glob.c +++ b/lib/search/glob.c @@ -51,10 +51,12 @@ static GString * mc_search__glob_translate_to_regex (const GString * astr) { const char *str = astr->str; - GString *buff = g_string_new (""); + GString *buff; gsize loop = 0; gboolean inside_group = FALSE; + buff = g_string_sized_new (32); + while (loop < astr->len) { switch (str[loop]) @@ -124,9 +126,12 @@ mc_search__glob_translate_to_regex (const GString * astr) static GString * mc_search__translate_replace_glob_to_regex (gchar * str) { - GString *buff = g_string_sized_new (32); + GString *buff; int cnt = '0'; gboolean escaped_mode = FALSE; + + buff = g_string_sized_new (32); + while (*str) { char c = *str++; diff --git a/lib/search/hex.c b/lib/search/hex.c index c1531e50b..d1d52febe 100644 --- a/lib/search/hex.c +++ b/lib/search/hex.c @@ -52,12 +52,13 @@ static GString * mc_search__hex_translate_to_regex (const GString * astr) { const char *str = astr->str; - GString *buff = g_string_new (""); - gchar *tmp_str = g_strndup (str, astr->len); - gchar *tmp_str2; + GString *buff; + gchar *tmp_str, *tmp_str2; gsize loop = 0; int val, ptr; + buff = g_string_sized_new (64); + tmp_str = g_strndup (str, astr->len); g_strchug (tmp_str); /* trim leadind whitespaces */ while (loop < astr->len) { diff --git a/lib/search/normal.c b/lib/search/normal.c index 761567204..94f4f8937 100644 --- a/lib/search/normal.c +++ b/lib/search/normal.c @@ -49,9 +49,11 @@ static GString * mc_search__normal_translate_to_regex (const GString * astr) { const char *str = astr->str; - GString *buff = g_string_new (""); + GString *buff; gsize loop = 0; + buff = g_string_sized_new (32); + while (loop < astr->len) { switch (str[loop]) { case '*': diff --git a/lib/search/regex.c b/lib/search/regex.c index 2e40f6035..757fc6624 100644 --- a/lib/search/regex.c +++ b/lib/search/regex.c @@ -199,9 +199,9 @@ mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString *ast GString *accumulator, *spec_char, *ret_str; gsize loop; - ret_str = g_string_new (""); - accumulator = g_string_new (""); - spec_char = g_string_new (""); + ret_str = g_string_sized_new (64); + accumulator = g_string_sized_new (64); + spec_char = g_string_sized_new (64); loop = 0; while (loop <= astr->len) @@ -584,7 +584,7 @@ mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data, if (lc_mc_search->regex_buffer != NULL) g_string_free (lc_mc_search->regex_buffer, TRUE); - lc_mc_search->regex_buffer = g_string_new (""); + lc_mc_search->regex_buffer = g_string_sized_new (64); virtual_pos = current_pos = start_search; while (virtual_pos <= end_search) @@ -696,8 +696,9 @@ mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * repla return NULL; } - ret = g_string_new (""); + ret = g_string_sized_new (64); prev_str = replace_str->str; + for (loop = 0; loop < replace_str->len - 1; loop++) { lc_index = mc_search_regex__process_replace_str (replace_str, loop, &len, &replace_flags); diff --git a/lib/search/search.c b/lib/search/search.c index f79bc02a4..2aa0451b4 100644 --- a/lib/search/search.c +++ b/lib/search/search.c @@ -339,12 +339,12 @@ char * mc_search_prepare_replace_str2 (mc_search_t * lc_mc_search, char *replace_str) { GString *ret; - GString *replace_str2 = g_string_new (replace_str); + GString *replace_str2; + + replace_str2 = g_string_new (replace_str); ret = mc_search_prepare_replace_str (lc_mc_search, replace_str2); g_string_free (replace_str2, TRUE); - if (ret) - return g_string_free (ret, FALSE); - return NULL; + return (ret != NULL) ? g_string_free (ret, FALSE) : NULL; } /* --------------------------------------------------------------------------------------------- */ From 54a9e72250ee06f7b13201c4290d2d70726d666f Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Thu, 21 Oct 2010 17:33:32 +0400 Subject: [PATCH 9/9] 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; }