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

Side by Side Diff: sdk/lib/convert/ascii.dart

Issue 745573002: Create generic check methods for RangeError causing checks. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix long line Created 6 years 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 | « sdk/lib/collection/set.dart ('k') | sdk/lib/convert/latin1.dart » ('j') | 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 [AsciiCodec]. 8 * An instance of the default implementation of the [AsciiCodec].
9 * 9 *
10 * This instance provides a convenient access to the most common ASCII 10 * This instance provides a convenient access to the most common ASCII
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 const _UnicodeSubsetEncoder(this._subsetMask); 75 const _UnicodeSubsetEncoder(this._subsetMask);
76 76
77 /** 77 /**
78 * Converts the [String] into a list of its code units. 78 * Converts the [String] into a list of its code units.
79 * 79 *
80 * If [start] and [end] are provided, only the substring 80 * If [start] and [end] are provided, only the substring
81 * `string.substring(start, end)` is used as input to the conversion. 81 * `string.substring(start, end)` is used as input to the conversion.
82 */ 82 */
83 List<int> convert(String string, [int start = 0, int end]) { 83 List<int> convert(String string, [int start = 0, int end]) {
84 int stringLength = string.length; 84 int stringLength = string.length;
85 if (start < 0 || start > stringLength) { 85 RangeError.checkValidRange(start, end, stringLength);
86 throw new RangeError.range(start, 0, stringLength, "start"); 86 if (end == null) end = stringLength;
87 }
88 if (end == null) {
89 end = stringLength;
90 } else {
91 if (end < start || end > stringLength) {
92 throw new RangeError.range(end, start, stringLength, "end");
93 }
94 }
95 int length = end - start; 87 int length = end - start;
96 List result = new Uint8List(length); 88 List result = new Uint8List(length);
97 for (int i = 0; i < length; i++) { 89 for (int i = 0; i < length; i++) {
98 var codeUnit = string.codeUnitAt(start + i); 90 var codeUnit = string.codeUnitAt(start + i);
99 if ((codeUnit & ~_subsetMask) != 0) { 91 if ((codeUnit & ~_subsetMask) != 0) {
100 throw new ArgumentError("String contains invalid characters."); 92 throw new ArgumentError("String contains invalid characters.");
101 } 93 }
102 result[i] = codeUnit; 94 result[i] = codeUnit;
103 } 95 }
104 return result; 96 return result;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 final ByteConversionSink _sink; 128 final ByteConversionSink _sink;
137 final int _subsetMask; 129 final int _subsetMask;
138 130
139 _UnicodeSubsetEncoderSink(this._subsetMask, this._sink); 131 _UnicodeSubsetEncoderSink(this._subsetMask, this._sink);
140 132
141 void close() { 133 void close() {
142 _sink.close(); 134 _sink.close();
143 } 135 }
144 136
145 void addSlice(String source, int start, int end, bool isLast) { 137 void addSlice(String source, int start, int end, bool isLast) {
146 if (start < 0 || start > source.length) { 138 RangeError.checkValidRange(start, end, source.length);
147 throw new RangeError.range(start, 0, source.length);
148 }
149 if (end < start || end > source.length) {
150 throw new RangeError.range(end, start, source.length);
151 }
152 for (int i = start; i < end; i++) { 139 for (int i = start; i < end; i++) {
153 int codeUnit = source.codeUnitAt(i); 140 int codeUnit = source.codeUnitAt(i);
154 if ((codeUnit & ~_subsetMask) != 0) { 141 if ((codeUnit & ~_subsetMask) != 0) {
155 throw new ArgumentError( 142 throw new ArgumentError(
156 "Source contains invalid character with code point: $codeUnit."); 143 "Source contains invalid character with code point: $codeUnit.");
157 } 144 }
158 } 145 }
159 _sink.add(source.codeUnits.sublist(start, end)); 146 _sink.add(source.codeUnits.sublist(start, end));
160 if (isLast) { 147 if (isLast) {
161 close(); 148 close();
(...skipping 27 matching lines...) Expand all
189 176
190 /** 177 /**
191 * Converts the [bytes] (a list of unsigned 7- or 8-bit integers) to the 178 * Converts the [bytes] (a list of unsigned 7- or 8-bit integers) to the
192 * corresponding string. 179 * corresponding string.
193 * 180 *
194 * If [start] and [end] are provided, only the sub-list of bytes from 181 * If [start] and [end] are provided, only the sub-list of bytes from
195 * `start` to `end` (`end` not inclusive) is used as input to the conversion. 182 * `start` to `end` (`end` not inclusive) is used as input to the conversion.
196 */ 183 */
197 String convert(List<int> bytes, [int start = 0, int end]) { 184 String convert(List<int> bytes, [int start = 0, int end]) {
198 int byteCount = bytes.length; 185 int byteCount = bytes.length;
199 if (start < 0 || start > byteCount) { 186 RangeError.checkValidRange(start, end, byteCount);
200 throw new RangeError.range(start, 0, byteCount, "start"); 187 if (end == null) end = byteCount;
201 }
202 if (end == null) {
203 end = byteCount;
204 } else {
205 if (end < start || end > byteCount) {
206 throw new RangeError.range(end, start, byteCount, "end");
207 }
208 }
209 int length = end - start; 188 int length = end - start;
210 189
211 for (int i = start; i < end; i++) { 190 for (int i = start; i < end; i++) {
212 int byte = bytes[i]; 191 int byte = bytes[i];
213 if ((byte & ~_subsetMask) != 0) { 192 if ((byte & ~_subsetMask) != 0) {
214 if (!_allowInvalid) { 193 if (!_allowInvalid) {
215 throw new FormatException("Invalid value in input: $byte"); 194 throw new FormatException("Invalid value in input: $byte");
216 } 195 }
217 return _convertInvalid(bytes, start, end); 196 return _convertInvalid(bytes, start, end);
218 } 197 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 256
278 void close() { 257 void close() {
279 _utf8Sink.close(); 258 _utf8Sink.close();
280 } 259 }
281 260
282 void add(List<int> source) { 261 void add(List<int> source) {
283 addSlice(source, 0, source.length, false); 262 addSlice(source, 0, source.length, false);
284 } 263 }
285 264
286 void addSlice(List<int> source, int start, int end, bool isLast) { 265 void addSlice(List<int> source, int start, int end, bool isLast) {
287 if (start < 0 || start > source.length) { 266 RangeError.checkValidRange(start, end, source.length);
288 throw new RangeError.range(start, 0, source.length);
289 }
290 if (end < start || end > source.length) {
291 throw new RangeError.range(end, start, source.length);
292 }
293 for (int i = start; i < end; i++) { 267 for (int i = start; i < end; i++) {
294 if ((source[i] & ~_ASCII_MASK) != 0) { 268 if ((source[i] & ~_ASCII_MASK) != 0) {
295 if (i > start) _utf8Sink.addSlice(source, start, i, false); 269 if (i > start) _utf8Sink.addSlice(source, start, i, false);
296 // Add UTF-8 encoding of U+FFFD. 270 // Add UTF-8 encoding of U+FFFD.
297 _utf8Sink.add(const<int>[0xEF, 0xBF, 0xBD]); 271 _utf8Sink.add(const<int>[0xEF, 0xBF, 0xBD]);
298 start = i + 1; 272 start = i + 1;
299 } 273 }
300 } 274 }
301 if (start < end) { 275 if (start < end) {
302 _utf8Sink.addSlice(source, start, end, isLast); 276 _utf8Sink.addSlice(source, start, end, isLast);
(...skipping 15 matching lines...) Expand all
318 for (int i = 0; i < source.length; i++) { 292 for (int i = 0; i < source.length; i++) {
319 if ((source[i] & ~_ASCII_MASK) != 0) { 293 if ((source[i] & ~_ASCII_MASK) != 0) {
320 throw new FormatException("Source contains non-ASCII bytes."); 294 throw new FormatException("Source contains non-ASCII bytes.");
321 } 295 }
322 } 296 }
323 _sink.add(new String.fromCharCodes(source)); 297 _sink.add(new String.fromCharCodes(source));
324 } 298 }
325 299
326 void addSlice(List<int> source, int start, int end, bool isLast) { 300 void addSlice(List<int> source, int start, int end, bool isLast) {
327 final int length = source.length; 301 final int length = source.length;
328 if (start < 0 || start > length) { 302 RangeError.checkValidRange(start, end, length);
329 throw new RangeError.range(start, 0, length);
330 }
331 if (end < start || end > length) {
332 throw new RangeError.range(end, start, length);
333 }
334 if (start < end) { 303 if (start < end) {
335 if (start != 0 || end != length) { 304 if (start != 0 || end != length) {
336 source = source.sublist(start, end); 305 source = source.sublist(start, end);
337 } 306 }
338 add(source); 307 add(source);
339 } 308 }
340 if (isLast) close(); 309 if (isLast) close();
341 } 310 }
342 } 311 }
OLDNEW
« no previous file with comments | « sdk/lib/collection/set.dart ('k') | sdk/lib/convert/latin1.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698