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

Side by Side Diff: pkg/fixnum/lib/src/int32.dart

Issue 23441004: More efficient Int64 parsing and printing. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: addres code review comments Created 7 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 | pkg/fixnum/lib/src/int64.dart » ('j') | pkg/fixnum/lib/src/int64.dart » ('J')
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 4
5 part of fixnum; 5 part of fixnum;
6 6
7 /** 7 /**
8 * An immutable 32-bit signed integer, in the range [-2^31, 2^31 - 1]. 8 * An immutable 32-bit signed integer, in the range [-2^31, 2^31 - 1].
9 * Arithmetic operations may overflow in order to maintain this range. 9 * Arithmetic operations may overflow in order to maintain this range.
10 */ 10 */
(...skipping 27 matching lines...) Expand all
38 static const Int32 TWO = const Int32._internal(2); 38 static const Int32 TWO = const Int32._internal(2);
39 39
40 // Hex digit char codes 40 // Hex digit char codes
41 static const int _CC_0 = 48; // '0'.codeUnitAt(0) 41 static const int _CC_0 = 48; // '0'.codeUnitAt(0)
42 static const int _CC_9 = 57; // '9'.codeUnitAt(0) 42 static const int _CC_9 = 57; // '9'.codeUnitAt(0)
43 static const int _CC_a = 97; // 'a'.codeUnitAt(0) 43 static const int _CC_a = 97; // 'a'.codeUnitAt(0)
44 static const int _CC_z = 122; // 'z'.codeUnitAt(0) 44 static const int _CC_z = 122; // 'z'.codeUnitAt(0)
45 static const int _CC_A = 65; // 'A'.codeUnitAt(0) 45 static const int _CC_A = 65; // 'A'.codeUnitAt(0)
46 static const int _CC_Z = 90; // 'Z'.codeUnitAt(0) 46 static const int _CC_Z = 90; // 'Z'.codeUnitAt(0)
47 47
48 static int _decodeHex(int c) { 48 static int _decodeDigit(int c) {
49 if (c >= _CC_0 && c <= _CC_9) { 49 if (c >= _CC_0 && c <= _CC_9) {
50 return c - _CC_0; 50 return c - _CC_0;
51 } else if (c >= _CC_a && c <= _CC_z) { 51 } else if (c >= _CC_a && c <= _CC_z) {
52 return c - _CC_a + 10; 52 return c - _CC_a + 10;
53 } else if (c >= _CC_A && c <= _CC_Z) { 53 } else if (c >= _CC_A && c <= _CC_Z) {
54 return c - _CC_A + 10; 54 return c - _CC_A + 10;
55 } else { 55 } else {
56 return -1; // bad char code 56 return -1; // bad char code
57 } 57 }
58 } 58 }
59 59
60 /** 60 /**
61 * Parses a [String] in a given [radix] between 2 and 16 and returns an 61 * Parses a [String] in a given [radix] between 2 and 16 and returns an
62 * [Int32]. 62 * [Int32].
63 */ 63 */
64 // TODO(rice) - Make this faster by converting several digits at once. 64 // TODO(rice) - Make this faster by converting several digits at once.
65 static Int32 parseRadix(String s, int radix) { 65 static Int32 parseRadix(String s, int radix) {
66 if ((radix <= 1) || (radix > 16)) { 66 if ((radix <= 1) || (radix > 16)) {
67 throw new ArgumentError("Bad radix: $radix"); 67 throw new ArgumentError("Bad radix: $radix");
68 } 68 }
69 Int32 x = ZERO; 69 Int32 x = ZERO;
70 for (int i = 0; i < s.length; i++) { 70 for (int i = 0; i < s.length; i++) {
71 int c = s.codeUnitAt(i); 71 int c = s.codeUnitAt(i);
72 int digit = _decodeHex(c); 72 int digit = _decodeDigit(c);
73 if (digit < 0 || digit >= radix) { 73 if (digit < 0 || digit >= radix) {
74 throw new Exception("Non-radix code unit: $c"); 74 throw new Exception("Non-radix code unit: $c");
75 } 75 }
76 x = (x * radix) + digit; 76 x = (x * radix) + digit;
77 } 77 }
78 return x; 78 return x;
79 } 79 }
80 80
81 /** 81 /**
82 * Parses a decimal [String] and returns an [Int32]. 82 * Parses a decimal [String] and returns an [Int32].
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 } 343 }
344 344
345 int toInt() => _i; 345 int toInt() => _i;
346 Int32 toInt32() => this; 346 Int32 toInt32() => this;
347 Int64 toInt64() => new Int64.fromInt(_i); 347 Int64 toInt64() => new Int64.fromInt(_i);
348 348
349 String toString() => _i.toString(); 349 String toString() => _i.toString();
350 String toHexString() => _i.toRadixString(16); 350 String toHexString() => _i.toRadixString(16);
351 String toRadixString(int radix) => _i.toRadixString(radix); 351 String toRadixString(int radix) => _i.toRadixString(radix);
352 } 352 }
OLDNEW
« no previous file with comments | « no previous file | pkg/fixnum/lib/src/int64.dart » ('j') | pkg/fixnum/lib/src/int64.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698