| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 // Generic utility functions. | |
| 6 | |
| 7 /** Turns [name] into something that's safe to use as a file name. */ | |
| 8 String sanitize(String name) => name.replaceAll(':', '_').replaceAll('/', '_'); | |
| 9 | |
| 10 /** Returns the number of times [search] occurs in [text]. */ | |
| 11 int countOccurrences(String text, String search) { | |
| 12 int start = 0; | |
| 13 int count = 0; | |
| 14 | |
| 15 while (true) { | |
| 16 start = text.indexOf(search, start); | |
| 17 if (start == -1) break; | |
| 18 count++; | |
| 19 // Offsetting by search length means overlapping results are not counted. | |
| 20 start += search.length; | |
| 21 } | |
| 22 | |
| 23 return count; | |
| 24 } | |
| 25 | |
| 26 /** Repeats [text] [count] times, separated by [separator] if given. */ | |
| 27 String repeat(String text, int count, [String separator]) { | |
| 28 // TODO(rnystrom): Should be in corelib. | |
| 29 final buffer = new StringBuffer(); | |
| 30 for (int i = 0; i < count; i++) { | |
| 31 buffer.add(text); | |
| 32 if ((i < count - 1) && (separator !== null)) buffer.add(separator); | |
| 33 } | |
| 34 | |
| 35 return buffer.toString(); | |
| 36 } | |
| 37 | |
| 38 /** Removes up to [indentation] leading whitespace characters from [text]. */ | |
| 39 String unindent(String text, int indentation) { | |
| 40 var start; | |
| 41 for (start = 0; start < min(indentation, text.length); start++) { | |
| 42 // Stop if we hit a non-whitespace character. | |
| 43 if (text[start] != ' ') break; | |
| 44 } | |
| 45 | |
| 46 return text.substring(start); | |
| 47 } | |
| 48 | |
| 49 /** Sorts the map by the key, doing a case-insensitive comparison. */ | |
| 50 List<Mirror> orderByName(Collection<Mirror> list) { | |
| 51 final elements = new List<Mirror>.from(list); | |
| 52 elements.sort((a,b) { | |
| 53 String aName = a.simpleName; | |
| 54 String bName = b.simpleName; | |
| 55 bool doma = aName.startsWith(@"$dom"); | |
| 56 bool domb = bName.startsWith(@"$dom"); | |
| 57 return doma == domb ? aName.compareTo(bName) : doma ? 1 : -1; | |
| 58 }); | |
| 59 return elements; | |
| 60 } | |
| 61 | |
| 62 /** | |
| 63 * Joins [items] into a single, comma-separated string using [conjunction]. | |
| 64 * E.g. `['A', 'B', 'C']` becomes `"A, B, and C"`. | |
| 65 */ | |
| 66 String joinWithCommas(List<String> items, [String conjunction = 'and']) { | |
| 67 if (items.length == 1) return items[0]; | |
| 68 if (items.length == 2) return "${items[0]} $conjunction ${items[1]}"; | |
| 69 return '${Strings.join(items.getRange(0, items.length - 1), ', ')}' | |
| 70 ', $conjunction ${items[items.length - 1]}'; | |
| 71 } | |
| 72 | |
| 73 void writeString(File file, String text) { | |
| 74 var randomAccessFile = file.openSync(FileMode.WRITE); | |
| 75 randomAccessFile.writeStringSync(text); | |
| 76 randomAccessFile.closeSync(); | |
| 77 } | |
| OLD | NEW |