Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/core/regex/include/sourcemeta/core/regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,20 @@ enum class RegexIndex : std::uint8_t {
};
#endif

/// @ingroup regex
/// The dialects that a regular expression pattern can be interpreted with.
enum class RegexDialect : std::uint8_t {
/// A permissive superset of ECMA 262 with PCRE2 extensions
Permissive
};

/// @ingroup regex
///
/// Compile a regular expression from a string. If the regular expression is
/// invalid, no value is returned. In this function:
///
/// - Regexes are NOT automatically anchored
/// - Regexes assume `DOTALL`
/// - Permissive regexes are NOT automatically anchored
/// - Permissive regexes assume `DOTALL`
/// - Regexes assume Unicode
/// - Regexes are case sensitive
/// - No matching happens (only boolean validation)
Expand All @@ -87,11 +94,14 @@ enum class RegexIndex : std::uint8_t {
/// #include <cassert>
///
/// const sourcemeta::core::Regex regex{
/// sourcemeta::core::to_regex("^foo")};
/// sourcemeta::core::to_regex("^foo",
/// sourcemeta::core::RegexDialect::Permissive)};
/// assert(regex.has_value());
/// ```
SOURCEMETA_CORE_REGEX_EXPORT
auto to_regex(const std::string_view pattern) -> std::optional<Regex>;
auto to_regex(const std::string_view pattern,
Comment thread
jviotti marked this conversation as resolved.
const RegexDialect dialect = RegexDialect::Permissive)
-> std::optional<Regex>;

/// @ingroup regex
///
Expand Down Expand Up @@ -123,7 +133,9 @@ auto matches(const Regex &regex, const std::string_view value) -> bool;
/// ```
SOURCEMETA_CORE_REGEX_EXPORT
auto matches_if_valid(const std::string_view pattern,
Comment thread
jviotti marked this conversation as resolved.
const std::string_view value) -> bool;
const std::string_view value,
const RegexDialect dialect = RegexDialect::Permissive)
-> bool;

/// @ingroup regex
///
Expand Down
9 changes: 6 additions & 3 deletions src/core/regex/regex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

namespace sourcemeta::core {

auto to_regex(const std::string_view pattern) -> std::optional<Regex> {
auto to_regex(const std::string_view pattern,
[[maybe_unused]] const RegexDialect dialect)
-> std::optional<Regex> {
if (pattern == ".*" || pattern == "^.*$" || pattern == "^(.*)$" ||
pattern == "(.*)" || pattern == "[\\s\\S]*" || pattern == "^[\\s\\S]*$") {
return RegexTypeNoop{};
Expand Down Expand Up @@ -128,8 +130,9 @@ auto matches(const Regex &regex, const std::string_view value) -> bool {
}

auto matches_if_valid(const std::string_view pattern,
const std::string_view value) -> bool {
const auto regex{to_regex(pattern)};
const std::string_view value, const RegexDialect dialect)
-> bool {
const auto regex{to_regex(pattern, dialect)};
return regex.has_value() && matches(regex.value(), value);
}

Expand Down
4 changes: 2 additions & 2 deletions test/regex/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
sourcemeta_test(NAMESPACE sourcemeta PROJECT core NAME regex
SOURCES
regex_matches_if_valid_test.cc
regex_matches_ecma262_test.cc
regex_matches_rfc9485_test.cc
regex_permissive_matches_ecma262_test.cc
regex_permissive_matches_rfc9485_test.cc
regex_is_ecma_test.cc
regex_to_regex_test.cc
regex_test.cc)
Expand Down
Loading
Loading