michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: File Name: 15.5.4.6-1.js michael@0: ECMA Section: 15.5.4.6 String.prototype.indexOf( searchString, pos) michael@0: Description: If the given searchString appears as a substring of the michael@0: result of converting this object to a string, at one or michael@0: more positions that are at or to the right of the michael@0: specified position, then the index of the leftmost such michael@0: position is returned; otherwise -1 is returned. If michael@0: positionis undefined or not supplied, 0 is assumed, so michael@0: as to search all of the string. michael@0: michael@0: When the indexOf method is called with two arguments, michael@0: searchString and pos, the following steps are taken: michael@0: michael@0: 1. Call ToString, giving it the this value as its michael@0: argument. michael@0: 2. Call ToString(searchString). michael@0: 3. Call ToInteger(position). (If position is undefined michael@0: or not supplied, this step produces the value 0). michael@0: 4. Compute the number of characters in Result(1). michael@0: 5. Compute min(max(Result(3), 0), Result(4)). michael@0: 6. Compute the number of characters in the string that michael@0: is Result(2). michael@0: 7. Compute the smallest possible integer k not smaller michael@0: than Result(5) such that k+Result(6) is not greater michael@0: than Result(4), and for all nonnegative integers j michael@0: less than Result(6), the character at position k+j michael@0: of Result(1) is the same as the character at position michael@0: j of Result(2); but if there is no such integer k, michael@0: then compute the value -1. michael@0: 8. Return Result(7). michael@0: michael@0: Note that the indexOf function is intentionally generic; michael@0: it does not require that its this value be a String object. michael@0: Therefore it can be transferred to other kinds of objects michael@0: for use as a method. michael@0: michael@0: Author: christine@netscape.com michael@0: Date: 2 october 1997 michael@0: */ michael@0: var SECTION = "15.5.4.6-1"; michael@0: var VERSION = "ECMA_1"; michael@0: startTest(); michael@0: var TITLE = "String.protoype.indexOf"; michael@0: michael@0: var TEST_STRING = new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ); michael@0: michael@0: writeHeaderToLog( SECTION + " "+ TITLE); michael@0: michael@0: var j = 0; michael@0: michael@0: for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { michael@0: new TestCase( SECTION, michael@0: "String.indexOf(" +String.fromCharCode(i)+ ", 0)", michael@0: k, michael@0: TEST_STRING.indexOf( String.fromCharCode(i), 0 ) ); michael@0: } michael@0: michael@0: for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { michael@0: new TestCase( SECTION, michael@0: "String.indexOf("+String.fromCharCode(i)+ ", "+ k +")", michael@0: k, michael@0: TEST_STRING.indexOf( String.fromCharCode(i), k ) ); michael@0: } michael@0: michael@0: for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { michael@0: new TestCase( SECTION, michael@0: "String.indexOf("+String.fromCharCode(i)+ ", "+k+1+")", michael@0: -1, michael@0: TEST_STRING.indexOf( String.fromCharCode(i), k+1 ) ); michael@0: } michael@0: michael@0: for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { michael@0: new TestCase( SECTION, michael@0: "String.indexOf("+(String.fromCharCode(i) + michael@0: String.fromCharCode(i+1)+ michael@0: String.fromCharCode(i+2)) +", "+0+")", michael@0: k, michael@0: TEST_STRING.indexOf( (String.fromCharCode(i)+ michael@0: String.fromCharCode(i+1)+ michael@0: String.fromCharCode(i+2)), michael@0: 0 ) ); michael@0: } michael@0: michael@0: for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { michael@0: new TestCase( SECTION, michael@0: "String.indexOf("+(String.fromCharCode(i) + michael@0: String.fromCharCode(i+1)+ michael@0: String.fromCharCode(i+2)) +", "+ k +")", michael@0: k, michael@0: TEST_STRING.indexOf( (String.fromCharCode(i)+ michael@0: String.fromCharCode(i+1)+ michael@0: String.fromCharCode(i+2)), michael@0: k ) ); michael@0: } michael@0: for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { michael@0: new TestCase( SECTION, michael@0: "String.indexOf("+(String.fromCharCode(i) + michael@0: String.fromCharCode(i+1)+ michael@0: String.fromCharCode(i+2)) +", "+ k+1 +")", michael@0: -1, michael@0: TEST_STRING.indexOf( (String.fromCharCode(i)+ michael@0: String.fromCharCode(i+1)+ michael@0: String.fromCharCode(i+2)), michael@0: k+1 ) ); michael@0: } michael@0: michael@0: new TestCase( SECTION, "String.indexOf(" +TEST_STRING + ", 0 )", 0, TEST_STRING.indexOf( TEST_STRING, 0 ) ); michael@0: michael@0: new TestCase( SECTION, "String.indexOf(" +TEST_STRING + ", 1 )", -1, TEST_STRING.indexOf( TEST_STRING, 1 )); michael@0: michael@0: print( "TEST_STRING = new String(\" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\")" ); michael@0: michael@0: michael@0: test();