Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: compiler/lib/implementation/string.js

Issue 8511023: Use strict when possible. Don't even try to cache the hashcode for strings. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « compiler/lib/implementation/number.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 function native_StringImplementation__indexOperator(index) { 5 function native_StringImplementation__indexOperator(index) {
6 "use strict";
6 return this[index]; 7 return this[index];
7 } 8 }
8 9
9 function native_StringImplementation__charCodeAt(index) { 10 function native_StringImplementation__charCodeAt(index) {
11 "use strict";
10 return this.charCodeAt(index); 12 return this.charCodeAt(index);
11 } 13 }
12 14
13 function native_StringImplementation_get$length() { 15 function native_StringImplementation_get$length() {
16 "use strict";
14 return this.length; 17 return this.length;
15 } 18 }
16 19
17 function native_StringImplementation_EQ(other) { 20 function native_StringImplementation_EQ(other) {
21 "use strict";
18 return typeof other == 'string' && this == other; 22 return typeof other == 'string' && this == other;
19 } 23 }
20 24
21 function native_StringImplementation__nativeIndexOf(other, startIndex) { 25 function native_StringImplementation__nativeIndexOf(other, startIndex) {
26 "use strict";
22 return this.indexOf(other, startIndex); 27 return this.indexOf(other, startIndex);
23 } 28 }
24 29
25 function native_StringImplementation__nativeLastIndexOf(other, fromIndex) { 30 function native_StringImplementation__nativeLastIndexOf(other, fromIndex) {
31 "use strict";
26 if (other == "") { 32 if (other == "") {
27 return Math.min(this.length, fromIndex); 33 return Math.min(this.length, fromIndex);
28 } 34 }
29 return this.lastIndexOf(other, fromIndex); 35 return this.lastIndexOf(other, fromIndex);
30 } 36 }
31 37
32 function native_StringImplementation_concat(other) { 38 function native_StringImplementation_concat(other) {
39 "use strict";
33 return this.concat(other); 40 return this.concat(other);
34 } 41 }
35 42
36 function native_StringImplementation__substringUnchecked(startIndex, endIndex) { 43 function native_StringImplementation__substringUnchecked(startIndex, endIndex) {
44 "use strict";
37 return this.substring(startIndex, endIndex); 45 return this.substring(startIndex, endIndex);
38 } 46 }
39 47
40 function native_StringImplementation_trim() { 48 function native_StringImplementation_trim() {
49 "use strict";
41 if (this.trim) return this.trim(); 50 if (this.trim) return this.trim();
42 return this.replace(new RegExp("^[\s]+|[\s]+$", "g"), ""); 51 return this.replace(new RegExp("^[\s]+|[\s]+$", "g"), "");
43 } 52 }
44 53
45 function native_StringImplementation__replace(from, to) { 54 function native_StringImplementation__replace(from, to) {
55 "use strict";
46 if ($isString(from)) { 56 if ($isString(from)) {
47 return this.replace(from, to); 57 return this.replace(from, to);
48 } else { 58 } else {
49 return this.replace($DartRegExpToJSRegExp(from), to); 59 return this.replace($DartRegExpToJSRegExp(from), to);
50 } 60 }
51 } 61 }
52 62
53 function native_StringImplementation__replaceAll(from, to) { 63 function native_StringImplementation__replaceAll(from, to) {
64 "use strict";
54 if ($isString(from)) { 65 if ($isString(from)) {
55 var regexp = new RegExp( 66 var regexp = new RegExp(
56 from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'g'); 67 from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'g');
57 return this.replace(regexp, to); 68 return this.replace(regexp, to);
58 } else { 69 } else {
59 var regexp = $DartRegExpToJSRegExp(from); 70 var regexp = $DartRegExpToJSRegExp(from);
60 return this.replace(regexp, to); 71 return this.replace(regexp, to);
61 } 72 }
62 } 73 }
63 74
64 function native_StringImplementation__split(pattern) { 75 function native_StringImplementation__split(pattern) {
76 "use strict";
65 if ($isString(pattern)) { 77 if ($isString(pattern)) {
66 return this.split(pattern); 78 return this.split(pattern);
67 } else { 79 } else {
68 return this.split($DartRegExpToJSRegExp(pattern)); 80 return this.split($DartRegExpToJSRegExp(pattern));
69 } 81 }
70 } 82 }
71 83
72 function native_StringImplementation_toLowerCase() { 84 function native_StringImplementation_toLowerCase() {
85 "use strict";
73 return this.toLowerCase(); 86 return this.toLowerCase();
74 } 87 }
75 88
76 function native_StringImplementation_toUpperCase() { 89 function native_StringImplementation_toUpperCase() {
90 "use strict";
77 return this.toUpperCase(); 91 return this.toUpperCase();
78 } 92 }
79 93
80 // Inherited from Hashable. 94 // Inherited from Hashable.
81 function native_StringImplementation_hashCode() { 95 function native_StringImplementation_hashCode() {
82 if (this.hash_ === undefined) { 96 "use strict";
sra1 2011/11/22 18:13:39 What is the benefit of "use strict" in this and th
floitsch 2011/11/23 09:21:54 "use strict" doesn't wrap the object. So (3).hashC
83 for (var i = 0; i < this.length; i++) { 97 var hash = 0;
84 var ch = this.charCodeAt(i); 98 for (var i = 0; i < this.length; i++) {
85 this.hash_ += ch; 99 var ch = this.charCodeAt(i);
86 this.hash_ += this.hash_ << 10; 100 hash += ch;
87 this.hash_ ^= this.hash_ >> 6; 101 hash += hash << 10;
sra1 2011/11/22 18:13:39 The result of += can be outside the 32-bit range o
floitsch 2011/11/23 09:21:54 Given that we can't store the hash we should rethi
88 } 102 hash ^= hash >> 6;
103 }
89 104
90 this.hash_ += this.hash_ << 3; 105 hash += hash << 3;
91 this.hash_ ^= this.hash_ >> 11; 106 hash ^= hash >> 11;
92 this.hash_ += this.hash_ << 15; 107 hash += hash << 15;
93 this.hash_ = this.hash_ & ((1 << 29) - 1); 108 hash = hash & ((1 << 29) - 1);
94 } 109
95 return this.hash_; 110 return hash;
96 } 111 }
97 112
98 function native_StringImplementation_toString() { 113 function native_StringImplementation_toString() {
114 "use strict";
99 // Return the primitive string of this String object. 115 // Return the primitive string of this String object.
100 return String(this); 116 return String(this);
101 } 117 }
102 118
103 // TODO(floitsch): If we allow comparison operators on the String class we 119 // TODO(floitsch): If we allow comparison operators on the String class we
104 // should move this function into dart world. 120 // should move this function into dart world.
105 function native_StringImplementation_compareTo(other) { 121 function native_StringImplementation_compareTo(other) {
122 "use strict";
106 if (this == other) return 0; 123 if (this == other) return 0;
107 if (this < other) return -1; 124 if (this < other) return -1;
108 return 1; 125 return 1;
109 } 126 }
110 127
111 function native_StringImplementation__newFromValues(array) { 128 function native_StringImplementation__newFromValues(array) {
129 "use strict";
112 if (!(array instanceof Array)) { 130 if (!(array instanceof Array)) {
113 var length = native__ListJsUtil__listLength(array); 131 var length = native__ListJsUtil__listLength(array);
114 var tmp = new Array(length); 132 var tmp = new Array(length);
115 for (var i = 0; i < length; i++) { 133 for (var i = 0; i < length; i++) {
116 tmp[i] = INDEX$operator(array, i); 134 tmp[i] = INDEX$operator(array, i);
117 } 135 }
118 array = tmp; 136 array = tmp;
119 } 137 }
120 return String.fromCharCode.apply(this, array); 138 return String.fromCharCode.apply(this, array);
121 } 139 }
122 140
123 // Deprecated old name of new String.fromValues(..). 141 // Deprecated old name of new String.fromValues(..).
124 function native_StringBase_createFromCharCodes(array) { 142 function native_StringBase_createFromCharCodes(array) {
125 return native_StringImplementation__newFromValues(array); 143 return native_StringImplementation__newFromValues(array);
126 } 144 }
OLDNEW
« no previous file with comments | « compiler/lib/implementation/number.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698