Bug 11231

Summary: RegExp bug when handling newline characters
Product: WebKit Reporter: Brad Choate <bchoate+webkit>
Component: JavaScriptCoreAssignee: Darin Adler <darin>
Status: RESOLVED FIXED    
Severity: Normal CC: ap, darin
Priority: P2    
Version: 420+   
Hardware: Mac   
OS: OS X 10.4   
Attachments:
Description Flags
Test case from Comment #0
none
patch for this and a few other problems ggaren: review+

Description Brad Choate 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.
Comment 1 David Kilzer (:ddkilzer) 2007-11-04 07:45:44 PST
Created attachment 17031 [details]
Test case from Comment #0
Comment 2 Darin Adler 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.
Comment 3 Darin Adler 2007-11-12 16:26:28 PST
Created attachment 17216 [details]
patch for this and a few other problems
Comment 4 Geoffrey Garen 2007-11-12 23:22:38 PST
Comment on attachment 17216 [details]
patch for this and a few other problems

r=me
Comment 5 Darin Adler 2007-11-13 09:25:57 PST
Committed revision 27752.
Comment 6 Eric Seidel (no email) 2007-11-13 10:12:45 PST
I take it this was no change for sunspider?
Comment 7 Darin Adler 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.
Comment 8 Darin Adler 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.