1
1

Some optimization of loops in translation functions.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
Этот коммит содержится в:
Andrew Borodin 2010-10-21 17:33:32 +04:00 коммит произвёл Slava Zanko
родитель b6fd832a8a
Коммит 54a9e72250
3 изменённых файлов: 35 добавлений и 47 удалений

Просмотреть файл

@ -52,55 +52,38 @@ mc_search__glob_translate_to_regex (const GString * astr)
{ {
const char *str = astr->str; const char *str = astr->str;
GString *buff; GString *buff;
gsize loop = 0; gsize loop;
gboolean inside_group = FALSE; gboolean inside_group = FALSE;
buff = g_string_sized_new (32); buff = g_string_sized_new (32);
while (loop < astr->len) for (loop = 0; loop < astr->len; loop++)
{
switch (str[loop]) switch (str[loop])
{ {
case '*': case '*':
if (!strutils_is_char_escaped (str, &(str[loop]))) if (!strutils_is_char_escaped (str, &(str[loop])))
{ g_string_append (buff, inside_group ? ".*" : "(.*)");
g_string_append (buff, (inside_group) ? ".*" : "(.*)");
loop++;
continue;
}
break; break;
case '?': case '?':
if (!strutils_is_char_escaped (str, &(str[loop]))) if (!strutils_is_char_escaped (str, &(str[loop])))
{ g_string_append (buff, inside_group ? "." : "(.)");
g_string_append (buff, (inside_group) ? "." : "(.)");
loop++;
continue;
}
break; break;
case ',': case ',':
if (!strutils_is_char_escaped (str, &(str[loop]))) if (!strutils_is_char_escaped (str, &(str[loop])))
{ g_string_append_c (buff, '|');
g_string_append (buff, "|");
loop++;
continue;
}
break; break;
case '{': case '{':
if (!strutils_is_char_escaped (str, &(str[loop]))) if (!strutils_is_char_escaped (str, &(str[loop])))
{ {
g_string_append (buff, "("); g_string_append_c (buff, '(');
inside_group = TRUE; inside_group = TRUE;
loop++;
continue;
} }
break; break;
case '}': case '}':
if (!strutils_is_char_escaped (str, &(str[loop]))) if (!strutils_is_char_escaped (str, &(str[loop])))
{ {
g_string_append (buff, ")"); g_string_append_c (buff, ')');
inside_group = FALSE; inside_group = FALSE;
loop++;
continue;
} }
break; break;
case '+': case '+':
@ -110,12 +93,10 @@ mc_search__glob_translate_to_regex (const GString * astr)
case ')': case ')':
case '^': case '^':
g_string_append_c (buff, '\\'); g_string_append_c (buff, '\\');
/* fall through */
default:
g_string_append_c (buff, str[loop]); g_string_append_c (buff, str[loop]);
loop++; break;
continue;
}
g_string_append_c (buff, str[loop]);
loop++;
} }
return buff; return buff;

Просмотреть файл

@ -53,7 +53,7 @@ mc_search__hex_translate_to_regex (const GString * astr)
{ {
const char *str = astr->str; const char *str = astr->str;
GString *buff; GString *buff;
gchar *tmp_str, *tmp_str2; gchar *tmp_str;
gsize loop = 0; gsize loop = 0;
int val, ptr; int val, ptr;
@ -61,32 +61,40 @@ mc_search__hex_translate_to_regex (const GString * astr)
tmp_str = g_strndup (str, astr->len); tmp_str = g_strndup (str, astr->len);
g_strchug (tmp_str); /* trim leadind whitespaces */ g_strchug (tmp_str); /* trim leadind whitespaces */
while (loop < astr->len) { while (loop < astr->len)
if (sscanf (tmp_str + loop, "%i%n", &val, &ptr)) { {
if (val < -128 || val > 255) { if (sscanf (tmp_str + loop, "%i%n", &val, &ptr))
{
gchar *tmp_str2;
if (val < -128 || val > 255)
{
loop++; loop++;
continue; continue;
} }
tmp_str2 = g_strdup_printf ("\\x%02X", (unsigned char) val); tmp_str2 = g_strdup_printf ("\\x%02X", (unsigned char) val);
g_string_append (buff, tmp_str2); g_string_append (buff, tmp_str2);
g_free (tmp_str2); g_free (tmp_str2);
loop += ptr; loop += ptr;
continue;
} }
else if (*(tmp_str + loop) == '"')
if (*(tmp_str + loop) == '"') { {
gsize loop2 = 0; gsize loop2 = 0;
loop++; loop++;
while (loop + loop2 < astr->len) { while (loop + loop2 < astr->len)
{
if (*(tmp_str + loop + loop2) == '"' && if (*(tmp_str + loop + loop2) == '"' &&
!strutils_is_char_escaped (tmp_str, tmp_str + loop + loop2)) !strutils_is_char_escaped (tmp_str, tmp_str + loop + loop2))
break; break;
loop2++; loop2++;
} }
g_string_append_len (buff, tmp_str + loop, loop2 - 1); g_string_append_len (buff, tmp_str + loop, loop2 - 1);
loop += loop2; loop += loop2;
continue;
} }
else
loop++; loop++;
} }

Просмотреть файл

@ -50,12 +50,13 @@ mc_search__normal_translate_to_regex (const GString * astr)
{ {
const char *str = astr->str; const char *str = astr->str;
GString *buff; GString *buff;
gsize loop = 0; gsize loop;
buff = g_string_sized_new (32); buff = g_string_sized_new (32);
while (loop < astr->len) { for (loop = 0; loop < astr->len; loop++)
switch (str[loop]) { switch (str[loop])
{
case '*': case '*':
case '?': case '?':
case ',': case ',':
@ -73,12 +74,10 @@ mc_search__normal_translate_to_regex (const GString * astr)
case '-': case '-':
case '|': case '|':
g_string_append_c (buff, '\\'); g_string_append_c (buff, '\\');
/* fall through */
default:
g_string_append_c (buff, str[loop]); g_string_append_c (buff, str[loop]);
loop++; break;
continue;
}
g_string_append_c (buff, str[loop]);
loop++;
} }
return buff; return buff;