summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibRegex
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-07-30 09:41:41 -0400
committerLinus Groh <mail@linusgroh.de>2021-07-30 21:26:31 +0100
commit1400e3cf58c14594f06d98db25e10f908480ce67 (patch)
tree44e9cc09f5ed609286d75977ec3928f9ec54f375 /Userland/Libraries/LibRegex
parentb162517065fb78639173b04dc050dfe84ddcebfd (diff)
downloadserenity-1400e3cf58c14594f06d98db25e10f908480ce67.zip
LibRegex: Allow separately parsing patterns and creating Regex objects
Adds a static method to parse a regex pattern and return the result, and a constructor to accept a parse result. This is to allow LibJS to parse the pattern string of a RegExpLiteral once and hand off regex objects any number of times thereafter.
Diffstat (limited to 'Userland/Libraries/LibRegex')
-rw-r--r--Userland/Libraries/LibRegex/RegexMatcher.cpp18
-rw-r--r--Userland/Libraries/LibRegex/RegexMatcher.h3
2 files changed, 21 insertions, 0 deletions
diff --git a/Userland/Libraries/LibRegex/RegexMatcher.cpp b/Userland/Libraries/LibRegex/RegexMatcher.cpp
index bd9680d41a..e30d285716 100644
--- a/Userland/Libraries/LibRegex/RegexMatcher.cpp
+++ b/Userland/Libraries/LibRegex/RegexMatcher.cpp
@@ -19,6 +19,15 @@ static RegexDebug s_regex_dbg(stderr);
#endif
template<class Parser>
+regex::Parser::Result Regex<Parser>::parse_pattern(StringView pattern, typename ParserTraits<Parser>::OptionsType regex_options)
+{
+ regex::Lexer lexer(pattern);
+
+ Parser parser(lexer, regex_options);
+ return parser.parse();
+}
+
+template<class Parser>
Regex<Parser>::Regex(String pattern, typename ParserTraits<Parser>::OptionsType regex_options)
: pattern_value(move(pattern))
{
@@ -32,6 +41,15 @@ Regex<Parser>::Regex(String pattern, typename ParserTraits<Parser>::OptionsType
}
template<class Parser>
+Regex<Parser>::Regex(regex::Parser::Result parse_result, String pattern, typename ParserTraits<Parser>::OptionsType regex_options)
+ : pattern_value(move(pattern))
+ , parser_result(move(parse_result))
+{
+ if (parser_result.error == regex::Error::NoError)
+ matcher = make<Matcher<Parser>>(this, regex_options);
+}
+
+template<class Parser>
Regex<Parser>::Regex(Regex&& regex)
: pattern_value(move(regex.pattern_value))
, parser_result(move(regex.parser_result))
diff --git a/Userland/Libraries/LibRegex/RegexMatcher.h b/Userland/Libraries/LibRegex/RegexMatcher.h
index 35e72962ce..1229bacc94 100644
--- a/Userland/Libraries/LibRegex/RegexMatcher.h
+++ b/Userland/Libraries/LibRegex/RegexMatcher.h
@@ -80,7 +80,10 @@ public:
OwnPtr<Matcher<Parser>> matcher { nullptr };
mutable size_t start_offset { 0 };
+ static regex::Parser::Result parse_pattern(StringView pattern, typename ParserTraits<Parser>::OptionsType regex_options = {});
+
explicit Regex(String pattern, typename ParserTraits<Parser>::OptionsType regex_options = {});
+ Regex(regex::Parser::Result parse_result, String pattern, typename ParserTraits<Parser>::OptionsType regex_options = {});
~Regex() = default;
Regex(Regex&&);
Regex& operator=(Regex&&);