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

Side by Side Diff: runtime/lib/string_buffer_patch.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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 patch /* abstract */ class StringBuffer { 5 patch class StringBuffer {
6 /* patch */ factory StringBuffer([Object content = ""])
7 => new _StringBufferImpl(content);
8 }
9
10 class _StringBufferImpl implements StringBuffer {
11
12 List<String> _buffer; 6 List<String> _buffer;
13 int _length; 7 int _length;
14 8
15 /// Creates the string buffer with an initial content. 9 /* patch */ int get length => _length;
16 _StringBufferImpl(Object content) {
17 clear();
18 add(content);
19 }
20
21 /// Returns the length of the buffer.
22 int get length => _length;
23
24 bool get isEmpty => _length == 0;
25 10
26 /// Adds [obj] to the buffer. 11 /// Adds [obj] to the buffer.
27 void add(Object obj) { 12 /* patch */ void _write(String str) {
28 // TODO(srdjan): The following four lines could be replaced by
29 // '$obj', but apparently this is too slow on the Dart VM.
30 String str = obj.toString();
31 if (str is !String) {
32 throw new ArgumentError('toString() did not return a string');
33 }
34 if (str.isEmpty) return;
35 _buffer.add(str); 13 _buffer.add(str);
36 _length += str.length; 14 _length += str.length;
37 } 15 }
38 16
39 /// Adds all items in [objects] to the buffer.
40 void addAll(Iterable objects) {
41 for (Object obj in objects) add(obj);
42 }
43
44 /// Adds the string representation of [charCode] to the buffer.
45 void addCharCode(int charCode) {
46 add(new String.fromCharCodes([charCode]));
47 }
48
49 /// Clears the string buffer. 17 /// Clears the string buffer.
50 void clear() { 18 /* patch */ void clear() {
51 _buffer = new List<String>(); 19 _buffer = new List<String>();
52 _length = 0; 20 _length = 0;
53 } 21 }
54 22
55 /// Returns the contents of buffer as a concatenated string. 23 /// Returns the contents of buffer as a concatenated string.
56 String toString() { 24 /* patch */ String toString() {
57 if (_buffer.length == 0) return ""; 25 if (_buffer.length == 0) return "";
58 if (_buffer.length == 1) return _buffer[0]; 26 if (_buffer.length == 1) return _buffer[0];
59 String result = Strings.concatAll(_buffer); 27 String result = Strings.concatAll(_buffer);
60 _buffer.clear(); 28 _buffer.clear();
61 _buffer.add(result); 29 _buffer.add(result);
62 // Since we track the length at each add operation, there is no 30 // Since we track the length at each add operation, there is no
63 // need to update it in this function. 31 // need to update it in this function.
64 return result; 32 return result;
65 } 33 }
66 } 34 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/lib/core_patch.dart » ('j') | sdk/lib/core/string_buffer.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698