LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-09-12 17:30:27 +04:30
|
|
|
#include "Forward.h"
|
LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
#include "RegexOptions.h"
|
2023-01-27 06:43:50 -06:00
|
|
|
#include <AK/Error.h>
|
LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2024-03-11 16:49:55 +01:00
|
|
|
#include <AK/COWVector.h>
|
2025-03-18 18:08:02 -05:00
|
|
|
#include <AK/FlyString.h>
|
2021-07-18 00:24:55 +04:30
|
|
|
#include <AK/MemMem.h>
|
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <AK/StringView.h>
|
2021-07-20 22:33:00 -04:00
|
|
|
#include <AK/Utf16View.h>
|
2021-07-18 00:24:55 +04:30
|
|
|
#include <AK/Utf32View.h>
|
2021-07-18 05:07:01 +04:30
|
|
|
#include <AK/Utf8View.h>
|
|
|
|
#include <AK/Variant.h>
|
2021-07-18 00:24:55 +04:30
|
|
|
#include <AK/Vector.h>
|
LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
|
|
|
|
namespace regex {
|
|
|
|
|
2020-06-09 00:15:09 +02:00
|
|
|
class RegexStringView {
|
|
|
|
public:
|
2022-07-11 20:19:57 +00:00
|
|
|
RegexStringView() = default;
|
2020-06-09 00:15:09 +02:00
|
|
|
|
2023-01-27 06:43:50 -06:00
|
|
|
RegexStringView(String const& string)
|
|
|
|
: m_view(string.bytes_as_string_view())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-23 20:25:14 +04:30
|
|
|
RegexStringView(StringView const view)
|
2021-07-18 05:07:01 +04:30
|
|
|
: m_view(view)
|
2020-06-09 00:15:09 +02:00
|
|
|
{
|
|
|
|
}
|
2021-07-18 05:07:01 +04:30
|
|
|
|
2021-07-20 22:33:00 -04:00
|
|
|
RegexStringView(Utf16View view)
|
|
|
|
: m_view(view)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2025-03-24 20:12:52 +00:00
|
|
|
RegexStringView(String&&) = delete;
|
2021-09-04 17:53:43 +03:00
|
|
|
|
2021-07-20 22:33:00 -04:00
|
|
|
Utf16View const& u16_view() const
|
|
|
|
{
|
|
|
|
return m_view.get<Utf16View>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool unicode() const { return m_unicode; }
|
|
|
|
void set_unicode(bool unicode) { m_unicode = unicode; }
|
|
|
|
|
2020-06-09 00:15:09 +02:00
|
|
|
bool is_empty() const
|
|
|
|
{
|
2021-07-18 05:07:01 +04:30
|
|
|
return m_view.visit([](auto& view) { return view.is_empty(); });
|
2020-06-09 00:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_null() const
|
|
|
|
{
|
2021-07-18 05:07:01 +04:30
|
|
|
return m_view.visit([](auto& view) { return view.is_null(); });
|
2020-06-09 00:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t length() const
|
|
|
|
{
|
2021-07-20 22:33:00 -04:00
|
|
|
if (unicode()) {
|
|
|
|
return m_view.visit(
|
|
|
|
[](Utf16View const& view) { return view.length_in_code_points(); },
|
|
|
|
[](auto const& view) { return view.length(); });
|
|
|
|
}
|
|
|
|
|
2021-08-02 15:06:43 -04:00
|
|
|
return length_in_code_units();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t length_in_code_units() const
|
|
|
|
{
|
2021-07-20 22:33:00 -04:00
|
|
|
return m_view.visit(
|
|
|
|
[](Utf16View const& view) { return view.length_in_code_units(); },
|
|
|
|
[](auto const& view) { return view.length(); });
|
2021-07-18 05:07:01 +04:30
|
|
|
}
|
|
|
|
|
2021-08-02 15:06:43 -04:00
|
|
|
size_t length_of_code_point(u32 code_point) const
|
|
|
|
{
|
|
|
|
return m_view.visit(
|
|
|
|
[&](Utf16View const&) {
|
|
|
|
if (code_point < 0x10000)
|
|
|
|
return 1;
|
|
|
|
return 2;
|
|
|
|
},
|
|
|
|
[&](auto const&) {
|
|
|
|
if (code_point <= 0x7f)
|
|
|
|
return 1;
|
2021-12-21 17:54:45 +01:00
|
|
|
if (code_point <= 0x07ff)
|
2021-08-02 15:06:43 -04:00
|
|
|
return 2;
|
2021-12-21 17:54:45 +01:00
|
|
|
if (code_point <= 0xffff)
|
2021-08-02 15:06:43 -04:00
|
|
|
return 3;
|
|
|
|
return 4;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-24 12:55:04 +04:30
|
|
|
RegexStringView typed_null_view()
|
|
|
|
{
|
|
|
|
auto view = m_view.visit(
|
|
|
|
[&]<typename T>(T const&) {
|
|
|
|
return RegexStringView { T {} };
|
|
|
|
});
|
|
|
|
view.set_unicode(unicode());
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
RegexStringView construct_as_same(Span<u32> data, Optional<ByteString>& optional_string_storage, Utf16Data& optional_utf16_storage) const
|
2021-07-18 05:07:01 +04:30
|
|
|
{
|
2021-07-20 22:33:00 -04:00
|
|
|
auto view = m_view.visit(
|
2025-04-02 17:56:49 +02:00
|
|
|
[&optional_string_storage, data]<typename T>(T const&) {
|
2021-07-18 05:07:01 +04:30
|
|
|
StringBuilder builder;
|
|
|
|
for (auto ch : data)
|
|
|
|
builder.append(ch); // Note: The type conversion is intentional.
|
2023-12-16 17:49:34 +03:30
|
|
|
optional_string_storage = builder.to_byte_string();
|
2021-07-18 05:07:01 +04:30
|
|
|
return RegexStringView { T { *optional_string_storage } };
|
|
|
|
},
|
2025-04-02 17:56:49 +02:00
|
|
|
[&optional_utf16_storage, data](Utf16View) {
|
|
|
|
auto conversion_result = utf32_to_utf16(Utf32View { data.data(), data.size() }).release_value_but_fixme_should_propagate_errors();
|
|
|
|
optional_utf16_storage = conversion_result.data;
|
|
|
|
auto view = Utf16View { optional_utf16_storage };
|
|
|
|
view.unsafe_set_code_point_length(conversion_result.code_point_count);
|
|
|
|
return RegexStringView { view };
|
2021-07-18 05:07:01 +04:30
|
|
|
});
|
2021-07-20 22:33:00 -04:00
|
|
|
|
|
|
|
view.set_unicode(unicode());
|
|
|
|
return view;
|
2020-06-09 00:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<RegexStringView> lines() const
|
|
|
|
{
|
2021-07-18 05:07:01 +04:30
|
|
|
return m_view.visit(
|
|
|
|
[](StringView view) {
|
2024-03-08 11:27:48 -05:00
|
|
|
auto views = view.lines(StringView::ConsiderCarriageReturn::No);
|
2021-07-18 05:07:01 +04:30
|
|
|
Vector<RegexStringView> new_views;
|
|
|
|
for (auto& view : views)
|
|
|
|
new_views.empend(view);
|
|
|
|
return new_views;
|
|
|
|
},
|
2021-07-20 22:33:00 -04:00
|
|
|
[](Utf16View view) {
|
2022-01-25 00:21:06 +03:30
|
|
|
if (view.is_empty())
|
|
|
|
return Vector<RegexStringView> { view };
|
|
|
|
|
2021-07-20 22:33:00 -04:00
|
|
|
Vector<RegexStringView> views;
|
|
|
|
while (!view.is_empty()) {
|
2025-06-26 12:52:23 -04:00
|
|
|
auto position = view.find_code_unit_offset(u'\n');
|
2021-07-20 22:33:00 -04:00
|
|
|
if (!position.has_value())
|
|
|
|
break;
|
|
|
|
auto offset = position.value() / sizeof(u16);
|
|
|
|
views.empend(view.substring_view(0, offset));
|
|
|
|
view = view.substring_view(offset + 1, view.length_in_code_units() - offset - 1);
|
|
|
|
}
|
|
|
|
if (!view.is_empty())
|
|
|
|
views.empend(view);
|
|
|
|
return views;
|
2021-07-18 05:07:01 +04:30
|
|
|
});
|
2020-06-09 00:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RegexStringView substring_view(size_t offset, size_t length) const
|
|
|
|
{
|
2021-07-20 22:33:00 -04:00
|
|
|
if (unicode()) {
|
|
|
|
auto view = m_view.visit(
|
|
|
|
[&](auto view) { return RegexStringView { view.substring_view(offset, length) }; },
|
2025-04-15 16:11:13 +02:00
|
|
|
[&](Utf16View const& view) { return RegexStringView { view.unicode_substring_view(offset, length) }; });
|
2021-07-20 22:33:00 -04:00
|
|
|
|
|
|
|
view.set_unicode(unicode());
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto view = m_view.visit([&](auto view) { return RegexStringView { view.substring_view(offset, length) }; });
|
|
|
|
view.set_unicode(unicode());
|
|
|
|
return view;
|
2020-06-09 00:15:09 +02:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString to_byte_string() const
|
2020-06-09 00:15:09 +02:00
|
|
|
{
|
2021-07-18 05:07:01 +04:30
|
|
|
return m_view.visit(
|
2023-12-16 17:49:34 +03:30
|
|
|
[](StringView view) { return view.to_byte_string(); },
|
2025-06-26 19:52:09 -04:00
|
|
|
[](Utf16View view) { return view.to_byte_string().release_value_but_fixme_should_propagate_errors(); },
|
2021-07-18 05:07:01 +04:30
|
|
|
[](auto& view) {
|
|
|
|
StringBuilder builder;
|
|
|
|
for (auto it = view.begin(); it != view.end(); ++it)
|
|
|
|
builder.append_code_point(*it);
|
2023-12-16 17:49:34 +03:30
|
|
|
return builder.to_byte_string();
|
2021-07-18 05:07:01 +04:30
|
|
|
});
|
2020-06-09 00:15:09 +02:00
|
|
|
}
|
|
|
|
|
2023-01-27 06:43:50 -06:00
|
|
|
ErrorOr<String> to_string() const
|
|
|
|
{
|
|
|
|
return m_view.visit(
|
|
|
|
[](StringView view) { return String::from_utf8(view); },
|
2025-06-26 19:52:09 -04:00
|
|
|
[](Utf16View view) { return view.to_utf8(); },
|
2023-01-27 06:43:50 -06:00
|
|
|
[](auto& view) -> ErrorOr<String> {
|
|
|
|
StringBuilder builder;
|
|
|
|
for (auto it = view.begin(); it != view.end(); ++it)
|
|
|
|
TRY(builder.try_append_code_point(*it));
|
|
|
|
return builder.to_string();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-02 15:06:43 -04:00
|
|
|
// Note: index must always be the code unit offset to return.
|
2020-06-09 00:15:09 +02:00
|
|
|
u32 operator[](size_t index) const
|
|
|
|
{
|
2021-07-18 05:07:01 +04:30
|
|
|
return m_view.visit(
|
|
|
|
[&](StringView view) -> u32 {
|
|
|
|
auto ch = view[index];
|
2022-10-12 22:00:21 +02:00
|
|
|
if constexpr (IsSigned<char>) {
|
|
|
|
if (ch < 0)
|
|
|
|
return 256u + ch;
|
|
|
|
return ch;
|
|
|
|
}
|
2021-07-18 05:07:01 +04:30
|
|
|
},
|
2025-04-15 16:11:13 +02:00
|
|
|
[&](Utf16View const& view) -> u32 { return view.code_point_at(index); });
|
2020-06-09 00:15:09 +02:00
|
|
|
}
|
|
|
|
|
2023-07-28 20:57:51 +03:30
|
|
|
u32 code_unit_at(size_t code_unit_index) const
|
|
|
|
{
|
|
|
|
if (unicode())
|
|
|
|
return operator[](code_unit_index);
|
|
|
|
|
|
|
|
return m_view.visit(
|
|
|
|
[&](StringView view) -> u32 {
|
|
|
|
auto ch = view[code_unit_index];
|
|
|
|
if constexpr (IsSigned<char>) {
|
|
|
|
if (ch < 0)
|
|
|
|
return 256u + ch;
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
},
|
2025-04-15 16:11:13 +02:00
|
|
|
[&](Utf16View const& view) -> u32 { return view.code_unit_at(code_unit_index); });
|
2023-07-28 20:57:51 +03:30
|
|
|
}
|
|
|
|
|
2021-08-16 10:28:26 -04:00
|
|
|
size_t code_unit_offset_of(size_t code_point_index) const
|
|
|
|
{
|
|
|
|
return m_view.visit(
|
2021-11-11 00:55:02 +01:00
|
|
|
[&](StringView view) -> u32 {
|
2021-08-16 10:28:26 -04:00
|
|
|
Utf8View utf8_view { view };
|
|
|
|
return utf8_view.byte_offset_of(code_point_index);
|
|
|
|
},
|
|
|
|
[&](Utf16View const& view) -> u32 {
|
|
|
|
return view.code_unit_offset_of(code_point_index);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-23 20:25:14 +04:30
|
|
|
bool operator==(char const* cstring) const
|
2020-06-09 00:15:09 +02:00
|
|
|
{
|
2021-07-18 05:07:01 +04:30
|
|
|
return m_view.visit(
|
2023-12-16 17:49:34 +03:30
|
|
|
[&](Utf16View) { return to_byte_string() == cstring; },
|
2021-07-18 05:07:01 +04:30
|
|
|
[&](StringView view) { return view == cstring; });
|
2020-06-09 00:15:09 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
bool operator==(StringView string) const
|
2020-06-09 00:15:09 +02:00
|
|
|
{
|
2021-07-18 05:07:01 +04:30
|
|
|
return m_view.visit(
|
2023-12-16 17:49:34 +03:30
|
|
|
[&](Utf16View) { return to_byte_string() == string; },
|
2021-07-18 05:07:01 +04:30
|
|
|
[&](StringView view) { return view == string; });
|
2020-06-09 00:15:09 +02:00
|
|
|
}
|
|
|
|
|
2021-07-20 22:33:00 -04:00
|
|
|
bool operator==(Utf16View const& other) const
|
|
|
|
{
|
|
|
|
return m_view.visit(
|
|
|
|
[&](Utf16View const& view) { return view == other; },
|
2023-12-16 17:49:34 +03:30
|
|
|
[&](StringView view) { return view == RegexStringView { other }.to_byte_string(); });
|
2021-07-20 22:33:00 -04:00
|
|
|
}
|
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
bool equals(RegexStringView other) const
|
2020-06-09 00:15:09 +02:00
|
|
|
{
|
2021-12-21 18:08:36 +01:00
|
|
|
return other.m_view.visit([this](auto const& view) { return operator==(view); });
|
2021-07-18 05:07:01 +04:30
|
|
|
}
|
2020-06-09 00:15:09 +02:00
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
bool equals_ignoring_case(RegexStringView other) const
|
2021-07-18 05:07:01 +04:30
|
|
|
{
|
|
|
|
// FIXME: Implement equals_ignoring_case() for unicode.
|
|
|
|
return m_view.visit(
|
|
|
|
[&](StringView view) {
|
|
|
|
return other.m_view.visit(
|
2023-03-10 08:48:54 +01:00
|
|
|
[&](StringView other_view) { return view.equals_ignoring_ascii_case(other_view); },
|
2021-07-18 05:07:01 +04:30
|
|
|
[](auto&) -> bool { TODO(); });
|
|
|
|
},
|
2021-07-21 16:38:12 -04:00
|
|
|
[&](Utf16View view) {
|
|
|
|
return other.m_view.visit(
|
|
|
|
[&](Utf16View other_view) { return view.equals_ignoring_case(other_view); },
|
|
|
|
[](auto&) -> bool { TODO(); });
|
|
|
|
},
|
2021-07-18 05:07:01 +04:30
|
|
|
[](auto&) -> bool { TODO(); });
|
2020-06-09 00:15:09 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
bool starts_with(StringView str) const
|
2020-06-09 00:15:09 +02:00
|
|
|
{
|
2021-07-18 05:07:01 +04:30
|
|
|
return m_view.visit(
|
2021-07-20 22:33:00 -04:00
|
|
|
[&](Utf16View) -> bool {
|
|
|
|
TODO();
|
|
|
|
},
|
2021-07-18 05:07:01 +04:30
|
|
|
[&](StringView view) { return view.starts_with(str); });
|
2020-06-09 00:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2025-05-08 19:27:27 -07:00
|
|
|
NO_UNIQUE_ADDRESS Variant<StringView, Utf16View> m_view { StringView {} };
|
|
|
|
NO_UNIQUE_ADDRESS bool m_unicode { false };
|
2020-06-09 00:15:09 +02:00
|
|
|
};
|
|
|
|
|
LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
class Match final {
|
|
|
|
public:
|
|
|
|
Match() = default;
|
|
|
|
~Match() = default;
|
|
|
|
|
2021-12-21 18:06:24 +01:00
|
|
|
Match(RegexStringView view_, size_t const line_, size_t const column_, size_t const global_offset_)
|
LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
: view(view_)
|
|
|
|
, line(line_)
|
|
|
|
, column(column_)
|
|
|
|
, global_offset(global_offset_)
|
|
|
|
, left_column(column_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2025-04-13 18:24:31 +02:00
|
|
|
Match(RegexStringView const view_, size_t capture_group_name_, size_t const line_, size_t const column_, size_t const global_offset_)
|
2021-08-14 16:28:54 -04:00
|
|
|
: view(view_)
|
2025-04-13 18:24:31 +02:00
|
|
|
, capture_group_name(capture_group_name_)
|
2021-08-14 16:28:54 -04:00
|
|
|
, line(line_)
|
|
|
|
, column(column_)
|
|
|
|
, global_offset(global_offset_)
|
LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
, left_column(column_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-24 12:55:04 +04:30
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
view = view.typed_null_view();
|
2025-04-14 15:33:33 +02:00
|
|
|
capture_group_name = -1;
|
2021-07-24 12:55:04 +04:30
|
|
|
line = 0;
|
|
|
|
column = 0;
|
|
|
|
global_offset = 0;
|
|
|
|
left_column = 0;
|
|
|
|
}
|
|
|
|
|
2022-07-11 20:19:57 +00:00
|
|
|
RegexStringView view {};
|
2025-04-14 15:33:33 +02:00
|
|
|
|
|
|
|
// This is a string table index. -1 if none. Not using Optional to keep the struct trivially copyable.
|
|
|
|
ssize_t capture_group_name { -1 };
|
|
|
|
|
LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
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 {
|
2022-07-11 20:19:57 +00:00
|
|
|
RegexStringView view {};
|
LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
AllOptions regex_options {};
|
2020-11-19 01:50:00 +03:30
|
|
|
size_t start_offset { 0 }; // For Stateful matches, saved and restored from Regex::start_offset.
|
LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
|
|
|
|
size_t match_index { 0 };
|
|
|
|
size_t line { 0 };
|
|
|
|
size_t column { 0 };
|
|
|
|
|
2020-06-09 00:15:09 +02:00
|
|
|
size_t global_offset { 0 }; // For multiline matching, knowing the offset from start could be important
|
2020-11-27 19:33:53 +03:30
|
|
|
|
|
|
|
mutable size_t fail_counter { 0 };
|
|
|
|
mutable Vector<size_t> saved_positions;
|
2021-08-02 15:06:43 -04:00
|
|
|
mutable Vector<size_t> saved_code_unit_positions;
|
2021-12-25 05:35:09 +03:30
|
|
|
mutable Vector<size_t> saved_forks_since_last_save;
|
2021-09-12 17:30:27 +04:30
|
|
|
mutable Optional<size_t> fork_to_replace;
|
LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct MatchState {
|
2025-04-15 15:31:08 +02:00
|
|
|
size_t capture_group_count;
|
2021-06-16 10:14:12 +00:00
|
|
|
size_t string_position_before_match { 0 };
|
LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
size_t string_position { 0 };
|
2021-08-02 15:06:43 -04:00
|
|
|
size_t string_position_in_code_units { 0 };
|
LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
size_t instruction_position { 0 };
|
|
|
|
size_t fork_at_position { 0 };
|
2021-12-25 05:35:09 +03:30
|
|
|
size_t forks_since_last_save { 0 };
|
2021-09-12 17:30:27 +04:30
|
|
|
Optional<size_t> initiating_fork;
|
2022-11-03 02:36:25 +03:30
|
|
|
COWVector<Match> matches;
|
2025-04-15 15:31:08 +02:00
|
|
|
COWVector<Match> flat_capture_group_matches; // Vector<Vector<Match>> indexed by match index, then by capture group id; flattened for performance
|
2022-11-03 02:36:25 +03:30
|
|
|
COWVector<u64> repetition_marks;
|
2024-10-08 19:22:33 +02:00
|
|
|
Vector<u64, 64> checkpoints;
|
2025-01-15 15:22:01 +01:00
|
|
|
|
2025-04-15 15:31:08 +02:00
|
|
|
explicit MatchState(size_t capture_group_count)
|
|
|
|
: capture_group_count(capture_group_count)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MatchState(MatchState const&) = default;
|
|
|
|
MatchState(MatchState&&) = default;
|
|
|
|
|
|
|
|
MatchState& operator=(MatchState const&) = default;
|
|
|
|
MatchState& operator=(MatchState&&) = default;
|
|
|
|
|
|
|
|
static MatchState only_for_enumeration() { return MatchState { 0 }; }
|
|
|
|
|
|
|
|
size_t capture_group_matches_size() const
|
|
|
|
{
|
|
|
|
return flat_capture_group_matches.size() / capture_group_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
Span<Match const> capture_group_matches(size_t match_index) const
|
|
|
|
{
|
|
|
|
return flat_capture_group_matches.span().slice(match_index * capture_group_count, capture_group_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
Span<Match> mutable_capture_group_matches(size_t match_index)
|
|
|
|
{
|
|
|
|
return flat_capture_group_matches.mutable_span().slice(match_index * capture_group_count, capture_group_count);
|
|
|
|
}
|
|
|
|
|
2025-01-15 15:22:01 +01:00
|
|
|
// For size_t in {0..100}, ips in {0..500} and repetitions in {0..30}, there are zero collisions.
|
|
|
|
// For the full range, zero collisions were found in 8 million random samples.
|
|
|
|
u64 u64_hash() const
|
|
|
|
{
|
|
|
|
u64 hash = 0xcbf29ce484222325;
|
|
|
|
auto combine = [&hash](auto value) {
|
|
|
|
hash ^= value + 0x9e3779b97f4a7c15 + (hash << 6) + (hash >> 2);
|
|
|
|
};
|
2025-04-18 12:28:45 +02:00
|
|
|
auto combine_vector = [&hash](auto const& vector, auto tag) {
|
|
|
|
hash ^= tag * (vector.size() + 1);
|
2025-01-15 15:22:01 +01:00
|
|
|
for (auto& value : vector) {
|
|
|
|
hash ^= value;
|
|
|
|
hash *= 0x100000001b3;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
combine(string_position_before_match);
|
|
|
|
combine(string_position);
|
|
|
|
combine(string_position_in_code_units);
|
|
|
|
combine(instruction_position);
|
|
|
|
combine(fork_at_position);
|
|
|
|
combine(initiating_fork.value_or(0) + initiating_fork.has_value());
|
2025-04-18 12:28:45 +02:00
|
|
|
combine_vector(repetition_marks, 0xbeefbeefbeefbeef);
|
|
|
|
combine_vector(checkpoints, 0xfacefacefaceface);
|
2025-01-15 15:22:01 +01:00
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
LibRegex: Add a regular expression library
This commit is a mix of several commits, squashed into one because the
commits before 'Move regex to own Library and fix all the broken stuff'
were not fixable in any elegant way.
The commits are listed below for "historical" purposes:
- AK: Add options/flags and Errors for regular expressions
Flags can be provided for any possible flavour by adding a new scoped enum.
Handling of flags is done by templated Options class and the overloaded
'|' and '&' operators.
- AK: Add Lexer for regular expressions
The lexer parses the input and extracts tokens needed to parse a regular
expression.
- AK: Add regex Parser and PosixExtendedParser
This patchset adds a abstract parser class that can be derived to implement
different parsers. A parser produces bytecode to be executed within the
regex matcher.
- AK: Add regex matcher
This patchset adds an regex matcher based on the principles of the T-REX VM.
The bytecode pruduced by the respective Parser is put into the matcher and
the VM will recursively execute the bytecode according to the available OpCodes.
Possible improvement: the recursion could be replaced by multi threading capabilities.
To match a Regular expression, e.g. for the Posix standard regular expression matcher
use the following API:
```
Pattern<PosixExtendedParser> pattern("^.*$");
auto result = pattern.match("Well, hello friends!\nHello World!"); // Match whole needle
EXPECT(result.count == 1);
EXPECT(result.matches.at(0).view.starts_with("Well"));
EXPECT(result.matches.at(0).view.end() == "!");
result = pattern.match("Well, hello friends!\nHello World!", PosixFlags::Multiline); // Match line by line
EXPECT(result.count == 2);
EXPECT(result.matches.at(0).view == "Well, hello friends!");
EXPECT(result.matches.at(1).view == "Hello World!");
EXPECT(pattern.has_match("Well,....")); // Just check if match without a result, which saves some resources.
```
- AK: Rework regex to work with opcodes objects
This patchsets reworks the matcher to work on a more structured base.
For that an abstract OpCode class and derived classes for the specific
OpCodes have been added. The respective opcode logic is contained in
each respective execute() method.
- AK: Add benchmark for regex
- AK: Some optimization in regex for runtime and memory
- LibRegex: Move regex to own Library and fix all the broken stuff
Now regex works again and grep utility is also in place for testing.
This commit also fixes the use of regex.h in C by making `regex_t`
an opaque (-ish) type, which makes its behaviour consistent between
C and C++ compilers.
Previously, <regex.h> would've blown C compilers up, and even if it
didn't, would've caused a leak in C code, and not in C++ code (due to
the existence of `OwnPtr` inside the struct).
To make this whole ordeal easier to deal with (for now), this pulls the
definitions of `reg*()` into LibRegex.
pros:
- The circular dependency between LibC and LibRegex is broken
- Eaiser to test (without accidentally pulling in the host's libc!)
cons:
- Using any of the regex.h functions will require the user to link -lregex
- The symbols will be missing from libc, which will be a big surprise
down the line (especially with shared libs).
Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2020-04-26 14:45:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2020-06-09 00:15:09 +02:00
|
|
|
|
|
|
|
using regex::RegexStringView;
|
2021-01-17 16:06:30 +01:00
|
|
|
|
|
|
|
template<>
|
|
|
|
struct AK::Formatter<regex::RegexStringView> : Formatter<StringView> {
|
2021-11-16 01:15:21 +01:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, regex::RegexStringView value)
|
2021-01-17 16:06:30 +01:00
|
|
|
{
|
2023-12-16 17:49:34 +03:30
|
|
|
auto string = value.to_byte_string();
|
2021-07-18 05:07:01 +04:30
|
|
|
return Formatter<StringView>::format(builder, string);
|
2021-01-17 16:06:30 +01:00
|
|
|
}
|
|
|
|
};
|
2025-04-13 18:25:46 +02:00
|
|
|
|
|
|
|
template<>
|
|
|
|
struct AK::Traits<regex::Match> : public AK::DefaultTraits<regex::Match> {
|
|
|
|
constexpr static bool is_trivial() { return true; }
|
|
|
|
};
|