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 ***** */
39 gTestfile = '15.5.4.7-1.js';
42 File Name: 15.5.4.7-1.js
43 ECMA Section: 15.5.4.7 String.prototype.lastIndexOf( searchString, pos)
46 If the given searchString appears as a substring of the result of
47 converting this object to a string, at one or more positions that are
48 at or to the left of the specified position, then the index of the
49 rightmost such position is returned; otherwise -1 is returned. If position
50 is undefined or not supplied, the length of this string value is assumed,
51 so as to search all of the string.
53 When the lastIndexOf method is called with two arguments searchString and
54 position, the following steps are taken:
56 1.Call ToString, giving it the this value as its argument.
57 2.Call ToString(searchString).
58 3.Call ToNumber(position). (If position is undefined or not supplied, this step produces the value NaN).
59 4.If Result(3) is NaN, use +; otherwise, call ToInteger(Result(3)).
60 5.Compute the number of characters in Result(1).
61 6.Compute min(max(Result(4), 0), Result(5)).
62 7.Compute the number of characters in the string that is Result(2).
63 8.Compute the largest possible integer k not larger than Result(6) such that k+Result(7) is not greater
64 than Result(5), and for all nonnegative integers j less than Result(7), the character at position k+j of
65 Result(1) is the same as the character at position j of Result(2); but if there is no such integer k, then
70 Note that the lastIndexOf function is intentionally generic; it does not require that its this value be a
71 String object. Therefore it can be transferred to other kinds of objects for use as a method.
73 Author: christine@netscape.com
76 var SECTION = "15.5.4.7-1";
77 var VERSION = "ECMA_1";
79 var TITLE = "String.protoype.lastIndexOf";
81 var TEST_STRING = new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" );
83 writeHeaderToLog( SECTION + " "+ TITLE);
87 for ( k = 0, i = 0x0021; i < 0x007e; i++, j++, k++ ) {
88 new TestCase( SECTION,
89 "String.lastIndexOf(" +String.fromCharCode(i)+ ", 0)",
91 TEST_STRING.lastIndexOf( String.fromCharCode(i), 0 ) );
94 for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) {
95 new TestCase( SECTION,
96 "String.lastIndexOf("+String.fromCharCode(i)+ ", "+ k +")",
98 TEST_STRING.lastIndexOf( String.fromCharCode(i), k ) );
101 for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) {
102 new TestCase( SECTION,
103 "String.lastIndexOf("+String.fromCharCode(i)+ ", "+k+1+")",
105 TEST_STRING.lastIndexOf( String.fromCharCode(i), k+1 ) );
108 for ( k = 9, i = 0x0021; i < 0x007d; i++, j++, k++ ) {
109 new TestCase( SECTION,
111 "String.lastIndexOf("+(String.fromCharCode(i) +
112 String.fromCharCode(i+1)+
113 String.fromCharCode(i+2)) +", "+ 0 + ")",
114 LastIndexOf( TEST_STRING, String.fromCharCode(i) +
115 String.fromCharCode(i+1)+String.fromCharCode(i+2), 0),
116 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+
117 String.fromCharCode(i+1)+
118 String.fromCharCode(i+2)),
122 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
123 new TestCase( SECTION,
124 "String.lastIndexOf("+(String.fromCharCode(i) +
125 String.fromCharCode(i+1)+
126 String.fromCharCode(i+2)) +", "+ k +")",
128 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+
129 String.fromCharCode(i+1)+
130 String.fromCharCode(i+2)),
133 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
134 new TestCase( SECTION,
135 "String.lastIndexOf("+(String.fromCharCode(i) +
136 String.fromCharCode(i+1)+
137 String.fromCharCode(i+2)) +", "+ k+1 +")",
139 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+
140 String.fromCharCode(i+1)+
141 String.fromCharCode(i+2)),
144 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
145 new TestCase( SECTION,
146 "String.lastIndexOf("+
147 (String.fromCharCode(i) +
148 String.fromCharCode(i+1)+
149 String.fromCharCode(i+2)) +", "+ (k-1) +")",
150 LastIndexOf( TEST_STRING, String.fromCharCode(i) +
151 String.fromCharCode(i+1)+String.fromCharCode(i+2), k-1),
152 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+
153 String.fromCharCode(i+1)+
154 String.fromCharCode(i+2)),
158 new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ", 0 )", 0, TEST_STRING.lastIndexOf( TEST_STRING, 0 ) );
160 // new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ", 1 )", 0, TEST_STRING.lastIndexOf( TEST_STRING, 1 ));
162 new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ")", 0, TEST_STRING.lastIndexOf( TEST_STRING ));
164 print( "TEST_STRING = new String(\" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\")" );
168 function LastIndexOf( string, search, position ) {
169 string = String( string );
170 search = String( search );
172 position = Number( position )
174 if ( isNaN( position ) ) {
177 position = ToInteger( position );
180 result5= string.length;
181 result6 = Math.min(Math.max(position, 0), result5);
182 result7 = search.length;
185 return Math.min(position, result5);
190 for ( k = 0; k <= result6; k++ ) {
191 if ( k+ result7 > result5 ) {
194 for ( j = 0; j < result7; j++ ) {
195 if ( string.charAt(k+j) != search.charAt(j) ){
198 if ( j == result7 -1 ) {
207 function ToInteger( n ) {
212 if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) {
216 var sign = ( n < 0 ) ? -1 : 1;
218 return ( sign * Math.floor(Math.abs(n)) );