Files
sdk/runtime/lib/double_patch.dart
T
lrn@google.com e1b4afa85d Add num.parse.
The implementation is still primitive: It calls int.parse, then double.parse, and then fails if it hasn't found a result yet.

BUG= http://dartbug.com/8237
R=floitsch@google.com

Committed: https://code.google.com/p/dart/source/detail?r=30624

Review URL: https://codereview.chromium.org//85633003

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@30663 260f80e4-7a28-3924-810f-c04153c831b5
2013-11-26 08:44:38 +00:00

53 lines
1.5 KiB
Dart

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Dart core library.
// VM implementation of double.
patch class double {
static double _native_parse(_OneByteString string) native "Double_parse";
static double _parse(var str) {
str = str.trim();
if (str.length == 0) return null;
final ccid = str._cid;
_OneByteString oneByteString;
// TODO(floitsch): Allow _ExternalOneByteStrings. As of May 2013 they don't
// have any _classId.
if (ccid != _OneByteString._classId) {
int length = str.length;
var s = _OneByteString._allocate(length);
for (int i = 0; i < length; i++) {
int currentUnit = str.codeUnitAt(i);
// All valid trimmed double strings must be ASCII.
if (currentUnit < 128) {
s._setAt(i, currentUnit);
} else {
return null;
}
}
oneByteString = s;
} else {
oneByteString = str;
}
return _native_parse(oneByteString);
}
/* patch */ static double parse(String str,
[double onError(String str)]) {
var result = _parse(str);
if (result == null) {
if (onError == null) throw new FormatException(str);
return onError(str);
}
// Parse can create a NaN that is not identical to double.NAN.
if (result.isNaN) return NAN;
return result;
}
}