| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.codec; | |
| 6 | |
| 7 /** | |
| 8 * Open-ended Encoding enum. | |
| 9 */ | |
| 10 // TODO(floitsch): dart:io already has an Encoding class. If we can't | |
| 11 // consolitate them, we need to remove `Encoding` here. | |
| 12 abstract class Encoding extends Codec<String, List<int>> { | |
| 13 const Encoding(); | |
| 14 } | |
| 15 | |
| 16 // TODO(floitsch): add other encodings, like ASCII and ISO_8859_1. | |
| 17 const UTF8 = const Utf8Codec(); | |
| 18 | |
| 19 /** | |
| 20 * A [Utf8Codec] encodes strings to utf-8 code units (bytes) and decodes | |
| 21 * UTF-8 code units to strings. | |
| 22 */ | |
| 23 class Utf8Codec extends Encoding { | |
| 24 final bool _allowMalformed; | |
| 25 | |
| 26 /** | |
| 27 * Instantiates a new [Utf8Codec]. | |
| 28 * | |
| 29 * The optional [allowMalformed] argument defines how [decoder] (and [decode]) | |
| 30 * deal with invalid or unterminated character sequences. | |
| 31 * | |
| 32 * If it is `true` (and not overriden at the method invocation) [decode] and | |
| 33 * the [decoder] replace invalid (or unterminated) octet | |
| 34 * sequences with the Unicode Replacement character `U+FFFD` (�). Otherwise | |
| 35 * they throw a [FormatException]. | |
| 36 */ | |
| 37 const Utf8Codec({ bool allowMalformed: false }) | |
| 38 : _allowMalformed = allowMalformed; | |
| 39 | |
| 40 /** | |
| 41 * Decodes the UTF-8 [codeUnits] (a list of unsigned 8-bit integers) to the | |
| 42 * corresponding string. | |
| 43 * | |
| 44 * If [allowMalformed] is `true` the decoder replaces invalid (or | |
| 45 * unterminated) character sequences with the Unicode Replacement character | |
| 46 * `U+FFFD` (�). Otherwise it throws a [FormatException]. | |
| 47 * | |
| 48 * If [allowMalformed] is not given, it defaults to the `allowMalformed` that | |
| 49 * was used to instantiate `this`. | |
| 50 */ | |
| 51 String decode(List<int> codeUnits, { bool allowMalformed }) { | |
| 52 if (allowMalformed == null) allowMalformed = _allowMalformed; | |
| 53 return new Utf8Decoder(allowMalformed: allowMalformed).convert(codeUnits); | |
| 54 } | |
| 55 | |
| 56 Converter<String, List<int>> get encoder => new Utf8Encoder(); | |
| 57 Converter<List<int>, String> get decoder { | |
| 58 return new Utf8Decoder(allowMalformed: _allowMalformed); | |
| 59 } | |
| 60 } | |
| OLD | NEW |