| 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 part of dart.core; | |
| 6 | |
| 7 /** | |
| 8 * A class for concatenating strings efficiently. | |
| 9 * | |
| 10 * Allows for the incremental building of a string using write*() methods. | |
| 11 * The strings are concatenated to a single string only when [toString] is | |
| 12 * called. | |
| 13 */ | |
| 14 class StringBuffer implements StringSink { | |
| 15 | |
| 16 /** Creates the string buffer with an initial content. */ | |
| 17 StringBuffer([Object content = ""]) : _contents = '$content'; | |
| 18 | |
| 19 /** | |
| 20 * Returns the length of the content that has been accumulated so far. | |
| 21 * This is a constant-time operation. | |
| 22 */ | |
| 23 int get length => _contents.length; | |
| 24 | |
| 25 /** Returns whether the buffer is empty. This is a constant-time operation. */ | |
| 26 bool get isEmpty => length == 0; | |
| 27 | |
| 28 /** | |
| 29 * Returns whether the buffer is not empty. This is a constant-time | |
| 30 * operation. | |
| 31 */ | |
| 32 bool get isNotEmpty => !isEmpty; | |
| 33 | |
| 34 /// Adds the contents of [obj], converted to a string, to the buffer. | |
| 35 void write(Object obj) { | |
| 36 _writeString('$obj'); | |
| 37 } | |
| 38 | |
| 39 /// Adds the string representation of [charCode] to the buffer. | |
| 40 void writeCharCode(int charCode) { | |
| 41 _writeString(new String.fromCharCode(charCode)); | |
| 42 } | |
| 43 | |
| 44 void writeAll(Iterable objects, [String separator = ""]) { | |
| 45 Iterator iterator = objects.iterator; | |
| 46 if (!iterator.moveNext()) return; | |
| 47 if (separator.isEmpty) { | |
| 48 do { | |
| 49 write(iterator.current); | |
| 50 } while (iterator.moveNext()); | |
| 51 } else { | |
| 52 write(iterator.current); | |
| 53 while (iterator.moveNext()) { | |
| 54 write(separator); | |
| 55 write(iterator.current); | |
| 56 } | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 void writeln([Object obj = ""]) { | |
| 61 write(obj); | |
| 62 write("\n"); | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Clears the string buffer. | |
| 67 */ | |
| 68 void clear() { | |
| 69 _contents = ""; | |
| 70 } | |
| 71 | |
| 72 /// Returns the contents of buffer as a concatenated string. | |
| 73 String toString() => Primitives.flattenString(_contents); | |
| 74 | |
| 75 String _contents; | |
| 76 | |
| 77 void _writeString(str) { | |
| 78 _contents = Primitives.stringConcatUnchecked(_contents, str); | |
| 79 } | |
| 80 } | |
| OLD | NEW |