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 Regex::CompileOptions::MULTILINE_ONLY #14870

Merged
merged 15 commits into from
Oct 10, 2024
7 changes: 7 additions & 0 deletions spec/std/regex_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
end

it "unnamed option" do
{% if Regex::Engine.resolve.name == "Regex::PCRE" %}

Check failure on line 19 in spec/std/regex_spec.cr

View workflow job for this annotation

GitHub Actions / LLVM 13.0.0

Expected ArgumentError but nothing was raised

Check failure on line 19 in spec/std/regex_spec.cr

View workflow job for this annotation

GitHub Actions / LLVM 14.0.0

Expected ArgumentError but nothing was raised

Check failure on line 19 in spec/std/regex_spec.cr

View workflow job for this annotation

GitHub Actions / LLVM 15.0.6

Expected ArgumentError but nothing was raised

Check failure on line 19 in spec/std/regex_spec.cr

View workflow job for this annotation

GitHub Actions / LLVM 16.0.3

Expected ArgumentError but nothing was raised

Check failure on line 19 in spec/std/regex_spec.cr

View workflow job for this annotation

GitHub Actions / LLVM 17.0.6

Expected ArgumentError but nothing was raised

Check failure on line 19 in spec/std/regex_spec.cr

View workflow job for this annotation

GitHub Actions / LLVM 18.1.4

Expected ArgumentError but nothing was raised
Regex.new("^/foo$", Regex::CompileOptions.new(0x00000020)).matches?("/foo\n").should be_false
{% else %}
expect_raises ArgumentError, "Unknown Regex::Option value: 64" do
Expand Down Expand Up @@ -250,6 +250,13 @@
end
end

describe "multiline_only" do
it "anchor" do
((/^foo.*$/m).match("foo\nbar")).try(&.[](0)).should eq "foo\nbar"
((Regex.new("^foo.*?", Regex::Options::MULTILINE_ONLY)).match("foo\nbar")).try(&.[](0)).should eq "foo"
end
end

describe "extended" do
it "ignores white space" do
/foo bar/.matches?("foobar").should be_false
Expand Down
5 changes: 5 additions & 0 deletions src/regex.cr
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,17 @@ class Regex
# flag that activates both behaviours, so here we do the same by
# mapping `MULTILINE` to `PCRE_MULTILINE | PCRE_DOTALL`.
# The same applies for PCRE2 except that the native values are 0x200 and 0x400.
#
# For the behaviour of `PCRE_MULTILINE` use `MULTILINE_ONLY`.

# Multiline matching.
#
# Equivalent to `MULTILINE | DOTALL` in PCRE and PCRE2.
MULTILINE = 0x0000_0006

# Equivalent to `MULTILINE` in PCRE and PCRE2.
ralsina marked this conversation as resolved.
Show resolved Hide resolved
MULTILINE_ONLY = 0x0000_0040
ralsina marked this conversation as resolved.
Show resolved Hide resolved

DOTALL = 0x0000_0002

# Ignore white space and `#` comments.
Expand Down
3 changes: 2 additions & 1 deletion src/regex/pcre.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ module Regex::PCRE
if options.includes?(option)
flag |= case option
when .ignore_case? then LibPCRE::CASELESS
when .multiline? then LibPCRE::DOTALL | LibPCRE::MULTILINE
when .multiline? then LibPCRE::MULTILINE | LibPCRE::DOTALL
when .multiline_only? then LibPCRE::MULTILINE
when .dotall? then LibPCRE::DOTALL
when .extended? then LibPCRE::EXTENDED
when .anchored? then LibPCRE::ANCHORED
Expand Down
3 changes: 2 additions & 1 deletion src/regex/pcre2.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ module Regex::PCRE2
if options.includes?(option)
flag |= case option
when .ignore_case? then LibPCRE2::CASELESS
when .multiline? then LibPCRE2::DOTALL | LibPCRE2::MULTILINE
when .multiline? then LibPCRE2::MULTILINE | LibPCRE2::DOTALL
when .multiline_only? then LibPCRE2::MULTILINE
when .dotall? then LibPCRE2::DOTALL
when .extended? then LibPCRE2::EXTENDED
when .anchored? then LibPCRE2::ANCHORED
Expand Down
Loading