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
16 changes: 16 additions & 0 deletions spec/std/regex_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ describe "Regex" do
it "raises exception with invalid regex" do
expect_raises(ArgumentError) { Regex.new("+") }
end

describe "options" do
it "regular" do
Regex.new("", Regex::Options::ANCHORED).options.anchored?.should be_true
end

it "unnamed option" do
{% if Regex::Engine.resolve.name == "Regex::PCRE" %}
Regex.new("^/foo$", Regex::Options.new(0x00000020)).matches?("/foo\n").should be_false
{% else %}
expect_raises ArgumentError, "Unknown Regex::Option value: 32" do
Regex.new("", Regex::Options.new(32))
end
{% end %}
end
end
end

it "#options" do
Expand Down
34 changes: 20 additions & 14 deletions src/regex/pcre.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@ module Regex::PCRE

private def pcre_options(options)
flag = 0
options.each do |option|
flag |= case option
when .ignore_case? then LibPCRE::CASELESS
when .multiline? then LibPCRE::DOTALL | LibPCRE::MULTILINE
when .extended? then LibPCRE::EXTENDED
when .anchored? then LibPCRE::ANCHORED
when .utf_8? then LibPCRE::UTF8
when .no_utf8_check? then LibPCRE::NO_UTF8_CHECK
when .dupnames? then LibPCRE::DUPNAMES
when .ucp? then LibPCRE::UCP
else
# Unnamed values are explicitly used PCRE options, just pass them through:
option.value
end
Regex::Options.each do |option|
if options.includes?(option)
flag |= case option
when .ignore_case? then LibPCRE::CASELESS
when .multiline? then LibPCRE::DOTALL | LibPCRE::MULTILINE
when .extended? then LibPCRE::EXTENDED
when .anchored? then LibPCRE::ANCHORED
when .utf_8? then LibPCRE::UTF8
when .no_utf8_check? then LibPCRE::NO_UTF8_CHECK
when .dupnames? then LibPCRE::DUPNAMES
when .ucp? then LibPCRE::UCP
else
raise "unreachable"
end
options &= ~option
end
end

# Unnamed values are explicitly used PCRE options, just pass them through:
flag |= options.value

flag
end

Expand Down
32 changes: 19 additions & 13 deletions src/regex/pcre2.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,25 @@ module Regex::PCRE2

private def pcre2_options(options)
flag = 0
options.each do |option|
flag |= case option
when .ignore_case? then LibPCRE2::CASELESS
when .multiline? then LibPCRE2::DOTALL | LibPCRE2::MULTILINE
when .extended? then LibPCRE2::EXTENDED
when .anchored? then LibPCRE2::ANCHORED
when .utf_8? then LibPCRE2::UTF
when .no_utf8_check? then LibPCRE2::NO_UTF_CHECK
when .dupnames? then LibPCRE2::DUPNAMES
when .ucp? then LibPCRE2::UCP
else
raise "unreachable"
end
Regex::Options.each do |option|
if options.includes?(option)
flag |= case option
when .ignore_case? then LibPCRE2::CASELESS
when .multiline? then LibPCRE2::DOTALL | LibPCRE2::MULTILINE
when .extended? then LibPCRE2::EXTENDED
when .anchored? then LibPCRE2::ANCHORED
when .utf_8? then LibPCRE2::UTF
when .no_utf8_check? then LibPCRE2::NO_UTF_CHECK
when .dupnames? then LibPCRE2::DUPNAMES
when .ucp? then LibPCRE2::UCP
else
raise "unreachable"
end
options &= ~option
end
end
unless options.none?
raise ArgumentError.new("Unknown Regex::Option value: #{options}")
end
flag
end
Expand Down