| 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 import 'dart:typed_data' show Uint16List; | 5 import 'dart:typed_data' show Uint16List; |
| 6 | 6 |
| 7 @patch | 7 @patch class StringBuffer { |
| 8 class StringBuffer { | |
| 9 static const int _BUFFER_SIZE = 64; | 8 static const int _BUFFER_SIZE = 64; |
| 10 static const int _PARTS_TO_COMPACT = 128; | 9 static const int _PARTS_TO_COMPACT = 128; |
| 11 static const int _PARTS_TO_COMPACT_SIZE_LIMIT = _PARTS_TO_COMPACT * 8; | 10 static const int _PARTS_TO_COMPACT_SIZE_LIMIT = _PARTS_TO_COMPACT * 8; |
| 12 | 11 |
| 13 /** | 12 /** |
| 14 * When strings are written to the string buffer, we add them to a | 13 * When strings are written to the string buffer, we add them to a |
| 15 * list of string parts. | 14 * list of string parts. |
| 16 */ | 15 */ |
| 17 List<String> _parts; | 16 List<String> _parts; |
| 18 | 17 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 44 * Collects the approximate maximal magnitude of the code units added | 43 * Collects the approximate maximal magnitude of the code units added |
| 45 * to the buffer. | 44 * to the buffer. |
| 46 * | 45 * |
| 47 * The value of each added code unit is or'ed with this variable, so the | 46 * The value of each added code unit is or'ed with this variable, so the |
| 48 * most significant bit set in any code unit is also set in this value. | 47 * most significant bit set in any code unit is also set in this value. |
| 49 * If below 256, the string in the buffer is a Latin-1 string. | 48 * If below 256, the string in the buffer is a Latin-1 string. |
| 50 */ | 49 */ |
| 51 int _bufferCodeUnitMagnitude = 0; | 50 int _bufferCodeUnitMagnitude = 0; |
| 52 | 51 |
| 53 /// Creates the string buffer with an initial content. | 52 /// Creates the string buffer with an initial content. |
| 54 @patch | 53 @patch StringBuffer([Object content = ""]) { |
| 55 StringBuffer([Object content = ""]) { | |
| 56 write(content); | 54 write(content); |
| 57 } | 55 } |
| 58 | 56 |
| 59 @patch | 57 @patch int get length => _partsCodeUnits + _bufferPosition; |
| 60 int get length => _partsCodeUnits + _bufferPosition; | |
| 61 | 58 |
| 62 @patch | 59 @patch void write(Object obj) { |
| 63 void write(Object obj) { | |
| 64 String str = '$obj'; | 60 String str = '$obj'; |
| 65 if (str.isEmpty) return; | 61 if (str.isEmpty) return; |
| 66 _consumeBuffer(); | 62 _consumeBuffer(); |
| 67 _addPart(str); | 63 _addPart(str); |
| 68 } | 64 } |
| 69 | 65 |
| 70 @patch | 66 @patch void writeCharCode(int charCode) { |
| 71 void writeCharCode(int charCode) { | |
| 72 if (charCode <= 0xFFFF) { | 67 if (charCode <= 0xFFFF) { |
| 73 if (charCode < 0) { | 68 if (charCode < 0) { |
| 74 throw new RangeError.range(charCode, 0, 0x10FFFF); | 69 throw new RangeError.range(charCode, 0, 0x10FFFF); |
| 75 } | 70 } |
| 76 _ensureCapacity(1); | 71 _ensureCapacity(1); |
| 77 _buffer[_bufferPosition++] = charCode; | 72 _buffer[_bufferPosition++] = charCode; |
| 78 _bufferCodeUnitMagnitude |= charCode; | 73 _bufferCodeUnitMagnitude |= charCode; |
| 79 } else { | 74 } else { |
| 80 if (charCode > 0x10FFFF) { | 75 if (charCode > 0x10FFFF) { |
| 81 throw new RangeError.range(charCode, 0, 0x10FFFF); | 76 throw new RangeError.range(charCode, 0, 0x10FFFF); |
| 82 } | 77 } |
| 83 _ensureCapacity(2); | 78 _ensureCapacity(2); |
| 84 int bits = charCode - 0x10000; | 79 int bits = charCode - 0x10000; |
| 85 _buffer[_bufferPosition++] = 0xD800 | (bits >> 10); | 80 _buffer[_bufferPosition++] = 0xD800 | (bits >> 10); |
| 86 _buffer[_bufferPosition++] = 0xDC00 | (bits & 0x3FF); | 81 _buffer[_bufferPosition++] = 0xDC00 | (bits & 0x3FF); |
| 87 _bufferCodeUnitMagnitude |= 0xFFFF; | 82 _bufferCodeUnitMagnitude |= 0xFFFF; |
| 88 } | 83 } |
| 89 } | 84 } |
| 90 | 85 |
| 91 @patch | 86 @patch void writeAll(Iterable objects, [String separator = ""]) { |
| 92 void writeAll(Iterable objects, [String separator = ""]) { | |
| 93 Iterator iterator = objects.iterator; | 87 Iterator iterator = objects.iterator; |
| 94 if (!iterator.moveNext()) return; | 88 if (!iterator.moveNext()) return; |
| 95 if (separator.isEmpty) { | 89 if (separator.isEmpty) { |
| 96 do { | 90 do { |
| 97 write(iterator.current); | 91 write(iterator.current); |
| 98 } while (iterator.moveNext()); | 92 } while (iterator.moveNext()); |
| 99 } else { | 93 } else { |
| 100 write(iterator.current); | 94 write(iterator.current); |
| 101 while (iterator.moveNext()) { | 95 while (iterator.moveNext()) { |
| 102 write(separator); | 96 write(separator); |
| 103 write(iterator.current); | 97 write(iterator.current); |
| 104 } | 98 } |
| 105 } | 99 } |
| 106 } | 100 } |
| 107 | 101 |
| 108 @patch | 102 @patch void writeln([Object obj = ""]) { |
| 109 void writeln([Object obj = ""]) { | |
| 110 write(obj); | 103 write(obj); |
| 111 write("\n"); | 104 write("\n"); |
| 112 } | 105 } |
| 113 | 106 |
| 114 /** Makes the buffer empty. */ | 107 /** Makes the buffer empty. */ |
| 115 @patch | 108 @patch void clear() { |
| 116 void clear() { | |
| 117 _parts = null; | 109 _parts = null; |
| 118 _partsCodeUnits = _bufferPosition = _bufferCodeUnitMagnitude = 0; | 110 _partsCodeUnits = _bufferPosition = _bufferCodeUnitMagnitude = 0; |
| 119 } | 111 } |
| 120 | 112 |
| 121 /** Returns the contents of buffer as a string. */ | 113 /** Returns the contents of buffer as a string. */ |
| 122 @patch | 114 @patch String toString() { |
| 123 String toString() { | |
| 124 _consumeBuffer(); | 115 _consumeBuffer(); |
| 125 return (_partsCodeUnits == 0) | 116 return (_partsCodeUnits == 0) ? |
| 126 ? "" | 117 "" : |
| 127 : _StringBase._concatRange(_parts, 0, _parts.length); | 118 _StringBase._concatRange(_parts, 0, _parts.length); |
| 128 } | 119 } |
| 129 | 120 |
| 130 /** Ensures that the buffer has enough capacity to add n code units. */ | 121 /** Ensures that the buffer has enough capacity to add n code units. */ |
| 131 void _ensureCapacity(int n) { | 122 void _ensureCapacity(int n) { |
| 132 if (_buffer == null) { | 123 if (_buffer == null) { |
| 133 _buffer = new Uint16List(_BUFFER_SIZE); | 124 _buffer = new Uint16List(_BUFFER_SIZE); |
| 134 } else if (_bufferPosition + n > _buffer.length) { | 125 } else if (_bufferPosition + n > _buffer.length) { |
| 135 _consumeBuffer(); | 126 _consumeBuffer(); |
| 136 } | 127 } |
| 137 } | 128 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 } | 162 } |
| 172 | 163 |
| 173 /** | 164 /** |
| 174 * Compacts the last N parts if their average size allows us to save a | 165 * Compacts the last N parts if their average size allows us to save a |
| 175 * lot of memory by turning them all into a single part. | 166 * lot of memory by turning them all into a single part. |
| 176 */ | 167 */ |
| 177 void _compact() { | 168 void _compact() { |
| 178 if (_partsCodeUnitsSinceCompaction < _PARTS_TO_COMPACT_SIZE_LIMIT) { | 169 if (_partsCodeUnitsSinceCompaction < _PARTS_TO_COMPACT_SIZE_LIMIT) { |
| 179 String compacted = _StringBase._concatRange( | 170 String compacted = _StringBase._concatRange( |
| 180 _parts, | 171 _parts, |
| 181 _partsCompactionIndex, // Start | 172 _partsCompactionIndex, // Start |
| 182 _partsCompactionIndex + _PARTS_TO_COMPACT // End | 173 _partsCompactionIndex + _PARTS_TO_COMPACT // End |
| 183 ); | 174 ); |
| 184 _parts.length = _parts.length - _PARTS_TO_COMPACT; | 175 _parts.length = _parts.length - _PARTS_TO_COMPACT; |
| 185 _parts.add(compacted); | 176 _parts.add(compacted); |
| 186 } | 177 } |
| 187 _partsCodeUnitsSinceCompaction = 0; | 178 _partsCodeUnitsSinceCompaction = 0; |
| 188 _partsCompactionIndex = _parts.length; | 179 _partsCompactionIndex = _parts.length; |
| 189 } | 180 } |
| 190 | 181 |
| 191 /** | 182 /** |
| 192 * Create a [String] from the UFT-16 code units in buffer. | 183 * Create a [String] from the UFT-16 code units in buffer. |
| 193 */ | 184 */ |
| 194 static String _create(Uint16List buffer, int length, bool isLatin1) | 185 static String _create(Uint16List buffer, int length, bool isLatin1) |
| 195 native "StringBuffer_createStringFromUint16Array"; | 186 native "StringBuffer_createStringFromUint16Array"; |
| 196 } | 187 } |
| OLD | NEW |