(mc_search__recode_str): return GString.

(mc_search__get_one_symbol): likewise.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2022-09-18 17:04:53 +03:00
parent 7a65d5200b
commit 1da094ae4a
4 changed files with 59 additions and 64 deletions

View File

@ -38,9 +38,9 @@ typedef struct mc_search_cond_struct
/* search/lib.c : */
gchar *mc_search__recode_str (const char *str, gsize str_len, const char *charset_from,
const char *charset_to, gsize * bytes_written);
gchar *mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len,
GString *mc_search__recode_str (const char *str, gsize str_len, const char *charset_from,
const char *charset_to);
GString *mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len,
gboolean * just_letters);
GString *mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len);
GString *mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len);

View File

@ -68,30 +68,27 @@ mc_search__change_case_str (const char *charset, const char *str, gsize str_len,
gchar *dst_ptr;
gsize dst_len;
#ifdef HAVE_CHARSET
gchar *converted_str;
gsize converted_str_len;
GString *converted_str;
const char *src_ptr;
if (charset == NULL)
charset = cp_source;
converted_str = mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
converted_str = mc_search__recode_str (str, str_len, charset, cp_display);
dst_str = g_malloc (converted_str_len);
dst_len = converted_str_len + 1; /* +1 is required for str_toupper/str_tolower */
dst_str = g_malloc (converted_str->len);
dst_len = converted_str->len + 1; /* +1 is required for str_toupper/str_tolower */
for (src_ptr = converted_str, dst_ptr = dst_str;
for (src_ptr = converted_str->str, dst_ptr = dst_str;
case_conv (src_ptr, &dst_ptr, &dst_len); src_ptr += str_length_char (src_ptr))
;
*dst_ptr = '\0';
g_free (converted_str);
dst_len = converted_str->len;
g_string_free (converted_str, TRUE);
dst_ptr = mc_search__recode_str (dst_str, converted_str_len, cp_display, charset, &dst_len);
ret = mc_search__recode_str (dst_str, dst_len, cp_display, charset);
g_free (dst_str);
ret = g_string_new_len (dst_ptr, dst_len);
g_free (dst_ptr);
#else
(void) charset;
@ -112,11 +109,11 @@ mc_search__change_case_str (const char *charset, const char *str, gsize str_len,
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
gchar *
mc_search__recode_str (const char *str, gsize str_len,
const char *charset_from, const char *charset_to, gsize * bytes_written)
GString *
mc_search__recode_str (const char *str, gsize str_len, const char *charset_from,
const char *charset_to)
{
gchar *ret = NULL;
GString *ret = NULL;
if (charset_from != NULL && charset_to != NULL
&& g_ascii_strcasecmp (charset_to, charset_from) != 0)
@ -126,60 +123,61 @@ mc_search__recode_str (const char *str, gsize str_len,
conv = g_iconv_open (charset_to, charset_from);
if (conv != INVALID_CONV)
{
gsize bytes_read;
gchar *val;
gsize bytes_read = 0;
gsize bytes_written = 0;
val = g_convert_with_iconv (str, str_len, conv, &bytes_read, &bytes_written, NULL);
ret = g_convert_with_iconv (str, str_len, conv, &bytes_read, bytes_written, NULL);
g_iconv_close (conv);
if (val != NULL)
{
ret = g_string_new_len (val, bytes_written);
g_free (val);
}
}
}
if (ret == NULL)
{
*bytes_written = str_len;
ret = g_strndup (str, str_len);
}
ret = g_string_new_len (str, str_len);
return ret;
}
/* --------------------------------------------------------------------------------------------- */
gchar *
GString *
mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len,
gboolean * just_letters)
{
gchar *converted_str;
GString *converted_str;
const gchar *next_char;
gsize tmp_len;
#ifdef HAVE_CHARSET
gsize converted_str_len;
gchar *converted_str2;
GString *converted_str2;
if (charset == NULL)
charset = cp_source;
converted_str = mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
converted_str = mc_search__recode_str (str, str_len, charset, cp_display);
#else
(void) charset;
converted_str = g_strndup (str, str_len);
converted_str = g_string_new_len (str, str_len);
#endif
next_char = str_cget_next_char (converted_str);
tmp_len = next_char - converted_str;
converted_str[tmp_len] = '\0';
next_char = str_cget_next_char (converted_str->str);
g_string_set_size (converted_str, (gsize) (next_char - converted_str->str));
#ifdef HAVE_CHARSET
converted_str2 =
mc_search__recode_str (converted_str, tmp_len, cp_display, charset, &converted_str_len);
mc_search__recode_str (converted_str->str, converted_str->len, cp_display, charset);
#endif
if (just_letters != NULL)
*just_letters = str_isalnum (converted_str) && !str_isdigit (converted_str);
*just_letters = str_isalnum (converted_str->str) && !str_isdigit (converted_str->str);
#ifdef HAVE_CHARSET
g_free (converted_str);
g_string_free (converted_str, TRUE);
return converted_str2;
#else
return converted_str;

View File

@ -163,29 +163,27 @@ mc_search__cond_struct_new_regex_accum_append (const char *charset, GString * st
while (loop < str_from->len)
{
gchar *one_char;
gsize one_char_len;
GString *one_char;
gboolean just_letters;
one_char =
mc_search__get_one_symbol (charset, &(str_from->str[loop]),
mc_search__get_one_symbol (charset, str_from->str + loop,
MIN (str_from->len - loop, 6), &just_letters);
one_char_len = strlen (one_char);
if (one_char_len == 0)
if (one_char->len == 0)
loop++;
else
{
loop += one_char_len;
loop += one_char->len;
if (just_letters)
mc_search__cond_struct_new_regex_hex_add (charset, recoded_part, one_char,
one_char_len);
mc_search__cond_struct_new_regex_hex_add (charset, recoded_part, one_char->str,
one_char->len);
else
g_string_append_len (recoded_part, one_char, one_char_len);
g_string_append_len (recoded_part, one_char->str, one_char->len);
}
g_free (one_char);
g_string_free (one_char, TRUE);
}
g_string_append_len (str_to, recoded_part->str, recoded_part->len);
@ -644,35 +642,35 @@ mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize
for (loop = 0; loop < len; loop += char_len)
{
GString *tmp_string = NULL;
char *tmp_str;
GString *s;
tmp_str = mc_search__get_one_symbol (NULL, from + loop, len - loop, NULL);
char_len = strlen (tmp_str);
s = mc_search__get_one_symbol (NULL, from + loop, len - loop, NULL);
char_len = s->len;
if ((*replace_flags & REPLACE_T_UPP_TRANSFORM_CHAR) != 0)
{
*replace_flags &= ~REPLACE_T_UPP_TRANSFORM_CHAR;
tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
tmp_string = mc_search__toupper_case_str (NULL, s->str, char_len);
g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
}
else if ((*replace_flags & REPLACE_T_LOW_TRANSFORM_CHAR) != 0)
{
*replace_flags &= ~REPLACE_T_LOW_TRANSFORM_CHAR;
tmp_string = mc_search__tolower_case_str (NULL, tmp_str, char_len);
tmp_string = mc_search__tolower_case_str (NULL, s->str, char_len);
g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
}
else if ((*replace_flags & REPLACE_T_UPP_TRANSFORM) != 0)
{
tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
tmp_string = mc_search__toupper_case_str (NULL, s->str, char_len);
g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
}
else if ((*replace_flags & REPLACE_T_LOW_TRANSFORM) != 0)
{
tmp_string = mc_search__tolower_case_str (NULL, tmp_str, char_len);
tmp_string = mc_search__tolower_case_str (NULL, s->str, char_len);
g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
}
g_free (tmp_str);
g_string_free (s, TRUE);
if (tmp_string != NULL)
g_string_free (tmp_string, TRUE);
}

View File

@ -222,8 +222,7 @@ mc_search_prepare (mc_search_t * lc_mc_search)
for (loop1 = 0; loop1 < codepages->len; loop1++)
{
const char *id;
gsize recoded_str_len;
gchar *buffer;
GString *buffer;
id = ((codepage_desc *) g_ptr_array_index (codepages, loop1))->id;
if (g_ascii_strcasecmp (id, lc_mc_search->original.charset) == 0)
@ -239,12 +238,12 @@ mc_search_prepare (mc_search_t * lc_mc_search)
buffer =
mc_search__recode_str (lc_mc_search->original.str->str,
lc_mc_search->original.str->len,
lc_mc_search->original.charset, id, &recoded_str_len);
lc_mc_search->original.charset, id);
g_ptr_array_add (ret,
mc_search__cond_struct_new (lc_mc_search, buffer,
recoded_str_len, id));
g_free (buffer);
mc_search__cond_struct_new (lc_mc_search, buffer->str,
buffer->len, id));
g_string_free (buffer, TRUE);
}
}
else