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 [AsciiCodec]. | 8 * An instance of the default implementation of the [AsciiCodec]. |
9 * | 9 * |
10 * This instance provides a convenient access to the most common ASCII | 10 * This instance provides a convenient access to the most common ASCII |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 /** | 44 /** |
45 * Decodes the ASCII [bytes] (a list of unsigned 7-bit integers) to the | 45 * Decodes the ASCII [bytes] (a list of unsigned 7-bit integers) to the |
46 * corresponding string. | 46 * corresponding string. |
47 * | 47 * |
48 * If [bytes] contains values that are not in the range 0 .. 127, the decoder | 48 * If [bytes] contains values that are not in the range 0 .. 127, the decoder |
49 * will eventually throw a [FormatException]. | 49 * will eventually throw a [FormatException]. |
50 * | 50 * |
51 * If [allowInvalid] is not provided, it defaults to the value used to create | 51 * If [allowInvalid] is not provided, it defaults to the value used to create |
52 * this [AsciiCodec]. | 52 * this [AsciiCodec]. |
53 */ | 53 */ |
54 String decode(List<int> bytes, { bool allowInvalid }) { | 54 String decode(List<int> bytes, {bool allowInvalid}) { |
55 if (allowInvalid == null) allowInvalid = _allowInvalid; | 55 if (allowInvalid == null) allowInvalid = _allowInvalid; |
56 if (allowInvalid) { | 56 if (allowInvalid) { |
57 return const AsciiDecoder(allowInvalid: true).convert(bytes); | 57 return const AsciiDecoder(allowInvalid: true).convert(bytes); |
58 } else { | 58 } else { |
59 return const AsciiDecoder(allowInvalid: false).convert(bytes); | 59 return const AsciiDecoder(allowInvalid: false).convert(bytes); |
60 } | 60 } |
61 } | 61 } |
62 | 62 |
63 AsciiEncoder get encoder => const AsciiEncoder(); | 63 AsciiEncoder get encoder => const AsciiEncoder(); |
64 | 64 |
65 AsciiDecoder get decoder => | 65 AsciiDecoder get decoder => _allowInvalid |
66 _allowInvalid ? const AsciiDecoder(allowInvalid: true) | 66 ? const AsciiDecoder(allowInvalid: true) |
67 : const AsciiDecoder(allowInvalid: false); | 67 : const AsciiDecoder(allowInvalid: false); |
68 } | 68 } |
69 | 69 |
70 // Superclass for [AsciiEncoder] and [Latin1Encoder]. | 70 // Superclass for [AsciiEncoder] and [Latin1Encoder]. |
71 // Generalizes common operations that only differ by a mask; | 71 // Generalizes common operations that only differ by a mask; |
72 class _UnicodeSubsetEncoder extends Converter<String, List<int>> | 72 class _UnicodeSubsetEncoder extends Converter<String, List<int>> |
73 implements ChunkedConverter<String, List<int>, String, List<int>> { | 73 implements ChunkedConverter<String, List<int>, String, List<int>> { |
74 | |
75 final int _subsetMask; | 74 final int _subsetMask; |
76 | 75 |
77 const _UnicodeSubsetEncoder(this._subsetMask); | 76 const _UnicodeSubsetEncoder(this._subsetMask); |
78 | 77 |
79 /** | 78 /** |
80 * Converts the [String] into a list of its code units. | 79 * Converts the [String] into a list of its code units. |
81 * | 80 * |
82 * If [start] and [end] are provided, only the substring | 81 * If [start] and [end] are provided, only the substring |
83 * `string.substring(start, end)` is used as input to the conversion. | 82 * `string.substring(start, end)` is used as input to the conversion. |
84 */ | 83 */ |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 } | 150 } |
152 } | 151 } |
153 } | 152 } |
154 | 153 |
155 /** | 154 /** |
156 * This class converts Latin-1 bytes (lists of unsigned 8-bit integers) | 155 * This class converts Latin-1 bytes (lists of unsigned 8-bit integers) |
157 * to a string. | 156 * to a string. |
158 */ | 157 */ |
159 abstract class _UnicodeSubsetDecoder extends Converter<List<int>, String> | 158 abstract class _UnicodeSubsetDecoder extends Converter<List<int>, String> |
160 implements ChunkedConverter<List<int>, String, List<int>, String> { | 159 implements ChunkedConverter<List<int>, String, List<int>, String> { |
161 | |
162 final bool _allowInvalid; | 160 final bool _allowInvalid; |
163 final int _subsetMask; | 161 final int _subsetMask; |
164 | 162 |
165 /** | 163 /** |
166 * Instantiates a new decoder. | 164 * Instantiates a new decoder. |
167 * | 165 * |
168 * The [_allowInvalid] argument defines how [convert] deals | 166 * The [_allowInvalid] argument defines how [convert] deals |
169 * with invalid bytes. | 167 * with invalid bytes. |
170 * | 168 * |
171 * The [_subsetMask] argument is a bit mask used to define the subset | 169 * The [_subsetMask] argument is a bit mask used to define the subset |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 void add(List<int> source) { | 262 void add(List<int> source) { |
265 addSlice(source, 0, source.length, false); | 263 addSlice(source, 0, source.length, false); |
266 } | 264 } |
267 | 265 |
268 void addSlice(List<int> source, int start, int end, bool isLast) { | 266 void addSlice(List<int> source, int start, int end, bool isLast) { |
269 RangeError.checkValidRange(start, end, source.length); | 267 RangeError.checkValidRange(start, end, source.length); |
270 for (int i = start; i < end; i++) { | 268 for (int i = start; i < end; i++) { |
271 if ((source[i] & ~_ASCII_MASK) != 0) { | 269 if ((source[i] & ~_ASCII_MASK) != 0) { |
272 if (i > start) _utf8Sink.addSlice(source, start, i, false); | 270 if (i > start) _utf8Sink.addSlice(source, start, i, false); |
273 // Add UTF-8 encoding of U+FFFD. | 271 // Add UTF-8 encoding of U+FFFD. |
274 _utf8Sink.add(const<int>[0xEF, 0xBF, 0xBD]); | 272 _utf8Sink.add(const <int>[0xEF, 0xBF, 0xBD]); |
275 start = i + 1; | 273 start = i + 1; |
276 } | 274 } |
277 } | 275 } |
278 if (start < end) { | 276 if (start < end) { |
279 _utf8Sink.addSlice(source, start, end, isLast); | 277 _utf8Sink.addSlice(source, start, end, isLast); |
280 } else if (isLast) { | 278 } else if (isLast) { |
281 close(); | 279 close(); |
282 } | 280 } |
283 } | 281 } |
284 } | 282 } |
(...skipping 20 matching lines...) Expand all Loading... |
305 RangeError.checkValidRange(start, end, length); | 303 RangeError.checkValidRange(start, end, length); |
306 if (start < end) { | 304 if (start < end) { |
307 if (start != 0 || end != length) { | 305 if (start != 0 || end != length) { |
308 source = source.sublist(start, end); | 306 source = source.sublist(start, end); |
309 } | 307 } |
310 add(source); | 308 add(source); |
311 } | 309 } |
312 if (isLast) close(); | 310 if (isLast) close(); |
313 } | 311 } |
314 } | 312 } |
OLD | NEW |