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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 > _LATIN1_MASK || char < 0) {
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 > _LATIN1_MASK || char < 0) {
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 }
OLDNEW
« 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