| 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 /** | 5 /** |
| 6 * The StringBuffer class is useful for concatenating strings | 6 * The StringBuffer class is useful for concatenating strings |
| 7 * efficiently. Only on a call to [toString] are the strings | 7 * efficiently. Only on a call to [toString] are the strings |
| 8 * concatenated to a single String. | 8 * concatenated to a single String. |
| 9 */ | 9 */ |
| 10 abstract class StringBuffer { | 10 abstract class StringBuffer { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 } | 61 } |
| 62 | 62 |
| 63 /** | 63 /** |
| 64 * Returns the length of the buffer. | 64 * Returns the length of the buffer. |
| 65 */ | 65 */ |
| 66 int get length { | 66 int get length { |
| 67 return _length; | 67 return _length; |
| 68 } | 68 } |
| 69 | 69 |
| 70 bool get isEmpty { | 70 bool get isEmpty { |
| 71 return _length === 0; | 71 return _length == 0; |
| 72 } | 72 } |
| 73 | 73 |
| 74 /** | 74 /** |
| 75 * Adds [obj] to the buffer. Returns [this]. | 75 * Adds [obj] to the buffer. Returns [this]. |
| 76 */ | 76 */ |
| 77 StringBuffer add(Object obj) { | 77 StringBuffer add(Object obj) { |
| 78 String str = obj.toString(); | 78 String str = obj.toString(); |
| 79 if (str === null || str.isEmpty) { | 79 if (str == null || str.isEmpty) { |
| 80 return this; | 80 return this; |
| 81 } | 81 } |
| 82 _buffer.add(str); | 82 _buffer.add(str); |
| 83 _length += str.length; | 83 _length += str.length; |
| 84 return this; | 84 return this; |
| 85 } | 85 } |
| 86 | 86 |
| 87 /** | 87 /** |
| 88 * Adds all items in [objects] to the buffer. Returns [this]. | 88 * Adds all items in [objects] to the buffer. Returns [this]. |
| 89 */ | 89 */ |
| (...skipping 18 matching lines...) Expand all Loading... |
| 108 StringBuffer clear() { | 108 StringBuffer clear() { |
| 109 _buffer = new List<String>(); | 109 _buffer = new List<String>(); |
| 110 _length = 0; | 110 _length = 0; |
| 111 return this; | 111 return this; |
| 112 } | 112 } |
| 113 | 113 |
| 114 /** | 114 /** |
| 115 * Returns the contents of buffer as a concatenated string. | 115 * Returns the contents of buffer as a concatenated string. |
| 116 */ | 116 */ |
| 117 String toString() { | 117 String toString() { |
| 118 if (_buffer.length === 0) return ""; | 118 if (_buffer.length == 0) return ""; |
| 119 if (_buffer.length === 1) return _buffer[0]; | 119 if (_buffer.length == 1) return _buffer[0]; |
| 120 String result = _StringImpl.concatAll(_buffer); | 120 String result = _StringImpl.concatAll(_buffer); |
| 121 _buffer.clear(); | 121 _buffer.clear(); |
| 122 _buffer.add(result); | 122 _buffer.add(result); |
| 123 // Since we track the length at each add operation, there is no | 123 // Since we track the length at each add operation, there is no |
| 124 // need to update it in this function. | 124 // need to update it in this function. |
| 125 return result; | 125 return result; |
| 126 } | 126 } |
| 127 | 127 |
| 128 List<String> _buffer; | 128 List<String> _buffer; |
| 129 int _length; | 129 int _length; |
| 130 } | 130 } |
| OLD | NEW |