| OLD | NEW |
| 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 class StringBuffer { | 5 patch /* abstract */ class StringBuffer { |
| 6 /* patch */ factory StringBuffer([Object content = ""]) |
| 7 => new _StringBufferImpl(content); |
| 8 } |
| 9 |
| 10 class _StringBufferImpl implements StringBuffer { |
| 11 |
| 6 List<String> _buffer; | 12 List<String> _buffer; |
| 7 int _length; | 13 int _length; |
| 8 | 14 |
| 9 /// Creates the string buffer with an initial content. | 15 /// Creates the string buffer with an initial content. |
| 10 /* patch */ StringBuffer([Object content = ""]) { | 16 _StringBufferImpl(Object content) { |
| 11 _buffer = new List<String>(); | 17 clear(); |
| 12 _length = 0; | 18 add(content); |
| 13 write(content); | |
| 14 } | 19 } |
| 15 | 20 |
| 16 /* patch */ int get length => _length; | 21 /// Returns the length of the buffer. |
| 22 int get length => _length; |
| 23 |
| 24 bool get isEmpty => _length == 0; |
| 17 | 25 |
| 18 /// Adds [obj] to the buffer. | 26 /// Adds [obj] to the buffer. |
| 19 /* patch */ void write(Object obj) { | 27 void add(Object obj) { |
| 20 // TODO(srdjan): The following four lines could be replaced by | 28 // TODO(srdjan): The following four lines could be replaced by |
| 21 // '$obj', but apparently this is too slow on the Dart VM. | 29 // '$obj', but apparently this is too slow on the Dart VM. |
| 22 String str; | 30 String str = obj.toString(); |
| 23 if (obj is String) { | 31 if (str is !String) { |
| 24 str = obj; | 32 throw new ArgumentError('toString() did not return a string'); |
| 25 } else { | |
| 26 str = obj.toString(); | |
| 27 if (str is! String) { | |
| 28 throw new ArgumentError('toString() did not return a string'); | |
| 29 } | |
| 30 } | 33 } |
| 31 if (str.isEmpty) return; | 34 if (str.isEmpty) return; |
| 32 _buffer.add(str); | 35 _buffer.add(str); |
| 33 _length += str.length; | 36 _length += str.length; |
| 34 } | 37 } |
| 35 | 38 |
| 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 |
| 36 /// Clears the string buffer. | 49 /// Clears the string buffer. |
| 37 /* patch */ void clear() { | 50 void clear() { |
| 38 _buffer = new List<String>(); | 51 _buffer = new List<String>(); |
| 39 _length = 0; | 52 _length = 0; |
| 40 } | 53 } |
| 41 | 54 |
| 42 /// Returns the contents of buffer as a concatenated string. | 55 /// Returns the contents of buffer as a concatenated string. |
| 43 /* patch */ String toString() { | 56 String toString() { |
| 44 if (_buffer.length == 0) return ""; | 57 if (_buffer.length == 0) return ""; |
| 45 if (_buffer.length == 1) return _buffer[0]; | 58 if (_buffer.length == 1) return _buffer[0]; |
| 46 String result = Strings.concatAll(_buffer); | 59 String result = Strings.concatAll(_buffer); |
| 47 _buffer.clear(); | 60 _buffer.clear(); |
| 48 _buffer.add(result); | 61 _buffer.add(result); |
| 49 // 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 |
| 50 // need to update it in this function. | 63 // need to update it in this function. |
| 51 return result; | 64 return result; |
| 52 } | 65 } |
| 53 } | 66 } |
| OLD | NEW |