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

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

Issue 736583008: Make Utf8Decoder and Utf8Encoder's convert methods take start and end too. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add documentation for new parameters. Created 6 years, 1 month 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 | sdk/lib/convert/utf.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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 */ 53 */
54 String decode(List<int> bytes, { bool allowInvalid }) { 54 String decode(List<int> bytes, { bool allowInvalid }) {
55 if (allowInvalid == null) allowInvalid = _allowInvalid; 55 if (allowInvalid == null) allowInvalid = _allowInvalid;
56 if (allowInvalid) { 56 if (allowInvalid) {
57 return const AsciiDecoder(allowInvalid: true).convert(bytes); 57 return const AsciiDecoder(allowInvalid: true).convert(bytes);
58 } else { 58 } else {
59 return const AsciiDecoder(allowInvalid: false).convert(bytes); 59 return const AsciiDecoder(allowInvalid: false).convert(bytes);
60 } 60 }
61 } 61 }
62 62
63 Converter<String, List<int>> get encoder => const AsciiEncoder(); 63 AsciiEncoder get encoder => const AsciiEncoder();
64 64
65 Converter<List<int>, String> get decoder => 65 AsciiDecoder get decoder =>
66 _allowInvalid ? const AsciiDecoder(allowInvalid: true) 66 _allowInvalid ? const AsciiDecoder(allowInvalid: true)
67 : const AsciiDecoder(allowInvalid: false); 67 : const AsciiDecoder(allowInvalid: false);
68 } 68 }
69 69
70 // Superclass for [AsciiEncoder] and [Latin1Encoder]. 70 // Superclass for [AsciiEncoder] and [Latin1Encoder].
71 // Generalizes common operations that only differ by a mask; 71 // Generalizes common operations that only differ by a mask;
72 class _UnicodeSubsetEncoder extends Converter<String, List<int>> { 72 class _UnicodeSubsetEncoder extends Converter<String, List<int>> {
73 final int _subsetMask; 73 final int _subsetMask;
74 74
75 const _UnicodeSubsetEncoder(this._subsetMask); 75 const _UnicodeSubsetEncoder(this._subsetMask);
76 76
77 List<int> convert(String string) { 77 /**
78 List result = new Uint8List(string.length); 78 * Converts the [String] into a list of its code units.
79 for (int i = 0; i < string.length; i++) { 79 *
80 var codeUnit = string.codeUnitAt(i); 80 * If [start] and [end] are provided, only the substring
81 * `string.substring(start, end)` is used as input to the conversion.
82 */
83 List<int> convert(String string, [int start = 0, int end]) {
84 int stringLength = string.length;
85 if (start < 0 || start > stringLength) {
86 throw new RangeError.range(start, 0, stringLength, "start");
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;
96 List result = new Uint8List(length);
97 for (int i = 0; i < length; i++) {
98 var codeUnit = string.codeUnitAt(start + i);
81 if ((codeUnit & ~_subsetMask) != 0) { 99 if ((codeUnit & ~_subsetMask) != 0) {
82 throw new ArgumentError("String contains invalid characters."); 100 throw new ArgumentError("String contains invalid characters.");
83 } 101 }
84 result[i] = codeUnit; 102 result[i] = codeUnit;
85 } 103 }
86 return result; 104 return result;
87 } 105 }
88 106
89 /** 107 /**
90 * Starts a chunked conversion. 108 * Starts a chunked conversion.
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 * 183 *
166 * If [_allowInvalid] is `true`, [convert] replaces invalid bytes with the 184 * If [_allowInvalid] is `true`, [convert] replaces invalid bytes with the
167 * Unicode Replacement character `U+FFFD` (�). 185 * Unicode Replacement character `U+FFFD` (�).
168 * Otherwise it throws a [FormatException]. 186 * Otherwise it throws a [FormatException].
169 */ 187 */
170 const _UnicodeSubsetDecoder(this._allowInvalid, this._subsetMask); 188 const _UnicodeSubsetDecoder(this._allowInvalid, this._subsetMask);
171 189
172 /** 190 /**
173 * Converts the [bytes] (a list of unsigned 7- or 8-bit integers) to the 191 * Converts the [bytes] (a list of unsigned 7- or 8-bit integers) to the
174 * corresponding string. 192 * corresponding string.
193 *
194 * 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.
175 */ 196 */
176 String convert(List<int> bytes) { 197 String convert(List<int> bytes, [int start = 0, int end]) {
177 for (int i = 0; i < bytes.length; i++) { 198 int byteCount = bytes.length;
199 if (start < 0 || start > byteCount) {
200 throw new RangeError.range(start, 0, byteCount, "start");
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;
210
211 for (int i = start; i < end; i++) {
178 int byte = bytes[i]; 212 int byte = bytes[i];
179 if ((byte & ~_subsetMask) != 0) { 213 if ((byte & ~_subsetMask) != 0) {
180 if (!_allowInvalid) { 214 if (!_allowInvalid) {
181 throw new FormatException("Invalid value in input: $byte"); 215 throw new FormatException("Invalid value in input: $byte");
182 } 216 }
183 return _convertInvalid(bytes); 217 return _convertInvalid(bytes, start, end);
184 } 218 }
185 } 219 }
186 return new String.fromCharCodes(bytes); 220 return new String.fromCharCodes(bytes, start, end);
187 } 221 }
188 222
189 String _convertInvalid(List<int> bytes) { 223 String _convertInvalid(List<int> bytes, int start, int end) {
190 StringBuffer buffer = new StringBuffer(); 224 StringBuffer buffer = new StringBuffer();
191 for (int i = 0; i < bytes.length; i++) { 225 for (int i = start; i < end; i++) {
192 int value = bytes[i]; 226 int value = bytes[i];
193 if ((value & ~_subsetMask) != 0) value = 0xFFFD; 227 if ((value & ~_subsetMask) != 0) value = 0xFFFD;
194 buffer.writeCharCode(value); 228 buffer.writeCharCode(value);
195 } 229 }
196 return buffer.toString(); 230 return buffer.toString();
197 } 231 }
198 232
199 /** 233 /**
200 * Starts a chunked conversion. 234 * Starts a chunked conversion.
201 * 235 *
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 } 333 }
300 if (start < end) { 334 if (start < end) {
301 if (start != 0 || end != length) { 335 if (start != 0 || end != length) {
302 source = source.sublist(start, end); 336 source = source.sublist(start, end);
303 } 337 }
304 add(source); 338 add(source);
305 } 339 }
306 if (isLast) close(); 340 if (isLast) close();
307 } 341 }
308 } 342 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/convert/utf.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698