| 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 part of dartdoc; |
| 6 |
| 5 // Generic utility functions. | 7 // Generic utility functions. |
| 6 | 8 |
| 7 /** Turns [name] into something that's safe to use as a file name. */ | 9 /** Turns [name] into something that's safe to use as a file name. */ |
| 8 String sanitize(String name) => name.replaceAll(':', '_').replaceAll('/', '_'); | 10 String sanitize(String name) => name.replaceAll(':', '_').replaceAll('/', '_'); |
| 9 | 11 |
| 10 /** Returns the number of times [search] occurs in [text]. */ | 12 /** Returns the number of times [search] occurs in [text]. */ |
| 11 int countOccurrences(String text, String search) { | 13 int countOccurrences(String text, String search) { |
| 12 int start = 0; | 14 int start = 0; |
| 13 int count = 0; | 15 int count = 0; |
| 14 | 16 |
| 15 while (true) { | 17 while (true) { |
| 16 start = text.indexOf(search, start); | 18 start = text.indexOf(search, start); |
| 17 if (start == -1) break; | 19 if (start == -1) break; |
| 18 count++; | 20 count++; |
| 19 // Offsetting by search length means overlapping results are not counted. | 21 // Offsetting by search length means overlapping results are not counted. |
| 20 start += search.length; | 22 start += search.length; |
| 21 } | 23 } |
| 22 | 24 |
| 23 return count; | 25 return count; |
| 24 } | 26 } |
| 25 | 27 |
| 26 /** Repeats [text] [count] times, separated by [separator] if given. */ | 28 /** Repeats [text] [count] times, separated by [separator] if given. */ |
| 27 String repeat(String text, int count, {String separator}) { | 29 String repeat(String text, int count, {String separator}) { |
| 28 // TODO(rnystrom): Should be in corelib. | 30 // TODO(rnystrom): Should be in corelib. |
| 29 final buffer = new StringBuffer(); | 31 final buffer = new StringBuffer(); |
| 30 for (int i = 0; i < count; i++) { | 32 for (int i = 0; i < count; i++) { |
| 31 buffer.add(text); | 33 buffer.add(text); |
| 32 if ((i < count - 1) && (separator !== null)) buffer.add(separator); | 34 if ((i < count - 1) && (separator != null)) buffer.add(separator); |
| 33 } | 35 } |
| 34 | 36 |
| 35 return buffer.toString(); | 37 return buffer.toString(); |
| 36 } | 38 } |
| 37 | 39 |
| 38 /** Removes up to [indentation] leading whitespace characters from [text]. */ | 40 /** Removes up to [indentation] leading whitespace characters from [text]. */ |
| 39 String unindent(String text, int indentation) { | 41 String unindent(String text, int indentation) { |
| 40 var start; | 42 var start; |
| 41 for (start = 0; start < min(indentation, text.length); start++) { | 43 for (start = 0; start < min(indentation, text.length); start++) { |
| 42 // Stop if we hit a non-whitespace character. | 44 // Stop if we hit a non-whitespace character. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 68 if (items.length == 2) return "${items[0]} $conjunction ${items[1]}"; | 70 if (items.length == 2) return "${items[0]} $conjunction ${items[1]}"; |
| 69 return '${Strings.join(items.getRange(0, items.length - 1), ', ')}' | 71 return '${Strings.join(items.getRange(0, items.length - 1), ', ')}' |
| 70 ', $conjunction ${items[items.length - 1]}'; | 72 ', $conjunction ${items[items.length - 1]}'; |
| 71 } | 73 } |
| 72 | 74 |
| 73 void writeString(File file, String text) { | 75 void writeString(File file, String text) { |
| 74 var randomAccessFile = file.openSync(FileMode.WRITE); | 76 var randomAccessFile = file.openSync(FileMode.WRITE); |
| 75 randomAccessFile.writeStringSync(text); | 77 randomAccessFile.writeStringSync(text); |
| 76 randomAccessFile.closeSync(); | 78 randomAccessFile.closeSync(); |
| 77 } | 79 } |
| OLD | NEW |