summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibRegex/RegexMatch.h
blob: f5ddf929f73ed09ab0b0973ffc5a426a223a7c81 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#include "RegexOptions.h"

#include "AK/FlyString.h"
#include "AK/HashMap.h"
#include "AK/String.h"
#include "AK/StringBuilder.h"
#include "AK/StringView.h"
#include "AK/Utf32View.h"
#include "AK/Vector.h"

namespace regex {

class RegexStringView {
public:
    RegexStringView(const char* chars)
        : m_u8view(chars)
    {
    }

    RegexStringView(const String& string)
        : m_u8view(string)
    {
    }

    RegexStringView(const StringView view)
        : m_u8view(view)
    {
    }
    RegexStringView(const Utf32View view)
        : m_u32view(view)
    {
    }

    bool is_u8_view() const { return m_u8view.has_value(); }
    bool is_u32_view() const { return m_u32view.has_value(); }

    const StringView& u8view() const
    {
        VERIFY(m_u8view.has_value());
        return m_u8view.value();
    };

    const Utf32View& u32view() const
    {
        VERIFY(m_u32view.has_value());
        return m_u32view.value();
    };

    bool is_empty() const
    {
        if (is_u8_view())
            return m_u8view.value().is_empty();
        else
            return m_u32view.value().is_empty();
    }

    bool is_null() const
    {
        if (is_u8_view())
            return m_u8view.value().is_null();
        else
            return m_u32view.value().code_points() == nullptr;
    }

    size_t length() const
    {
        if (is_u8_view())
            return m_u8view.value().length();
        else
            return m_u32view.value().length();
    }

    Vector<RegexStringView> lines() const
    {
        if (is_u8_view()) {
            auto views = u8view().lines(false);
            Vector<RegexStringView> new_views;
            for (auto& view : views)
                new_views.append(move(view));
            return new_views;
        }

        // FIXME: line splitting for Utf32View needed
        Vector<RegexStringView> views;
        views.append(m_u32view.value());
        return views;
    }

    RegexStringView substring_view(size_t offset, size_t length) const
    {
        if (is_u8_view()) {
            return u8view().substring_view(offset, length);
        }
        return u32view().substring_view(offset, length);
    }

    String to_string() const
    {
        if (is_u8_view()) {
            return u8view().to_string();
        }

        StringBuilder builder;
        builder.append(u32view());
        return builder.to_string();
    }

    u32 operator[](size_t index) const
    {
        if (is_u8_view()) {
            i8 ch = u8view()[index];
            u8 value = *reinterpret_cast<u8*>(&ch);
            return static_cast<u32>(value);
        }
        return u32view().code_points()[index];
    }

    bool operator==(const char* cstring) const
    {
        if (is_u8_view())
            return u8view() == cstring;

        return to_string() == cstring;
    }

    bool operator!=(const char* cstring) const
    {
        return !(*this == cstring);
    }

    bool operator==(const String& string) const
    {
        if (is_u8_view())
            return u8view() == string;

        return to_string() == string;
    }

    bool operator==(const StringView& other) const
    {
        if (is_u8_view())
            return u8view() == other;

        return false;
    }

    bool operator!=(const StringView& other) const
    {
        return !(*this == other);
    }

    bool operator==(const Utf32View& other) const
    {
        if (is_u32_view()) {
            StringBuilder builder;
            builder.append(other);
            return to_string() == builder.to_string();
        }

        return false;
    }

    bool operator!=(const Utf32View& other) const
    {
        return !(*this == other);
    }

    const char* characters_without_null_termination() const
    {
        if (is_u8_view())
            return u8view().characters_without_null_termination();

        return to_string().characters(); // FIXME: it contains the null termination, does that actually matter?
    }

    bool starts_with(const StringView& str) const
    {
        if (is_u32_view())
            return false;
        return u8view().starts_with(str);
    }

    bool starts_with(const Utf32View& str) const
    {
        if (is_u8_view())
            return false;

        StringBuilder builder;
        builder.append(str);
        return to_string().starts_with(builder.to_string());
    }

private:
    Optional<StringView> m_u8view;
    Optional<Utf32View> m_u32view;
};

class Match final {
private:
    Optional<FlyString> string;

public:
    Match() = default;
    ~Match() = default;

    Match(const RegexStringView view_, const size_t line_, const size_t column_, const size_t global_offset_)
        : view(view_)
        , line(line_)
        , column(column_)
        , global_offset(global_offset_)
        , left_column(column_)
    {
    }

    Match(const String string_, const size_t line_, const size_t column_, const size_t global_offset_)
        : string(string_)
        , view(string.value().view())
        , line(line_)
        , column(column_)
        , global_offset(global_offset_)
        , left_column(column_)
    {
    }

    RegexStringView view { nullptr };
    size_t line { 0 };
    size_t column { 0 };
    size_t global_offset { 0 };

    // ugly, as not usable by user, but needed to prevent to create extra vectors that are
    // able to store the column when the left paren has been found
    size_t left_column { 0 };
};

struct MatchInput {
    RegexStringView view { nullptr };
    AllOptions regex_options {};
    size_t start_offset { 0 }; // For Stateful matches, saved and restored from Regex::start_offset.

    size_t match_index { 0 };
    size_t line { 0 };
    size_t column { 0 };

    size_t global_offset { 0 }; // For multiline matching, knowing the offset from start could be important

    mutable size_t fail_counter { 0 };
    mutable Vector<size_t> saved_positions;
};

struct MatchState {
    size_t string_position { 0 };
    size_t instruction_position { 0 };
    size_t fork_at_position { 0 };
};

struct MatchOutput {
    size_t operations;
    Vector<Match> matches;
    Vector<Vector<Match>> capture_group_matches;
    Vector<HashMap<String, Match>> named_capture_group_matches;
};

}

using regex::RegexStringView;

template<>
struct AK::Formatter<regex::RegexStringView> : Formatter<StringView> {
    void format(FormatBuilder& builder, const regex::RegexStringView& value)
    {
        return Formatter<StringView>::format(builder, { value.characters_without_null_termination(), value.length() });
    }
};