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

Unified Diff: sdk/lib/convert/latin1.dart

Issue 445333004: Optimize latin1-decoder. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Change the test order, so the most commen error case is first. Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/convert/latin1.dart
diff --git a/sdk/lib/convert/latin1.dart b/sdk/lib/convert/latin1.dart
index 9ac63799c508aebb03f4376d47bf444982735996..92eaf5a90badf5f9e9c4d3f39a6abb222f808af0 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 > _LATIN1_MASK || char < 0) {
+ 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 > _LATIN1_MASK || char < 0) {
+ 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) {
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698