OLD | NEW |
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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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). |
86 * | 86 * |
87 * If [start] and [end] are provided, only the substring | 87 * If [start] and [end] are provided, only the substring |
88 * `string.substring(start, end)` is converted. | 88 * `string.substring(start, end)` is converted. |
89 */ | 89 */ |
90 List<int> convert(String string, [int start = 0, int end]) { | 90 List<int> convert(String string, [int start = 0, int end]) { |
91 int stringLength = string.length; | 91 int stringLength = string.length; |
92 if (start < 0 || start > stringLength) { | 92 RangeError.checkValidRange(start, end, stringLength); |
93 throw new RangeError.range(start, 0, stringLength, "start"); | 93 if (end == null) end = stringLength; |
94 } | |
95 if (end == null) { | |
96 end = stringLength; | |
97 } else if (end < start || end > stringLength) { | |
98 throw new RangeError.range(end, start, stringLength, "end"); | |
99 } | |
100 int length = end - start; | 94 int length = end - start; |
101 if (length == 0) return new Uint8List(0); | 95 if (length == 0) return new Uint8List(0); |
102 // Create a new encoder with a length that is guaranteed to be big enough. | 96 // Create a new encoder with a length that is guaranteed to be big enough. |
103 // A single code unit uses at most 3 bytes, a surrogate pair at most 4. | 97 // A single code unit uses at most 3 bytes, a surrogate pair at most 4. |
104 _Utf8Encoder encoder = new _Utf8Encoder.withBufferSize(length * 3); | 98 _Utf8Encoder encoder = new _Utf8Encoder.withBufferSize(length * 3); |
105 int endPosition = encoder._fillBuffer(string, start, end); | 99 int endPosition = encoder._fillBuffer(string, start, end); |
106 assert(endPosition >= end - 1); | 100 assert(endPosition >= end - 1); |
107 if (endPosition != end) { | 101 if (endPosition != end) { |
108 // Encoding skipped the last code unit. | 102 // Encoding skipped the last code unit. |
109 // That can only happen if the last code unit is a leadsurrogate. | 103 // That can only happen if the last code unit is a leadsurrogate. |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 * corresponding string. | 325 * corresponding string. |
332 * | 326 * |
333 * Uses the code units from [start] to, but no including, [end]. | 327 * Uses the code units from [start] to, but no including, [end]. |
334 * If [end] is omitted, it defaults to `codeUnits.length`. | 328 * If [end] is omitted, it defaults to `codeUnits.length`. |
335 * | 329 * |
336 * If the [codeUnits] start with a leading [UNICODE_BOM_CHARACTER_RUNE] this | 330 * If the [codeUnits] start with a leading [UNICODE_BOM_CHARACTER_RUNE] this |
337 * character is discarded. | 331 * character is discarded. |
338 */ | 332 */ |
339 String convert(List<int> codeUnits, [int start = 0, int end]) { | 333 String convert(List<int> codeUnits, [int start = 0, int end]) { |
340 int length = codeUnits.length; | 334 int length = codeUnits.length; |
341 if (start < 0 || start > length) { | 335 RangeError.checkValidRange(start, end, length); |
342 throw new RangeError.range(start, 0, length, "start"); | 336 if (end == null) end = length; |
343 } | |
344 if (end == null) { | |
345 end = length; | |
346 } else if (end < start || end > length) { | |
347 throw new RangeError.range(end, start, length, "end"); | |
348 } | |
349 StringBuffer buffer = new StringBuffer(); | 337 StringBuffer buffer = new StringBuffer(); |
350 _Utf8Decoder decoder = new _Utf8Decoder(buffer, _allowMalformed); | 338 _Utf8Decoder decoder = new _Utf8Decoder(buffer, _allowMalformed); |
351 decoder.convert(codeUnits, start, end); | 339 decoder.convert(codeUnits, start, end); |
352 decoder.close(); | 340 decoder.close(); |
353 return buffer.toString(); | 341 return buffer.toString(); |
354 } | 342 } |
355 | 343 |
356 /** | 344 /** |
357 * Starts a chunked conversion. | 345 * Starts a chunked conversion. |
358 * | 346 * |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
567 } | 555 } |
568 break loop; | 556 break loop; |
569 } | 557 } |
570 if (expectedUnits > 0) { | 558 if (expectedUnits > 0) { |
571 _value = value; | 559 _value = value; |
572 _expectedUnits = expectedUnits; | 560 _expectedUnits = expectedUnits; |
573 _extraUnits = extraUnits; | 561 _extraUnits = extraUnits; |
574 } | 562 } |
575 } | 563 } |
576 } | 564 } |
OLD | NEW |