Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 // This file relies on the fact that the following declaration has been made | 7 // This file relies on the fact that the following declaration has been made |
| 8 // in runtime.js: | 8 // in runtime.js: |
| 9 // var $String = global.String; | 9 // var $String = global.String; |
| 10 // var $Array = global.Array; | 10 // var $Array = global.Array; |
| 11 | 11 |
| 12 // ------------------------------------------------------------------- | 12 // ------------------------------------------------------------------- |
| 13 | 13 |
| 14 // ES6 draft 01-20-14, section 21.1.3.13 | 14 // ES6 draft 01-20-14, section 21.1.3.13 |
| 15 function StringRepeat(count) { | 15 function StringRepeat(count) { |
| 16 CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat"); | 16 CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat"); |
| 17 | 17 |
| 18 var s = TO_STRING_INLINE(this); | 18 var s = TO_STRING_INLINE(this); |
| 19 var n = ToInteger(count); | 19 var n = ToInteger(count); |
| 20 if (n < 0 || !NUMBER_IS_FINITE(n)) { | 20 if (n < 0 || !NUMBER_IS_FINITE(n)) { |
| 21 throw MakeRangeError("invalid_count_value", []); | 21 throw MakeRangeError("invalid_count_value", []); |
| 22 } | 22 } |
| 23 | 23 |
| 24 var elements = new InternalArray(n); | 24 // O(log n) algorithm |
| 25 for (var i = 0; i < n; i++) { | 25 |
| 26 elements[i] = s; | 26 if (n < 1) return ""; |
| 27 | |
| 28 var res = ""; | |
| 29 | |
| 30 while (n > 1) { | |
| 31 if (n % 2) res += s; | |
|
Yang
2014/08/19 12:29:00
two-character indent please.
| |
| 32 n /= 2; | |
| 33 s += s; | |
| 27 } | 34 } |
| 28 | 35 |
| 29 return %StringBuilderConcat(elements, n, ""); | 36 // unroll last iteration, no need to double the initial string again at the |
|
Yang
2014/08/19 12:29:00
Capitalize "unroll".
| |
| 37 // final iteration. | |
| 38 | |
| 39 // This flattens the string. It is a small overhead to significantly improve | |
| 40 // string handling | |
| 41 var array = new InternalArray(1); | |
| 42 array[0] = res + s; | |
| 43 return %StringBuilderConcat(array, 1, ""); | |
|
Yang
2014/08/19 12:29:00
Use %FlattenString
| |
| 30 } | 44 } |
| 31 | 45 |
| 32 | 46 |
| 33 // ES6 draft 04-05-14, section 21.1.3.18 | 47 // ES6 draft 04-05-14, section 21.1.3.18 |
| 34 function StringStartsWith(searchString /* position */) { // length == 1 | 48 function StringStartsWith(searchString /* position */) { // length == 1 |
| 35 CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith"); | 49 CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith"); |
| 36 | 50 |
| 37 var s = TO_STRING_INLINE(this); | 51 var s = TO_STRING_INLINE(this); |
| 38 | 52 |
| 39 if (IS_REGEXP(searchString)) { | 53 if (IS_REGEXP(searchString)) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 var s_len = s.length; | 88 var s_len = s.length; |
| 75 var pos = s_len; | 89 var pos = s_len; |
| 76 if (%_ArgumentsLength() > 1) { | 90 if (%_ArgumentsLength() > 1) { |
| 77 var arg = %_Arguments(1); // position | 91 var arg = %_Arguments(1); // position |
| 78 if (!IS_UNDEFINED(arg)) { | 92 if (!IS_UNDEFINED(arg)) { |
| 79 pos = ToInteger(arg); | 93 pos = ToInteger(arg); |
| 80 } | 94 } |
| 81 } | 95 } |
| 82 | 96 |
| 83 var end = MathMin(MathMax(pos, 0), s_len); | 97 var end = MathMin(MathMax(pos, 0), s_len); |
| 84 var ss_len = ss.length; | 98 var start = end - ss.length; |
| 85 var start = end - ss_len; | |
| 86 if (start < 0) { | 99 if (start < 0) { |
| 87 return false; | 100 return false; |
| 88 } | 101 } |
| 89 | 102 |
| 90 return %StringLastIndexOf(s, ss, start) === start; | 103 return %StringLastIndexOf(s, ss, start) === start; |
| 91 } | 104 } |
| 92 | 105 |
| 93 | 106 |
| 94 // ES6 draft 04-05-14, section 21.1.3.6 | 107 // ES6 draft 04-05-14, section 21.1.3.6 |
| 95 function StringContains(searchString /* position */) { // length == 1 | 108 function StringContains(searchString /* position */) { // length == 1 |
| 96 CHECK_OBJECT_COERCIBLE(this, "String.prototype.contains"); | 109 CHECK_OBJECT_COERCIBLE(this, "String.prototype.contains"); |
| 97 | 110 |
| 98 var s = TO_STRING_INLINE(this); | 111 var s = TO_STRING_INLINE(this); |
| 99 | 112 |
| 100 if (IS_REGEXP(searchString)) { | 113 if (IS_REGEXP(searchString)) { |
| 101 throw MakeTypeError("first_argument_not_regexp", | 114 throw MakeTypeError("first_argument_not_regexp", |
| 102 ["String.prototype.contains"]); | 115 ["String.prototype.contains"]); |
| 103 } | 116 } |
| 104 | 117 |
| 105 var ss = TO_STRING_INLINE(searchString); | 118 var ss = TO_STRING_INLINE(searchString); |
| 106 var pos = 0; | 119 var pos = 0; |
| 107 if (%_ArgumentsLength() > 1) { | 120 if (%_ArgumentsLength() > 1) { |
| 108 pos = %_Arguments(1); // position | 121 pos = %_Arguments(1); // position |
| 109 pos = ToInteger(pos); | 122 pos = ToInteger(pos); |
| 110 } | 123 } |
| 111 | 124 |
| 112 var s_len = s.length; | 125 var s_len = s.length; |
| 113 var start = MathMin(MathMax(pos, 0), s_len); | 126 var start = MathMin(MathMax(pos, 0), s_len); |
| 114 var ss_len = ss.length; | 127 if (ss.length + start > s_len) { |
| 115 if (ss_len + start > s_len) { | |
| 116 return false; | 128 return false; |
| 117 } | 129 } |
| 118 | 130 |
| 119 return %StringIndexOf(s, ss, start) !== -1; | 131 return %StringIndexOf(s, ss, start) !== -1; |
| 120 } | 132 } |
| 121 | 133 |
| 122 | 134 |
| 123 // ES6 Draft 05-22-2014, section 21.1.3.3 | 135 // ES6 Draft 05-22-2014, section 21.1.3.3 |
| 124 function StringCodePointAt(pos) { | 136 function StringCodePointAt(pos) { |
| 125 CHECK_OBJECT_COERCIBLE(this, "String.prototype.codePointAt"); | 137 CHECK_OBJECT_COERCIBLE(this, "String.prototype.codePointAt"); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 InstallFunctions($String.prototype, DONT_ENUM, $Array( | 194 InstallFunctions($String.prototype, DONT_ENUM, $Array( |
| 183 "codePointAt", StringCodePointAt, | 195 "codePointAt", StringCodePointAt, |
| 184 "contains", StringContains, | 196 "contains", StringContains, |
| 185 "endsWith", StringEndsWith, | 197 "endsWith", StringEndsWith, |
| 186 "repeat", StringRepeat, | 198 "repeat", StringRepeat, |
| 187 "startsWith", StringStartsWith | 199 "startsWith", StringStartsWith |
| 188 )); | 200 )); |
| 189 } | 201 } |
| 190 | 202 |
| 191 ExtendStringPrototype(); | 203 ExtendStringPrototype(); |
| OLD | NEW |