1
0
mirror of https://github.com/irssi/irssi.git synced 2024-12-04 14:46:39 -05:00

get rid of new_text

This commit is contained in:
ailin-nemui 2017-06-05 11:41:50 +02:00
parent 48899a123d
commit 4edfccfce7
6 changed files with 50 additions and 40 deletions

View File

@ -69,7 +69,7 @@ static int ignore_match_pattern(IGNORE_REC *rec, const char *text)
if (rec->regexp) {
return rec->preg != NULL &&
i_regex_match(rec->preg, text, 0, NULL, NULL);
i_regex_match(rec->preg, text, 0, NULL);
}
return rec->fullword ?

View File

@ -2,6 +2,11 @@
#include "iregex.h"
struct _MatchInfo {
const char *valid_string;
GMatchInfo *g_match_info;
};
static const gchar *
make_valid_utf8(const gchar *text, gboolean *free_ret)
{
@ -59,28 +64,29 @@ i_regex_unref (Regex *regex)
g_regex_unref(regex);
}
/* if new_string is present, the caller must free new_string.
otherwise, g_match_info_get_string must not be used.
if string is not vali utf8, new_string will be assigned
a similar, but valid utf8, string */
gboolean
i_regex_match (const Regex *regex,
const gchar *string,
GRegexMatchFlags match_options,
MatchInfo **match_info,
const gchar **new_string)
MatchInfo **match_info)
{
gboolean ret;
gboolean free_valid_string;
const gchar *valid_string = make_valid_utf8(string, &free_valid_string);
ret = g_regex_match(regex, valid_string, match_options, match_info);
if (match_info != NULL)
*match_info = g_new0(MatchInfo, 1);
ret = g_regex_match(regex, valid_string, match_options,
match_info != NULL ? &(*match_info)->g_match_info : NULL);
if (free_valid_string) {
if (new_string)
*new_string = valid_string;
if (match_info != NULL)
(*match_info)->valid_string = valid_string;
else
g_free_not_null((gchar *)valid_string);
}
return ret;
}
@ -114,18 +120,20 @@ gboolean
i_match_info_fetch_pos (const MatchInfo *match_info,
gint match_num,
gint *start_pos,
gint *end_pos,
const gchar *new_string)
gint *end_pos)
{
gint tmp_start, tmp_end, new_start_pos;
gboolean ret;
if (!new_string || (!start_pos && !end_pos))
return g_match_info_fetch_pos(match_info, match_num, start_pos, end_pos);
if (!match_info->valid_string || (!start_pos && !end_pos))
return g_match_info_fetch_pos(match_info->g_match_info,
match_num, start_pos, end_pos);
ret = g_match_info_fetch_pos(match_info, match_num, &tmp_start, &tmp_end);
ret = g_match_info_fetch_pos(match_info->g_match_info,
match_num, &tmp_start, &tmp_end);
if (start_pos || end_pos) {
gchar *to_start = g_strndup(new_string, tmp_start);
const gchar *str = match_info->valid_string;
gchar *to_start = g_strndup(str, tmp_start);
new_start_pos = strlen_pua_oddly(to_start);
g_free_not_null(to_start);
@ -133,10 +141,25 @@ i_match_info_fetch_pos (const MatchInfo *match_info,
*start_pos = new_start_pos;
if (end_pos) {
gchar *to_end = g_strndup(new_string + tmp_start, tmp_end - tmp_start);
gchar *to_end = g_strndup(str + tmp_start, tmp_end - tmp_start);
*end_pos = new_start_pos + strlen_pua_oddly(to_end);
g_free_not_null(to_end);
}
}
return ret;
}
gboolean
i_match_info_matches (const MatchInfo *match_info)
{
g_return_val_if_fail(match_info != NULL, FALSE);
return g_match_info_matches(match_info->g_match_info);
}
void
i_match_info_free (MatchInfo *match_info)
{
g_match_info_free(match_info->g_match_info);
g_free(match_info);
}

View File

@ -47,8 +47,7 @@ gboolean
i_regex_match (const Regex *regex,
const gchar *string,
GRegexMatchFlags match_options,
MatchInfo **match_info,
const gchar **new_string)
MatchInfo **match_info)
{
int groups;
int eflags;
@ -75,8 +74,7 @@ gboolean
i_match_info_fetch_pos (const MatchInfo *match_info,
gint match_num,
gint *start_pos,
gint *end_pos,
const gchar *new_string)
gint *end_pos)
{
if (start_pos != NULL)
*start_pos = match_info[match_num].rm_so;

View File

@ -7,10 +7,7 @@
#include <glib.h>
typedef GRegex Regex;
typedef GMatchInfo MatchInfo;
#define i_match_info_matches g_match_info_matches
#define i_match_info_free g_match_info_free
typedef struct _MatchInfo MatchInfo;
#else
@ -18,14 +15,14 @@ typedef GMatchInfo MatchInfo;
typedef regex_t Regex;
typedef regmatch_t MatchInfo;
#endif
gboolean
i_match_info_matches (const MatchInfo *match_info);
void
i_match_info_free (MatchInfo *match_info);
#endif
Regex *
i_regex_new (const gchar *pattern,
GRegexCompileFlags compile_options,
@ -39,14 +36,12 @@ gboolean
i_regex_match (const Regex *regex,
const gchar *string,
GRegexMatchFlags match_options,
MatchInfo **match_info,
const gchar **new_string);
MatchInfo **match_info);
gboolean
i_match_info_fetch_pos (const MatchInfo *match_info,
gint match_num,
gint *start_pos,
gint *end_pos,
const gchar *new_string);
gint *end_pos);
#endif

View File

@ -197,15 +197,12 @@ static gboolean hilight_match_text(HILIGHT_REC *rec, const char *text,
if (rec->regexp) {
if (rec->preg != NULL) {
MatchInfo *match;
const char *new_text = NULL;
i_regex_match(rec->preg, text, 0, &match, &new_text);
i_regex_match(rec->preg, text, 0, &match);
if (i_match_info_matches(match))
ret = i_match_info_fetch_pos(match, 0, match_beg, match_end, new_text);
ret = i_match_info_fetch_pos(match, 0, match_beg, match_end);
i_match_info_free(match);
g_free_not_null((char *)new_text);
}
} else {
char *match;

View File

@ -576,16 +576,13 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
(line->info.level & nolevel) == 0;
if (*text != '\0') {
const char *tmp = NULL;
textbuffer_line2text(line, FALSE, str);
if (line_matched) {
line_matched = regexp ?
i_regex_match(preg, str->str, 0, NULL, &tmp)
i_regex_match(preg, str->str, 0, NULL)
: match_func(str->str, text) != NULL;
}
if (tmp && tmp != str->str)
g_free_not_null((char *)tmp);
}
if (line_matched) {