1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is Mozilla Communicator client code, released
18
* The Initial Developer of the Original Code is
19
* Netscape Communications Corporation.
20
* Portions created by the Initial Developer are Copyright (C) 1998
21
* the Initial Developer. All Rights Reserved.
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the MPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the MPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
40
File Name: 15.5.4.7-2.js
41
ECMA Section: 15.5.4.7 String.prototype.lastIndexOf( searchString, pos)
44
If the given searchString appears as a substring of the result of
45
converting this object to a string, at one or more positions that are
46
at or to the left of the specified position, then the index of the
47
rightmost such position is returned; otherwise -1 is returned. If position
48
is undefined or not supplied, the length of this string value is assumed,
49
so as to search all of the string.
51
When the lastIndexOf method is called with two arguments searchString and
52
position, the following steps are taken:
54
1.Call ToString, giving it the this value as its argument.
55
2.Call ToString(searchString).
56
3.Call ToNumber(position). (If position is undefined or not supplied, this step produces the value NaN).
57
4.If Result(3) is NaN, use +; otherwise, call ToInteger(Result(3)).
58
5.Compute the number of characters in Result(1).
59
6.Compute min(max(Result(4), 0), Result(5)).
60
7.Compute the number of characters in the string that is Result(2).
61
8.Compute the largest possible integer k not larger than Result(6) such that k+Result(7) is not greater
62
than Result(5), and for all nonnegative integers j less than Result(7), the character at position k+j of
63
Result(1) is the same as the character at position j of Result(2); but if there is no such integer k, then
68
Note that the lastIndexOf function is intentionally generic; it does not require that its this value be a
69
String object. Therefore it can be transferred to other kinds of objects for use as a method.
71
Author: christine@netscape.com, pschwartau@netscape.com
73
Modified: 14 July 2002
74
Reason: See http://bugzilla.mozilla.org/show_bug.cgi?id=155289
75
ECMA-262 Ed.3 Section 15.5.4.8
76
The length property of the lastIndexOf method is 1
79
var SECTION = "15.5.4.7-2";
80
var VERSION = "ECMA_1";
82
var TITLE = "String.protoype.lastIndexOf";
84
writeHeaderToLog( SECTION + " "+ TITLE);
87
new TestCase( SECTION, "String.prototype.lastIndexOf.length", 1, String.prototype.lastIndexOf.length );
88
new TestCase( SECTION, "delete String.prototype.lastIndexOf.length", false, delete String.prototype.lastIndexOf.length );
89
new TestCase( SECTION, "delete String.prototype.lastIndexOf.length; String.prototype.lastIndexOf.length", 1, eval("delete String.prototype.lastIndexOf.length; String.prototype.lastIndexOf.length" ) );
91
new TestCase( SECTION, "var s = new String(''); s.lastIndexOf('', 0)", LastIndexOf("","",0), eval("var s = new String(''); s.lastIndexOf('', 0)") );
92
new TestCase( SECTION, "var s = new String(''); s.lastIndexOf('')", LastIndexOf("",""), eval("var s = new String(''); s.lastIndexOf('')") );
93
new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('', 0)", LastIndexOf("hello","",0), eval("var s = new String('hello'); s.lastIndexOf('',0)") );
94
new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('')", LastIndexOf("hello",""), eval("var s = new String('hello'); s.lastIndexOf('')") );
96
new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll')", LastIndexOf("hello","ll"), eval("var s = new String('hello'); s.lastIndexOf('ll')") );
97
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)") );
98
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)") );
99
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)") );
100
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)") );
101
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)") );
102
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)") );
103
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)") );
105
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)") );
106
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)") );
107
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)") );
108
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)") );
110
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)") );
111
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)") );
112
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)") );
113
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)") );
114
for ( var i = 0; i < ( "[object Object]" ).length; i++ ) {
115
new TestCase( SECTION,
116
"var o = new Object(); o.lastIndexOf = String.prototype.lastIndexOf; o.lastIndexOf('b', "+ i + ")",
117
( i < 2 ? -1 : ( i < 9 ? 2 : 9 )) ,
118
eval("var o = new Object(); o.lastIndexOf = String.prototype.lastIndexOf; o.lastIndexOf('b', "+ i + ")") );
120
for ( var i = 0; i < 5; i ++ ) {
121
new TestCase( SECTION,
122
"var b = new Boolean(); b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('l', "+ i + ")",
124
eval("var b = new Boolean(); b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('l', "+ i + ")") );
126
for ( var i = 0; i < 5; i ++ ) {
127
new TestCase( SECTION,
128
"var b = new Boolean(); b.toString = Object.prototype.toString; b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('o', "+ i + ")",
129
( i < 1 ? -1 : ( i < 9 ? 1 : ( i < 10 ? 9 : 10 ) ) ),
130
eval("var b = new Boolean(); b.toString = Object.prototype.toString; b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('o', "+ i + ")") );
132
for ( var i = 0; i < 9; i++ ) {
133
new TestCase( SECTION,
134
"var n = new Number(Infinity); n.lastIndexOf = String.prototype.lastIndexOf; n.lastIndexOf( 'i', " + i + " )",
135
( i < 3 ? -1 : ( i < 5 ? 3 : 5 ) ),
136
eval("var n = new Number(Infinity); n.lastIndexOf = String.prototype.lastIndexOf; n.lastIndexOf( 'i', " + i + " )") );
138
var a = new Array( "abc","def","ghi","jkl","mno","pqr","stu","vwx","yz" );
140
for ( var i = 0; i < (a.toString()).length; i++ ) {
141
new TestCase( SECTION,
142
"var a = new Array( 'abc','def','ghi','jkl','mno','pqr','stu','vwx','yz' ); a.lastIndexOf = String.prototype.lastIndexOf; a.lastIndexOf( ',mno,p', "+i+" )",
143
( i < 15 ? -1 : 15 ),
144
eval("var a = new Array( 'abc','def','ghi','jkl','mno','pqr','stu','vwx','yz' ); a.lastIndexOf = String.prototype.lastIndexOf; a.lastIndexOf( ',mno,p', "+i+" )") );
147
for ( var i = 0; i < 15; i ++ ) {
148
new TestCase( SECTION,
149
"var m = Math; m.lastIndexOf = String.prototype.lastIndexOf; m.lastIndexOf('t', "+ i + ")",
150
( i < 6 ? -1 : ( i < 10 ? 6 : 10 ) ),
151
eval("var m = Math; m.lastIndexOf = String.prototype.lastIndexOf; m.lastIndexOf('t', "+ i + ")") );
154
for ( var i = 0; i < 15; i++ ) {
155
new TestCase( SECTION,
156
"var d = new Date(); d.lastIndexOf = String.prototype.lastIndexOf; d.lastIndexOf( '0' )",
164
function LastIndexOf( string, search, position ) {
165
string = String( string );
166
search = String( search );
168
position = Number( position )
170
if ( isNaN( position ) ) {
173
position = ToInteger( position );
176
result5= string.length;
177
result6 = Math.min(Math.max(position, 0), result5);
178
result7 = search.length;
181
return Math.min(position, result5);
186
for ( k = 0; k <= result6; k++ ) {
187
if ( k+ result7 > result5 ) {
190
for ( j = 0; j < result7; j++ ) {
191
if ( string.charAt(k+j) != search.charAt(j) ){
194
if ( j == result7 -1 ) {
203
function ToInteger( n ) {
208
if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) {
212
var sign = ( n < 0 ) ? -1 : 1;
214
return ( sign * Math.floor(Math.abs(n)) );