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