Chromium Code Reviews| Index: sdk/lib/convert/latin1.dart |
| diff --git a/sdk/lib/convert/latin1.dart b/sdk/lib/convert/latin1.dart |
| index 9ac63799c508aebb03f4376d47bf444982735996..e8dbf7e0e69130450a44815baefcfa293b105b53 100644 |
| --- a/sdk/lib/convert/latin1.dart |
| +++ b/sdk/lib/convert/latin1.dart |
| @@ -105,14 +105,14 @@ class Latin1Decoder extends _UnicodeSubsetDecoder { |
| stringSink = new StringConversionSink.from(sink); |
| } |
| // TODO(lrn): Use stringSink.asUtf16Sink() if it becomes available. |
| - return new _Latin1DecoderSink(_allowInvalid, stringSink); |
| + if (!_allowInvalid) return new _Latin1DecoderSink(stringSink); |
| + return new _Latin1AllowInvalidDecoderSink(stringSink); |
| } |
| } |
| class _Latin1DecoderSink extends ByteConversionSinkBase { |
| - final bool _allowInvalid; |
| StringConversionSink _sink; |
| - _Latin1DecoderSink(this._allowInvalid, this._sink); |
| + _Latin1DecoderSink(this._sink); |
| void close() { |
| _sink.close(); |
| @@ -143,15 +143,37 @@ class _Latin1DecoderSink extends ByteConversionSinkBase { |
| throw new RangeError.range(end, start, source.length); |
| } |
| for (int i = start; i < end; i++) { |
| - if ((source[i] & ~_LATIN1_MASK) != 0) { |
| - if (_allowInvalid) { |
| - if (i > start) _addSliceToSink(source, start, i, false); |
| - // Add UTF-8 encoding of U+FFFD. |
| - _addSliceToSink(const[0xFFFD], 0, 1, false); |
| - start = i + 1; |
| - } else { |
| - throw new FormatException("Source contains non-Latin-1 characters."); |
| - } |
| + int char = source[i]; |
| + 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
|
| + throw new FormatException("Source contains non-Latin-1 characters."); |
| + } |
| + } |
| + if (start < end) { |
| + _addSliceToSink(source, start, end, isLast); |
| + } |
| + if (isLast) { |
| + close(); |
| + } |
| + } |
| +} |
| + |
| +class _Latin1AllowInvalidDecoderSink extends _Latin1DecoderSink { |
| + _Latin1AllowInvalidDecoderSink(StringSink sink): super(sink); |
| + |
| + void addSlice(List<int> source, int start, int end, bool isLast) { |
| + if (start < 0 || start > source.length) { |
| + throw new RangeError.range(start, 0, source.length); |
| + } |
| + if (end < start || end > source.length) { |
| + throw new RangeError.range(end, start, source.length); |
| + } |
| + for (int i = start; i < end; i++) { |
| + int char = source[i]; |
| + if (char < 0 || char > _LATIN1_MASK) { |
| + if (i > start) _addSliceToSink(source, start, i, false); |
| + // Add UTF-8 encoding of U+FFFD. |
| + _addSliceToSink(const[0xFFFD], 0, 1, false); |
| + start = i + 1; |
| } |
| } |
| if (start < end) { |