| 1 |
function toLatin1(s) { |
| 2 |
assertEq(isLatin1(s), true); |
| 3 |
return s; |
| 4 |
} |
| 5 |
function testDollarReplacement() { |
| 6 |
// Latin1 input, pat and replacement |
| 7 |
var s = toLatin1("Foobarbaz123"); |
| 8 |
var pat = toLatin1("bar"); |
| 9 |
assertEq(s.replace(pat, toLatin1("AA")), "FooAAbaz123"); |
| 10 |
assertEq(s.replace(pat, toLatin1("A$$A")), "FooA$Abaz123"); |
| 11 |
assertEq(s.replace(pat, toLatin1("A$`A")), "FooAFooAbaz123"); |
| 12 |
assertEq(s.replace(pat, toLatin1("A$&A")), "FooAbarAbaz123"); |
| 13 |
assertEq(s.replace(pat, toLatin1("A$'A")), "FooAbaz123Abaz123"); |
| 14 |
assertEq(isLatin1(s.replace(pat, "A$'A")), true); |
| 15 |
|
| 16 |
// Latin1 input and pat, TwoByte replacement |
| 17 |
assertEq(s.replace(pat, "A\u1200"), "FooA\u1200baz123"); |
| 18 |
assertEq(s.replace(pat, "A$$\u1200"), "FooA$\u1200baz123"); |
| 19 |
assertEq(s.replace(pat, "A$`\u1200"), "FooAFoo\u1200baz123"); |
| 20 |
assertEq(s.replace(pat, "A$&\u1200"), "FooAbar\u1200baz123"); |
| 21 |
assertEq(s.replace(pat, "A$'\u1200"), "FooAbaz123\u1200baz123"); |
| 22 |
|
| 23 |
// TwoByte input, Latin1 pat and replacement |
| 24 |
s = "Foobarbaz123\u1200"; |
| 25 |
assertEq(s.replace(pat, toLatin1("A")), "FooAbaz123\u1200"); |
| 26 |
assertEq(s.replace(pat, toLatin1("A$$")), "FooA$baz123\u1200"); |
| 27 |
assertEq(s.replace(pat, toLatin1("A$`")), "FooAFoobaz123\u1200"); |
| 28 |
assertEq(s.replace(pat, toLatin1("A$&")), "FooAbarbaz123\u1200"); |
| 29 |
assertEq(s.replace(pat, toLatin1("A$'")), "FooAbaz123\u1200baz123\u1200"); |
| 30 |
|
| 31 |
// TwoByte input and pat, Latin1 replacement |
| 32 |
s = "Foobar\u1200baz123"; |
| 33 |
pat += "\u1200"; |
| 34 |
assertEq(s.replace(pat, toLatin1("AB")), "FooABbaz123"); |
| 35 |
assertEq(s.replace(pat, toLatin1("A$$B")), "FooA$Bbaz123"); |
| 36 |
assertEq(s.replace(pat, toLatin1("A$`B")), "FooAFooBbaz123"); |
| 37 |
assertEq(s.replace(pat, toLatin1("A$&B")), "FooAbar\u1200Bbaz123"); |
| 38 |
assertEq(s.replace(pat, toLatin1("A$'B")), "FooAbaz123Bbaz123"); |
| 39 |
|
| 40 |
// TwoByte input, pat and replacement |
| 41 |
assertEq(s.replace(pat, "A\u1300"), "FooA\u1300baz123"); |
| 42 |
assertEq(s.replace(pat, "A$$\u1300"), "FooA$\u1300baz123"); |
| 43 |
assertEq(s.replace(pat, "A$`\u1300"), "FooAFoo\u1300baz123"); |
| 44 |
assertEq(s.replace(pat, "A$&\u1300"), "FooAbar\u1200\u1300baz123"); |
| 45 |
assertEq(s.replace(pat, "A$'\u1300"), "FooAbaz123\u1300baz123"); |
| 46 |
} |
| 47 |
testDollarReplacement(); |
| 48 |
|
| 49 |
function testRegExp() { |
| 50 |
var s = toLatin1("Foobar123bar234"); |
| 51 |
var res = s.replace(/bar\d\d/, "456"); |
| 52 |
assertEq(res, "Foo4563bar234"); |
| 53 |
assertEq(isLatin1(res), true); |
| 54 |
|
| 55 |
// Latin1 input and replacement |
| 56 |
var re1 = /bar\d\d/; |
| 57 |
res = s.replace(re1, toLatin1("789")); |
| 58 |
assertEq(res, "Foo7893bar234"); |
| 59 |
assertEq(isLatin1(res), true); |
| 60 |
|
| 61 |
var re2 = /bar\d\d/g; |
| 62 |
res = s.replace(re2, toLatin1("789\u00ff")); |
| 63 |
assertEq(res, "Foo789\u00ff3789\u00ff4"); |
| 64 |
assertEq(isLatin1(res), true); |
| 65 |
|
| 66 |
// Latin1 input, TwoByte replacement |
| 67 |
assertEq(s.replace(re1, "789\u1200"), "Foo789\u12003bar234"); |
| 68 |
assertEq(s.replace(re2, "789\u1200"), "Foo789\u12003789\u12004"); |
| 69 |
|
| 70 |
// TwoByte input, Latin1 replacement |
| 71 |
s += "\u1200"; |
| 72 |
assertEq(s.replace(re1, toLatin1("7890")), "Foo78903bar234\u1200"); |
| 73 |
assertEq(s.replace(re2, toLatin1("7890\u00ff")), "Foo7890\u00ff37890\u00ff4\u1200"); |
| 74 |
|
| 75 |
// TwoByte input and replacement |
| 76 |
assertEq(s.replace(re1, "789\u1200"), "Foo789\u12003bar234\u1200"); |
| 77 |
assertEq(s.replace(re2, "789\u1200"), "Foo789\u12003789\u12004\u1200"); |
| 78 |
} |
| 79 |
testRegExp(); |
| 80 |
|
| 81 |
function testRegExpDollar() { |
| 82 |
var s = toLatin1("Foobar123bar2345"); |
| 83 |
|
| 84 |
// Latin1 input and replacement |
| 85 |
var re1 = /bar\d\d/; |
| 86 |
var re2 = /bar(\d\d)/g; |
| 87 |
assertEq(s.replace(re1, toLatin1("--$&--")), "Foo--bar12--3bar2345"); |
| 88 |
assertEq(s.replace(re2, toLatin1("--$'\u00ff--")), "Foo--3bar2345\xFF--3--45\xFF--45"); |
| 89 |
assertEq(s.replace(re2, toLatin1("--$`--")), "Foo--Foo--3--Foobar123--45"); |
| 90 |
assertEq(isLatin1(s.replace(re2, toLatin1("--$`--"))), true); |
| 91 |
|
| 92 |
// Latin1 input, TwoByte replacement |
| 93 |
assertEq(s.replace(re1, "\u1200$$"), "Foo\u1200$3bar2345"); |
| 94 |
assertEq(s.replace(re2, "\u1200$1"), "Foo\u1200123\u12002345"); |
| 95 |
assertEq(s.replace(re2, "\u1200$'"), "Foo\u12003bar23453\u12004545"); |
| 96 |
|
| 97 |
// TwoByte input, Latin1 replacement |
| 98 |
s += "\u1200"; |
| 99 |
assertEq(s.replace(re1, toLatin1("**$&**")), "Foo**bar12**3bar2345\u1200"); |
| 100 |
assertEq(s.replace(re2, toLatin1("**$1**")), "Foo**12**3**23**45\u1200"); |
| 101 |
assertEq(s.replace(re2, toLatin1("**$`**")), "Foo**Foo**3**Foobar123**45\u1200"); |
| 102 |
assertEq(s.replace(re2, toLatin1("**$'$$**")), "Foo**3bar2345\u1200$**3**45\u1200$**45\u1200"); |
| 103 |
|
| 104 |
// TwoByte input and replacement |
| 105 |
assertEq(s.replace(re1, "**$&**\ueeee"), "Foo**bar12**\ueeee3bar2345\u1200"); |
| 106 |
assertEq(s.replace(re2, "**$1**\ueeee"), "Foo**12**\ueeee3**23**\ueeee45\u1200"); |
| 107 |
assertEq(s.replace(re2, "\ueeee**$`**"), "Foo\ueeee**Foo**3\ueeee**Foobar123**45\u1200"); |
| 108 |
assertEq(s.replace(re2, "\ueeee**$'$$**"), "Foo\ueeee**3bar2345\u1200$**3\ueeee**45\u1200$**45\u1200"); |
| 109 |
} |
| 110 |
testRegExpDollar(); |
| 111 |
|
| 112 |
function testFlattenPattern() { |
| 113 |
var s = "abcdef[g]abc"; |
| 114 |
|
| 115 |
// Latin1 pattern |
| 116 |
var res = s.replace(toLatin1("def[g]"), "--$&--", "gi"); |
| 117 |
assertEq(res, "abc--def[g]--abc"); |
| 118 |
assertEq(isLatin1(res), true); |
| 119 |
|
| 120 |
// TwoByte pattern |
| 121 |
s = "abcdef[g]\u1200abc"; |
| 122 |
assertEq(s.replace("def[g]\u1200", "++$&++", "gi"), "abc++def[g]\u1200++abc"); |
| 123 |
} |
| 124 |
testFlattenPattern(); |
| 125 |
|
| 126 |
function testReplaceEmpty() { |
| 127 |
// Latin1 |
| 128 |
var s = toLatin1("--abcdefghijkl--abcdefghijkl--abcdefghijkl--abcdefghijkl"); |
| 129 |
var res = s.replace(/abcd[eE]/g, ""); |
| 130 |
assertEq(res, "--fghijkl--fghijkl--fghijkl--fghijkl"); |
| 131 |
assertEq(isLatin1(res), true); |
| 132 |
|
| 133 |
s = "--abcdEf--"; |
| 134 |
res = s.replace(/abcd[eE]/g, ""); |
| 135 |
assertEq(res, "--f--"); |
| 136 |
assertEq(isLatin1(res), true); |
| 137 |
|
| 138 |
// TwoByte |
| 139 |
s = "--abcdefghijkl--abcdefghijkl--abcdefghijkl--abcdefghijkl\u1200"; |
| 140 |
assertEq(s.replace(/abcd[eE]/g, ""), "--fghijkl--fghijkl--fghijkl--fghijkl\u1200"); |
| 141 |
|
| 142 |
s = "--abcdEf--\u1200"; |
| 143 |
assertEq(s.replace(/abcd[eE]/g, ""), "--f--\u1200"); |
| 144 |
} |
| 145 |
testReplaceEmpty(); |