Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 /// Generic utility functions. Stuff that should possibly be in core. | 5 /// Generic utility functions. Stuff that should possibly be in core. |
| 6 import 'dart:async'; | 6 import 'dart:async'; |
| 7 import "dart:convert"; | 7 import "dart:convert"; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:math' as math; | |
| 9 | 10 |
| 10 import "package:crypto/crypto.dart"; | 11 import "package:crypto/crypto.dart"; |
| 11 import 'package:path/path.dart' as path; | 12 import 'package:path/path.dart' as path; |
| 12 import "package:stack_trace/stack_trace.dart"; | 13 import "package:stack_trace/stack_trace.dart"; |
| 13 | 14 |
| 14 import 'exceptions.dart'; | 15 import 'exceptions.dart'; |
| 15 import 'io.dart'; | 16 import 'io.dart'; |
| 16 import 'log.dart' as log; | 17 import 'log.dart' as log; |
| 17 | 18 |
| 18 export 'asset/dart/utils.dart'; | 19 export 'asset/dart/utils.dart'; |
| 19 | 20 |
| 20 /// A regular expression matching a Dart identifier. | 21 /// A regular expression matching a Dart identifier. |
| 21 /// | 22 /// |
| 22 /// This also matches a package name, since they must be Dart identifiers. | 23 /// This also matches a package name, since they must be Dart identifiers. |
| 23 final identifierRegExp = new RegExp(r"[a-zA-Z_]\w*"); | 24 final identifierRegExp = new RegExp(r"[a-zA-Z_]\w*"); |
| 24 | 25 |
| 25 /// Like [identifierRegExp], but anchored so that it only matches strings that | 26 /// Like [identifierRegExp], but anchored so that it only matches strings that |
| 26 /// are *just* Dart identifiers. | 27 /// are *just* Dart identifiers. |
| 27 final onlyIdentifierRegExp = new RegExp("^${identifierRegExp.pattern}\$"); | 28 final onlyIdentifierRegExp = new RegExp("^${identifierRegExp.pattern}\$"); |
| 28 | 29 |
| 29 /// Dart reserved words, from the Dart spec. | 30 /// Dart reserved words, from the Dart spec. |
| 30 const reservedWords = const [ | 31 const reservedWords = const [ |
| 31 "assert", "break", "case", "catch", "class", "const", "continue", "default", | 32 "assert", "break", "case", "catch", "class", "const", "continue", "default", |
| 32 "do", "else", "extends", "false", "final", "finally", "for", "if", "in", "is", | 33 "do", "else", "extends", "false", "final", "finally", "for", "if", "in", "is", |
| 33 "new", "null", "return", "super", "switch", "this", "throw", "true", "try", | 34 "new", "null", "return", "super", "switch", "this", "throw", "true", "try", |
| 34 "var", "void", "while", "with" | 35 "var", "void", "while", "with" |
| 35 ]; | 36 ]; |
| 36 | 37 |
| 38 /// An cryptographically secure instance of [math.Random]. | |
| 39 final random = new math.Random.secure(); | |
| 40 | |
| 37 /// A pair of values. | 41 /// A pair of values. |
| 38 class Pair<E, F> { | 42 class Pair<E, F> { |
| 39 E first; | 43 E first; |
| 40 F last; | 44 F last; |
| 41 | 45 |
| 42 Pair(this.first, this.last); | 46 Pair(this.first, this.last); |
| 43 | 47 |
| 44 String toString() => '($first, $last)'; | 48 String toString() => '($first, $last)'; |
| 45 | 49 |
| 46 bool operator==(other) { | 50 bool operator==(other) { |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 helper(element); | 284 helper(element); |
| 281 } else { | 285 } else { |
| 282 result.add(element); | 286 result.add(element); |
| 283 } | 287 } |
| 284 } | 288 } |
| 285 } | 289 } |
| 286 helper(nested); | 290 helper(nested); |
| 287 return result; | 291 return result; |
| 288 } | 292 } |
| 289 | 293 |
| 294 /// Randomly chooses a single element in [elements]. | |
| 295 choose(List elements) => elements[random.nextInt(elements.length)]; | |
|
Bob Nystrom
2016/04/01 20:35:27
Make this a generic method. :)
nweiz
2016/04/01 20:38:16
Done.
| |
| 296 | |
| 290 /// Returns a set containing all elements in [minuend] that are not in | 297 /// Returns a set containing all elements in [minuend] that are not in |
| 291 /// [subtrahend]. | 298 /// [subtrahend]. |
| 292 Set setMinus(Iterable minuend, Iterable subtrahend) { | 299 Set setMinus(Iterable minuend, Iterable subtrahend) { |
| 293 var minuendSet = new Set.from(minuend); | 300 var minuendSet = new Set.from(minuend); |
| 294 minuendSet.removeAll(subtrahend); | 301 minuendSet.removeAll(subtrahend); |
| 295 return minuendSet; | 302 return minuendSet; |
| 296 } | 303 } |
| 297 | 304 |
| 298 /// Returns whether there's any overlap between [set1] and [set2]. | 305 /// Returns whether there's any overlap between [set1] and [set2]. |
| 299 bool overlaps(Set set1, Set set2) { | 306 bool overlaps(Set set1, Set set2) { |
| (...skipping 640 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 940 } else { | 947 } else { |
| 941 throw new ApplicationException(message); | 948 throw new ApplicationException(message); |
| 942 } | 949 } |
| 943 } | 950 } |
| 944 | 951 |
| 945 /// Throw a [DataException] with [message] to indicate that the command has | 952 /// Throw a [DataException] with [message] to indicate that the command has |
| 946 /// failed because of invalid input data. | 953 /// failed because of invalid input data. |
| 947 /// | 954 /// |
| 948 /// This will report the error and cause pub to exit with [exit_codes.DATA]. | 955 /// This will report the error and cause pub to exit with [exit_codes.DATA]. |
| 949 void dataError(String message) => throw new DataException(message); | 956 void dataError(String message) => throw new DataException(message); |
| OLD | NEW |