Search engine: regex replace now works correct

This commit is contained in:
Slava Zanko 2009-04-30 10:32:45 +03:00
parent 4c5c8d4247
commit a802e51566
3 changed files with 24 additions and 21 deletions

View File

@ -228,8 +228,7 @@ mc_search__regex_found_cond_one (mc_search_t * mc_search,
g_match_info_free (match_info); g_match_info_free (match_info);
if (error) { if (error) {
mc_search->error = MC_SEARCH_E_REGEX; mc_search->error = MC_SEARCH_E_REGEX;
mc_search->error_str = str_conv_gerror_message (error, mc_search->error_str = str_conv_gerror_message (error, _(" Regular expression error "));
_(" Regular expression error "));
g_error_free (error); g_error_free (error);
return COND__FOUND_ERROR; return COND__FOUND_ERROR;
} }
@ -303,17 +302,19 @@ mc_search__run_regex (mc_search_t * mc_search, const void *user_data,
gsize start_search, gsize end_search, gsize * found_len) gsize start_search, gsize end_search, gsize * found_len)
{ {
#if GLIB_CHECK_VERSION (2, 14, 0) #if GLIB_CHECK_VERSION (2, 14, 0)
GString *buffer;
gsize current_pos, start_buffer; gsize current_pos, start_buffer;
int current_chr = 0; int current_chr = 0;
gint start_pos; gint start_pos;
gint end_pos; gint end_pos;
buffer = g_string_new (""); if (mc_search->regex_buffer != NULL)
g_string_free (mc_search->regex_buffer, TRUE);
mc_search->regex_buffer = g_string_new ("");
current_pos = start_search; current_pos = start_search;
while (current_pos <= end_search) { while (current_pos <= end_search) {
g_string_set_size (buffer, 0); g_string_set_size (mc_search->regex_buffer, 0);
start_buffer = current_pos; start_buffer = current_pos;
while (1) { while (1) {
@ -321,7 +322,7 @@ mc_search__run_regex (mc_search_t * mc_search, const void *user_data,
if (current_chr == -1) if (current_chr == -1)
break; break;
g_string_append_c (buffer, (char) current_chr); g_string_append_c (mc_search->regex_buffer, (char) current_chr);
current_pos++; current_pos++;
@ -335,24 +336,24 @@ mc_search__run_regex (mc_search_t * mc_search, const void *user_data,
if (current_chr == -1) if (current_chr == -1)
break; break;
switch (mc_search__regex_found_cond (mc_search, buffer)) { switch (mc_search__regex_found_cond (mc_search, mc_search->regex_buffer)) {
case COND__FOUND_OK: case COND__FOUND_OK:
g_match_info_fetch_pos (mc_search->regex_match_info, 0, &start_pos, &end_pos); g_match_info_fetch_pos (mc_search->regex_match_info, 0, &start_pos, &end_pos);
*found_len = end_pos - start_pos; *found_len = end_pos - start_pos;
mc_search->normal_offset = start_buffer + start_pos; mc_search->normal_offset = start_buffer + start_pos;
g_string_free (buffer, TRUE);
return TRUE; return TRUE;
break; break;
case COND__NOT_ALL_FOUND: case COND__NOT_ALL_FOUND:
break; break;
default: default:
g_string_free (buffer, TRUE); g_string_free (mc_search->regex_buffer, TRUE);
mc_search->regex_buffer = NULL;
return FALSE; return FALSE;
break; break;
} }
} }
g_string_free (buffer, TRUE); g_string_free (mc_search->regex_buffer, TRUE);
mc_search->regex_buffer = NULL;
#endif #endif
mc_search->error = MC_SEARCH_E_NOTFOUND; mc_search->error = MC_SEARCH_E_NOTFOUND;
mc_search->error_str = g_strdup (_(STR_E_NOTFOUND)); mc_search->error_str = g_strdup (_(STR_E_NOTFOUND));
@ -368,11 +369,10 @@ mc_search_regex_prepare_replace_str (mc_search_t * mc_search, GString * replace_
gchar *tmp_str; gchar *tmp_str;
GError *error = NULL; GError *error = NULL;
tmp_str = g_match_info_expand_references (mc_search->regex_match_info, tmp_str = g_match_info_expand_references (mc_search->regex_match_info,
replace_str->str, &error); replace_str->str, &error);
if (error){ if (error) {
mc_search->error = MC_SEARCH_E_REGEX_REPLACE; mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
mc_search->error_str = g_strdup (error->message); mc_search->error_str = g_strdup (error->message);
g_error_free (error); g_error_free (error);
@ -380,7 +380,7 @@ mc_search_regex_prepare_replace_str (mc_search_t * mc_search, GString * replace_
} }
ret = g_string_new (tmp_str); ret = g_string_new (tmp_str);
g_free(tmp_str); g_free (tmp_str);
return ret; return ret;
#else #else
return g_string_new_len (replace_str->str, replace_str->len); return g_string_new_len (replace_str->str, replace_str->len);

View File

@ -203,6 +203,8 @@ mc_search_free (mc_search_t * mc_search)
#if GLIB_CHECK_VERSION (2, 14, 0) #if GLIB_CHECK_VERSION (2, 14, 0)
if (mc_search->regex_match_info) if (mc_search->regex_match_info)
g_match_info_free (mc_search->regex_match_info); g_match_info_free (mc_search->regex_match_info);
if (mc_search->regex_buffer != NULL)
g_string_free (mc_search->regex_buffer, TRUE);
#endif #endif
g_free (mc_search); g_free (mc_search);
@ -293,12 +295,12 @@ mc_search_types_list_get (void)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
GString * GString *
mc_search_prepare_replace_str (mc_search_t * mc_search, GString *replace_str) mc_search_prepare_replace_str (mc_search_t * mc_search, GString * replace_str)
{ {
GString *ret; GString *ret;
if (mc_search == NULL) if (mc_search == NULL)
return g_string_new_len(replace_str->str,replace_str->len); return g_string_new_len (replace_str->str, replace_str->len);
switch (mc_search->search_type) { switch (mc_search->search_type) {
case MC_SEARCH_T_REGEX: case MC_SEARCH_T_REGEX:
@ -308,7 +310,7 @@ mc_search_prepare_replace_str (mc_search_t * mc_search, GString *replace_str)
case MC_SEARCH_T_HEX: case MC_SEARCH_T_HEX:
case MC_SEARCH_T_GLOB: case MC_SEARCH_T_GLOB:
default: default:
ret = g_string_new_len(replace_str->str,replace_str->len); ret = g_string_new_len (replace_str->str, replace_str->len);
break; break;
} }
return ret; return ret;

View File

@ -64,6 +64,7 @@ typedef struct mc_search_struct {
/* some data for regexp */ /* some data for regexp */
#if GLIB_CHECK_VERSION (2, 14, 0) #if GLIB_CHECK_VERSION (2, 14, 0)
GMatchInfo *regex_match_info; GMatchInfo *regex_match_info;
GString *regex_buffer;
#endif #endif
/* some data for sscanf */ /* some data for sscanf */
@ -105,6 +106,6 @@ gboolean mc_search_is_type_avail (mc_search_type_t);
mc_search_type_str_t *mc_search_types_list_get (void); mc_search_type_str_t *mc_search_types_list_get (void);
GString *mc_search_prepare_replace_str (mc_search_t * mc_search, GString *replace_str); GString *mc_search_prepare_replace_str (mc_search_t * mc_search, GString * replace_str);
#endif #endif