1/* The contents of this file are subject to the Netscape Public 2 * License Version 1.1 (the "License"); you may not use this file 3 * except in compliance with the License. You may obtain a copy of 4 * the License at http://www.mozilla.org/NPL/ 5 * 6 * Software distributed under the License is distributed on an "AS 7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 8 * implied. See the License for the specific language governing 9 * rights and limitations under the License. 10 * 11 * The Original Code is Mozilla Communicator client code, released March 12 * 31, 1998. 13 * 14 * The Initial Developer of the Original Code is Netscape Communications 15 * Corporation. Portions created by Netscape are 16 * Copyright (C) 1998 Netscape Communications Corporation. All 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 */ 22/** 23 File Name: 15.5.4.7-2.js 24 ECMA Section: 15.5.4.7 String.prototype.lastIndexOf( searchString, pos) 25 Description: 26 27 If the given searchString appears as a substring of the result of 28 converting this object to a string, at one or more positions that are 29 at or to the left of the specified position, then the index of the 30 rightmost such position is returned; otherwise -1 is returned. If position 31 is undefined or not supplied, the length of this string value is assumed, 32 so as to search all of the string. 33 34 When the lastIndexOf method is called with two arguments searchString and 35 position, the following steps are taken: 36 37 1.Call ToString, giving it the this value as its argument. 38 2.Call ToString(searchString). 39 3.Call ToNumber(position). (If position is undefined or not supplied, this step produces the value NaN). 40 4.If Result(3) is NaN, use +; otherwise, call ToInteger(Result(3)). 41 5.Compute the number of characters in Result(1). 42 6.Compute min(max(Result(4), 0), Result(5)). 43 7.Compute the number of characters in the string that is Result(2). 44 8.Compute the largest possible integer k not larger than Result(6) such that k+Result(7) is not greater 45 than Result(5), and for all nonnegative integers j less than Result(7), the character at position k+j of 46 Result(1) is the same as the character at position j of Result(2); but if there is no such integer k, then 47 compute the value -1. 48 49 1.Return Result(8). 50 51 Note that the lastIndexOf function is intentionally generic; it does not require that its this value be a 52 String object. Therefore it can be transferred to other kinds of objects for use as a method. 53 54 Author: christine@netscape.com, pschwartau@netscape.com 55 Date: 02 October 1997 56 Modified: 14 July 2002 57 Reason: See http://bugzilla.mozilla.org/show_bug.cgi?id=155289 58 ECMA-262 Ed.3 Section 15.5.4.8 59 The length property of the lastIndexOf method is 1 60* 61*/ 62 var SECTION = "15.5.4.7-2"; 63 var VERSION = "ECMA_1"; 64 startTest(); 65 var TITLE = "String.protoype.lastIndexOf"; 66 67 writeHeaderToLog( SECTION + " "+ TITLE); 68 69 var testcases = getTestCases(); 70 test(); 71 72 73function getTestCases() { 74 var array = new Array(); 75 var item = 0; 76 77 array[item++] = new TestCase( SECTION, "String.prototype.lastIndexOf.length", 1, String.prototype.lastIndexOf.length ); 78 array[item++] = new TestCase( SECTION, "delete String.prototype.lastIndexOf.length", false, delete String.prototype.lastIndexOf.length ); 79 array[item++] = new TestCase( SECTION, "delete String.prototype.lastIndexOf.length; String.prototype.lastIndexOf.length", 1, eval("delete String.prototype.lastIndexOf.length; String.prototype.lastIndexOf.length" ) ); 80 81 array[item++] = new TestCase( SECTION, "var s = new String(''); s.lastIndexOf('', 0)", LastIndexOf("","",0), eval("var s = new String(''); s.lastIndexOf('', 0)") ); 82 array[item++] = new TestCase( SECTION, "var s = new String(''); s.lastIndexOf('')", LastIndexOf("",""), eval("var s = new String(''); s.lastIndexOf('')") ); 83 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('', 0)", LastIndexOf("hello","",0), eval("var s = new String('hello'); s.lastIndexOf('',0)") ); 84 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('')", LastIndexOf("hello",""), eval("var s = new String('hello'); s.lastIndexOf('')") ); 85 86 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll')", LastIndexOf("hello","ll"), eval("var s = new String('hello'); s.lastIndexOf('ll')") ); 87 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 0)", LastIndexOf("hello","ll",0), eval("var s = new String('hello'); s.lastIndexOf('ll', 0)") ); 88 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 1)", LastIndexOf("hello","ll",1), eval("var s = new String('hello'); s.lastIndexOf('ll', 1)") ); 89 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 2)", LastIndexOf("hello","ll",2), eval("var s = new String('hello'); s.lastIndexOf('ll', 2)") ); 90 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 3)", LastIndexOf("hello","ll",3), eval("var s = new String('hello'); s.lastIndexOf('ll', 3)") ); 91 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 4)", LastIndexOf("hello","ll",4), eval("var s = new String('hello'); s.lastIndexOf('ll', 4)") ); 92 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 5)", LastIndexOf("hello","ll",5), eval("var s = new String('hello'); s.lastIndexOf('ll', 5)") ); 93 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 6)", LastIndexOf("hello","ll",6), eval("var s = new String('hello'); s.lastIndexOf('ll', 6)") ); 94 95 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 1.5)", LastIndexOf('hello','ll', 1.5), eval("var s = new String('hello'); s.lastIndexOf('ll', 1.5)") ); 96 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 2.5)", LastIndexOf('hello','ll', 2.5), eval("var s = new String('hello'); s.lastIndexOf('ll', 2.5)") ); 97 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', -1)", LastIndexOf('hello','ll', -1), eval("var s = new String('hello'); s.lastIndexOf('ll', -1)") ); 98 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', -1.5)",LastIndexOf('hello','ll', -1.5), eval("var s = new String('hello'); s.lastIndexOf('ll', -1.5)") ); 99 100 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', -Infinity)", LastIndexOf("hello","ll",-Infinity), eval("var s = new String('hello'); s.lastIndexOf('ll', -Infinity)") ); 101 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', Infinity)", LastIndexOf("hello","ll",Infinity), eval("var s = new String('hello'); s.lastIndexOf('ll', Infinity)") ); 102 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', NaN)", LastIndexOf("hello","ll",NaN), eval("var s = new String('hello'); s.lastIndexOf('ll', NaN)") ); 103 array[item++] = new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', -0)", LastIndexOf("hello","ll",-0), eval("var s = new String('hello'); s.lastIndexOf('ll', -0)") ); 104 for ( var i = 0; i < ( "[object Object]" ).length; i++ ) { 105 array[item++] = new TestCase( SECTION, 106 "var o = new Object(); o.lastIndexOf = String.prototype.lastIndexOf; o.lastIndexOf('b', "+ i + ")", 107 ( i < 2 ? -1 : ( i < 9 ? 2 : 9 )) , 108 eval("var o = new Object(); o.lastIndexOf = String.prototype.lastIndexOf; o.lastIndexOf('b', "+ i + ")") ); 109 } 110 for ( var i = 0; i < 5; i ++ ) { 111 array[item++] = new TestCase( SECTION, 112 "var b = new Boolean(); b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('l', "+ i + ")", 113 ( i < 2 ? -1 : 2 ), 114 eval("var b = new Boolean(); b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('l', "+ i + ")") ); 115 } 116 for ( var i = 0; i < 5; i ++ ) { 117 array[item++] = new TestCase( SECTION, 118 "var b = new Boolean(); b.toString = Object.prototype.toString; b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('o', "+ i + ")", 119 ( i < 1 ? -1 : ( i < 9 ? 1 : ( i < 10 ? 9 : 10 ) ) ), 120 eval("var b = new Boolean(); b.toString = Object.prototype.toString; b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('o', "+ i + ")") ); 121 } 122 for ( var i = 0; i < 9; i++ ) { 123 array[item++] = new TestCase( SECTION, 124 "var n = new Number(Infinity); n.lastIndexOf = String.prototype.lastIndexOf; n.lastIndexOf( 'i', " + i + " )", 125 ( i < 3 ? -1 : ( i < 5 ? 3 : 5 ) ), 126 eval("var n = new Number(Infinity); n.lastIndexOf = String.prototype.lastIndexOf; n.lastIndexOf( 'i', " + i + " )") ); 127 } 128 var a = new Array( "abc","def","ghi","jkl","mno","pqr","stu","vwx","yz" ); 129 130 for ( var i = 0; i < (a.toString()).length; i++ ) { 131 array[item++] = new TestCase( SECTION, 132 "var a = new Array( 'abc','def','ghi','jkl','mno','pqr','stu','vwx','yz' ); a.lastIndexOf = String.prototype.lastIndexOf; a.lastIndexOf( ',mno,p', "+i+" )", 133 ( i < 15 ? -1 : 15 ), 134 eval("var a = new Array( 'abc','def','ghi','jkl','mno','pqr','stu','vwx','yz' ); a.lastIndexOf = String.prototype.lastIndexOf; a.lastIndexOf( ',mno,p', "+i+" )") ); 135 } 136 137 for ( var i = 0; i < 15; i ++ ) { 138 array[item++] = new TestCase( SECTION, 139 "var m = Math; m.lastIndexOf = String.prototype.lastIndexOf; m.lastIndexOf('t', "+ i + ")", 140 ( i < 6 ? -1 : ( i < 10 ? 6 : 10 ) ), 141 eval("var m = Math; m.lastIndexOf = String.prototype.lastIndexOf; m.lastIndexOf('t', "+ i + ")") ); 142 } 143/* 144 for ( var i = 0; i < 15; i++ ) { 145 array[item++] = new TestCase( SECTION, 146 "var d = new Date(); d.lastIndexOf = String.prototype.lastIndexOf; d.lastIndexOf( '0' )", 147 ) 148 } 149 150*/ 151 return array; 152} 153 154function test() { 155 for ( tc = 0; tc < testcases.length; tc++ ) { 156 157 testcases[tc].passed = writeTestCaseResult( 158 testcases[tc].expect, 159 testcases[tc].actual, 160 testcases[tc].description +" = "+ testcases[tc].actual ); 161 162 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value " 163 } 164 stopTest(); 165 166 return ( testcases ); 167} 168 169function LastIndexOf( string, search, position ) { 170 string = String( string ); 171 search = String( search ); 172 173 position = Number( position ) 174 175 if ( isNaN( position ) ) { 176 position = Infinity; 177 } else { 178 position = ToInteger( position ); 179 } 180 181 result5= string.length; 182 result6 = Math.min(Math.max(position, 0), result5); 183 result7 = search.length; 184 185 if (result7 == 0) { 186 return Math.min(position, result5); 187 } 188 189 result8 = -1; 190 191 for ( k = 0; k <= result6; k++ ) { 192 if ( k+ result7 > result5 ) { 193 break; 194 } 195 for ( j = 0; j < result7; j++ ) { 196 if ( string.charAt(k+j) != search.charAt(j) ){ 197 break; 198 } else { 199 if ( j == result7 -1 ) { 200 result8 = k; 201 } 202 } 203 } 204 } 205 206 return result8; 207} 208function ToInteger( n ) { 209 n = Number( n ); 210 if ( isNaN(n) ) { 211 return 0; 212 } 213 if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) { 214 return n; 215 } 216 217 var sign = ( n < 0 ) ? -1 : 1; 218 219 return ( sign * Math.floor(Math.abs(n)) ); 220}