diff --git a/lib/Regexp/Parser/Handlers.pm b/lib/Regexp/Parser/Handlers.pm index e700f73..0380672 100644 --- a/lib/Regexp/Parser/Handlers.pm +++ b/lib/Regexp/Parser/Handlers.pm @@ -637,6 +637,10 @@ sub init { if ($S->can($posix)) { $$ret = $S->$posix($neg, $how) } else { $S->error($S->RPe_BADPOS, "$how$neg$name$how") } } + elsif (&Rf & 0x200 and ${&Rx} =~ m{ \G [ \t]+ }xgc) { + # /xx: skip unescaped spaces and tabs inside character classes (Perl 5.26+) + redo; + } elsif (${&Rx} =~ m{ \G (.) }xgcs) { $$ret = $S->force_object(anyof_char => $1); } diff --git a/t/08regex_flags.t b/t/08regex_flags.t index ee9c779..523343e 100644 --- a/t/08regex_flags.t +++ b/t/08regex_flags.t @@ -1,4 +1,4 @@ -use Test::More tests => 18; +use Test::More tests => 30; use Regexp::Parser; ok(1); @@ -46,6 +46,30 @@ $w = $r->walker; ($n) = $w->(); is( $n->type, 'mbol', '/m makes ^ match \\n boundaries' ); +# Test 8: /xx flag strips spaces and tabs inside character classes (Perl 5.26+) +ok( $r->regex('[a b c]', 'xx') ); +is( $r->visual, '[abc]', 'xx flag strips spaces in char class' ); + +# Test 9: /xx flag does not strip newlines inside character classes +ok( $r->regex("[a\nb]", 'xx') ); +is( $r->visual, "[a\nb]", 'xx flag preserves newlines in char class' ); + +# Test 10: /xx flag strips tabs inside character classes +ok( $r->regex("[a\tb]", 'xx') ); +is( $r->visual, '[ab]', 'xx flag strips tabs in char class' ); + +# Test 11: /x flag does NOT strip spaces inside character classes +ok( $r->regex('[a b]', 'x') ); +is( $r->visual, '[a b]', 'x flag preserves spaces in char class' ); + +# Test 12: inline (?xx:) strips spaces inside character classes +ok( $r->regex('(?xx:[a b c])') ); +is( $r->visual, '(?xx:[abc])', 'inline (?xx:) strips spaces in char class' ); + +# Test 13: /xx escaping -- backslash-space is kept +ok( $r->regex('[a\\ b]', 'xx') ); +is( $r->visual, '[a\ b]', 'xx flag preserves escaped space in char class' ); + __DATA__ 0 exact exactf abc ---