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

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

Issue 581593002: Add argument error for int.parse(null). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 3 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 is64Bit() => 1 << 32 is _Smi; 10 static bool is64Bit() => 1 << 32 is _Smi;
11 11
12 static int _tryParseSmi(String str, int first, int last) { 12 static int _tryParseSmi(String str, int first, int last) {
13 assert(first <= last); 13 assert(first <= last);
14 var ix = first; 14 var ix = first;
15 var sign = 1; 15 var sign = 1;
16 var c = str.codeUnitAt(ix); 16 var c = str.codeUnitAt(ix);
17 // Check for leading '+' or '-'. 17 // Check for leading '+' or '-'.
18 if ((c == 0x2b) || (c == 0x2d)) { 18 if ((c == 0x2b) || (c == 0x2d)) {
19 ix++; 19 ix++;
20 sign = 0x2c - c; // -1 for '-', +1 for '+'. 20 sign = 0x2c - c; // -1 for '-', +1 for '+'.
21 if (ix > last) { 21 if (ix > last) {
22 return null; // Empty. 22 return null; // Empty.
23 } 23 }
24 } 24 }
25 int smiLimit = is64Bit() ? 18 : 9; 25 var smiLimit = is64Bit() ? 18 : 9;
26 if ((last - ix) >= smiLimit) { 26 if ((last - ix) >= smiLimit) {
27 return null; // May not fit into a Smi. 27 return null; // May not fit into a Smi.
28 } 28 }
29 var result = 0; 29 var result = 0;
30 for (int i = ix; i <= last; i++) { 30 for (int i = ix; i <= last; i++) {
31 var c = str.codeUnitAt(i) - 0x30; 31 var c = 0x30 ^ str.codeUnitAt(i);
32 if ((c > 9) || (c < 0)) { 32 if (9 < c) {
33 return null; 33 return null;
34 } 34 }
35 result = result * 10 + c; 35 result = 10 * result + c;
36 } 36 }
37 return sign * result; 37 return sign * result;
38 } 38 }
39 39
40 static int _tryParseSmiWhitespace(String str) { 40 static int _tryParseSmiWhitespace(String str) {
41 int first = str._firstNonWhitespace(); 41 int first = str._firstNonWhitespace();
42 if (first < str.length) { 42 if (first < str.length) {
43 int last = str._lastNonWhitespace(); 43 int last = str._lastNonWhitespace();
44 int res = _tryParseSmi(str, first, last); 44 int res = _tryParseSmi(str, first, last);
45 if (res != null) return res; 45 if (res != null) return res;
46 } 46 }
47 return _native_parse(str); 47 return _native_parse(str);
48 } 48 }
49 49
50 static int _parse(String str) { 50 static int _parse(String str) {
51 int res = _tryParseSmi(str, 0, str.length - 1); 51 int res = _tryParseSmi(str, 0, str.length - 1);
52 if (res != null) return res; 52 if (res != null) return res;
53 return _tryParseSmiWhitespace(str); 53 return _tryParseSmiWhitespace(str);
54 } 54 }
55 55
56 static int _native_parse(String str) native "Integer_parse"; 56 static int _native_parse(String str) native "Integer_parse";
57 57
58 static int _throwFormatException(String source, int position) { 58 static int _throwFormatException(String source, int position) {
59 throw new FormatException("", source, position); 59 throw new FormatException("", source, position);
60 } 60 }
61 61
62 /* patch */ static int parse(String source, 62 /* patch */ static int parse(String source,
63 { int radix, 63 { int radix,
64 int onError(String str) }) { 64 int onError(String str) }) {
65 if (identical(source, null)) throw new ArgumentError(source);
srdjan 2014/09/17 15:13:53 Please explain why you are using identical instead
Lasse Reichstein Nielsen 2014/09/17 15:28:04 Ack, a silly attempt to see if it made any differe
65 if (radix == null) { 66 if (radix == null) {
66 int result; 67 int result;
67 if (source.isNotEmpty) result = _parse(source); 68 if (source.isNotEmpty) result = _parse(source);
68 if (result == null) { 69 if (result == null) {
69 if (onError == null) { 70 if (onError == null) {
70 throw new FormatException("", source); 71 throw new FormatException("", source);
71 } 72 }
72 return onError(source); 73 return onError(source);
73 } 74 }
74 return result; 75 return result;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 int digit = digits[code - 0x30]; 122 int digit = digits[code - 0x30];
122 if (digit >= radix) return onError(source); 123 if (digit >= radix) return onError(source);
123 result = result * radix + digit; 124 result = result * radix + digit;
124 i++; 125 i++;
125 if (i == end) break; 126 if (i == end) break;
126 code = source.codeUnitAt(i); 127 code = source.codeUnitAt(i);
127 } while (true); 128 } while (true);
128 return negative ? -result : result; 129 return negative ? -result : result;
129 } 130 }
130 } 131 }
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