| 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. | 5 // Generic utility functions. |
| 6 | 6 |
| 7 /** Invokes [callback] and returns how long it took to execute in ms. */ | |
| 8 num time(callback()) { | |
| 9 final watch = new Stopwatch(); | |
| 10 watch.start(); | |
| 11 callback(); | |
| 12 watch.stop(); | |
| 13 return watch.elapsedInMs(); | |
| 14 } | |
| 15 | |
| 16 /** Turns [name] into something that's safe to use as a file name. */ | 7 /** Turns [name] into something that's safe to use as a file name. */ |
| 17 String sanitize(String name) => name.replaceAll(':', '_').replaceAll('/', '_'); | 8 String sanitize(String name) => name.replaceAll(':', '_').replaceAll('/', '_'); |
| 18 | 9 |
| 19 /** Returns the number of times [search] occurs in [text]. */ | 10 /** Returns the number of times [search] occurs in [text]. */ |
| 20 int countOccurrences(String text, String search) { | 11 int countOccurrences(String text, String search) { |
| 21 int start = 0; | 12 int start = 0; |
| 22 int count = 0; | 13 int count = 0; |
| 23 | 14 |
| 24 while (true) { | 15 while (true) { |
| 25 start = text.indexOf(search, start); | 16 start = text.indexOf(search, start); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 /** | 61 /** |
| 71 * Joins [items] into a single, comma-separated string using [conjunction]. | 62 * Joins [items] into a single, comma-separated string using [conjunction]. |
| 72 * E.g. `['A', 'B', 'C']` becomes `"A, B, and C"`. | 63 * E.g. `['A', 'B', 'C']` becomes `"A, B, and C"`. |
| 73 */ | 64 */ |
| 74 String joinWithCommas(List<String> items, [String conjunction = 'and']) { | 65 String joinWithCommas(List<String> items, [String conjunction = 'and']) { |
| 75 if (items.length == 1) return items[0]; | 66 if (items.length == 1) return items[0]; |
| 76 if (items.length == 2) return "${items[0]} $conjunction ${items[1]}"; | 67 if (items.length == 2) return "${items[0]} $conjunction ${items[1]}"; |
| 77 return Strings.join(items.getRange(0, items.length - 1), ', ') + | 68 return Strings.join(items.getRange(0, items.length - 1), ', ') + |
| 78 ', $conjunction ' + items[items.length - 1]; | 69 ', $conjunction ' + items[items.length - 1]; |
| 79 } | 70 } |
| OLD | NEW |