|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 File Name: 15.5.4.7-1.js |
|
9 ECMA Section: 15.5.4.7 String.prototype.lastIndexOf( searchString, pos) |
|
10 Description: |
|
11 |
|
12 If the given searchString appears as a substring of the result of |
|
13 converting this object to a string, at one or more positions that are |
|
14 at or to the left of the specified position, then the index of the |
|
15 rightmost such position is returned; otherwise -1 is returned. If position |
|
16 is undefined or not supplied, the length of this string value is assumed, |
|
17 so as to search all of the string. |
|
18 |
|
19 When the lastIndexOf method is called with two arguments searchString and |
|
20 position, the following steps are taken: |
|
21 |
|
22 1.Call ToString, giving it the this value as its argument. |
|
23 2.Call ToString(searchString). |
|
24 3.Call ToNumber(position). (If position is undefined or not supplied, this step produces the value NaN). |
|
25 4.If Result(3) is NaN, use +; otherwise, call ToInteger(Result(3)). |
|
26 5.Compute the number of characters in Result(1). |
|
27 6.Compute min(max(Result(4), 0), Result(5)). |
|
28 7.Compute the number of characters in the string that is Result(2). |
|
29 8.Compute the largest possible integer k not larger than Result(6) such that k+Result(7) is not greater |
|
30 than Result(5), and for all nonnegative integers j less than Result(7), the character at position k+j of |
|
31 Result(1) is the same as the character at position j of Result(2); but if there is no such integer k, then |
|
32 compute the value -1. |
|
33 |
|
34 1.Return Result(8). |
|
35 |
|
36 Note that the lastIndexOf function is intentionally generic; it does not require that its this value be a |
|
37 String object. Therefore it can be transferred to other kinds of objects for use as a method. |
|
38 |
|
39 Author: christine@netscape.com |
|
40 Date: 2 october 1997 |
|
41 */ |
|
42 var SECTION = "15.5.4.7-1"; |
|
43 var VERSION = "ECMA_1"; |
|
44 startTest(); |
|
45 var TITLE = "String.protoype.lastIndexOf"; |
|
46 |
|
47 var TEST_STRING = new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ); |
|
48 |
|
49 writeHeaderToLog( SECTION + " "+ TITLE); |
|
50 |
|
51 var j = 0; |
|
52 |
|
53 for ( k = 0, i = 0x0021; i < 0x007e; i++, j++, k++ ) { |
|
54 new TestCase( SECTION, |
|
55 "String.lastIndexOf(" +String.fromCharCode(i)+ ", 0)", |
|
56 -1, |
|
57 TEST_STRING.lastIndexOf( String.fromCharCode(i), 0 ) ); |
|
58 } |
|
59 |
|
60 for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { |
|
61 new TestCase( SECTION, |
|
62 "String.lastIndexOf("+String.fromCharCode(i)+ ", "+ k +")", |
|
63 k, |
|
64 TEST_STRING.lastIndexOf( String.fromCharCode(i), k ) ); |
|
65 } |
|
66 |
|
67 for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { |
|
68 new TestCase( SECTION, |
|
69 "String.lastIndexOf("+String.fromCharCode(i)+ ", "+k+1+")", |
|
70 k, |
|
71 TEST_STRING.lastIndexOf( String.fromCharCode(i), k+1 ) ); |
|
72 } |
|
73 |
|
74 for ( k = 9, i = 0x0021; i < 0x007d; i++, j++, k++ ) { |
|
75 new TestCase( SECTION, |
|
76 |
|
77 "String.lastIndexOf("+(String.fromCharCode(i) + |
|
78 String.fromCharCode(i+1)+ |
|
79 String.fromCharCode(i+2)) +", "+ 0 + ")", |
|
80 LastIndexOf( TEST_STRING, String.fromCharCode(i) + |
|
81 String.fromCharCode(i+1)+String.fromCharCode(i+2), 0), |
|
82 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ |
|
83 String.fromCharCode(i+1)+ |
|
84 String.fromCharCode(i+2)), |
|
85 0 ) ); |
|
86 } |
|
87 |
|
88 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { |
|
89 new TestCase( SECTION, |
|
90 "String.lastIndexOf("+(String.fromCharCode(i) + |
|
91 String.fromCharCode(i+1)+ |
|
92 String.fromCharCode(i+2)) +", "+ k +")", |
|
93 k, |
|
94 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ |
|
95 String.fromCharCode(i+1)+ |
|
96 String.fromCharCode(i+2)), |
|
97 k ) ); |
|
98 } |
|
99 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { |
|
100 new TestCase( SECTION, |
|
101 "String.lastIndexOf("+(String.fromCharCode(i) + |
|
102 String.fromCharCode(i+1)+ |
|
103 String.fromCharCode(i+2)) +", "+ k+1 +")", |
|
104 k, |
|
105 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ |
|
106 String.fromCharCode(i+1)+ |
|
107 String.fromCharCode(i+2)), |
|
108 k+1 ) ); |
|
109 } |
|
110 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { |
|
111 new TestCase( SECTION, |
|
112 "String.lastIndexOf("+ |
|
113 (String.fromCharCode(i) + |
|
114 String.fromCharCode(i+1)+ |
|
115 String.fromCharCode(i+2)) +", "+ (k-1) +")", |
|
116 LastIndexOf( TEST_STRING, String.fromCharCode(i) + |
|
117 String.fromCharCode(i+1)+String.fromCharCode(i+2), k-1), |
|
118 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ |
|
119 String.fromCharCode(i+1)+ |
|
120 String.fromCharCode(i+2)), |
|
121 k-1 ) ); |
|
122 } |
|
123 |
|
124 new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ", 0 )", 0, TEST_STRING.lastIndexOf( TEST_STRING, 0 ) ); |
|
125 |
|
126 // new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ", 1 )", 0, TEST_STRING.lastIndexOf( TEST_STRING, 1 )); |
|
127 |
|
128 new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ")", 0, TEST_STRING.lastIndexOf( TEST_STRING )); |
|
129 |
|
130 print( "TEST_STRING = new String(\" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\")" ); |
|
131 |
|
132 test(); |
|
133 |
|
134 function LastIndexOf( string, search, position ) { |
|
135 string = String( string ); |
|
136 search = String( search ); |
|
137 |
|
138 position = Number( position ) |
|
139 |
|
140 if ( isNaN( position ) ) { |
|
141 position = Infinity; |
|
142 } else { |
|
143 position = ToInteger( position ); |
|
144 } |
|
145 |
|
146 result5= string.length; |
|
147 result6 = Math.min(Math.max(position, 0), result5); |
|
148 result7 = search.length; |
|
149 |
|
150 if (result7 == 0) { |
|
151 return Math.min(position, result5); |
|
152 } |
|
153 |
|
154 result8 = -1; |
|
155 |
|
156 for ( k = 0; k <= result6; k++ ) { |
|
157 if ( k+ result7 > result5 ) { |
|
158 break; |
|
159 } |
|
160 for ( j = 0; j < result7; j++ ) { |
|
161 if ( string.charAt(k+j) != search.charAt(j) ){ |
|
162 break; |
|
163 } else { |
|
164 if ( j == result7 -1 ) { |
|
165 result8 = k; |
|
166 } |
|
167 } |
|
168 } |
|
169 } |
|
170 |
|
171 return result8; |
|
172 } |
|
173 function ToInteger( n ) { |
|
174 n = Number( n ); |
|
175 if ( isNaN(n) ) { |
|
176 return 0; |
|
177 } |
|
178 if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) { |
|
179 return n; |
|
180 } |
|
181 |
|
182 var sign = ( n < 0 ) ? -1 : 1; |
|
183 |
|
184 return ( sign * Math.floor(Math.abs(n)) ); |
|
185 } |