]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
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-1.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 | |
55 | Date: 2 october 1997 | |
56 | */ | |
57 | var SECTION = "15.5.4.7-1"; | |
58 | var VERSION = "ECMA_1"; | |
59 | startTest(); | |
60 | var TITLE = "String.protoype.lastIndexOf"; | |
61 | ||
62 | var TEST_STRING = new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ); | |
63 | ||
64 | writeHeaderToLog( SECTION + " "+ TITLE); | |
65 | ||
66 | var testcases = getTestCases(); | |
67 | test(); | |
68 | ||
69 | ||
70 | function getTestCases() { | |
71 | var array = new Array(); | |
72 | var j = 0; | |
73 | ||
74 | for ( k = 0, i = 0x0021; i < 0x007e; i++, j++, k++ ) { | |
75 | array[j] = new TestCase( SECTION, | |
76 | "String.lastIndexOf(" +String.fromCharCode(i)+ ", 0)", | |
77 | -1, | |
78 | TEST_STRING.lastIndexOf( String.fromCharCode(i), 0 ) ); | |
79 | } | |
80 | ||
81 | for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { | |
82 | array[j] = new TestCase( SECTION, | |
83 | "String.lastIndexOf("+String.fromCharCode(i)+ ", "+ k +")", | |
84 | k, | |
85 | TEST_STRING.lastIndexOf( String.fromCharCode(i), k ) ); | |
86 | } | |
87 | ||
88 | for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { | |
89 | array[j] = new TestCase( SECTION, | |
90 | "String.lastIndexOf("+String.fromCharCode(i)+ ", "+k+1+")", | |
91 | k, | |
92 | TEST_STRING.lastIndexOf( String.fromCharCode(i), k+1 ) ); | |
93 | } | |
94 | ||
95 | for ( k = 9, i = 0x0021; i < 0x007d; i++, j++, k++ ) { | |
96 | array[j] = new TestCase( SECTION, | |
97 | ||
98 | "String.lastIndexOf("+(String.fromCharCode(i) + | |
99 | String.fromCharCode(i+1)+ | |
100 | String.fromCharCode(i+2)) +", "+ 0 + ")", | |
101 | LastIndexOf( TEST_STRING, String.fromCharCode(i) + | |
102 | String.fromCharCode(i+1)+String.fromCharCode(i+2), 0), | |
103 | TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ | |
104 | String.fromCharCode(i+1)+ | |
105 | String.fromCharCode(i+2)), | |
106 | 0 ) ); | |
107 | } | |
108 | ||
109 | for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { | |
110 | array[j] = new TestCase( SECTION, | |
111 | "String.lastIndexOf("+(String.fromCharCode(i) + | |
112 | String.fromCharCode(i+1)+ | |
113 | String.fromCharCode(i+2)) +", "+ k +")", | |
114 | k, | |
115 | TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ | |
116 | String.fromCharCode(i+1)+ | |
117 | String.fromCharCode(i+2)), | |
118 | k ) ); | |
119 | } | |
120 | for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { | |
121 | array[j] = new TestCase( SECTION, | |
122 | "String.lastIndexOf("+(String.fromCharCode(i) + | |
123 | String.fromCharCode(i+1)+ | |
124 | String.fromCharCode(i+2)) +", "+ k+1 +")", | |
125 | k, | |
126 | TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ | |
127 | String.fromCharCode(i+1)+ | |
128 | String.fromCharCode(i+2)), | |
129 | k+1 ) ); | |
130 | } | |
131 | for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { | |
132 | array[j] = new TestCase( SECTION, | |
133 | "String.lastIndexOf("+ | |
134 | (String.fromCharCode(i) + | |
135 | String.fromCharCode(i+1)+ | |
136 | String.fromCharCode(i+2)) +", "+ (k-1) +")", | |
137 | LastIndexOf( TEST_STRING, String.fromCharCode(i) + | |
138 | String.fromCharCode(i+1)+String.fromCharCode(i+2), k-1), | |
139 | TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ | |
140 | String.fromCharCode(i+1)+ | |
141 | String.fromCharCode(i+2)), | |
142 | k-1 ) ); | |
143 | } | |
144 | ||
145 | array[j++] = new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ", 0 )", 0, TEST_STRING.lastIndexOf( TEST_STRING, 0 ) ); | |
146 | // array[j++] = new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ", 1 )", 0, TEST_STRING.lastIndexOf( TEST_STRING, 1 )); | |
147 | array[j++] = new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ")", 0, TEST_STRING.lastIndexOf( TEST_STRING )); | |
148 | ||
149 | return array; | |
150 | } | |
151 | ||
152 | function test() { | |
153 | writeLineToLog( "TEST_STRING = new String(\" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\")" ); | |
154 | ||
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 | } | |
165 | stopTest(); | |
166 | ||
167 | return ( testcases ); | |
168 | } | |
169 | ||
170 | function LastIndexOf( string, search, position ) { | |
171 | string = String( string ); | |
172 | search = String( search ); | |
173 | ||
174 | position = Number( position ) | |
175 | ||
176 | if ( isNaN( position ) ) { | |
177 | position = Infinity; | |
178 | } else { | |
179 | position = ToInteger( position ); | |
180 | } | |
181 | ||
182 | result5= string.length; | |
183 | result6 = Math.min(Math.max(position, 0), result5); | |
184 | result7 = search.length; | |
185 | ||
186 | if (result7 == 0) { | |
187 | return Math.min(position, result5); | |
188 | } | |
189 | ||
190 | result8 = -1; | |
191 | ||
192 | for ( k = 0; k <= result6; k++ ) { | |
193 | if ( k+ result7 > result5 ) { | |
194 | break; | |
195 | } | |
196 | for ( j = 0; j < result7; j++ ) { | |
197 | if ( string.charAt(k+j) != search.charAt(j) ){ | |
198 | break; | |
199 | } else { | |
200 | if ( j == result7 -1 ) { | |
201 | result8 = k; | |
202 | } | |
203 | } | |
204 | } | |
205 | } | |
206 | ||
207 | return result8; | |
208 | } | |
209 | function ToInteger( n ) { | |
210 | n = Number( n ); | |
211 | if ( isNaN(n) ) { | |
212 | return 0; | |
213 | } | |
214 | if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) { | |
215 | return n; | |
216 | } | |
217 | ||
218 | var sign = ( n < 0 ) ? -1 : 1; | |
219 | ||
220 | return ( sign * Math.floor(Math.abs(n)) ); | |
221 | } |