Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Side by Side Diff: pkg/dev_compiler/tool/input_sdk/lib/convert/codec.dart

Issue 2698353003: unfork DDC's copy of most SDK libraries (Closed)
Patch Set: revert core_patch Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.convert;
6
7 /**
8 * A [Codec] encodes and (if supported) decodes data.
9 *
10 * Codecs can be fused. For example fusing [JSON] and [UTF8] produces
11 * an encoder that can convert Json objects directly to bytes, or can decode
12 * bytes directly to json objects.
13 *
14 * Fused codecs generally attempt to optimize the operations and can be faster
15 * than executing each step of an encoding separately.
16 *
17 * *Codecs are still experimental and are subject to change without notice.*
18 */
19 abstract class Codec<S, T> {
20 const Codec();
21
22 T encode(S input) => encoder.convert(input);
23 S decode(T encoded) => decoder.convert(encoded);
24
25 /**
26 * Returns the encoder from [S] to [T].
27 *
28 * It may be stateful and should not be reused.
29 */
30 Converter<S, T> get encoder;
31 /**
32 * Returns the decoder of `this`, converting from [T] to [S].
33 *
34 * It may be stateful an should not be reused.
35 */
36 Converter<T, S> get decoder;
37
38 /**
39 * Fuses `this` with `other`.
40 *
41 * When encoding, the resulting codec encodes with `this` before
42 * encoding with [other].
43 *
44 * When decoding, the resulting codec decodes with [other] before decoding
45 * with `this`.
46 *
47 * In some cases one needs to use the [inverted] codecs to be able to fuse
48 * them correctly. That is, the output type of `this` ([T]) must match the
49 * input type of the second codec [other].
50 *
51 * Examples:
52 *
53 * final JSON_TO_BYTES = JSON.fuse(UTF8);
54 * List<int> bytes = JSON_TO_BYTES.encode(["json-object"]);
55 * var decoded = JSON_TO_BYTES.decode(bytes);
56 * assert(decoded is List && decoded[0] == "json-object");
57 *
58 * var inverted = JSON.inverted;
59 * var jsonIdentity = JSON.fuse(inverted);
60 * var jsonObject = jsonIdentity.encode(["1", 2]);
61 * assert(jsonObject is List && jsonObject[0] == "1" && jsonObject[1] == 2 );
62 */
63 // TODO(floitsch): use better example with line-splitter once that one is
64 // in this library.
65 Codec<S, dynamic/*=R*/> fuse/*<R>*/(Codec<T, dynamic/*=R*/> other) {
66 return new _FusedCodec<S, T, dynamic/*=R*/>(this, other);
67 }
68
69 /**
70 * Inverts `this`.
71 *
72 * The [encoder] and [decoder] of the resulting codec are swapped.
73 */
74 Codec<T, S> get inverted => new _InvertedCodec<T, S>(this);
75 }
76
77 /**
78 * Fuses the given codecs.
79 *
80 * In the non-chunked conversion simply invokes the non-chunked conversions in
81 * sequence.
82 */
83 class _FusedCodec<S, M, T> extends Codec<S, T> {
84 final Codec<S, M> _first;
85 final Codec<M, T> _second;
86
87 Converter<S, T> get encoder => _first.encoder.fuse/*<T>*/(_second.encoder);
88 Converter<T, S> get decoder => _second.decoder.fuse/*<S>*/(_first.decoder);
89
90 _FusedCodec(this._first, this._second);
91 }
92
93 class _InvertedCodec<T, S> extends Codec<T, S> {
94 final Codec<S, T> _codec;
95
96 _InvertedCodec(Codec<S, T> codec) : _codec = codec;
97
98 Converter<T, S> get encoder => _codec.decoder;
99 Converter<S, T> get decoder => _codec.encoder;
100
101 Codec<S, T> get inverted => _codec;
102 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698