develooper Front page | perl.perl5.porters | Postings from May 2003

[PATCH] Extra newline in stringified qr/\#/x

Thread Next
From:
Alex Vandiver
Date:
May 30, 2003 00:55
Subject:
[PATCH] Extra newline in stringified qr/\#/x
Message ID:
1054281327.1722.24.camel@supox
Heya,
  As discussed at http://perlmonks.org/?node_id=261206 there is a small
bug with stringification of regexes containing escaped #s with the /x
modifier.  The patch below fixes it; thanks to BrowserUK for the
impetus.
  As an aside, I couldn't make out much order to t/op/pat.t -- is there
a better place to put the regression test than where I put it?
 - Alex

diff -ruN perl-current/sv.c perl-patched/sv.c
--- perl-current/sv.c	Mon May 12 19:03:25 2003
+++ perl-patched/sv.c	Wed May 28 02:13:36 2003
@@ -3029,11 +3029,18 @@
                                     if (c == '\n')
                                         break; /* don't need another */
                                     if (c == '#') {
-                                        /* we end while in a comment, so we
-                                           need a newline */
-                                        mg->mg_len++; /* save space for it */
-                                        need_newline = 1; /* note to add it */
-					break;
+					int n =0;
+					while( endptr >= re->precomp && 
+					       (c = *(endptr--)) && 
+					       c == '\\' )  n++;
+					/* We end in a comment, we've an even number of
+					   backslashes (thus the # is not escaped), so
+					   we need the newline */
+					if ( (n & 1) == 0) {
+					    mg->mg_len++; /* save space for it */
+					    need_newline = 1; /* note to add it */
+					    break;
+					}
                                     }
                                 }
                             }
diff -ruN perl-current/t/op/pat.t perl-patched/t/op/pat.t
--- perl-current/t/op/pat.t	Fri Apr 18 16:38:30 2003
+++ perl-patched/t/op/pat.t	Wed May 28 03:05:55 2003
@@ -6,7 +6,7 @@
 
 $| = 1;
 
-print "1..998\n";
+print "1..999\n";
 
 BEGIN {
     chdir 't' if -d 't';
@@ -2935,6 +2935,10 @@
       "ok $test - ## in qr// doesn't corrupt memory [perl #17776]\n");
 ++$test;
 
+print(("(?x-ism:\\#)" eq qr/\#/x ? '' : 'not '),
+      "ok $test - Escaped # in qr doesn't insert newline when stringified\n");
+++$test;
+
 { use re 'eval';
 
 print(("$x$x" =~ /^$x(??{$x})\z/ ? '' : 'not '),
diff -ruN perl-current/patchlevel.h perl-patched/patchlevel.h
--- perl-current/patchlevel.h	Wed Apr 16 17:25:36 2003
+++ perl-patched/patchlevel.h	Wed May 28 03:10:15 2003
@@ -122,0 +122,1 @@
+	,"PERLMONKS261206 - Extra newline in stringified qr with escaped #"



-- 
Networking -- only one letter away from not working.


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About