2 * Sylpheed -- regexp pattern matching utilities
3 * Copyright (C) 2001 Thomas Link, Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "string_match.h"
24 int string_match_regexp(char * txt, char * rexp,
25 int size, regmatch_t matches[],
26 int cflags, int eflags)
31 g_return_val_if_fail(txt, 0);
32 g_return_val_if_fail(rexp, 0);
34 problem = regcomp(&re, rexp, cflags);
36 problem = regexec(&re, txt, size, matches, eflags);
42 int string_remove_match(char * txt, char * rexp, int cflags, int eflags)
44 regmatch_t matches[STRING_MATCH_MR_SIZE];
47 g_return_val_if_fail(txt, -1);
48 g_return_val_if_fail(rexp, -1);
50 if (strlen(txt) > 0 && strlen(rexp) > 0) {
51 foundp = string_match_regexp(txt, rexp, STRING_MATCH_MR_SIZE,
52 matches, cflags, eflags);
57 if (foundp && matches[0].rm_so != -1 && matches[0].rm_eo != -1) {
58 if (matches[0].rm_so == matches[0].rm_eo) {
59 /* in case the match is an empty string */
60 return matches[0].rm_so + 1;
62 strcpy(txt + matches[0].rm_so, txt + matches[0].rm_eo);
63 return matches[0].rm_so;
70 int string_remove_all_matches(char * txt, char * rexp, int cflags, int eflags)
75 g_return_val_if_fail(txt, 0);
76 g_return_val_if_fail(rexp, 0);
78 while (pos0 >= 0 && pos < strlen(txt) && strlen(txt) > 0) {
79 /* printf("%s %d:%d\n", txt, pos0, pos); */
80 pos0 = string_remove_match(txt + pos, rexp, cflags, eflags);