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

Side by Side Diff: utils/pub/utils.dart

Issue 10628016: Add a Pub source for pub.dartlang.org. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 6 months 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 buf = new StringBuffer();
Bob Nystrom 2012/06/22 22:35:44 "buf" -> "buffer".
nweiz 2012/06/25 22:25:17 Done.
89 var i = 0;
Bob Nystrom 2012/06/22 22:35:44 "i" -> "start"
nweiz 2012/06/25 22:25:17 Done.
90 for (var match in matcher.allMatches(source)) {
91 buf.add(source.substring(i, match.start()));
92 i = match.end();
93 buf.add(fn(match));
94 }
95 buf.add(source.substring(i));
96 return buf.toString();
97 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698