| 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 /** | 5 /** |
| 6 * Generic utility functions. Stuff that should possibly be in core. | 6 * Generic utility functions. Stuff that should possibly be in core. |
| 7 */ | 7 */ |
| 8 #library('utils'); | 8 #library('utils'); |
| 9 | 9 |
| 10 /** Thrown by methods that parse text when the text isn't a valid. */ | 10 /** Thrown by methods that parse text when the text isn't a valid. */ |
| 11 class FormatException implements Exception { | 11 class FormatException implements Exception { |
| 12 final String message; | 12 final String message; |
| 13 | 13 |
| 14 FormatException(this.message); | 14 FormatException(this.message); |
| 15 | 15 |
| 16 String toString() => message; | 16 String toString() => message; |
| 17 } | 17 } |
| 18 | 18 |
| 19 /** A pair of values. */ | |
| 20 class Pair<E, F> { | |
| 21 E first; | |
| 22 F last; | |
| 23 | |
| 24 Pair(this.first, this.last); | |
| 25 | |
| 26 String toString() => '($first, $last)'; | |
| 27 } | |
| 28 | |
| 29 // TODO(rnystrom): Move into String? | 19 // TODO(rnystrom): Move into String? |
| 30 /** Pads [source] to [length] by adding spaces at the end. */ | 20 /** Pads [source] to [length] by adding spaces at the end. */ |
| 31 String padRight(String source, int length) { | 21 String padRight(String source, int length) { |
| 32 final result = new StringBuffer(); | 22 final result = new StringBuffer(); |
| 33 result.add(source); | 23 result.add(source); |
| 34 | 24 |
| 35 while (result.length < length) { | 25 while (result.length < length) { |
| 36 result.add(' '); | 26 result.add(' '); |
| 37 } | 27 } |
| 38 | 28 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 /** | 63 /** |
| 74 * Asserts that [iter] contains only one element, and returns it. | 64 * Asserts that [iter] contains only one element, and returns it. |
| 75 */ | 65 */ |
| 76 only(Iterable iter) { | 66 only(Iterable iter) { |
| 77 var iterator = iter.iterator(); | 67 var iterator = iter.iterator(); |
| 78 assert(iterator.hasNext()); | 68 assert(iterator.hasNext()); |
| 79 var obj = iterator.next(); | 69 var obj = iterator.next(); |
| 80 assert(!iterator.hasNext()); | 70 assert(!iterator.hasNext()); |
| 81 return obj; | 71 return obj; |
| 82 } | 72 } |
| 83 | |
| 84 /** | |
| 85 * Replace each instance of [matcher] in [source] with the return value of [fn]. | |
| 86 */ | |
| 87 String replace(String source, Pattern matcher, String fn(Match)) { | |
| 88 var buffer = new StringBuffer(); | |
| 89 var start = 0; | |
| 90 for (var match in matcher.allMatches(source)) { | |
| 91 buffer.add(source.substring(start, match.start())); | |
| 92 start = match.end(); | |
| 93 buffer.add(fn(match)); | |
| 94 } | |
| 95 buffer.add(source.substring(start)); | |
| 96 return buffer.toString(); | |
| 97 } | |
| OLD | NEW |