OLD | NEW |
| (Empty) |
1 part of dart.convert; | |
2 const Latin1Codec LATIN1 = const Latin1Codec(); | |
3 const int _LATIN1_MASK = 0xFF; | |
4 class Latin1Codec extends Encoding {final bool _allowInvalid; | |
5 const Latin1Codec({ | |
6 bool allowInvalid : false} | |
7 ) : _allowInvalid = allowInvalid; | |
8 String get name => "iso-8859-1"; | |
9 String decode(List<int> bytes, { | |
10 bool allowInvalid} | |
11 ) { | |
12 if (allowInvalid == null) allowInvalid = _allowInvalid; | |
13 if (allowInvalid) { | |
14 return const Latin1Decoder(allowInvalid: true).convert(bytes); | |
15 } | |
16 else { | |
17 return const Latin1Decoder(allowInvalid: false).convert(bytes); | |
18 } | |
19 } | |
20 Converter<String, List<int>> get encoder => const Latin1Encoder(); | |
21 Converter<List<int>, String> get decoder => _allowInvalid ? const Latin1Decoder
(allowInvalid: true) : const Latin1Decoder(allowInvalid: false); | |
22 } | |
23 class Latin1Encoder extends _UnicodeSubsetEncoder {const Latin1Encoder() : supe
r(_LATIN1_MASK); | |
24 } | |
25 class Latin1Decoder extends _UnicodeSubsetDecoder {const Latin1Decoder({ | |
26 bool allowInvalid : false} | |
27 ) : super(allowInvalid, _LATIN1_MASK); | |
28 ByteConversionSink startChunkedConversion(Sink<String> sink) { | |
29 StringConversionSink stringSink; | |
30 if (sink is StringConversionSink) { | |
31 stringSink = sink; | |
32 } | |
33 else { | |
34 stringSink = new StringConversionSink.from(sink); | |
35 } | |
36 if (!_allowInvalid) return new _Latin1DecoderSink(stringSink); | |
37 return new _Latin1AllowInvalidDecoderSink(stringSink); | |
38 } | |
39 } | |
40 class _Latin1DecoderSink extends ByteConversionSinkBase {StringConversionSink _
sink; | |
41 _Latin1DecoderSink(this._sink); | |
42 void close() { | |
43 _sink.close(); | |
44 } | |
45 void add(List<int> source) { | |
46 addSlice(source, 0, source.length, false); | |
47 } | |
48 void _addSliceToSink(List<int> source, int start, int end, bool isLast) { | |
49 _sink.add(new String.fromCharCodes(source, start, end)); | |
50 if (isLast) close(); | |
51 } | |
52 void addSlice(List<int> source, int start, int end, bool isLast) { | |
53 RangeError.checkValidRange(start, end, source.length); | |
54 for (int i = start; i < end; i++) { | |
55 int char = source[i]; | |
56 if (char > _LATIN1_MASK || char < 0) { | |
57 throw new FormatException("Source contains non-Latin-1 characters."); | |
58 } | |
59 } | |
60 if (start < end) { | |
61 _addSliceToSink(source, start, end, isLast); | |
62 } | |
63 if (isLast) { | |
64 close(); | |
65 } | |
66 } | |
67 } | |
68 class _Latin1AllowInvalidDecoderSink extends _Latin1DecoderSink {_Latin1AllowIn
validDecoderSink(StringConversionSink sink) : super(sink); | |
69 void addSlice(List<int> source, int start, int end, bool isLast) { | |
70 RangeError.checkValidRange(start, end, source.length); | |
71 for (int i = start; i < end; i++) { | |
72 int char = source[i]; | |
73 if (char > _LATIN1_MASK || char < 0) { | |
74 if (i > start) _addSliceToSink(source, start, i, false); | |
75 _addSliceToSink(const <int> [0xFFFD], 0, 1, false); | |
76 start = i + 1; | |
77 } | |
78 } | |
79 if (start < end) { | |
80 _addSliceToSink(source, start, end, isLast); | |
81 } | |
82 if (isLast) { | |
83 close(); | |
84 } | |
85 } | |
86 } | |
OLD | NEW |