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

Unified Diff: sdk/lib/core/errors.dart

Issue 745573002: Create generic check methods for RangeError causing checks. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix long line Created 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/convert/utf.dart ('k') | sdk/lib/core/iterable.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/core/errors.dart
diff --git a/sdk/lib/core/errors.dart b/sdk/lib/core/errors.dart
index b8a3157afe87cc397756b05ad7488c3e2f09543b..0ac18808e643191f51e96665a57656df8ad4e2bf 100644
--- a/sdk/lib/core/errors.dart
+++ b/sdk/lib/core/errors.dart
@@ -218,8 +218,9 @@ class RangeError extends ArgumentError {
/**
* Create a new [RangeError] with for an invalid value being outside a range.
*
- * The allowed range is from [start] to [end], inclusive.
- * If `start` or `end` are `null`, the range is infinite in that direction.
+ * The allowed range is from [minValue] to [maxValue], inclusive.
+ * If `minValue` or `maxValue` are `null`, the range is infinite in
+ * that direction.
*
* For a range from 0 to the length of something, end exclusive, use
* [RangeError.index].
@@ -228,9 +229,11 @@ class RangeError extends ArgumentError {
* invalid value, and the [message] can override the default error
* description.
*/
- RangeError.range(num invalidValue, this.start, this.end,
+ RangeError.range(num invalidValue, int minValue, int maxValue,
[String name, String message])
- : super.value(invalidValue, name,
+ : start = minValue,
+ end = maxValue,
+ super.value(invalidValue, name,
(message != null) ? message : "Invalid value");
/**
@@ -249,6 +252,74 @@ class RangeError extends ArgumentError {
String message,
int length]) = IndexError;
+ /**
+ * Check that a [value] lies in a specific interval.
+ *
+ * Throws if [value] is not in the interval.
+ * The interval is from [minValue] to [maxValue], both inclusive.
+ */
+ static void checkValueInInterval(int value, int minValue, int maxValue,
+ [String name, String message]) {
+ if (value < minValue || value > maxValue) {
+ throw new RangeError.range(value, minValue, maxValue, name, message);
+ }
+ }
+
+ /**
+ * Check that a value is a valid index into an indexable object.
+ *
+ * Throws if [index] is not a valid index into [indexable].
+ *
+ * An indexable object is one that has a `length` and a and index-operator
+ * `[]` that accepts an index if `0 <= index < length`.
+ *
+ * If [length] is provided, it is used as the length of the indexable object,
+ * otherwise the length is found as `idexable.length`.
+ */
+ static void checkValidIndex(int index, var indexable,
+ [String name, int length, String message]) {
+ if (length == null) length = indexable.length;
+ if (index < 0 || index >= length) {
+ if (name == null) name = "index";
+ throw new RangeError.index(index, indexable, name, message, length);
+ }
+ }
+
+ /**
+ * Check that a range represents a slice of an indexable object.
+ *
+ * Throws if the range is not valid for an indexable object with
+ * the given [length].
+ * A range is valid for an indexable object with a given [length]
+ *
+ * if `0 <= [start] <= [end] <= [length]`.
+ * An `end` of `null` is considered equivalent to `length`.
+ *
+ * The [startName] and [endName] defaults to `"start"` and `"end"`,
+ * respectively.
+ */
+ static void checkValidRange(int start, int end, int length,
+ [String startName, String endName,
+ String message]) {
+ if (start < 0 || start > length) {
+ if (startName == null) startName = "start";
+ throw new RangeError.range(start, 0, length, startName, message);
+ }
+ if (end != null && (end < start || end > length)) {
+ if (endName == null) endName = "end";
+ throw new RangeError.range(end, start, length, endName, message);
+ }
+ }
+
+ /**
+ * Check that an integer value isn't negative.
+ *
+ * Throws if the value is negative.
+ */
+ static void checkNotNegative(int value, [String name, String message]) {
+ if (value < 0) throw new RangeError.range(value, 0, null, name, message);
+ }
+
String toString() {
if (!_hasValue) return "RangeError: $message";
String value = Error.safeToString(invalidValue);
« no previous file with comments | « sdk/lib/convert/utf.dart ('k') | sdk/lib/core/iterable.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698