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

Side by Side Diff: dart/sdk/lib/core/string_buffer.dart

Issue 11565034: Improve performance of dart2js StringBuffer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Updated to lib_v2 and private libraries. Created 7 years, 11 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) 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 abstract class StringBuffer {
13 13
14 /// Creates the string buffer with an initial content. 14 /// Creates the string buffer with an initial content.
15 factory StringBuffer([Object content = ""]) => new _StringBufferImpl(content); 15 external factory StringBuffer([Object content = ""]);
16 16
17 /// Returns the length of the buffer. 17 /// Returns the length of the buffer.
18 int get length; 18 int get length;
19 19
20 // Returns whether the buffer is empty. 20 // Returns whether the buffer is empty.
21 bool get isEmpty; 21 bool get isEmpty;
22 22
23 /// Converts [obj] to a string and adds it to the buffer. 23 /// Converts [obj] to a string and adds it to the buffer.
24 void add(Object obj); 24 void add(Object obj);
25 25
26 /// Adds the string representation of [charCode] to the buffer. 26 /// Adds the string representation of [charCode] to the buffer.
27 void addCharCode(int charCode); 27 void addCharCode(int charCode);
28 28
29 /// Adds all items in [objects] to the buffer. 29 /// Adds all items in [objects] to the buffer.
30 void addAll(Iterable objects); 30 void addAll(Iterable objects);
31 31
32 /// Clears the string buffer. 32 /// Clears the string buffer.
33 void clear(); 33 void clear();
34 34
35 /// Returns the contents of buffer as a concatenated string. 35 /// Returns the contents of buffer as a concatenated string.
36 String toString(); 36 String toString();
37 } 37 }
38
39 class _StringBufferImpl implements StringBuffer {
40
41 List<String> _buffer;
42 int _length;
43
44 /// Creates the string buffer with an initial content.
45 _StringBufferImpl(Object content) {
46 clear();
47 add(content);
48 }
49
50 /// Returns the length of the buffer.
51 int get length => _length;
52
53 bool get isEmpty => _length == 0;
54
55 /// Adds [obj] to the buffer.
56 void add(Object obj) {
57 // TODO(srdjan): The following four lines could be replaced by
58 // '$obj', but apparently this is too slow on the Dart VM.
59 String str = obj.toString();
60 if (str is !String) {
61 throw new ArgumentError('toString() did not return a string');
62 }
63 if (str.isEmpty) return;
64 _buffer.add(str);
65 _length += str.length;
66 }
67
68 /// Adds all items in [objects] to the buffer.
69 void addAll(Iterable objects) {
70 for (Object obj in objects) add(obj);
71 }
72
73 /// Adds the string representation of [charCode] to the buffer.
74 void addCharCode(int charCode) {
75 add(new String.fromCharCodes([charCode]));
76 }
77
78 /// Clears the string buffer.
79 void clear() {
80 _buffer = new List<String>();
81 _length = 0;
82 }
83
84 /// Returns the contents of buffer as a concatenated string.
85 String toString() {
86 if (_buffer.length == 0) return "";
87 if (_buffer.length == 1) return _buffer[0];
88 String result = Strings.concatAll(_buffer);
89 _buffer.clear();
90 _buffer.add(result);
91 // Since we track the length at each add operation, there is no
92 // need to update it in this function.
93 return result;
94 }
95 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698