OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 // Generic utility functions. | |
6 | |
7 num time(callback()) { | |
8 // 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.
| |
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. */ | |
17 String sanitize(String name) => name.replaceAll(':', '_').replaceAll('/', '_'); | |
18 | |
19 /** Returns the number of times [search] occurs in [text]. */ | |
20 int countOccurrences(String text, String search) { | |
21 int start = 0; | |
22 int count = 0; | |
23 | |
24 while (true) { | |
25 start = text.indexOf(search, start); | |
26 if (start == -1) break; | |
27 count++; | |
28 // 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.
| |
29 start += search.length; | |
30 } | |
31 | |
32 return count; | |
33 } | |
34 | |
35 /** Repeats [text] [count] times, separated by [separator] if given. */ | |
36 String repeat(String text, int count, [String separator]) { | |
37 // 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
| |
38 final buffer = new StringBuffer(); | |
39 for (int i = 0; i < count; i++) { | |
40 buffer.add(text); | |
41 if ((i < count - 1) && (separator !== null)) buffer.add(separator); | |
42 } | |
43 | |
44 return buffer.toString(); | |
45 } | |
46 | |
47 /** Removes up to [indentation] leading whitespace characters from [text]. */ | |
48 String unindent(String text, int indentation) { | |
49 var start; | |
50 for (start = 0; start < Math.min(indentation, text.length); start++) { | |
51 // Stop if we hit a non-whitespace character. | |
52 if (text[start] != ' ') break; | |
53 } | |
54 | |
55 return text.substring(start); | |
56 } | |
OLD | NEW |