OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart.convert; | 5 part of dart.convert; |
6 | 6 |
7 /** | 7 /** |
8 * An instance of the default implementation of the [Latin1Codec]. | 8 * An instance of the default implementation of the [Latin1Codec]. |
9 * | 9 * |
10 * This instance provides a convenient access to the most common ISO Latin 1 | 10 * This instance provides a convenient access to the most common ISO Latin 1 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 } | 119 } |
120 | 120 |
121 void add(List<int> source) { | 121 void add(List<int> source) { |
122 addSlice(source, 0, source.length, false); | 122 addSlice(source, 0, source.length, false); |
123 } | 123 } |
124 | 124 |
125 void _addSliceToSink(List<int> source, int start, int end, bool isLast) { | 125 void _addSliceToSink(List<int> source, int start, int end, bool isLast) { |
126 // If _sink was a UTF-16 conversion sink, just add the slice directly with | 126 // If _sink was a UTF-16 conversion sink, just add the slice directly with |
127 // _sink.addSlice(source, start, end, isLast). | 127 // _sink.addSlice(source, start, end, isLast). |
128 // The code below is an incredibly stupid workaround until a real | 128 // The code below is an incredibly stupid workaround until a real |
129 // solution can be made. | 129 // solution can be made. |
Lasse Reichstein Nielsen
2014/03/24 09:21:01
Is it still incredibly stupid, or can we downgrade
Anders Johnsen
2014/03/24 12:01:01
Done.
| |
130 _sink.add(new String.fromCharCodes(source.getRange(start, end))); | 130 if (start == 0 && end == source.length) { |
131 _sink.add(new String.fromCharCodes(source)); | |
132 } else { | |
133 _sink.add(new String.fromCharCodes(source.sublist(start, end))); | |
134 } | |
131 if (isLast) close(); | 135 if (isLast) close(); |
132 } | 136 } |
133 | 137 |
134 void addSlice(List<int> source, int start, int end, bool isLast) { | 138 void addSlice(List<int> source, int start, int end, bool isLast) { |
139 // If Uint8List, just add dircetly. | |
140 if (source is Uint8List || source is Int8List) { | |
Lasse Reichstein Nielsen
2014/03/24 09:21:01
Int8List isn't safe. It can contain negative numbe
Anders Johnsen
2014/03/24 12:01:01
Done.
| |
141 _addSliceToSink(source, start, end, isLast); | |
142 return; | |
143 } | |
135 if (start < 0 || start > source.length) { | 144 if (start < 0 || start > source.length) { |
136 throw new RangeError.range(start, 0, source.length); | 145 throw new RangeError.range(start, 0, source.length); |
137 } | 146 } |
138 if (end < start || end > source.length) { | 147 if (end < start || end > source.length) { |
139 throw new RangeError.range(end, start, source.length); | 148 throw new RangeError.range(end, start, source.length); |
140 } | 149 } |
141 for (int i = start; i < end; i++) { | 150 for (int i = start; i < end; i++) { |
142 if ((source[i] & ~_LATIN1_MASK) != 0) { | 151 if ((source[i] & ~_LATIN1_MASK) != 0) { |
143 if (_allowInvalid) { | 152 if (_allowInvalid) { |
144 if (i > start) _addSliceToSink(source, start, i, false); | 153 if (i > start) _addSliceToSink(source, start, i, false); |
145 // Add UTF-8 encoding of U+FFFD. | 154 // Add UTF-8 encoding of U+FFFD. |
146 _addSliceToSink(const[0xFFFD], 0, 1, false); | 155 _addSliceToSink(const[0xFFFD], 0, 1, false); |
147 start = i + 1; | 156 start = i + 1; |
148 } else { | 157 } else { |
149 throw new FormatException("Source contains non-Latin-1 characters."); | 158 throw new FormatException("Source contains non-Latin-1 characters."); |
150 } | 159 } |
151 } | 160 } |
152 } | 161 } |
153 if (start < end) { | 162 if (start < end) { |
154 _addSliceToSink(source, start, end, isLast); | 163 _addSliceToSink(source, start, end, isLast); |
155 } else if (isLast) { | 164 } else if (isLast) { |
156 close(); | 165 close(); |
157 } | 166 } |
158 } | 167 } |
159 } | 168 } |
OLD | NEW |