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 const UTF8 = const Utf8Codec(); | |
8 | |
9 /** | |
10 * A [Utf8Codec] encodes strings to utf-8 code units (bytes) and decodes | |
11 * UTF-8 code units to strings. | |
12 */ | |
13 class Utf8Codec extends Encoding { | |
14 final bool _allowMalformed; | |
15 | |
16 /** | |
17 * Instantiates a new [Utf8Codec]. | |
18 * | |
19 * The optional [allowMalformed] argument defines how [decoder] (and [decode]) | |
20 * deal with invalid or unterminated character sequences. | |
21 * | |
22 * If it is `true` (and not overriden at the method invocation) [decode] and | |
23 * the [decoder] replace invalid (or unterminated) octet | |
24 * sequences with the Unicode Replacement character `U+FFFD` (�). Otherwise | |
Søren Gjesse
2013/07/22 12:04:42
Should it be possible to configure the replacement
floitsch
2013/07/22 12:24:58
We thought of it, but in the end decided against i
| |
25 * they throw a [FormatException]. | |
26 */ | |
27 const Utf8Codec({ bool allowMalformed: false }) | |
28 : _allowMalformed = allowMalformed; | |
29 | |
30 /** | |
31 * Decodes the UTF-8 [codeUnits] (a list of unsigned 8-bit integers) to the | |
32 * corresponding string. | |
33 * | |
34 * If [allowMalformed] is `true` the decoder replaces invalid (or | |
35 * unterminated) character sequences with the Unicode Replacement character | |
36 * `U+FFFD` (�). Otherwise it throws a [FormatException]. | |
37 * | |
38 * If [allowMalformed] is not given, it defaults to the `allowMalformed` that | |
39 * was used to instantiate `this`. | |
40 */ | |
41 String decode(List<int> codeUnits, { bool allowMalformed }) { | |
42 if (allowMalformed == null) allowMalformed = _allowMalformed; | |
43 return new Utf8Decoder(allowMalformed: allowMalformed).convert(codeUnits); | |
44 } | |
45 | |
46 Converter<String, List<int>> get encoder => new Utf8Encoder(); | |
47 Converter<List<int>, String> get decoder { | |
48 return new Utf8Decoder(allowMalformed: _allowMalformed); | |
49 } | |
50 } | |
51 | |
7 /** | 52 /** |
8 * A [Utf8Encoder] converts strings to their UTF-8 code units (a list of | 53 * A [Utf8Encoder] converts strings to their UTF-8 code units (a list of |
9 * unsigned 8-bit integers). | 54 * unsigned 8-bit integers). |
10 */ | 55 */ |
11 class Utf8Encoder extends Converter<String, List<int>> { | 56 class Utf8Encoder extends Converter<String, List<int>> { |
12 /** | 57 /** |
13 * Converts [string] to its UTF-8 code units (a list of | 58 * Converts [string] to its UTF-8 code units (a list of |
14 * unsigned 8-bit integers). | 59 * unsigned 8-bit integers). |
15 */ | 60 */ |
16 List<int> convert(String string) => OLD_UTF_LIB.encodeUtf8(string); | 61 List<int> convert(String string) => OLD_UTF_LIB.encodeUtf8(string); |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 } | 243 } |
199 break loop; | 244 break loop; |
200 } | 245 } |
201 if (expectedUnits > 0) { | 246 if (expectedUnits > 0) { |
202 _value = value; | 247 _value = value; |
203 _expectedUnits = expectedUnits; | 248 _expectedUnits = expectedUnits; |
204 _extraUnits = extraUnits; | 249 _extraUnits = extraUnits; |
205 } | 250 } |
206 } | 251 } |
207 } | 252 } |
OLD | NEW |