|
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.6-1.js |
|
9 ECMA Section: 15.5.4.6 String.prototype.indexOf( searchString, pos) |
|
10 Description: If the given searchString appears as a substring of the |
|
11 result of converting this object to a string, at one or |
|
12 more positions that are at or to the right of the |
|
13 specified position, then the index of the leftmost such |
|
14 position is returned; otherwise -1 is returned. If |
|
15 positionis undefined or not supplied, 0 is assumed, so |
|
16 as to search all of the string. |
|
17 |
|
18 When the indexOf method is called with two arguments, |
|
19 searchString and pos, the following steps are taken: |
|
20 |
|
21 1. Call ToString, giving it the this value as its |
|
22 argument. |
|
23 2. Call ToString(searchString). |
|
24 3. Call ToInteger(position). (If position is undefined |
|
25 or not supplied, this step produces the value 0). |
|
26 4. Compute the number of characters in Result(1). |
|
27 5. Compute min(max(Result(3), 0), Result(4)). |
|
28 6. Compute the number of characters in the string that |
|
29 is Result(2). |
|
30 7. Compute the smallest possible integer k not smaller |
|
31 than Result(5) such that k+Result(6) is not greater |
|
32 than Result(4), and for all nonnegative integers j |
|
33 less than Result(6), the character at position k+j |
|
34 of Result(1) is the same as the character at position |
|
35 j of Result(2); but if there is no such integer k, |
|
36 then compute the value -1. |
|
37 8. Return Result(7). |
|
38 |
|
39 Note that the indexOf function is intentionally generic; |
|
40 it does not require that its this value be a String object. |
|
41 Therefore it can be transferred to other kinds of objects |
|
42 for use as a method. |
|
43 |
|
44 Author: christine@netscape.com |
|
45 Date: 2 october 1997 |
|
46 */ |
|
47 var SECTION = "15.5.4.6-1"; |
|
48 var VERSION = "ECMA_1"; |
|
49 startTest(); |
|
50 var TITLE = "String.protoype.indexOf"; |
|
51 |
|
52 var TEST_STRING = new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ); |
|
53 |
|
54 writeHeaderToLog( SECTION + " "+ TITLE); |
|
55 |
|
56 var j = 0; |
|
57 |
|
58 for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { |
|
59 new TestCase( SECTION, |
|
60 "String.indexOf(" +String.fromCharCode(i)+ ", 0)", |
|
61 k, |
|
62 TEST_STRING.indexOf( String.fromCharCode(i), 0 ) ); |
|
63 } |
|
64 |
|
65 for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { |
|
66 new TestCase( SECTION, |
|
67 "String.indexOf("+String.fromCharCode(i)+ ", "+ k +")", |
|
68 k, |
|
69 TEST_STRING.indexOf( String.fromCharCode(i), k ) ); |
|
70 } |
|
71 |
|
72 for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { |
|
73 new TestCase( SECTION, |
|
74 "String.indexOf("+String.fromCharCode(i)+ ", "+k+1+")", |
|
75 -1, |
|
76 TEST_STRING.indexOf( String.fromCharCode(i), k+1 ) ); |
|
77 } |
|
78 |
|
79 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { |
|
80 new TestCase( SECTION, |
|
81 "String.indexOf("+(String.fromCharCode(i) + |
|
82 String.fromCharCode(i+1)+ |
|
83 String.fromCharCode(i+2)) +", "+0+")", |
|
84 k, |
|
85 TEST_STRING.indexOf( (String.fromCharCode(i)+ |
|
86 String.fromCharCode(i+1)+ |
|
87 String.fromCharCode(i+2)), |
|
88 0 ) ); |
|
89 } |
|
90 |
|
91 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { |
|
92 new TestCase( SECTION, |
|
93 "String.indexOf("+(String.fromCharCode(i) + |
|
94 String.fromCharCode(i+1)+ |
|
95 String.fromCharCode(i+2)) +", "+ k +")", |
|
96 k, |
|
97 TEST_STRING.indexOf( (String.fromCharCode(i)+ |
|
98 String.fromCharCode(i+1)+ |
|
99 String.fromCharCode(i+2)), |
|
100 k ) ); |
|
101 } |
|
102 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { |
|
103 new TestCase( SECTION, |
|
104 "String.indexOf("+(String.fromCharCode(i) + |
|
105 String.fromCharCode(i+1)+ |
|
106 String.fromCharCode(i+2)) +", "+ k+1 +")", |
|
107 -1, |
|
108 TEST_STRING.indexOf( (String.fromCharCode(i)+ |
|
109 String.fromCharCode(i+1)+ |
|
110 String.fromCharCode(i+2)), |
|
111 k+1 ) ); |
|
112 } |
|
113 |
|
114 new TestCase( SECTION, "String.indexOf(" +TEST_STRING + ", 0 )", 0, TEST_STRING.indexOf( TEST_STRING, 0 ) ); |
|
115 |
|
116 new TestCase( SECTION, "String.indexOf(" +TEST_STRING + ", 1 )", -1, TEST_STRING.indexOf( TEST_STRING, 1 )); |
|
117 |
|
118 print( "TEST_STRING = new String(\" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\")" ); |
|
119 |
|
120 |
|
121 test(); |