|
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-3.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-3"; |
|
43 var VERSION = "ECMA_2"; |
|
44 startTest(); |
|
45 var TITLE = "String.protoype.lastIndexOf"; |
|
46 |
|
47 writeHeaderToLog( SECTION + " "+ TITLE); |
|
48 |
|
49 new TestCase( SECTION, |
|
50 "var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 0 )", |
|
51 -1, |
|
52 eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 0 )") ); |
|
53 |
|
54 new TestCase( SECTION, |
|
55 "var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 1 )", |
|
56 1, |
|
57 eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 1 )") ); |
|
58 |
|
59 new TestCase( SECTION, |
|
60 "var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 2 )", |
|
61 1, |
|
62 eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 2 )") ); |
|
63 |
|
64 new TestCase( SECTION, |
|
65 "var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 10 )", |
|
66 1, |
|
67 eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 10 )") ); |
|
68 |
|
69 new TestCase( SECTION, |
|
70 "var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r' )", |
|
71 1, |
|
72 eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r' )") ); |
|
73 |
|
74 test(); |
|
75 |
|
76 function LastIndexOf( string, search, position ) { |
|
77 string = String( string ); |
|
78 search = String( search ); |
|
79 |
|
80 position = Number( position ) |
|
81 |
|
82 if ( isNaN( position ) ) { |
|
83 position = Infinity; |
|
84 } else { |
|
85 position = ToInteger( position ); |
|
86 } |
|
87 |
|
88 result5= string.length; |
|
89 result6 = Math.min(Math.max(position, 0), result5); |
|
90 result7 = search.length; |
|
91 |
|
92 if (result7 == 0) { |
|
93 return Math.min(position, result5); |
|
94 } |
|
95 |
|
96 result8 = -1; |
|
97 |
|
98 for ( k = 0; k <= result6; k++ ) { |
|
99 if ( k+ result7 > result5 ) { |
|
100 break; |
|
101 } |
|
102 for ( j = 0; j < result7; j++ ) { |
|
103 if ( string.charAt(k+j) != search.charAt(j) ){ |
|
104 break; |
|
105 } else { |
|
106 if ( j == result7 -1 ) { |
|
107 result8 = k; |
|
108 } |
|
109 } |
|
110 } |
|
111 } |
|
112 |
|
113 return result8; |
|
114 } |
|
115 function ToInteger( n ) { |
|
116 n = Number( n ); |
|
117 if ( isNaN(n) ) { |
|
118 return 0; |
|
119 } |
|
120 if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) { |
|
121 return n; |
|
122 } |
|
123 |
|
124 var sign = ( n < 0 ) ? -1 : 1; |
|
125 |
|
126 return ( sign * Math.floor(Math.abs(n)) ); |
|
127 } |