Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
regex.h
1#ifndef SOURCEMETA_CORE_REGEX_H_
2#define SOURCEMETA_CORE_REGEX_H_
3
4#ifndef SOURCEMETA_CORE_REGEX_EXPORT
5#include <sourcemeta/core/regex_export.h>
6#endif
7
8#include <cstddef> // std::size_t
9#include <cstdint> // std::uint8_t
10#include <memory> // std::shared_ptr
11#include <optional> // std::optional
12#include <string> // std::string
13#include <string_view> // std::string_view
14#include <utility> // std::pair
15#include <variant> // std::variant
16
26
27namespace sourcemeta::core {
28
31using RegexTypePrefix = std::string;
32
35 auto operator==(const RegexTypeNonEmpty &) const noexcept -> bool = default;
36};
37
40using RegexTypeRange = std::pair<std::size_t, std::size_t>;
41
46 std::shared_ptr<void> code;
47 auto operator==(const RegexTypePCRE2 &other) const noexcept -> bool {
48 return this->code == other.code;
49 }
50};
51
54 auto operator==(const RegexTypeNoop &) const noexcept -> bool = default;
55};
56
61#if !defined(DOXYGEN)
62// For fast internal dispatching. It must stay in sync with the variant above
63enum class RegexIndex : std::uint8_t {
64 Prefix = 0,
65 NonEmpty,
66 Range,
67 PCRE2,
68 Noop
69};
70#endif
71
74enum class RegexDialect : std::uint8_t {
84};
85
112SOURCEMETA_CORE_REGEX_EXPORT
113auto to_regex(const std::string_view pattern,
115 const bool optimise_for_match = true) -> std::optional<Regex>;
116
130SOURCEMETA_CORE_REGEX_EXPORT
131auto matches(const Regex &regex, const std::string_view value) -> bool;
132
145SOURCEMETA_CORE_REGEX_EXPORT
146auto matches_if_valid(const std::string_view pattern,
147 const std::string_view value,
149 -> bool;
150
169SOURCEMETA_CORE_REGEX_EXPORT
170auto replace_all(const Regex &regex, const std::string_view subject,
171 const std::string_view replacement) -> std::string;
172
186SOURCEMETA_CORE_REGEX_EXPORT
187auto is_regex_ecma(const std::string_view pattern) -> bool;
188
189} // namespace sourcemeta::core
190
191#endif
std::shared_ptr< void > code
The opaque handle to the compiled expression.
Definition regex.h:46
SOURCEMETA_CORE_REGEX_EXPORT auto to_regex(const std::string_view pattern, const RegexDialect dialect=RegexDialect::Permissive, const bool optimise_for_match=true) -> std::optional< Regex >
RegexDialect
Definition regex.h:74
std::string RegexTypePrefix
Definition regex.h:31
std::pair< std::size_t, std::size_t > RegexTypeRange
Definition regex.h:40
SOURCEMETA_CORE_REGEX_EXPORT auto matches_if_valid(const std::string_view pattern, const std::string_view value, const RegexDialect dialect=RegexDialect::Permissive) -> bool
std::variant< RegexTypePrefix, RegexTypeNonEmpty, RegexTypeRange, RegexTypePCRE2, RegexTypeNoop > Regex
Definition regex.h:59
SOURCEMETA_CORE_REGEX_EXPORT auto matches(const Regex &regex, const std::string_view value) -> bool
SOURCEMETA_CORE_REGEX_EXPORT auto replace_all(const Regex &regex, const std::string_view subject, const std::string_view replacement) -> std::string
SOURCEMETA_CORE_REGEX_EXPORT auto is_regex_ecma(const std::string_view pattern) -> bool
@ IRegexp
Definition regex.h:80
@ Permissive
A permissive superset of ECMA 262 with PCRE2 extensions.
Definition regex.h:76
@ IRegexpSearch
Definition regex.h:83
Definition regex.h:53