summaryrefslogtreecommitdiff
path: root/src/inc/regex_util.h
blob: 438c8b133a971e72d3e6fd3af113c7a102280422 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// See regex_base.h for more information.
//
// This header creates some concrete instantiations of RegExBase for commonly used scenarios. In
// particular, basic regular expression matching base on the regular expression language described in
// clr::regex::ItemTraitsBase (found in regex_base.h) is instantiated for use with SString, ASCII and
// UNICODE strings (clr::regex::SStringRegex, clr::regex::WSTRRegEx, and clr::regex::STRRegEx
// respectively). Each type definition includes an example of its use.
//

//

#ifndef _REGEX_UTIL_H_
#define _REGEX_UTIL_H_

#ifndef MODE_ANY
#define MODE_ANY
#endif

#include "regex_base.h"

#ifdef _DEBUG

namespace clr
{
namespace regex
{

//=======================================================================================================
// Derives from Group to provide two additional convenience methods (GetSString variants).

class SStringGroup : public Group<SString::CIterator>
{
public:
    SStringGroup()
        : Group<SString::CIterator>()
        { WRAPPER_NO_CONTRACT; }

    SStringGroup(const InputIterator& _start, const InputIterator& _end, bool _isClosed = false)
        : Group<SString::CIterator>(_start, _end, _isClosed)
        { WRAPPER_NO_CONTRACT; }

    // Since SStrings constructed from ranges require the original source string, this is a required
    // input argument. Returns the input substring that matches the corresponding grouping.
    SString GetSString(const SString& src)
        { WRAPPER_NO_CONTRACT; return SString(src, Begin(), End()); }

    // Since SStrings constructed from ranges require the original source string, this is a required
    // input argument. This version takes a target SString as a buffer, and also returns this buffer
    // as a reference. Returns the input substring that matches the corresponding grouping.
    SString& GetSString(const SString& src, SString& tgt)
        { WRAPPER_NO_CONTRACT; tgt.Set(src, Begin(), End()); return tgt; }
};

//=======================================================================================================
typedef WCHARItemTraits<SString::CIterator> SStringItemTraits;

//=======================================================================================================
// Regular expression matching with SStrings.
//
// Here is an example of how to use SStringRegEx with grouping enabled.
//
//      using namespace clr::regex;
//      SString input(SL"Simmons"); // usually this is derived from some variable source
//      SStringRegEx::GroupingContainer container;
//      if (SStringRegEx::Match(SL"(Sim+on)", input, container)) {
//          printf("%S", container[1].GetSString(input).GetUnicode());
//      }
//
//      This sample should result in "Simmon" being printed.


class SStringRegEx : public RegExBase<SStringItemTraits>
{
    typedef RegExBase<SStringItemTraits> PARENT_TYPE;

public:
    using PARENT_TYPE::Match;
    using PARENT_TYPE::Matches;

    typedef PARENT_TYPE::InputIterator InputIterator;

    typedef GroupContainer<InputIterator, SStringGroup > GroupingContainer;

    static bool Match(
        const SString&            regex,
        const SString&            input,
        GroupingContainer& groups,
        MatchFlags                flags = DefaultMatchFlags)
    {
        WRAPPER_NO_CONTRACT;
        return Match(regex.Begin(), regex.End(), input.Begin(), input.End(), groups, flags);
    }

    static bool Matches(
        const SString&            regex,
        const SString&            input,
        MatchFlags                flags = DefaultMatchFlags)
    {
        WRAPPER_NO_CONTRACT;
        return Matches(regex.Begin(), regex.End(), input.Begin(), input.End(), flags);
    }

};

//=======================================================================================================
// Regular expression matching with UNICODE strings.
//
// Here is an example of how to use WSTRRegEx to match against a null-terminated string without grouping.
//
//      using namespace clr::regex;
//      LPCWSTR input = L"Simmons";
//      if (WSTRRegEx::Matches(L"Sim+on", input))
//          printf("Match succeeded");
//      else
//          printf("Match failed");
//
//      This sample should result in "Match succeeded" being printed.

class WSTRRegEx : public RegExBase<WCHARItemTraits<LPCWSTR> >
{
    typedef RegExBase<WCHARItemTraits<LPCWSTR> > PARENT_TYPE;

public:
    using PARENT_TYPE::Match;
    using PARENT_TYPE::Matches;

    static bool Match(
        LPCWSTR                   regex,
        LPCWSTR                   input,
        GroupingContainer& groups,
        MatchFlags                flags = DefaultMatchFlags)
    {
        WRAPPER_NO_CONTRACT;
        return Match(regex, regex + wcslen(regex), input, input + wcslen(input), groups, flags);
    }

    static bool Matches(
        LPCWSTR                   regex,
        LPCWSTR                   input,
        MatchFlags                flags = DefaultMatchFlags)
    {
        CONTRACTL {
            NOTHROW;
            GC_NOTRIGGER;
            MODE_ANY;
        } CONTRACTL_END;

        return Matches(regex, regex + wcslen(regex), input, input + wcslen(input), flags);
    }
};

//=======================================================================================================
// Regular expression matching with ASCII strings.
//
// Here is an example of how to use STRRegEx to match against a substring based on begin and end range
// pointers, with grouping disabled and case insensitivity enabled.
//
//      using namespace clr::regex;
//      LPCSTR input = "123Simmons456";
//      if (STRRegEx::Matches("Sim+on", input+3, input+10, STRRegEx::MF_CASE_INSENSITIVE))
//          printf("Match succeeded");
//      else
//          printf("Match failed");
//
//      This sample should result in "Match succeeded" being printed.

class STRRegEx : public RegExBase<CHARItemTraits<LPCSTR> >
{
    typedef RegExBase<CHARItemTraits<LPCSTR> > PARENT_TYPE;

public:
    using PARENT_TYPE::Match;
    using PARENT_TYPE::Matches;

    static bool Match(
        LPCSTR                    regex,
        LPCSTR                    input,
        GroupingContainer& groups,
        MatchFlags                flags = DefaultMatchFlags)
    {
        WRAPPER_NO_CONTRACT;
        return Match(regex, regex + strlen(regex), input, input + strlen(input), groups, flags);
    }

    static bool Matches(
        LPCSTR                    regex,
        LPCSTR                    input,
        MatchFlags                flags = DefaultMatchFlags)
    {
        CONTRACTL {
            NOTHROW;
            GC_NOTRIGGER;
            MODE_ANY;
        } CONTRACTL_END;

        return Matches(regex, regex + strlen(regex), input, input + strlen(input), flags);
    }
};

} // namespace regex
} // namespace clr

#endif // _DEBUG

#endif // _REGEX_UTIL_H_