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

Side by Side Diff: sdk/lib/convert/utf.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: 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 | tests/lib/convert/utf8_encode_test.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 /** The Unicode Replacement character `U+FFFD` (�). */ 7 /** The Unicode Replacement character `U+FFFD` (�). */
8 const int UNICODE_REPLACEMENT_CHARACTER_RUNE = 0xFFFD; 8 const int UNICODE_REPLACEMENT_CHARACTER_RUNE = 0xFFFD;
9 9
10 /** The Unicode Byte Order Marker (BOM) character `U+FEFF`. */ 10 /** The Unicode Byte Order Marker (BOM) character `U+FEFF`. */
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 /** 75 /**
76 * This class converts strings to their UTF-8 code units (a list of 76 * This class converts strings to their UTF-8 code units (a list of
77 * unsigned 8-bit integers). 77 * unsigned 8-bit integers).
78 */ 78 */
79 class Utf8Encoder extends Converter<String, List<int>> { 79 class Utf8Encoder extends Converter<String, List<int>> {
80 80
81 const Utf8Encoder(); 81 const Utf8Encoder();
82 82
83 /** 83 /**
84 * Converts [string] to its UTF-8 code units (a list of 84 * Converts [string] to its UTF-8 code units (a list of
85 * unsigned 8-bit integers). 85 * unsigned 8-bit integers).
floitsch 2014/11/19 13:19:58 Update comment.
86 */ 86 */
87 List<int> convert(String string) { 87 List<int> convert(String string, [int start = 0, int end]) {
floitsch 2014/11/19 13:19:58 Make it named?
Lasse Reichstein Nielsen 2014/11/19 13:25:00 I'd rather not use named parameters. That's a lot
88 int stringLength = string.length;
89 if (start < 0 || start > stringLength) {
90 throw new RangeError.range(start, 0, stringLength, "start");
91 }
92 if (end == null) {
93 end = stringLength;
94 } else if (end < start || end > stringLength) {
95 throw new RangeError.range(end, start, stringLength, "end");
96 }
97 int length = end - start;
98 if (length == 0) return new Uint8List(0);
88 // Create a new encoder with a length that is guaranteed to be big enough. 99 // Create a new encoder with a length that is guaranteed to be big enough.
89 // A single code unit uses at most 3 bytes. Two code units at most 4. 100 // A single code unit uses at most 3 bytes, a surrogate pair at most 4.
90 _Utf8Encoder encoder = new _Utf8Encoder.withBufferSize(string.length * 3); 101 _Utf8Encoder encoder = new _Utf8Encoder.withBufferSize(length * 3);
91 int endPosition = encoder._fillBuffer(string, 0, string.length); 102 int endPosition = encoder._fillBuffer(string, start, end);
92 assert(endPosition >= string.length - 1); 103 assert(endPosition >= end - 1);
93 if (endPosition != string.length) { 104 if (endPosition != end) {
94 int lastCodeUnit = string.codeUnitAt(string.length - 1); 105 // Encoding skipped the last code unit.
106 // That can only happen if the last code unit is a leadsurrogate.
107 // Force encoding of the lead surrogate by itself.
108 int lastCodeUnit = string.codeUnitAt(end - 1);
95 assert(_isLeadSurrogate(lastCodeUnit)); 109 assert(_isLeadSurrogate(lastCodeUnit));
96 // We use a non-surrogate as `nextUnit` so that _writeSurrogate just 110 // We use a non-surrogate as `nextUnit` so that _writeSurrogate just
97 // writes the lead-surrogate. 111 // writes the lead-surrogate.
98 bool wasCombined = encoder._writeSurrogate(lastCodeUnit, 0); 112 bool wasCombined = encoder._writeSurrogate(lastCodeUnit, 0);
99 assert(!wasCombined); 113 assert(!wasCombined);
100 } 114 }
101 return encoder._buffer.sublist(0, encoder._bufferIndex); 115 return encoder._buffer.sublist(0, encoder._bufferIndex);
102 } 116 }
103 117
104 /** 118 /**
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 * sequences with the Unicode Replacement character `U+FFFD` (�). Otherwise 320 * sequences with the Unicode Replacement character `U+FFFD` (�). Otherwise
307 * it throws a [FormatException]. 321 * it throws a [FormatException].
308 */ 322 */
309 const Utf8Decoder({ bool allowMalformed: false }) 323 const Utf8Decoder({ bool allowMalformed: false })
310 : this._allowMalformed = allowMalformed; 324 : this._allowMalformed = allowMalformed;
311 325
312 /** 326 /**
313 * Converts the UTF-8 [codeUnits] (a list of unsigned 8-bit integers) to the 327 * Converts the UTF-8 [codeUnits] (a list of unsigned 8-bit integers) to the
314 * corresponding string. 328 * corresponding string.
315 * 329 *
330 * Uses the code units from [start] to, but no including, [end].
331 * If [end] is omitted, it defaults to `codeUnits.length`.
332 *
316 * If the [codeUnits] start with a leading [UNICODE_BOM_CHARACTER_RUNE] this 333 * If the [codeUnits] start with a leading [UNICODE_BOM_CHARACTER_RUNE] this
317 * character is discarded. 334 * character is discarded.
318 */ 335 */
319 String convert(List<int> codeUnits) { 336 String convert(List<int> codeUnits, [int start = 0, int end]) {
floitsch 2014/11/19 13:19:57 ditto.
337 int length = codeUnits.length;
338 if (start < 0 || start > length) {
339 throw new RangeError.range(start, 0, length, "start");
340 }
341 if (end == null) {
342 end = length;
343 } else if (end < start || end > length) {
344 throw new RangeError.range(end, start, length, "end");
345 }
320 StringBuffer buffer = new StringBuffer(); 346 StringBuffer buffer = new StringBuffer();
321 _Utf8Decoder decoder = new _Utf8Decoder(buffer, _allowMalformed); 347 _Utf8Decoder decoder = new _Utf8Decoder(buffer, _allowMalformed);
322 decoder.convert(codeUnits, 0, codeUnits.length); 348 decoder.convert(codeUnits, start, end);
323 decoder.close(); 349 decoder.close();
324 return buffer.toString(); 350 return buffer.toString();
325 } 351 }
326 352
327 /** 353 /**
328 * Starts a chunked conversion. 354 * Starts a chunked conversion.
329 * 355 *
330 * The converter works more efficiently if the given [sink] is a 356 * The converter works more efficiently if the given [sink] is a
331 * [StringConversionSink]. 357 * [StringConversionSink].
332 */ 358 */
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 } 564 }
539 break loop; 565 break loop;
540 } 566 }
541 if (expectedUnits > 0) { 567 if (expectedUnits > 0) {
542 _value = value; 568 _value = value;
543 _expectedUnits = expectedUnits; 569 _expectedUnits = expectedUnits;
544 _extraUnits = extraUnits; 570 _extraUnits = extraUnits;
545 } 571 }
546 } 572 }
547 } 573 }
OLDNEW
« no previous file with comments | « no previous file | tests/lib/convert/utf8_encode_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698