| 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 // TODO(floitsch): remove or rewrite this call to clear. It is currently | 17 clear(); |
| 12 // necessary because the VM doesn't allow default values for fields in | 18 add(content); |
| 13 // patch classes. | |
| 14 _buffer = new List<String>(); | |
| 15 _length = 0; | |
| 16 write(content); | |
| 17 } | 19 } |
| 18 | 20 |
| 19 /* patch */ int get length => _length; | 21 /// Returns the length of the buffer. |
| 22 int get length => _length; |
| 23 |
| 24 bool get isEmpty => _length == 0; |
| 20 | 25 |
| 21 /// Adds [obj] to the buffer. | 26 /// Adds [obj] to the buffer. |
| 22 /* patch */ void _write(String str) { | 27 void add(Object obj) { |
| 28 // TODO(srdjan): The following four lines could be replaced by |
| 29 // '$obj', but apparently this is too slow on the Dart VM. |
| 30 String str = obj.toString(); |
| 31 if (str is !String) { |
| 32 throw new ArgumentError('toString() did not return a string'); |
| 33 } |
| 34 if (str.isEmpty) return; |
| 23 _buffer.add(str); | 35 _buffer.add(str); |
| 24 _length += str.length; | 36 _length += str.length; |
| 25 } | 37 } |
| 26 | 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 |
| 27 /// Clears the string buffer. | 49 /// Clears the string buffer. |
| 28 /* patch */ void clear() { | 50 void clear() { |
| 29 _buffer = new List<String>(); | 51 _buffer = new List<String>(); |
| 30 _length = 0; | 52 _length = 0; |
| 31 } | 53 } |
| 32 | 54 |
| 33 /// Returns the contents of buffer as a concatenated string. | 55 /// Returns the contents of buffer as a concatenated string. |
| 34 /* patch */ String toString() { | 56 String toString() { |
| 35 if (_buffer.length == 0) return ""; | 57 if (_buffer.length == 0) return ""; |
| 36 if (_buffer.length == 1) return _buffer[0]; | 58 if (_buffer.length == 1) return _buffer[0]; |
| 37 String result = Strings.concatAll(_buffer); | 59 String result = Strings.concatAll(_buffer); |
| 38 _buffer.clear(); | 60 _buffer.clear(); |
| 39 _buffer.add(result); | 61 _buffer.add(result); |
| 40 // 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 |
| 41 // need to update it in this function. | 63 // need to update it in this function. |
| 42 return result; | 64 return result; |
| 43 } | 65 } |
| 44 } | 66 } |
| OLD | NEW |