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