RESOLVED FIXED 11231
RegExp bug when handling newline characters
https://bugs.webkit.org/show_bug.cgi?id=11231
Summary RegExp bug when handling newline characters
Brad Choate
Reported 2006-10-09 11:38:45 PDT
Test case: var s1 = "\nAbc\n"; var s2 = s1.replace(/(\n)[^\n]+$/, '$1'); if (s1 == s2) alert("expected: s1 == s2"); else alert('s1 != s2: "' + s2.replace(/\n/g,"\\n") + '" != "\\nAbc\\n"'); In FireFox, IE and Perl, s1 == s2. In Safari (even in the latest nightly builds), s2 is stripped of the inner 'Abc' characters. Changing the regex to use * instead of + doesn't change the result either.
Attachments
Test case from Comment #0 (206 bytes, text/html)
2007-11-04 07:45 PST, David Kilzer (:ddkilzer)
no flags
patch for this and a few other problems (76.72 KB, patch)
2007-11-12 16:26 PST, Darin Adler
ggaren: review+
David Kilzer (:ddkilzer)
Comment 1 2007-11-04 07:45:44 PST
Created attachment 17031 [details] Test case from Comment #0
Darin Adler
Comment 2 2007-11-12 00:48:26 PST
What's broken here is the $ operator. It's matching a newline at the end of the string. I have a fix.
Darin Adler
Comment 3 2007-11-12 16:26:28 PST
Created attachment 17216 [details] patch for this and a few other problems
Geoffrey Garen
Comment 4 2007-11-12 23:22:38 PST
Comment on attachment 17216 [details] patch for this and a few other problems r=me
Darin Adler
Comment 5 2007-11-13 09:25:57 PST
Committed revision 27752.
Eric Seidel (no email)
Comment 6 2007-11-13 10:12:45 PST
I take it this was no change for sunspider?
Darin Adler
Comment 7 2007-11-13 10:34:21 PST
(In reply to comment #6) > I take it this was no change for sunspider? I'm not sure. I'll test now.
Darin Adler
Comment 8 2007-11-13 10:44:29 PST
SunSpider claims this slowed things down almost 1%. But what is so crazy is that the effects were mostly on the tests that don't use regular expressions at all! For example, it says partial-sums is 2% slower. But there's not a single call into the regular expression for partial-sums. Similarly, it claims that string-base64 is 2% slower. Also doesn't use regular expressions at all. Claims that string-fasta is 2.6% slower. Also doesn't use regular expressions at all. It says regexp-dna is 0.6% faster. That one uses regular expressions. This patch is entirely about correctness, not performance, and it should make regular expressions slightly *faster*. I have no idea why SunSpider thinks it makes non-regular-expression tests slower. We may need to fix SunSpider.
Note You need to log in before you can comment on or make changes to this bug.