1
1
Signed-off-by: Slava Zanko <slavazanko@gmail.com>
Этот коммит содержится в:
Slava Zanko 2010-07-07 14:04:38 +03:00
родитель 973bbb70a2
Коммит 3aa6758f4f
2 изменённых файлов: 68 добавлений и 49 удалений

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

@ -54,31 +54,37 @@ mc_search__glob_translate_to_regex (gchar * str, gsize * len)
gsize orig_len = *len; gsize orig_len = *len;
gsize loop = 0; gsize loop = 0;
gboolean inside_group = FALSE; gboolean inside_group = FALSE;
while (loop < orig_len) { while (loop < orig_len)
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++; loop++;
continue; 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++; loop++;
continue; 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 (buff, "|");
loop++; loop++;
continue; 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 (buff, "(");
inside_group = TRUE; inside_group = TRUE;
loop++; loop++;
@ -86,7 +92,8 @@ mc_search__glob_translate_to_regex (gchar * str, gsize * len)
} }
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 (buff, ")");
inside_group = FALSE; inside_group = FALSE;
loop++; loop++;
@ -114,38 +121,40 @@ mc_search__glob_translate_to_regex (gchar * str, gsize * len)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static GString * static GString *
mc_search__translate_replace_glob_to_regex (gchar *str) mc_search__translate_replace_glob_to_regex (gchar * str)
{ {
GString *buff = g_string_new (""); GString *buff = g_string_new ("");
int cnt = '0'; int cnt = '0';
gboolean escaped_mode = FALSE; gboolean escaped_mode = FALSE;
while (*str) { while (*str)
char c = *str++; {
switch (c) { char c = *str++;
case '\\': switch (c)
if (!escaped_mode) {
{ case '\\':
escaped_mode = TRUE; if (!escaped_mode)
continue; {
} escaped_mode = TRUE;
break; continue;
case '*': }
case '?': break;
if (!escaped_mode) case '*':
{ case '?':
g_string_append_c (buff, '\\'); if (!escaped_mode)
c = ++cnt; {
continue; g_string_append_c (buff, '\\');
} c = ++cnt;
break; continue;
/* breaks copying: mc uses "\0" internally, it must not be changed */ }
/*case '\\':*/ break;
case '&': /* breaks copying: mc uses "\0" internally, it must not be changed */
g_string_append_c (buff, '\\'); /*case '\\': */
break; case '&':
} g_string_append_c (buff, '\\');
g_string_append_c (buff, c); break;
escaped_mode = FALSE; }
g_string_append_c (buff, c);
escaped_mode = FALSE;
} }
return buff; return buff;
} }
@ -161,7 +170,8 @@ mc_search__cond_struct_new_init_glob (const char *charset, mc_search_t * lc_mc_s
g_string_free (mc_search_cond->str, TRUE); g_string_free (mc_search_cond->str, TRUE);
if (lc_mc_search->is_entire_line) { if (lc_mc_search->is_entire_line)
{
g_string_prepend_c (tmp, '^'); g_string_prepend_c (tmp, '^');
g_string_append_c (tmp, '$'); g_string_append_c (tmp, '$');
} }
@ -186,7 +196,7 @@ mc_search__run_glob (mc_search_t * lc_mc_search, const void *user_data,
GString * GString *
mc_search_glob_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str) mc_search_glob_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
{ {
GString *repl = mc_search__translate_replace_glob_to_regex(replace_str->str); GString *repl = mc_search__translate_replace_glob_to_regex (replace_str->str);
GString *res = mc_search_regex_prepare_replace_str (lc_mc_search, repl); GString *res = mc_search_regex_prepare_replace_str (lc_mc_search, repl);
g_string_free (repl, TRUE); g_string_free (repl, TRUE);
return res; return res;

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

@ -39,7 +39,7 @@
static const char ESCAPE_SHELL_CHARS[] = " !#$%()&{}[]`?|<>;*\\\"'"; static const char ESCAPE_SHELL_CHARS[] = " !#$%()&{}[]`?|<>;*\\\"'";
static const char ESCAPE_REGEX_CHARS[] = "^!#$%()&{}[]`?|<>;*.\\"; static const char ESCAPE_REGEX_CHARS[] = "^!#$%()&{}[]`?|<>;*.\\";
static const char ESCAPE_GLOB_CHARS[] = "$*\\?"; static const char ESCAPE_GLOB_CHARS[] = "$*\\?";
/*** file scope functions ************************************************************************/ /*** file scope functions ************************************************************************/
@ -60,12 +60,15 @@ strutils_escape (const char *src, gsize src_len, const char *escaped_chars,
ret = g_string_new (""); ret = g_string_new ("");
if (src_len == (gsize)-1) if (src_len == (gsize) - 1)
src_len = strlen (src); src_len = strlen (src);
for (curr_index = 0; curr_index < src_len; curr_index++) { for (curr_index = 0; curr_index < src_len; curr_index++)
if (escape_non_printable) { {
switch (src[curr_index]) { if (escape_non_printable)
{
switch (src[curr_index])
{
case '\n': case '\n':
g_string_append (ret, "\\n"); g_string_append (ret, "\\n");
continue; continue;
@ -92,7 +95,7 @@ strutils_escape (const char *src, gsize src_len, const char *escaped_chars,
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
char * char *
strutils_unescape (const char *src, gsize src_len, const char *unescaped_chars, strutils_unescape (const char *src, gsize src_len, const char *unescaped_chars,
gboolean unescape_non_printable) gboolean unescape_non_printable)
{ {
GString *ret; GString *ret;
gsize curr_index; gsize curr_index;
@ -105,17 +108,21 @@ strutils_unescape (const char *src, gsize src_len, const char *unescaped_chars,
ret = g_string_new (""); ret = g_string_new ("");
if (src_len == (gsize)-1) if (src_len == (gsize) - 1)
src_len = strlen (src); src_len = strlen (src);
for (curr_index = 0; curr_index < src_len-1; curr_index++) { for (curr_index = 0; curr_index < src_len - 1; curr_index++)
if (src[curr_index] != '\\'){ {
if (src[curr_index] != '\\')
{
g_string_append_c (ret, src[curr_index]); g_string_append_c (ret, src[curr_index]);
continue; continue;
} }
curr_index++; curr_index++;
if (unescape_non_printable) { if (unescape_non_printable)
switch (src[curr_index]) { {
switch (src[curr_index])
{
case 'n': case 'n':
g_string_append_c (ret, '\n'); g_string_append_c (ret, '\n');
continue; continue;
@ -139,6 +146,7 @@ strutils_unescape (const char *src, gsize src_len, const char *unescaped_chars,
return g_string_free (ret, FALSE); return g_string_free (ret, FALSE);
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
/** To be compatible with the general posix command lines we have to escape /** To be compatible with the general posix command lines we have to escape
@ -228,7 +236,8 @@ strutils_is_char_escaped (const char *start, const char *current)
return FALSE; return FALSE;
current--; current--;
while (current >= start && *current == '\\') { while (current >= start && *current == '\\')
{
num_esc++; num_esc++;
current--; current--;
} }