Commit 31459a5c authored by ddkilzer's avatar ddkilzer

JavaScriptCore:

        Reviewed by ggaren.

        - http://bugzilla.opendarwin.org/show_bug.cgi?id=9234
          Implement $&, $' and $` replacement codes in String.prototype.replace

        Test: fast/js/string-replace-3.html

        * kjs/string_object.cpp:
        (substituteBackreferences): Added support for $& (matched substring),
        $` (everything preceding matched substring), $' (everything following
        matched substring) and 2-digit back references, and cleaned up a little.

LayoutTests:

        Reviewed by ggaren.

        - test for http://bugzilla.opendarwin.org/show_bug.cgi?id=9234
          Implement $&, $' and $` replacement codes in String.prototype.replace

        * fast/js/resources/string-replace-3.js: Added.
        * fast/js/string-replace-3-expected.txt: Added.
        * fast/js/string-replace-3.html: Added.



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@14705 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 7fd972a6
2006-06-02 Mitz Pettel <opendarwin.org@mitzpettel.com>
Reviewed by ggaren.
- http://bugzilla.opendarwin.org/show_bug.cgi?id=9234
Implement $&, $' and $` replacement codes in String.prototype.replace
Test: fast/js/string-replace-3.html
* kjs/string_object.cpp:
(substituteBackreferences): Added support for $& (matched substring),
$` (everything preceding matched substring), $' (everything following
matched substring) and 2-digit back references, and cleaned up a little.
2006-06-02 Adele Peterson <adele@apple.com>
Reviewed by Darin.
......
......@@ -232,23 +232,49 @@ static inline UString substituteBackreferences(const UString &replacement, const
{
UString substitutedReplacement = replacement;
bool converted;
int i = -1;
while ((i = substitutedReplacement.find(UString("$"), i + 1)) != -1) {
if (i+1 == substitutedReplacement.size())
break;
for (int i = 0; (i = substitutedReplacement.find(UString("$"), i)) != -1; i++) {
if (i+1 < substitutedReplacement.size() && substitutedReplacement[i+1] == '$') { // "$$" -> "$"
substitutedReplacement = substitutedReplacement.substr(0,i) + "$" + substitutedReplacement.substr(i+2);
continue;
}
// Assume number part is one char exactly
unsigned backrefIndex = substitutedReplacement.substr(i+1,1).toUInt32(&converted, false /* tolerate empty string */);
if (converted && backrefIndex <= (unsigned)reg->subPatterns()) {
int backrefStart = ovector[2*backrefIndex];
int backrefLength = ovector[2*backrefIndex+1] - backrefStart;
substitutedReplacement = substitutedReplacement.substr(0,i)
+ source.substr(backrefStart, backrefLength)
+ substitutedReplacement.substr(i+2);
i += backrefLength - 1; // -1 offsets i++
}
unsigned short ref = substitutedReplacement[i+1].unicode();
int backrefStart = 0;
int backrefLength = 0;
int advance = 0;
if (ref == '$') { // "$$" -> "$"
substitutedReplacement = substitutedReplacement.substr(0, i + 1) + substitutedReplacement.substr(i + 2);
continue;
} else if (ref == '&') {
backrefStart = ovector[0];
backrefLength = ovector[1] - backrefStart;
} else if (ref == '`') {
backrefStart = 0;
backrefLength = ovector[0];
} else if (ref == '\'') {
backrefStart = ovector[1];
backrefLength = source.size() - backrefStart;
} else if (ref >= '0' && ref <= '9') {
// 1- and 2-digit back references are allowed
unsigned backrefIndex = ref - '0';
if (substitutedReplacement.size() > i + 2) {
ref = substitutedReplacement[i+2].unicode();
if (ref >= '0' && ref <= '9') {
backrefIndex = 10 * backrefIndex + ref - '0';
advance = 1;
}
}
if (backrefIndex > (unsigned)reg->subPatterns()) {
i += advance;
continue;
}
backrefStart = ovector[2 * backrefIndex];
backrefLength = ovector[2 * backrefIndex + 1] - backrefStart;
} else
continue;
substitutedReplacement = substitutedReplacement.substr(0, i) + source.substr(backrefStart, backrefLength) + substitutedReplacement.substr(i + 2 + advance);
i += backrefLength - 1; // - 1 offsets 'i + 1'
}
return substitutedReplacement;
......
2006-06-02 Mitz Pettel <opendarwin.org@mitzpettel.com>
Reviewed by ggaren.
- test for http://bugzilla.opendarwin.org/show_bug.cgi?id=9234
Implement $&, $' and $` replacement codes in String.prototype.replace
* fast/js/resources/string-replace-3.js: Added.
* fast/js/string-replace-3-expected.txt: Added.
* fast/js/string-replace-3.html: Added.
2006-06-02 David Carson <dacarson@gmail.com>
Reviewed by Hyatt, checked in by Tim H.
description(
"String.replace(&hellip;) test &mdash; $&amp;, $`, $' and $nn"
);
var testString = "It's the end of the world as we know it, and I feel fine.";
shouldBe("testString",
"\"It's the end of the world as we know it, and I feel fine.\"");
shouldBe("testString.replace(/I feel/, 'yet $& just')",
"\"It's the end of the world as we know it, and yet I feel just fine.\"");
shouldBe("testString.replace(/the end/, 'nice. $`a picture')",
"\"It's nice. It's a picture of the world as we know it, and I feel fine.\"");
shouldBe('testString.replace(/feel fin/, "am gon$\' By")',
"\"It's the end of the world as we know it, and I am gone. Bye.\"");
shouldBe("testString.replace(/(t)(h)(e e)(n)(d)( o)(f)( )(t)(h)(e )([^r]*)(rld)/, 'not $12olly mammoth')",
"\"It's not woolly mammoth as we know it, and I feel fine.\"");
var successfullyParsed = true;
String.replace(…) test — $&, $`, $' and $nn
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
PASS testString is "It's the end of the world as we know it, and I feel fine."
PASS testString.replace(/I feel/, 'yet $& just') is "It's the end of the world as we know it, and yet I feel just fine."
PASS testString.replace(/the end/, 'nice. $`a picture') is "It's nice. It's a picture of the world as we know it, and I feel fine."
PASS testString.replace(/feel fin/, "am gon$' By") is "It's the end of the world as we know it, and I am gone. Bye."
PASS testString.replace(/(t)(h)(e e)(n)(d)( o)(f)( )(t)(h)(e )([^r]*)(rld)/, 'not $12olly mammoth') is "It's not woolly mammoth as we know it, and I feel fine."
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<link rel="stylesheet" href="resources/js-test-style.css">
<script src="resources/js-test-pre.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script src="resources/string-replace-3.js"></script>
<script src="resources/js-test-post.js"></script>
</body>
</html>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment