Chromium Code Reviews| Index: utils/dartdoc/utils.dart |
| diff --git a/utils/dartdoc/utils.dart b/utils/dartdoc/utils.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..975e2d2935671dab3fc861af8cc8f3193c62b4a3 |
| --- /dev/null |
| +++ b/utils/dartdoc/utils.dart |
| @@ -0,0 +1,56 @@ |
| +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +// Generic utility functions. |
| + |
| +num time(callback()) { |
| + // Unlike world.withTiming, returns the elapsed time. |
|
pquitslund
2011/12/05 18:14:58
Maybe this comment would be more useful as a prope
Bob Nystrom
2011/12/05 23:14:28
Done.
|
| + final watch = new Stopwatch(); |
| + watch.start(); |
| + callback(); |
| + watch.stop(); |
| + return watch.elapsedInMs(); |
| +} |
| + |
| +/** Turns [name] into something that's safe to use as a file name. */ |
| +String sanitize(String name) => name.replaceAll(':', '_').replaceAll('/', '_'); |
| + |
| +/** Returns the number of times [search] occurs in [text]. */ |
| +int countOccurrences(String text, String search) { |
| + int start = 0; |
| + int count = 0; |
| + |
| + while (true) { |
| + start = text.indexOf(search, start); |
| + if (start == -1) break; |
| + count++; |
| + // Offsetting by needle length means overlapping needles are not counted. |
|
pquitslund
2011/12/05 18:14:58
needle->text...
Bob Nystrom
2011/12/05 23:14:28
Done.
|
| + start += search.length; |
| + } |
| + |
| + return count; |
| +} |
| + |
| +/** Repeats [text] [count] times, separated by [separator] if given. */ |
| +String repeat(String text, int count, [String separator]) { |
| + // TODO(rnystrom): Should be in corelib. |
|
pquitslund
2011/12/05 18:14:58
Aha! You read my mind. ;)
Along those lines, if
Bob Nystrom
2011/12/05 23:14:28
Well, if you asked *me*, I'd say the type system s
|
| + final buffer = new StringBuffer(); |
| + for (int i = 0; i < count; i++) { |
| + buffer.add(text); |
| + if ((i < count - 1) && (separator !== null)) buffer.add(separator); |
| + } |
| + |
| + return buffer.toString(); |
| +} |
| + |
| +/** Removes up to [indentation] leading whitespace characters from [text]. */ |
| +String unindent(String text, int indentation) { |
| + var start; |
| + for (start = 0; start < Math.min(indentation, text.length); start++) { |
| + // Stop if we hit a non-whitespace character. |
| + if (text[start] != ' ') break; |
| + } |
| + |
| + return text.substring(start); |
| +} |