OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.convert; | 5 part of dart.convert; |
6 | 6 |
7 /** | 7 /** |
8 * An instance of the default implementation of the [Latin1Codec]. | 8 * An instance of the default implementation of the [Latin1Codec]. |
9 * | 9 * |
10 * This instance provides a convenient access to the most common ISO Latin 1 | 10 * This instance provides a convenient access to the most common ISO Latin 1 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 * [StringConversionSink]. | 98 * [StringConversionSink]. |
99 */ | 99 */ |
100 ByteConversionSink startChunkedConversion(Sink<String> sink) { | 100 ByteConversionSink startChunkedConversion(Sink<String> sink) { |
101 StringConversionSink stringSink; | 101 StringConversionSink stringSink; |
102 if (sink is StringConversionSink) { | 102 if (sink is StringConversionSink) { |
103 stringSink = sink; | 103 stringSink = sink; |
104 } else { | 104 } else { |
105 stringSink = new StringConversionSink.from(sink); | 105 stringSink = new StringConversionSink.from(sink); |
106 } | 106 } |
107 // TODO(lrn): Use stringSink.asUtf16Sink() if it becomes available. | 107 // TODO(lrn): Use stringSink.asUtf16Sink() if it becomes available. |
108 return new _Latin1DecoderSink(_allowInvalid, stringSink); | 108 if (!_allowInvalid) return new _Latin1DecoderSink(stringSink); |
109 return new _Latin1AllowInvalidDecoderSink(stringSink); | |
109 } | 110 } |
110 } | 111 } |
111 | 112 |
112 class _Latin1DecoderSink extends ByteConversionSinkBase { | 113 class _Latin1DecoderSink extends ByteConversionSinkBase { |
113 final bool _allowInvalid; | |
114 StringConversionSink _sink; | 114 StringConversionSink _sink; |
115 _Latin1DecoderSink(this._allowInvalid, this._sink); | 115 _Latin1DecoderSink(this._sink); |
116 | 116 |
117 void close() { | 117 void close() { |
118 _sink.close(); | 118 _sink.close(); |
119 } | 119 } |
120 | 120 |
121 void add(List<int> source) { | 121 void add(List<int> source) { |
122 addSlice(source, 0, source.length, false); | 122 addSlice(source, 0, source.length, false); |
123 } | 123 } |
124 | 124 |
125 void _addSliceToSink(List<int> source, int start, int end, bool isLast) { | 125 void _addSliceToSink(List<int> source, int start, int end, bool isLast) { |
(...skipping 10 matching lines...) Expand all Loading... | |
136 } | 136 } |
137 | 137 |
138 void addSlice(List<int> source, int start, int end, bool isLast) { | 138 void addSlice(List<int> source, int start, int end, bool isLast) { |
139 if (start < 0 || start > source.length) { | 139 if (start < 0 || start > source.length) { |
140 throw new RangeError.range(start, 0, source.length); | 140 throw new RangeError.range(start, 0, source.length); |
141 } | 141 } |
142 if (end < start || end > source.length) { | 142 if (end < start || end > source.length) { |
143 throw new RangeError.range(end, start, source.length); | 143 throw new RangeError.range(end, start, source.length); |
144 } | 144 } |
145 for (int i = start; i < end; i++) { | 145 for (int i = start; i < end; i++) { |
146 if ((source[i] & ~_LATIN1_MASK) != 0) { | 146 int char = source[i]; |
147 if (_allowInvalid) { | 147 if (char < 0 || char > _LATIN1_MASK) { |
Lasse Reichstein Nielsen
2014/08/08 07:29:01
This test seems to be ~10% faster than the "&" tes
| |
148 if (i > start) _addSliceToSink(source, start, i, false); | 148 throw new FormatException("Source contains non-Latin-1 characters."); |
149 // Add UTF-8 encoding of U+FFFD. | |
150 _addSliceToSink(const[0xFFFD], 0, 1, false); | |
151 start = i + 1; | |
152 } else { | |
153 throw new FormatException("Source contains non-Latin-1 characters."); | |
154 } | |
155 } | 149 } |
156 } | 150 } |
157 if (start < end) { | 151 if (start < end) { |
152 _addSliceToSink(source, start, end, isLast); | |
153 } | |
154 if (isLast) { | |
155 close(); | |
156 } | |
157 } | |
158 } | |
159 | |
160 class _Latin1AllowInvalidDecoderSink extends _Latin1DecoderSink { | |
161 _Latin1AllowInvalidDecoderSink(StringSink sink): super(sink); | |
162 | |
163 void addSlice(List<int> source, int start, int end, bool isLast) { | |
164 if (start < 0 || start > source.length) { | |
165 throw new RangeError.range(start, 0, source.length); | |
166 } | |
167 if (end < start || end > source.length) { | |
168 throw new RangeError.range(end, start, source.length); | |
169 } | |
170 for (int i = start; i < end; i++) { | |
171 int char = source[i]; | |
172 if (char < 0 || char > _LATIN1_MASK) { | |
173 if (i > start) _addSliceToSink(source, start, i, false); | |
174 // Add UTF-8 encoding of U+FFFD. | |
175 _addSliceToSink(const[0xFFFD], 0, 1, false); | |
176 start = i + 1; | |
177 } | |
178 } | |
179 if (start < end) { | |
158 _addSliceToSink(source, start, end, isLast); | 180 _addSliceToSink(source, start, end, isLast); |
159 } | 181 } |
160 if (isLast) { | 182 if (isLast) { |
161 close(); | 183 close(); |
162 } | 184 } |
163 } | 185 } |
164 } | 186 } |
OLD | NEW |