Chromium Code Reviews| 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 * The StringBuffer class is useful for concatenating strings | 8 * The StringBuffer class is useful for concatenating strings |
| 9 * efficiently. Only on a call to [toString] are the strings | 9 * efficiently. Only on a call to [toString] are the strings |
| 10 * concatenated to a single String. | 10 * concatenated to a single String. |
| 11 */ | 11 */ |
| 12 abstract class StringBuffer { | 12 class StringBuffer implements StringSink { |
| 13 | 13 |
| 14 /// Creates the string buffer with an initial content. | 14 /// Creates the string buffer with an initial content. |
| 15 external factory StringBuffer([Object content = ""]); | 15 StringBuffer([Object content = ""]) { |
|
Lasse Reichstein Nielsen
2013/02/11 12:30:11
Just make the constructor external.
floitsch
2013/02/11 17:10:49
Done.
| |
| 16 // TODO(floitsch): remove or rewrite this call to clear. It is currently | |
| 17 // necessary because the VM doesn't allow default values for fields in | |
| 18 // patch classes. | |
| 19 clear(); | |
| 20 write(content); | |
| 21 } | |
| 16 | 22 |
| 17 /// Returns the length of the buffer. | 23 /// Returns the length of the buffer. |
| 18 int get length; | 24 external int get length; |
| 19 | 25 |
| 20 // Returns whether the buffer is empty. | 26 /// Returns whether the buffer is empty. |
| 21 bool get isEmpty; | 27 bool get isEmpty => length == 0; |
| 22 | 28 |
| 23 /// Converts [obj] to a string and adds it to the buffer. | 29 /** |
| 24 void add(Object obj); | 30 * Converts [obj] to a string and adds it to the buffer. |
| 31 * | |
| 32 * *Deprecated*. Use [write] instead. | |
| 33 */ | |
| 34 @deprecated | |
| 35 void add(Object obj) => write(obj); | |
| 36 | |
| 37 void write(Object obj) { | |
| 38 // TODO(srdjan): The following four lines could be replaced by | |
| 39 // '$obj', but apparently this is too slow on the Dart VM. | |
| 40 String str = obj.toString(); | |
| 41 if (str is! String) { | |
| 42 throw new ArgumentError('toString() did not return a string'); | |
| 43 } | |
| 44 if (str.isEmpty) return; | |
| 45 _write(str); | |
| 46 } | |
| 47 | |
| 48 | |
| 49 void print(Object obj) { | |
| 50 write(obj); | |
| 51 write("\n"); | |
|
Lasse Reichstein Nielsen
2013/02/11 12:30:11
_write for the second call.
floitsch
2013/02/11 17:10:49
Done.
| |
| 52 } | |
| 25 | 53 |
| 26 /// Adds the string representation of [charCode] to the buffer. | 54 /// Adds the string representation of [charCode] to the buffer. |
| 27 void addCharCode(int charCode); | 55 void addCharCode(int charCode) { |
| 56 write(new String.fromCharCode(charCode)); | |
|
Lasse Reichstein Nielsen
2013/02/11 12:30:11
_write
floitsch
2013/02/11 17:10:49
Done.
| |
| 57 } | |
| 28 | 58 |
| 29 /// Adds all items in [objects] to the buffer. | 59 /** |
| 30 void addAll(Iterable objects); | 60 * Adds all items in [objects] to the buffer. |
| 61 * | |
| 62 * *Deprecated*. Use `objects.forEach(buffer.write)` instead. | |
|
Lasse Reichstein Nielsen
2013/02/11 12:30:11
Backquotes are not part of the dartdoc spec. Use [
floitsch
2013/02/11 17:10:49
Done.
| |
| 63 */ | |
| 64 @deprecated | |
|
floitsch
2013/02/09 00:59:02
Not sure about this.
Lasse Reichstein Nielsen
2013/02/11 12:30:11
Agree, I'd vote for removing it.
Let the iterable
| |
| 65 void addAll(Iterable objects) { | |
| 66 for (Object obj in objects) write(obj); | |
| 67 } | |
| 31 | 68 |
| 32 /// Clears the string buffer. | 69 /** |
| 33 void clear(); | 70 * Clears the string buffer. |
| 71 * | |
| 72 * *Deprecated*. | |
| 73 */ | |
| 74 @deprecated | |
|
floitsch
2013/02/09 00:59:02
Not sure about this.
Lasse Reichstein Nielsen
2013/02/11 12:30:11
I'm not sure we should have a clear method at all.
floitsch
2013/02/11 17:10:49
Let's deprecate it then.
| |
| 75 external void clear(); | |
| 34 | 76 |
| 35 /// Returns the contents of buffer as a concatenated string. | 77 /// Returns the contents of buffer as a concatenated string. |
| 36 String toString(); | 78 external String toString(); |
| 79 | |
| 80 external void _write(String str); | |
| 37 } | 81 } |
| OLD | NEW |