summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/pattern.h
blob: 204d58914b4c38c5b37aae2c605318e6fec40780 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef PATTERN_H
#define PATTERN_H

class base_pattern
{
public:
    virtual ~base_pattern() { }

    virtual bool valid() const = 0;
    virtual bool matches(const string &s) const = 0;
    virtual const string &tostring() const = 0;
};

class text_pattern : public base_pattern
{
public:
    text_pattern(const string &s, bool icase = false)
        : pattern(s), compiled_pattern(NULL),
          isvalid(true), ignore_case(icase)
    {
    }

    text_pattern()
        : pattern(), compiled_pattern(NULL),
         isvalid(false), ignore_case(false)
    {
    }

    text_pattern(const text_pattern &tp)
        : base_pattern(tp),
          pattern(tp.pattern),
          compiled_pattern(NULL),
          isvalid(tp.isvalid),
          ignore_case(tp.ignore_case)
    {
    }

    ~text_pattern();
    const text_pattern &operator= (const text_pattern &tp);
    const text_pattern &operator= (const string &spattern);
    bool operator== (const text_pattern &tp) const;
    bool compile() const;

    bool empty() const { return !pattern.length(); }

    bool valid() const
    {
        return isvalid
            && (compiled_pattern || (isvalid = compile()));
    }

    bool matches(const char *s, int length) const;

    bool matches(const char *s) const
    {
        return matches(s, strlen(s));
    }

    bool matches(const string &s) const
    {
        return matches(s.c_str(), s.length());
    }

    const string &tostring() const
    {
        return pattern;
    }

private:
    string pattern;
    mutable void *compiled_pattern;
    mutable bool isvalid;
    bool ignore_case;
};
#endif