Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | File Name: 15.5.4.8-3.js |
michael@0 | 9 | ECMA Section: 15.5.4.8 String.prototype.split( separator ) |
michael@0 | 10 | Description: |
michael@0 | 11 | |
michael@0 | 12 | Returns an Array object into which substrings of the result of converting |
michael@0 | 13 | this object to a string have been stored. The substrings are determined by |
michael@0 | 14 | searching from left to right for occurrences of the given separator; these |
michael@0 | 15 | occurrences are not part of any substring in the returned array, but serve |
michael@0 | 16 | to divide up this string value. The separator may be a string of any length. |
michael@0 | 17 | |
michael@0 | 18 | As a special case, if the separator is the empty string, the string is split |
michael@0 | 19 | up into individual characters; the length of the result array equals the |
michael@0 | 20 | length of the string, and each substring contains one character. |
michael@0 | 21 | |
michael@0 | 22 | If the separator is not supplied, then the result array contains just one |
michael@0 | 23 | string, which is the string. |
michael@0 | 24 | |
michael@0 | 25 | When the split method is called with one argument separator, the following steps are taken: |
michael@0 | 26 | |
michael@0 | 27 | 1. Call ToString, giving it the this value as its argument. |
michael@0 | 28 | 2. Create a new Array object of length 0 and call it A. |
michael@0 | 29 | 3. If separator is not supplied, call the [[Put]] method of A with 0 and |
michael@0 | 30 | Result(1) as arguments, and then return A. |
michael@0 | 31 | 4. Call ToString(separator). |
michael@0 | 32 | 5. Compute the number of characters in Result(1). |
michael@0 | 33 | 6. Compute the number of characters in the string that is Result(4). |
michael@0 | 34 | 7. Let p be 0. |
michael@0 | 35 | 8. If Result(6) is zero (the separator string is empty), go to step 17. |
michael@0 | 36 | 9. Compute the smallest possible integer k not smaller than p such that |
michael@0 | 37 | k+Result(6) is not greater than Result(5), and for all nonnegative |
michael@0 | 38 | integers j less than Result(6), the character at position k+j of |
michael@0 | 39 | Result(1) is the same as the character at position j of Result(2); |
michael@0 | 40 | but if there is no such integer k, then go to step 14. |
michael@0 | 41 | 10. Compute a string value equal to the substring of Result(1), consisting |
michael@0 | 42 | of the characters at positions p through k1, inclusive. |
michael@0 | 43 | 11. Call the [[Put]] method of A with A.length and Result(10) as arguments. |
michael@0 | 44 | 12. Let p be k+Result(6). |
michael@0 | 45 | 13. Go to step 9. |
michael@0 | 46 | 14. Compute a string value equal to the substring of Result(1), consisting |
michael@0 | 47 | of the characters from position p to the end of Result(1). |
michael@0 | 48 | 15. Call the [[Put]] method of A with A.length and Result(14) as arguments. |
michael@0 | 49 | 16. Return A. |
michael@0 | 50 | 17. If p equals Result(5), return A. |
michael@0 | 51 | 18. Compute a string value equal to the substring of Result(1), consisting of |
michael@0 | 52 | the single character at position p. |
michael@0 | 53 | 19. Call the [[Put]] method of A with A.length and Result(18) as arguments. |
michael@0 | 54 | 20. Increase p by 1. |
michael@0 | 55 | 21. Go to step 17. |
michael@0 | 56 | |
michael@0 | 57 | Note that the split function is intentionally generic; it does not require that its this value be a String |
michael@0 | 58 | object. Therefore it can be transferred to other kinds of objects for use as a method. |
michael@0 | 59 | |
michael@0 | 60 | Author: christine@netscape.com |
michael@0 | 61 | Date: 12 november 1997 |
michael@0 | 62 | */ |
michael@0 | 63 | |
michael@0 | 64 | var SECTION = "15.5.4.8-3"; |
michael@0 | 65 | var VERSION = "ECMA_1"; |
michael@0 | 66 | startTest(); |
michael@0 | 67 | var TITLE = "String.prototype.split"; |
michael@0 | 68 | |
michael@0 | 69 | writeHeaderToLog( SECTION + " "+ TITLE); |
michael@0 | 70 | |
michael@0 | 71 | var TEST_STRING = ""; |
michael@0 | 72 | var EXPECT = new Array(); |
michael@0 | 73 | |
michael@0 | 74 | // this.toString is the empty string. |
michael@0 | 75 | |
michael@0 | 76 | new TestCase( SECTION, |
michael@0 | 77 | "var s = new String(); s.split().length", |
michael@0 | 78 | 1, |
michael@0 | 79 | eval("var s = new String(); s.split().length") ); |
michael@0 | 80 | |
michael@0 | 81 | new TestCase( SECTION, |
michael@0 | 82 | "var s = new String(); s.split()[0]", |
michael@0 | 83 | "", |
michael@0 | 84 | eval("var s = new String(); s.split()[0]") ); |
michael@0 | 85 | |
michael@0 | 86 | // this.toString() is the empty string, separator is specified. |
michael@0 | 87 | |
michael@0 | 88 | new TestCase( SECTION, |
michael@0 | 89 | "var s = new String(); s.split('').length", |
michael@0 | 90 | 0, |
michael@0 | 91 | eval("var s = new String(); s.split('').length") ); |
michael@0 | 92 | |
michael@0 | 93 | new TestCase( SECTION, |
michael@0 | 94 | "var s = new String(); s.split(' ').length", |
michael@0 | 95 | 1, |
michael@0 | 96 | eval("var s = new String(); s.split(' ').length") ); |
michael@0 | 97 | |
michael@0 | 98 | // this to string is " " |
michael@0 | 99 | new TestCase( SECTION, |
michael@0 | 100 | "var s = new String(' '); s.split().length", |
michael@0 | 101 | 1, |
michael@0 | 102 | eval("var s = new String(' '); s.split().length") ); |
michael@0 | 103 | |
michael@0 | 104 | new TestCase( SECTION, |
michael@0 | 105 | "var s = new String(' '); s.split()[0]", |
michael@0 | 106 | " ", |
michael@0 | 107 | eval("var s = new String(' '); s.split()[0]") ); |
michael@0 | 108 | |
michael@0 | 109 | new TestCase( SECTION, |
michael@0 | 110 | "var s = new String(' '); s.split('').length", |
michael@0 | 111 | 1, |
michael@0 | 112 | eval("var s = new String(' '); s.split('').length") ); |
michael@0 | 113 | |
michael@0 | 114 | new TestCase( SECTION, |
michael@0 | 115 | "var s = new String(' '); s.split('')[0]", |
michael@0 | 116 | " ", |
michael@0 | 117 | eval("var s = new String(' '); s.split('')[0]") ); |
michael@0 | 118 | |
michael@0 | 119 | new TestCase( SECTION, |
michael@0 | 120 | "var s = new String(' '); s.split(' ').length", |
michael@0 | 121 | 2, |
michael@0 | 122 | eval("var s = new String(' '); s.split(' ').length") ); |
michael@0 | 123 | |
michael@0 | 124 | new TestCase( SECTION, |
michael@0 | 125 | "var s = new String(' '); s.split(' ')[0]", |
michael@0 | 126 | "", |
michael@0 | 127 | eval("var s = new String(' '); s.split(' ')[0]") ); |
michael@0 | 128 | |
michael@0 | 129 | new TestCase( SECTION, |
michael@0 | 130 | "\"\".split(\"\").length", |
michael@0 | 131 | 0, |
michael@0 | 132 | ("".split("")).length ); |
michael@0 | 133 | |
michael@0 | 134 | new TestCase( SECTION, |
michael@0 | 135 | "\"\".split(\"x\").length", |
michael@0 | 136 | 1, |
michael@0 | 137 | ("".split("x")).length ); |
michael@0 | 138 | |
michael@0 | 139 | new TestCase( SECTION, |
michael@0 | 140 | "\"\".split(\"x\")[0]", |
michael@0 | 141 | "", |
michael@0 | 142 | ("".split("x"))[0] ); |
michael@0 | 143 | |
michael@0 | 144 | test(); |
michael@0 | 145 | |
michael@0 | 146 | function Split( string, separator ) { |
michael@0 | 147 | string = String( string ); |
michael@0 | 148 | |
michael@0 | 149 | var A = new Array(); |
michael@0 | 150 | |
michael@0 | 151 | if ( arguments.length < 2 ) { |
michael@0 | 152 | A[0] = string; |
michael@0 | 153 | return A; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | separator = String( separator ); |
michael@0 | 157 | |
michael@0 | 158 | var str_len = String( string ).length; |
michael@0 | 159 | var sep_len = String( separator ).length; |
michael@0 | 160 | |
michael@0 | 161 | var p = 0; |
michael@0 | 162 | var k = 0; |
michael@0 | 163 | |
michael@0 | 164 | if ( sep_len == 0 ) { |
michael@0 | 165 | for ( ; p < str_len; p++ ) { |
michael@0 | 166 | A[A.length] = String( string.charAt(p) ); |
michael@0 | 167 | } |
michael@0 | 168 | } |
michael@0 | 169 | return A; |
michael@0 | 170 | } |