Chromium Code Reviews| Index: sdk/lib/convert/utf.dart |
| diff --git a/sdk/lib/convert/utf.dart b/sdk/lib/convert/utf.dart |
| index b7fc365265b3f99770a965a424871340b5f07665..99d97688ec9bc4adfcc09dfc6d9d681640eb61e1 100644 |
| --- a/sdk/lib/convert/utf.dart |
| +++ b/sdk/lib/convert/utf.dart |
| @@ -4,6 +4,51 @@ |
| part of dart.convert; |
| +const UTF8 = const Utf8Codec(); |
| + |
| +/** |
| + * A [Utf8Codec] encodes strings to utf-8 code units (bytes) and decodes |
| + * UTF-8 code units to strings. |
| + */ |
| +class Utf8Codec extends Encoding { |
| + final bool _allowMalformed; |
| + |
| + /** |
| + * Instantiates a new [Utf8Codec]. |
| + * |
| + * The optional [allowMalformed] argument defines how [decoder] (and [decode]) |
| + * deal with invalid or unterminated character sequences. |
| + * |
| + * If it is `true` (and not overriden at the method invocation) [decode] and |
| + * the [decoder] replace invalid (or unterminated) octet |
| + * 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
|
| + * they throw a [FormatException]. |
| + */ |
| + const Utf8Codec({ bool allowMalformed: false }) |
| + : _allowMalformed = allowMalformed; |
| + |
| + /** |
| + * Decodes the UTF-8 [codeUnits] (a list of unsigned 8-bit integers) to the |
| + * corresponding string. |
| + * |
| + * If [allowMalformed] is `true` the decoder replaces invalid (or |
| + * unterminated) character sequences with the Unicode Replacement character |
| + * `U+FFFD` (�). Otherwise it throws a [FormatException]. |
| + * |
| + * If [allowMalformed] is not given, it defaults to the `allowMalformed` that |
| + * was used to instantiate `this`. |
| + */ |
| + String decode(List<int> codeUnits, { bool allowMalformed }) { |
| + if (allowMalformed == null) allowMalformed = _allowMalformed; |
| + return new Utf8Decoder(allowMalformed: allowMalformed).convert(codeUnits); |
| + } |
| + |
| + Converter<String, List<int>> get encoder => new Utf8Encoder(); |
| + Converter<List<int>, String> get decoder { |
| + return new Utf8Decoder(allowMalformed: _allowMalformed); |
| + } |
| +} |
| + |
| /** |
| * A [Utf8Encoder] converts strings to their UTF-8 code units (a list of |
| * unsigned 8-bit integers). |