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 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 factory StringBuffer([Object content = ""]) => new _StringBufferImpl(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(Collection 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 | 38 |
39 class _StringBufferImpl implements StringBuffer { | 39 class _StringBufferImpl implements StringBuffer { |
40 | 40 |
(...skipping 18 matching lines...) Expand all Loading... |
59 String str = obj.toString(); | 59 String str = obj.toString(); |
60 if (str is !String) { | 60 if (str is !String) { |
61 throw new ArgumentError('toString() did not return a string'); | 61 throw new ArgumentError('toString() did not return a string'); |
62 } | 62 } |
63 if (str.isEmpty) return; | 63 if (str.isEmpty) return; |
64 _buffer.add(str); | 64 _buffer.add(str); |
65 _length += str.length; | 65 _length += str.length; |
66 } | 66 } |
67 | 67 |
68 /// Adds all items in [objects] to the buffer. | 68 /// Adds all items in [objects] to the buffer. |
69 void addAll(Collection objects) { | 69 void addAll(Iterable objects) { |
70 for (Object obj in objects) add(obj); | 70 for (Object obj in objects) add(obj); |
71 } | 71 } |
72 | 72 |
73 /// Adds the string representation of [charCode] to the buffer. | 73 /// Adds the string representation of [charCode] to the buffer. |
74 void addCharCode(int charCode) { | 74 void addCharCode(int charCode) { |
75 add(new String.fromCharCodes([charCode])); | 75 add(new String.fromCharCodes([charCode])); |
76 } | 76 } |
77 | 77 |
78 /// Clears the string buffer. | 78 /// Clears the string buffer. |
79 void clear() { | 79 void clear() { |
80 _buffer = new List<String>(); | 80 _buffer = new List<String>(); |
81 _length = 0; | 81 _length = 0; |
82 } | 82 } |
83 | 83 |
84 /// Returns the contents of buffer as a concatenated string. | 84 /// Returns the contents of buffer as a concatenated string. |
85 String toString() { | 85 String toString() { |
86 if (_buffer.length == 0) return ""; | 86 if (_buffer.length == 0) return ""; |
87 if (_buffer.length == 1) return _buffer[0]; | 87 if (_buffer.length == 1) return _buffer[0]; |
88 String result = Strings.concatAll(_buffer); | 88 String result = Strings.concatAll(_buffer); |
89 _buffer.clear(); | 89 _buffer.clear(); |
90 _buffer.add(result); | 90 _buffer.add(result); |
91 // Since we track the length at each add operation, there is no | 91 // Since we track the length at each add operation, there is no |
92 // need to update it in this function. | 92 // need to update it in this function. |
93 return result; | 93 return result; |
94 } | 94 } |
95 } | 95 } |
OLD | NEW |