js/src/tests/ecma/extensions/15.5.4.7-3.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     7 /**
     8    File Name:          15.5.4.7-3.js
     9    ECMA Section:       15.5.4.7 String.prototype.lastIndexOf( searchString, pos)
    10    Description:
    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.
    19    When the lastIndexOf method is called with two arguments searchString and
    20    position, the following steps are taken:
    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.
    34    1.Return Result(8).
    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.
    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";
    47 writeHeaderToLog( SECTION + " "+ TITLE);
    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 )") );
    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 )") );
    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 )") );
    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 )") );
    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' )") );
    74 test();
    76 function LastIndexOf( string, search, position ) {
    77   string = String( string );
    78   search = String( search );
    80   position = Number( position )
    82     if ( isNaN( position ) ) {
    83       position = Infinity;
    84     } else {
    85       position = ToInteger( position );
    86     }
    88   result5= string.length;
    89   result6 = Math.min(Math.max(position, 0), result5);
    90   result7 = search.length;
    92   if (result7 == 0) {
    93     return Math.min(position, result5);
    94   }
    96   result8 = -1;
    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   }
   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   }
   124   var sign = ( n < 0 ) ? -1 : 1;
   126   return ( sign * Math.floor(Math.abs(n)) );
   127 }

mercurial