| 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 |
| 19 // TODO(rnystrom): Move into String? | 29 // TODO(rnystrom): Move into String? |
| 20 /** Pads [source] to [length] by adding spaces at the end. */ | 30 /** Pads [source] to [length] by adding spaces at the end. */ |
| 21 String padRight(String source, int length) { | 31 String padRight(String source, int length) { |
| 22 final result = new StringBuffer(); | 32 final result = new StringBuffer(); |
| 23 result.add(source); | 33 result.add(source); |
| 24 | 34 |
| 25 while (result.length < length) { | 35 while (result.length < length) { |
| 26 result.add(' '); | 36 result.add(' '); |
| 27 } | 37 } |
| 28 | 38 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 /** | 73 /** |
| 64 * Asserts that [iter] contains only one element, and returns it. | 74 * Asserts that [iter] contains only one element, and returns it. |
| 65 */ | 75 */ |
| 66 only(Iterable iter) { | 76 only(Iterable iter) { |
| 67 var iterator = iter.iterator(); | 77 var iterator = iter.iterator(); |
| 68 assert(iterator.hasNext()); | 78 assert(iterator.hasNext()); |
| 69 var obj = iterator.next(); | 79 var obj = iterator.next(); |
| 70 assert(!iterator.hasNext()); | 80 assert(!iterator.hasNext()); |
| 71 return obj; | 81 return obj; |
| 72 } | 82 } |
| 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 |