Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1613)

Unified Diff: sdk/lib/core/string_buffer.dart

Issue 12218086: Add StringSink and update StringBuffer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/core/string_buffer.dart
diff --git a/sdk/lib/core/string_buffer.dart b/sdk/lib/core/string_buffer.dart
index d8b393583952238a7e66027ba20d0b45b86179e4..4a1ed4763a35fd41aa560dbc559e6cd7f7799202 100644
--- a/sdk/lib/core/string_buffer.dart
+++ b/sdk/lib/core/string_buffer.dart
@@ -9,29 +9,73 @@ part of dart.core;
* efficiently. Only on a call to [toString] are the strings
* concatenated to a single String.
*/
-abstract class StringBuffer {
+class StringBuffer implements StringSink {
/// Creates the string buffer with an initial content.
- external factory StringBuffer([Object content = ""]);
+ 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.
+ // TODO(floitsch): remove or rewrite this call to clear. It is currently
+ // necessary because the VM doesn't allow default values for fields in
+ // patch classes.
+ clear();
+ write(content);
+ }
/// Returns the length of the buffer.
- int get length;
+ external int get length;
- // Returns whether the buffer is empty.
- bool get isEmpty;
+ /// Returns whether the buffer is empty.
+ bool get isEmpty => length == 0;
- /// Converts [obj] to a string and adds it to the buffer.
- void add(Object obj);
+ /**
+ * Converts [obj] to a string and adds it to the buffer.
+ *
+ * *Deprecated*. Use [write] instead.
+ */
+ @deprecated
+ void add(Object obj) => write(obj);
+
+ void write(Object obj) {
+ // TODO(srdjan): The following four lines could be replaced by
+ // '$obj', but apparently this is too slow on the Dart VM.
+ String str = obj.toString();
+ if (str is! String) {
+ throw new ArgumentError('toString() did not return a string');
+ }
+ if (str.isEmpty) return;
+ _write(str);
+ }
+
+
+ void print(Object obj) {
+ write(obj);
+ write("\n");
Lasse Reichstein Nielsen 2013/02/11 12:30:11 _write for the second call.
floitsch 2013/02/11 17:10:49 Done.
+ }
/// Adds the string representation of [charCode] to the buffer.
- void addCharCode(int charCode);
+ void addCharCode(int charCode) {
+ write(new String.fromCharCode(charCode));
Lasse Reichstein Nielsen 2013/02/11 12:30:11 _write
floitsch 2013/02/11 17:10:49 Done.
+ }
- /// Adds all items in [objects] to the buffer.
- void addAll(Iterable objects);
+ /**
+ * Adds all items in [objects] to the buffer.
+ *
+ * *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.
+ */
+ @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
+ void addAll(Iterable objects) {
+ for (Object obj in objects) write(obj);
+ }
- /// Clears the string buffer.
- void clear();
+ /**
+ * Clears the string buffer.
+ *
+ * *Deprecated*.
+ */
+ @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.
+ external void clear();
/// Returns the contents of buffer as a concatenated string.
- String toString();
+ external String toString();
+
+ external void _write(String str);
}

Powered by Google App Engine
This is Rietveld 408576698