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 class StringBuffer { |
6 static const int _BUFFER_SIZE = 64; | 6 static const int _BUFFER_SIZE = 64; |
7 static const int _PARTS_TO_COMPACT = 128; | 7 static const int _PARTS_TO_COMPACT = 128; |
8 static const int _PARTS_TO_COMPACT_SIZE_LIMIT = _PARTS_TO_COMPACT * 8; | 8 static const int _PARTS_TO_COMPACT_SIZE_LIMIT = _PARTS_TO_COMPACT * 8; |
9 | 9 |
10 /** | 10 /** |
(...skipping 30 matching lines...) Expand all Loading... |
41 * Collects the approximate maximal magnitude of the code units added | 41 * Collects the approximate maximal magnitude of the code units added |
42 * to the buffer. | 42 * to the buffer. |
43 * | 43 * |
44 * The value of each added code unit is or'ed with this variable, so the | 44 * The value of each added code unit is or'ed with this variable, so the |
45 * most significant bit set in any code unit is also set in this value. | 45 * most significant bit set in any code unit is also set in this value. |
46 * If below 256, the string in the buffer is a Latin-1 string. | 46 * If below 256, the string in the buffer is a Latin-1 string. |
47 */ | 47 */ |
48 int _bufferCodeUnitMagnitude = 0; | 48 int _bufferCodeUnitMagnitude = 0; |
49 | 49 |
50 /// Creates the string buffer with an initial content. | 50 /// Creates the string buffer with an initial content. |
51 /* @patch */ StringBuffer([Object content = ""]) { | 51 @patch StringBuffer([Object content = ""]) { |
52 write(content); | 52 write(content); |
53 } | 53 } |
54 | 54 |
55 /* @patch */ int get length => _partsCodeUnits + _bufferPosition; | 55 @patch int get length => _partsCodeUnits + _bufferPosition; |
56 | 56 |
57 /* @patch */ void write(Object obj) { | 57 @patch void write(Object obj) { |
58 String str = '$obj'; | 58 String str = '$obj'; |
59 if (str.isEmpty) return; | 59 if (str.isEmpty) return; |
60 _consumeBuffer(); | 60 _consumeBuffer(); |
61 _addPart(str); | 61 _addPart(str); |
62 } | 62 } |
63 | 63 |
64 /* @patch */ void writeCharCode(int charCode) { | 64 @patch void writeCharCode(int charCode) { |
65 if (charCode <= 0xFFFF) { | 65 if (charCode <= 0xFFFF) { |
66 if (charCode < 0) { | 66 if (charCode < 0) { |
67 throw new RangeError.range(charCode, 0, 0x10FFFF); | 67 throw new RangeError.range(charCode, 0, 0x10FFFF); |
68 } | 68 } |
69 _ensureCapacity(1); | 69 _ensureCapacity(1); |
70 _buffer[_bufferPosition++] = charCode; | 70 _buffer[_bufferPosition++] = charCode; |
71 _bufferCodeUnitMagnitude |= charCode; | 71 _bufferCodeUnitMagnitude |= charCode; |
72 } else { | 72 } else { |
73 if (charCode > 0x10FFFF) { | 73 if (charCode > 0x10FFFF) { |
74 throw new RangeError.range(charCode, 0, 0x10FFFF); | 74 throw new RangeError.range(charCode, 0, 0x10FFFF); |
75 } | 75 } |
76 _ensureCapacity(2); | 76 _ensureCapacity(2); |
77 int bits = charCode - 0x10000; | 77 int bits = charCode - 0x10000; |
78 _buffer[_bufferPosition++] = 0xD800 | (bits >> 10); | 78 _buffer[_bufferPosition++] = 0xD800 | (bits >> 10); |
79 _buffer[_bufferPosition++] = 0xDC00 | (bits & 0x3FF); | 79 _buffer[_bufferPosition++] = 0xDC00 | (bits & 0x3FF); |
80 _bufferCodeUnitMagnitude |= 0xFFFF; | 80 _bufferCodeUnitMagnitude |= 0xFFFF; |
81 } | 81 } |
82 } | 82 } |
83 | 83 |
84 /* @patch */ void writeAll(Iterable objects, [String separator = ""]) { | 84 @patch void writeAll(Iterable objects, [String separator = ""]) { |
85 Iterator iterator = objects.iterator; | 85 Iterator iterator = objects.iterator; |
86 if (!iterator.moveNext()) return; | 86 if (!iterator.moveNext()) return; |
87 if (separator.isEmpty) { | 87 if (separator.isEmpty) { |
88 do { | 88 do { |
89 write(iterator.current); | 89 write(iterator.current); |
90 } while (iterator.moveNext()); | 90 } while (iterator.moveNext()); |
91 } else { | 91 } else { |
92 write(iterator.current); | 92 write(iterator.current); |
93 while (iterator.moveNext()) { | 93 while (iterator.moveNext()) { |
94 write(separator); | 94 write(separator); |
95 write(iterator.current); | 95 write(iterator.current); |
96 } | 96 } |
97 } | 97 } |
98 } | 98 } |
99 | 99 |
100 /* @patch */ void writeln([Object obj = ""]) { | 100 @patch void writeln([Object obj = ""]) { |
101 write(obj); | 101 write(obj); |
102 write("\n"); | 102 write("\n"); |
103 } | 103 } |
104 | 104 |
105 /** Makes the buffer empty. */ | 105 /** Makes the buffer empty. */ |
106 /* @patch */ void clear() { | 106 @patch void clear() { |
107 _parts = null; | 107 _parts = null; |
108 _partsCodeUnits = _bufferPosition = _bufferCodeUnitMagnitude = 0; | 108 _partsCodeUnits = _bufferPosition = _bufferCodeUnitMagnitude = 0; |
109 } | 109 } |
110 | 110 |
111 /** Returns the contents of buffer as a string. */ | 111 /** Returns the contents of buffer as a string. */ |
112 /* @patch */ String toString() { | 112 @patch String toString() { |
113 _consumeBuffer(); | 113 _consumeBuffer(); |
114 return (_partsCodeUnits == 0) ? | 114 return (_partsCodeUnits == 0) ? |
115 "" : | 115 "" : |
116 _StringBase._concatRange(_parts, 0, _parts.length); | 116 _StringBase._concatRange(_parts, 0, _parts.length); |
117 } | 117 } |
118 | 118 |
119 /** Ensures that the buffer has enough capacity to add n code units. */ | 119 /** Ensures that the buffer has enough capacity to add n code units. */ |
120 void _ensureCapacity(int n) { | 120 void _ensureCapacity(int n) { |
121 if (_buffer == null) { | 121 if (_buffer == null) { |
122 _buffer = new Uint16List(_BUFFER_SIZE); | 122 _buffer = new Uint16List(_BUFFER_SIZE); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 _partsCodeUnitsSinceCompaction = 0; | 176 _partsCodeUnitsSinceCompaction = 0; |
177 _partsCompactionIndex = _parts.length; | 177 _partsCompactionIndex = _parts.length; |
178 } | 178 } |
179 | 179 |
180 /** | 180 /** |
181 * Create a [String] from the UFT-16 code units in buffer. | 181 * Create a [String] from the UFT-16 code units in buffer. |
182 */ | 182 */ |
183 static String _create(Uint16List buffer, int length, bool isLatin1) | 183 static String _create(Uint16List buffer, int length, bool isLatin1) |
184 native "StringBuffer_createStringFromUint16Array"; | 184 native "StringBuffer_createStringFromUint16Array"; |
185 } | 185 } |
OLD | NEW |