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

Side by Side Diff: runtime/lib/integers_patch.dart

Issue 15670009: Check how many Smi digits are acceptable when parsing integers. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 6 months 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 | « no previous file | 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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 // Dart core library. 4 // Dart core library.
5 5
6 // VM implementation of int. 6 // VM implementation of int.
7 7
8 patch class int { 8 patch class int {
9 9
10 static bool _isWhitespace(int codePoint) { 10 static bool _isWhitespace(int codePoint) {
11 return 11 return
12 (codePoint == 32) || // Space. 12 (codePoint == 32) || // Space.
13 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. 13 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc.
14 } 14 }
15 15
16 static bool is64Bit() => 1 << 32 is _Smi;
17
16 static int _tryParseSmi(String str) { 18 static int _tryParseSmi(String str) {
17 if (str.isEmpty) return null; 19 if (str.isEmpty) return null;
18 var ix = 0; 20 var ix = 0;
19 var endIx = str.length - 1; 21 var endIx = str.length - 1;
20 // Find first and last non-whitespace. 22 // Find first and last non-whitespace.
21 while (ix <= endIx) { 23 while (ix <= endIx) {
22 if (!_isWhitespace(str.codeUnitAt(ix))) break; 24 if (!_isWhitespace(str.codeUnitAt(ix))) break;
23 ix++; 25 ix++;
24 } 26 }
25 if (endIx < ix) { 27 if (endIx < ix) {
26 return null; // Empty. 28 return null; // Empty.
27 } 29 }
28 while (endIx > ix) { 30 while (endIx > ix) {
29 if (!_isWhitespace(str.codeUnitAt(endIx))) break; 31 if (!_isWhitespace(str.codeUnitAt(endIx))) break;
30 endIx--; 32 endIx--;
31 } 33 }
32 34
33 var isNegative = false; 35 var isNegative = false;
34 var c = str.codeUnitAt(ix); 36 var c = str.codeUnitAt(ix);
35 // Check for leading '+' or '-'. 37 // Check for leading '+' or '-'.
36 if ((c == 0x2b) || (c == 0x2d)) { 38 if ((c == 0x2b) || (c == 0x2d)) {
37 ix++; 39 ix++;
38 isNegative = (c == 0x2d); 40 isNegative = (c == 0x2d);
39 if (ix > endIx) { 41 if (ix > endIx) {
40 return null; // Empty. 42 return null; // Empty.
41 } 43 }
42 } 44 }
43 if ((endIx - ix) >= 9) { 45 int smiLimit = is64Bit() ? 18 : 9;
46 if ((endIx - ix) >= smiLimit) {
44 return null; // May not fit into a Smi. 47 return null; // May not fit into a Smi.
45 } 48 }
46 var result = 0; 49 var result = 0;
47 for (int i = ix; i <= endIx; i++) { 50 for (int i = ix; i <= endIx; i++) {
48 var c = str.codeUnitAt(i) - 0x30; 51 var c = str.codeUnitAt(i) - 0x30;
49 if ((c > 9) || (c < 0)) { 52 if ((c > 9) || (c < 0)) {
50 return null; 53 return null;
51 } 54 }
52 result = result * 10 + c; 55 result = result * 10 + c;
53 } 56 }
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 int digit = digits[code - 0x30]; 126 int digit = digits[code - 0x30];
124 if (digit >= radix) return onError(source); 127 if (digit >= radix) return onError(source);
125 result = result * radix + digit; 128 result = result * radix + digit;
126 i++; 129 i++;
127 if (i == source.length) break; 130 if (i == source.length) break;
128 code = source.codeUnitAt(i); 131 code = source.codeUnitAt(i);
129 } while (true); 132 } while (true);
130 return negative ? -result : result; 133 return negative ? -result : result;
131 } 134 }
132 } 135 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698