Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / v8 / src / harmony-string.js
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 'use strict';
29
30 // This file relies on the fact that the following declaration has been made
31 // in runtime.js:
32 // var $String = global.String;
33 // var $Array = global.Array;
34
35 // -------------------------------------------------------------------
36
37 // ES6 draft 01-20-14, section 21.1.3.13
38 function StringRepeat(count) {
39   CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat");
40
41   var s = TO_STRING_INLINE(this);
42   var n = ToInteger(count);
43   if (n < 0 || !NUMBER_IS_FINITE(n)) {
44     throw MakeRangeError("invalid_count_value", []);
45   }
46
47   var elements = new InternalArray(n);
48   for (var i = 0; i < n; i++) {
49     elements[i] = s;
50   }
51
52   return %StringBuilderConcat(elements, n, "");
53 }
54
55
56 // ES6 draft 01-20-14, section 21.1.3.18
57 function StringStartsWith(searchString /* position */) {  // length == 1
58   CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith");
59
60   var s = TO_STRING_INLINE(this);
61
62   if (IS_REGEXP(searchString)) {
63     throw MakeTypeError("first_argument_not_regexp",
64                         ["String.prototype.startsWith"]);
65   }
66
67   var ss = TO_STRING_INLINE(searchString);
68   var pos = 0;
69   if (%_ArgumentsLength() > 1) {
70     pos = %_Arguments(1);  // position
71     pos = ToInteger(pos);
72   }
73
74   var s_len = s.length;
75   var start = MathMin(MathMax(pos, 0), s_len);
76   var ss_len = ss.length;
77   if (ss_len + start > s_len) {
78     return false;
79   }
80
81   return %StringIndexOf(s, ss, start) === start;
82 }
83
84
85 // ES6 draft 01-20-14, section 21.1.3.7
86 function StringEndsWith(searchString /* position */) {  // length == 1
87   CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith");
88
89   var s = TO_STRING_INLINE(this);
90
91   if (IS_REGEXP(searchString)) {
92     throw MakeTypeError("first_argument_not_regexp",
93                         ["String.prototype.endsWith"]);
94   }
95
96   var ss = TO_STRING_INLINE(searchString);
97   var s_len = s.length;
98   var pos = s_len;
99   if (%_ArgumentsLength() > 1) {
100     var arg = %_Arguments(1);  // position
101     if (!IS_UNDEFINED(arg)) {
102       pos = ToInteger(arg);
103     }
104   }
105
106   var end = MathMin(MathMax(pos, 0), s_len);
107   var ss_len = ss.length;
108   var start = end - ss_len;
109   if (start < 0) {
110     return false;
111   }
112
113   return %StringLastIndexOf(s, ss, start) === start;
114 }
115
116
117 // ES6 draft 01-20-14, section 21.1.3.6
118 function StringContains(searchString /* position */) {  // length == 1
119   CHECK_OBJECT_COERCIBLE(this, "String.prototype.contains");
120
121   var s = TO_STRING_INLINE(this);
122   var ss = TO_STRING_INLINE(searchString);
123   var pos = 0;
124   if (%_ArgumentsLength() > 1) {
125     pos = %_Arguments(1);  // position
126     pos = ToInteger(pos);
127   }
128
129   var s_len = s.length;
130   var start = MathMin(MathMax(pos, 0), s_len);
131   var ss_len = ss.length;
132   if (ss_len + start > s_len) {
133     return false;
134   }
135
136   return %StringIndexOf(s, ss, start) !== -1;
137 }
138
139
140 // -------------------------------------------------------------------
141
142 function ExtendStringPrototype() {
143   %CheckIsBootstrapping();
144
145   // Set up the non-enumerable functions on the String prototype object.
146   InstallFunctions($String.prototype, DONT_ENUM, $Array(
147     "repeat", StringRepeat,
148     "startsWith", StringStartsWith,
149     "endsWith", StringEndsWith,
150     "contains", StringContains
151   ));
152 }
153
154 ExtendStringPrototype();