Check for ICU for include of icu_I18N.h.
[idzebra-moved-to-github.git] / util / rob_regexp.c
1 /* $Id: rob_regexp.c,v 1.2 2007-10-23 12:36:22 adam Exp $
2    Copyright (C) 1995-2007
3    Index Data ApS
4
5    This file is part of the Zebra server.
6
7    Zebra is free software; you can redistribute it and/or modify it under
8    the terms of the GNU General Public License as published by the Free
9    Software Foundation; either version 2, or (at your option) any later
10    version.
11
12    Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13    WARRANTY; without even the implied warranty of MERCHANTABILITY or
14    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15    for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Zebra; see the file LICENSE.zebra.  If not, write to the
19    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.
21 */
22
23 /** 
24     \file rob_regexp.c
25     \brief Rob Pike's regular expression matcher
26     
27     Taken verbatim from Beautiful code.. ANSIfied a bit.
28  */
29   
30
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include <string.h>
35
36 #include "rob_regexp.h"
37 #include <yaz/xmalloc.h>
38 #include <yaz/wrbuf.h>
39 #include <yaz/log.h>
40
41 static int matchhere(const char *regexp, const char *text);
42 static int matchstar(int c, const char *regexp, const char *text);
43
44 int zebra_rob_regexp(const char *regexp, const char *text)
45 {
46     if (regexp[0] == '^')
47         return matchhere(regexp+1, text);
48     do 
49     {
50         if (matchhere(regexp, text))
51             return 1;
52     }
53     while (*text++);
54     return 0;
55 }
56
57 static int matchhere(const char *regexp, const char *text)
58 {
59     if (regexp[0] == '\0')
60         return 1;
61     if (regexp[1] == '*')
62         return matchstar(regexp[0], regexp+2, text);
63     if (regexp[0] == '$' && regexp[1] == '\0')
64         return *text == '\0';
65     if (*text && (regexp[0] == '.' || regexp[0] == *text))
66         return matchhere(regexp+1, text+1);
67     return 0;
68 }
69
70 static int matchstar(int c, const char *regexp, const char *text)
71 {
72     do
73     {
74         if (matchhere(regexp, text))
75             return 1;
76     }
77     while (*text && (*text++ == c || c == '.'));
78     return 0;
79 }
80
81
82 /*
83  * Local variables:
84  * c-basic-offset: 4
85  * indent-tabs-mode: nil
86  * End:
87  * vim: shiftwidth=4 tabstop=8 expandtab
88  */
89