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