| OLD | NEW |
| 1 // Copyright (c) 2011, 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 part of dart.core; | 5 patch /* abstract */ class StringBuffer { |
| 6 | 6 /* patch */ factory StringBuffer([Object content = ""]) |
| 7 /** | 7 => new _StringBufferImpl(content); |
| 8 * The StringBuffer class is useful for concatenating strings | |
| 9 * efficiently. Only on a call to [toString] are the strings | |
| 10 * concatenated to a single String. | |
| 11 */ | |
| 12 abstract class StringBuffer { | |
| 13 | |
| 14 /// Creates the string buffer with an initial content. | |
| 15 factory StringBuffer([Object content = ""]) => new _StringBufferImpl(content); | |
| 16 | |
| 17 /// Returns the length of the buffer. | |
| 18 int get length; | |
| 19 | |
| 20 // Returns whether the buffer is empty. | |
| 21 bool get isEmpty; | |
| 22 | |
| 23 /// Converts [obj] to a string and adds it to the buffer. | |
| 24 void add(Object obj); | |
| 25 | |
| 26 /// Adds the string representation of [charCode] to the buffer. | |
| 27 void addCharCode(int charCode); | |
| 28 | |
| 29 /// Adds all items in [objects] to the buffer. | |
| 30 void addAll(Iterable objects); | |
| 31 | |
| 32 /// Clears the string buffer. | |
| 33 void clear(); | |
| 34 | |
| 35 /// Returns the contents of buffer as a concatenated string. | |
| 36 String toString(); | |
| 37 } | 8 } |
| 38 | 9 |
| 39 class _StringBufferImpl implements StringBuffer { | 10 class _StringBufferImpl implements StringBuffer { |
| 40 | 11 |
| 41 List<String> _buffer; | 12 List<String> _buffer; |
| 42 int _length; | 13 int _length; |
| 43 | 14 |
| 44 /// Creates the string buffer with an initial content. | 15 /// Creates the string buffer with an initial content. |
| 45 _StringBufferImpl(Object content) { | 16 _StringBufferImpl(Object content) { |
| 46 clear(); | 17 clear(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 if (_buffer.length == 0) return ""; | 57 if (_buffer.length == 0) return ""; |
| 87 if (_buffer.length == 1) return _buffer[0]; | 58 if (_buffer.length == 1) return _buffer[0]; |
| 88 String result = Strings.concatAll(_buffer); | 59 String result = Strings.concatAll(_buffer); |
| 89 _buffer.clear(); | 60 _buffer.clear(); |
| 90 _buffer.add(result); | 61 _buffer.add(result); |
| 91 // Since we track the length at each add operation, there is no | 62 // Since we track the length at each add operation, there is no |
| 92 // need to update it in this function. | 63 // need to update it in this function. |
| 93 return result; | 64 return result; |
| 94 } | 65 } |
| 95 } | 66 } |
| OLD | NEW |