indexOf.js (5154B)
1 function toLatin1(s) { 2 assertEq(isLatin1(s), true); 3 return s; 4 } 5 function testLastIndexOf() { 6 var s1 = toLatin1("abcdefgh123456\u0081defg"); 7 var s2 = toLatin1("456\u0081de"); 8 9 // Latin1 + Latin1 10 assertEq(s1.lastIndexOf(s1), 0); 11 assertEq(s1.lastIndexOf(s2), 11); 12 assertEq(s1.lastIndexOf(s2, 10), -1); 13 assertEq(s2.lastIndexOf(s1), -1); 14 15 // Latin1 + TwoByte 16 assertEq(s1.lastIndexOf("abc\u1234"), -1); 17 assertEq(s1.lastIndexOf("def\u1234".substring(0, 3)), 15); 18 assertEq(s1.lastIndexOf("def\u1234".substring(0, 3), 9), 3); 19 20 // TwoByte + Latin1 21 var s3 = "123456\u0081defg\u1123a456\u0081defg"; 22 assertEq(isLatin1(s2), true); 23 assertEq(s3.lastIndexOf(s2), 13); 24 assertEq(s3.lastIndexOf(s2, 12), 3); 25 assertEq(s3.lastIndexOf(toLatin1("defg7")), -1); 26 27 // TwoByte + TwoByte 28 assertEq(s3.lastIndexOf("\u1123a4"), 11); 29 assertEq(s3.lastIndexOf("\u1123a4", 10), -1); 30 assertEq(s3.lastIndexOf("\u1123a\u1098"), -1); 31 assertEq(s3.lastIndexOf(s3), 0); 32 } 33 testLastIndexOf(); 34 35 function testIndexOf() { 36 var s1 = toLatin1("abcdefgh123456d\u00AAefghi"); 37 var s2 = toLatin1("456d\u00AA"); 38 39 // Latin1 + Latin1 40 assertEq(s1.indexOf(s1), 0); 41 assertEq(s1.indexOf(s2), 11); 42 assertEq(s1.indexOf(s2, 12), -1); 43 assertEq(s2.indexOf(s1), -1); 44 45 // Latin1 + TwoByte 46 assertEq(s1.indexOf("abc\u1234"), -1); 47 assertEq(s1.indexOf("def\u1234".substring(0, 3)), 3); 48 assertEq(s1.indexOf("d\u00AAef\u1234".substring(0, 3), 9), 14); 49 50 // TwoByte + Latin1 51 var s3 = "123456d\u00AAefg\u1123a456d\u00AAefg"; 52 assertEq(isLatin1(s2), true); 53 assertEq(s3.indexOf(s2), 3); 54 assertEq(s3.indexOf(s2, 11), 13); 55 assertEq(s3.indexOf(toLatin1("d\u00AAefg7")), -1); 56 57 // TwoByte + TwoByte 58 assertEq(s3.indexOf("\u1123a4"), 11); 59 assertEq(s3.indexOf("\u1123a4", 12), -1); 60 assertEq(s3.indexOf("\u1123a\u1098"), -1); 61 assertEq(s3.indexOf(s3), 0); 62 } 63 testIndexOf(); 64 65 function testincludes() { 66 var s1 = toLatin1("abcdefgh123456defghi\u00EEj"); 67 var s2 = toLatin1("456defghi\u00EE"); 68 69 // Latin1 + Latin1 70 assertEq(s1.includes(s1), true); 71 assertEq(s1.includes(s2), true); 72 assertEq(s1.includes(s2, 12), false); 73 assertEq(s2.includes(s1), false); 74 75 // Latin1 + TwoByte 76 assertEq(s1.includes("abc\u1234"), false); 77 assertEq(s1.includes("def\u1234".substring(0, 3)), true); 78 assertEq(s1.includes("def\u1234".substring(0, 3), 9), true); 79 80 // TwoByte + Latin1 81 var s3 = "123456defg\u1123a456defghi\u00EEj"; 82 assertEq(isLatin1(s2), true); 83 assertEq(s3.includes(s2), true); 84 assertEq(s3.includes(s2, 13), false); 85 assertEq(s3.includes(toLatin1("defg8")), false); 86 87 // TwoByte + TwoByte 88 assertEq(s3.includes("\u1123a4"), true); 89 assertEq(s3.includes("\u1123a4", 11), false); 90 assertEq(s3.includes("\u1123a\u1098"), false); 91 assertEq(s3.includes(s3), true); 92 } 93 testincludes(); 94 95 function testIndexOfBMH() { 96 // BoyerMooreHorspool algorithm is used for large strings. 97 var s = "012345678901234567890123456789".repeat(20); 98 var text = s + "abcdefghijklmnopqrst\u00C1uvwxyz"; 99 text.indexOf("333"); 100 101 var textL1 = toLatin1(text); 102 var patL1 = toLatin1("cdefghijklmnopqrst\u00C1uvwx"); 103 104 // Latin1 + Latin1 105 assertEq(textL1.indexOf(patL1), 602); 106 assertEq(textL1.indexOf(patL1, 603), -1); 107 assertEq(textL1.indexOf(textL1), 0); 108 109 // Latin1 + TwoByte 110 assertEq(textL1.indexOf("cdefghijklmnopqrst\u00C1uvwxy"), 602); 111 assertEq(textL1.indexOf("cdefghijklmnopqrst\u00C1uvwxy", 603), -1); 112 assertEq(textL1.indexOf("cdefghijklmnopqrst\u00C1uvwxy\uaa00", -1), -1); 113 114 // TwoByte + Latin1 115 var textTB = s + "abcdefghijklmnopqrst\u00C1uvwxyz\u1200"; 116 text.indexOf("333"); 117 assertEq(textTB.indexOf(patL1), 602); 118 assertEq(textTB.indexOf(patL1, 603), -1); 119 120 // TwoByte + TwoByte 121 assertEq(textTB.indexOf("defghijklmnopqrst\u00C1uvwxyz\u1200"), 603); 122 assertEq(textTB.indexOf("defghijklmnopqrst\u00C1uvwxyz\u1200", 604), -1); 123 assertEq(textTB.indexOf("defghijklmnopqrst\u00C1uvwxyz\u1201"), -1); 124 } 125 testIndexOfBMH(); 126 127 function testIndexOfLargePattern() { 128 // If the pattern length > 128, memcmp is used (text length has to be < 512 129 // or we'll use BoyerMooreHorspool). This is only valid if both 130 // strings have the same encoding. 131 var text = "012345678901234567890123456789".repeat(10) + "abcdefghijklmnopqrst\u00C1uvwxyz"; 132 text.indexOf("333"); // flatten 133 var pat = "012345678901234567890123456789".repeat(5) + "abcdefghijklmnopqr"; 134 pat.indexOf("333"); // flatten 135 136 // Latin1 + Latin1 137 text = toLatin1(text); 138 pat = toLatin1(pat); 139 assertEq(text.indexOf(pat), 150); 140 141 // Latin1 + TwoByte 142 assertEq(text.indexOf(pat + "\u1200"), -1); 143 assertEq(text.indexOf((pat + "\u1200").slice(0, -1)), 150); 144 145 // TwoByte + Latin1 146 text = text + "\u1100"; 147 assertEq(isLatin1(pat), true); 148 assertEq(text.indexOf(pat), 150); 149 150 // TwoByte + TwoByte 151 pat = pat + "st\u00C1uvwxyz\u1100"; 152 assertEq(text.indexOf(pat), 150); 153 assertEq(text.indexOf(pat + "\u2000"), -1); 154 } 155 testIndexOfLargePattern();