search.js (2106B)
1 function toLatin1(s) { 2 assertEq(isLatin1(s), true); 3 return s; 4 } 5 function testSearchFlat() { 6 var s1 = toLatin1("fooBar12345"); 7 var s2 = toLatin1("Bar1"); 8 9 // Latin1 + Latin1 10 assertEq(s1.search(s2), 3); 11 assertEq(s2.search(s1), -1); 12 assertEq(s1.search(s1), 0); 13 14 // Latin1 + TwoByte 15 assertEq(s1.search(s2 + "\u1200"), -1); 16 assertEq(s1.search(("12345\u1200").slice(0, -1)), 6); 17 18 // TwoByte + Latin1 19 assertEq("fooBar12345\u1200".search(s1), 0); 20 assertEq("fooBar12345\u1200".search(s2), 3); 21 22 // TwoByte + TwoByte 23 assertEq("fooBar12345\u1200".search("5\u1200"), 10); 24 assertEq("fooBar12345\u1200".search("5\u1201"), -1); 25 } 26 testSearchFlat(); 27 28 function testSearchRope() { 29 // Tests for the RopeMatch algorithm. 30 var s1 = "foobarbaz0123456789".repeat(10); 31 s1.indexOf("333"); // flatten 32 s1 = toLatin1(s1); 33 34 var ropeMixed = s1 + "abcdef\u1200"; 35 assertEq(isLatin1(ropeMixed), false); 36 37 var abc = toLatin1("abc"); 38 var baz = toLatin1("baz"); 39 40 // Mixed + Latin1 41 assertEq(ropeMixed.search(abc), 190); 42 assertEq(ropeMixed.search(baz), 6); 43 44 // Mixed + TwoByte 45 assertEq(ropeMixed.search("def\u1200"), 193); 46 47 // Latin1 + Latin1 48 s1 = "foobarbaz0123456789".repeat(10); 49 s1.indexOf("333"); // flatten 50 s1 = toLatin1(s1); 51 var ropeLatin1 = s1 + toLatin1("abcdef\u00AA"); 52 assertEq(isLatin1(ropeLatin1), true); 53 assertEq(ropeLatin1.search(abc), 190); 54 55 // Latin1 + TwoByte 56 assertEq(ropeLatin1.search("\u1200bc".substr(1)), 191); 57 58 // TwoByte + Latin1 59 s1 = "foobarbaz0123456789\u11AA".repeat(10); 60 var ropeTwoByte = s1 + "abcdef\u1200"; 61 assertEq(ropeTwoByte.search(abc), 200); 62 63 // TwoByte + TwoByte 64 assertEq(ropeTwoByte.search("def\u1200"), 203); 65 } 66 testSearchRope(); 67 68 function testSearchStringMatch() { 69 var re = /bar/; 70 71 // Latin1 72 assertEq(toLatin1("foobar1234").search(re), 3); 73 assertEq(toLatin1("foo1234").search(re), -1); 74 75 // TwoByte 76 assertEq("\u1200bar".search(re), 1); 77 assertEq("\u12001234".search(re), -1); 78 } 79 testSearchStringMatch();