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

Side by Side Diff: test/generated_sdk/lib/convert/latin1.dart

Issue 1162723007: remove generated_sdk from checked in code (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 6 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 * An instance of the default implementation of the [Latin1Codec].
9 *
10 * This instance provides a convenient access to the most common ISO Latin 1
11 * use cases.
12 *
13 * Examples:
14 *
15 * var encoded = LATIN1.encode("blåbærgrød");
16 * var decoded = LATIN1.decode([0x62, 0x6c, 0xe5, 0x62, 0xe6,
17 * 0x72, 0x67, 0x72, 0xf8, 0x64]);
18 */
19 const Latin1Codec LATIN1 = const Latin1Codec();
20
21 const int _LATIN1_MASK = 0xFF;
22
23 /**
24 * A [LatinCodec] encodes strings to ISO Latin-1 (aka ISO-8859-1) bytes
25 * and decodes Latin-1 bytes to strings.
26 */
27 class Latin1Codec extends Encoding {
28 final bool _allowInvalid;
29 /**
30 * Instantiates a new [Latin1Codec].
31 *
32 * If [allowInvalid] is true, the [decode] method and the converter
33 * returned by [decoder] will default to allowing invalid values. Invalid
34 * values are decoded into the Unicode Replacement character (U+FFFD).
35 * Calls to the [decode] method can override this default.
36 *
37 * Encoders will not accept invalid (non Latin-1) characters.
38 */
39 const Latin1Codec({bool allowInvalid: false}) : _allowInvalid = allowInvalid;
40
41 String get name => "iso-8859-1";
42
43 /**
44 * Decodes the Latin-1 [bytes] (a list of unsigned 8-bit integers) to the
45 * corresponding string.
46 *
47 * If [bytes] contains values that are not in the range 0 .. 255, the decoder
48 * will eventually throw a [FormatException].
49 *
50 * If [allowInvalid] is not provided, it defaults to the value used to create
51 * this [Latin1Codec].
52 */
53 String decode(List<int> bytes, { bool allowInvalid }) {
54 if (allowInvalid == null) allowInvalid = _allowInvalid;
55 if (allowInvalid) {
56 return const Latin1Decoder(allowInvalid: true).convert(bytes);
57 } else {
58 return const Latin1Decoder(allowInvalid: false).convert(bytes);
59 }
60 }
61
62 Converter<String, List<int>> get encoder => const Latin1Encoder();
63
64 Converter<List<int>, String> get decoder =>
65 _allowInvalid ? const Latin1Decoder(allowInvalid: true)
66 : const Latin1Decoder(allowInvalid: false);
67 }
68
69 /**
70 * This class converts strings of only ISO Latin-1 characters to bytes.
71 */
72 class Latin1Encoder extends _UnicodeSubsetEncoder {
73 const Latin1Encoder() : super(_LATIN1_MASK);
74 }
75
76 /**
77 * This class converts Latin-1 bytes (lists of unsigned 8-bit integers)
78 * to a string.
79 */
80 class Latin1Decoder extends _UnicodeSubsetDecoder {
81 /**
82 * Instantiates a new [Latin1Decoder].
83 *
84 * The optional [allowInvalid] argument defines how [convert] deals
85 * with invalid bytes.
86 *
87 * If it is `true`, [convert] replaces invalid bytes with the Unicode
88 * Replacement character `U+FFFD` (�).
89 * Otherwise it throws a [FormatException].
90 */
91 const Latin1Decoder({ bool allowInvalid: false })
92 : super(allowInvalid, _LATIN1_MASK);
93
94 /**
95 * Starts a chunked conversion.
96 *
97 * The converter works more efficiently if the given [sink] is a
98 * [StringConversionSink].
99 */
100 ByteConversionSink startChunkedConversion(Sink<String> sink) {
101 StringConversionSink stringSink;
102 if (sink is StringConversionSink) {
103 stringSink = sink;
104 } else {
105 stringSink = new StringConversionSink.from(sink);
106 }
107 // TODO(lrn): Use stringSink.asUtf16Sink() if it becomes available.
108 if (!_allowInvalid) return new _Latin1DecoderSink(stringSink);
109 return new _Latin1AllowInvalidDecoderSink(stringSink);
110 }
111 }
112
113 class _Latin1DecoderSink extends ByteConversionSinkBase {
114 StringConversionSink _sink;
115 _Latin1DecoderSink(this._sink);
116
117 void close() {
118 _sink.close();
119 }
120
121 void add(List<int> source) {
122 addSlice(source, 0, source.length, false);
123 }
124
125 void _addSliceToSink(List<int> source, int start, int end, bool isLast) {
126 // If _sink was a UTF-16 conversion sink, just add the slice directly with
127 // _sink.addSlice(source, start, end, isLast).
128 // The code below is an moderately stupid workaround until a real
129 // solution can be made.
130 _sink.add(new String.fromCharCodes(source, start, end));
131 if (isLast) close();
132 }
133
134 void addSlice(List<int> source, int start, int end, bool isLast) {
135 RangeError.checkValidRange(start, end, source.length);
136 for (int i = start; i < end; i++) {
137 int char = source[i];
138 if (char > _LATIN1_MASK || char < 0) {
139 throw new FormatException("Source contains non-Latin-1 characters.");
140 }
141 }
142 if (start < end) {
143 _addSliceToSink(source, start, end, isLast);
144 }
145 if (isLast) {
146 close();
147 }
148 }
149 }
150
151 class _Latin1AllowInvalidDecoderSink extends _Latin1DecoderSink {
152 _Latin1AllowInvalidDecoderSink(StringConversionSink sink): super(sink);
153
154 void addSlice(List<int> source, int start, int end, bool isLast) {
155 RangeError.checkValidRange(start, end, source.length);
156 for (int i = start; i < end; i++) {
157 int char = source[i];
158 if (char > _LATIN1_MASK || char < 0) {
159 if (i > start) _addSliceToSink(source, start, i, false);
160 // Add UTF-8 encoding of U+FFFD.
161 _addSliceToSink(const[0xFFFD], 0, 1, false);
162 start = i + 1;
163 }
164 }
165 if (start < end) {
166 _addSliceToSink(source, start, end, isLast);
167 }
168 if (isLast) {
169 close();
170 }
171 }
172 }
OLDNEW
« no previous file with comments | « test/generated_sdk/lib/convert/json.dart ('k') | test/generated_sdk/lib/convert/line_splitter.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698