Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inspection of Regex options support #13354

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions spec/std/regex_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,14 @@ describe "Regex" do
end
)
end

it ".supports_compile_options?" do
Regex.supports_compile_options?(:anchored).should be_true
Regex.supports_compile_options?(:endanchored).should eq Regex::Engine.version_number >= {10, 0}
end

it ".supports_match_options?" do
Regex.supports_compile_options?(:anchored).should be_true
Regex.supports_compile_options?(:endanchored).should eq Regex::Engine.version_number >= {10, 0}
HertzDevil marked this conversation as resolved.
Show resolved Hide resolved
end
end
16 changes: 16 additions & 0 deletions src/regex.cr
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ class Regex
# This alias is supposed to replace `Options`.
alias CompileOptions = Options

# Returns `true` if the regex engine supports all *options* flags when compiling a pattern.
def self.supports_compile_options?(options : CompileOptions) : Bool
options.each do |flag|
return false unless Engine.supports_compile_flag?(flag)
end
true
end

# Represents options passed to regex match methods such as `Regex#match`.
@[Flags]
enum MatchOptions
Expand All @@ -308,6 +316,14 @@ class Regex
NO_UTF_CHECK
end

# Returns `true` if the regex engine supports all *options* flags when matching a pattern.
def self.supports_match_options?(options : MatchOptions) : Bool
options.each do |flag|
return false unless Engine.supports_match_flag?(flag)
end
true
end

# Returns a `Regex::CompileOptions` representing the optional flags applied to this `Regex`.
#
# ```
Expand Down
8 changes: 8 additions & 0 deletions src/regex/pcre.cr
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ module Regex::PCRE
flag
end

def self.supports_compile_flag?(options)
!options.endanchored? && !options.match_invalid_utf?
end

private def pcre_match_options(options)
flag = 0
Regex::Options.each do |option|
Expand Down Expand Up @@ -113,6 +117,10 @@ module Regex::PCRE
flag
end

def self.supports_match_flag?(options)
!options.endanchored? && !options.no_jit?
end

def finalize
LibPCRE.free_study @extra
{% unless flag?(:interpreted) %}
Expand Down
8 changes: 8 additions & 0 deletions src/regex/pcre2.cr
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ module Regex::PCRE2
flag
end

def self.supports_compile_flag?(options)
true
end

private def pcre2_match_options(options)
flag = 0
Regex::Options.each do |option|
Expand Down Expand Up @@ -140,6 +144,10 @@ module Regex::PCRE2
flag
end

def self.supports_match_flag?(options)
true
end

protected def self.error_impl(source)
code = PCRE2.compile(source, LibPCRE2::UTF | LibPCRE2::DUPNAMES | LibPCRE2::UCP) do |error_message|
return error_message
Expand Down