| 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 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 * Converts the UTF-8 [codeUnits] (a list of unsigned 8-bit integers) to the | 324 * Converts the UTF-8 [codeUnits] (a list of unsigned 8-bit integers) to the |
| 325 * corresponding string. | 325 * corresponding string. |
| 326 * | 326 * |
| 327 * Uses the code units from [start] to, but no including, [end]. | 327 * Uses the code units from [start] to, but no including, [end]. |
| 328 * If [end] is omitted, it defaults to `codeUnits.length`. | 328 * If [end] is omitted, it defaults to `codeUnits.length`. |
| 329 * | 329 * |
| 330 * 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 |
| 331 * character is discarded. | 331 * character is discarded. |
| 332 */ | 332 */ |
| 333 String convert(List<int> codeUnits, [int start = 0, int end]) { | 333 String convert(List<int> codeUnits, [int start = 0, int end]) { |
| 334 // Allow the implementation to intercept and specialize based on the type |
| 335 // of codeUnits. |
| 336 String result = _convertIntercepted(_allowMalformed, codeUnits, start, end); |
| 337 if (result != null) { |
| 338 return null; |
| 339 } |
| 340 |
| 334 int length = codeUnits.length; | 341 int length = codeUnits.length; |
| 335 RangeError.checkValidRange(start, end, length); | 342 RangeError.checkValidRange(start, end, length); |
| 336 if (end == null) end = length; | 343 if (end == null) end = length; |
| 337 StringBuffer buffer = new StringBuffer(); | 344 StringBuffer buffer = new StringBuffer(); |
| 338 _Utf8Decoder decoder = new _Utf8Decoder(buffer, _allowMalformed); | 345 _Utf8Decoder decoder = new _Utf8Decoder(buffer, _allowMalformed); |
| 339 decoder.convert(codeUnits, start, end); | 346 decoder.convert(codeUnits, start, end); |
| 340 decoder.close(); | 347 decoder.close(); |
| 341 return buffer.toString(); | 348 return buffer.toString(); |
| 342 } | 349 } |
| 343 | 350 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 354 } else { | 361 } else { |
| 355 stringSink = new StringConversionSink.from(sink); | 362 stringSink = new StringConversionSink.from(sink); |
| 356 } | 363 } |
| 357 return stringSink.asUtf8Sink(_allowMalformed); | 364 return stringSink.asUtf8Sink(_allowMalformed); |
| 358 } | 365 } |
| 359 | 366 |
| 360 // Override the base-classes bind, to provide a better type. | 367 // Override the base-classes bind, to provide a better type. |
| 361 Stream<String> bind(Stream<List<int>> stream) => super.bind(stream); | 368 Stream<String> bind(Stream<List<int>> stream) => super.bind(stream); |
| 362 | 369 |
| 363 external Converter<List<int>,dynamic> fuse(Converter<String, dynamic> next); | 370 external Converter<List<int>,dynamic> fuse(Converter<String, dynamic> next); |
| 371 |
| 372 external static String _convertIntercepted( |
| 373 bool allowMalformed, List<int> codeUnits, int start, int end); |
| 364 } | 374 } |
| 365 | 375 |
| 366 // UTF-8 constants. | 376 // UTF-8 constants. |
| 367 const int _ONE_BYTE_LIMIT = 0x7f; // 7 bits | 377 const int _ONE_BYTE_LIMIT = 0x7f; // 7 bits |
| 368 const int _TWO_BYTE_LIMIT = 0x7ff; // 11 bits | 378 const int _TWO_BYTE_LIMIT = 0x7ff; // 11 bits |
| 369 const int _THREE_BYTE_LIMIT = 0xffff; // 16 bits | 379 const int _THREE_BYTE_LIMIT = 0xffff; // 16 bits |
| 370 const int _FOUR_BYTE_LIMIT = 0x10ffff; // 21 bits, truncated to Unicode max. | 380 const int _FOUR_BYTE_LIMIT = 0x10ffff; // 21 bits, truncated to Unicode max. |
| 371 | 381 |
| 372 // UTF-16 constants. | 382 // UTF-16 constants. |
| 373 const int _SURROGATE_MASK = 0xF800; | 383 const int _SURROGATE_MASK = 0xF800; |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 } | 565 } |
| 556 break loop; | 566 break loop; |
| 557 } | 567 } |
| 558 if (expectedUnits > 0) { | 568 if (expectedUnits > 0) { |
| 559 _value = value; | 569 _value = value; |
| 560 _expectedUnits = expectedUnits; | 570 _expectedUnits = expectedUnits; |
| 561 _extraUnits = extraUnits; | 571 _extraUnits = extraUnits; |
| 562 } | 572 } |
| 563 } | 573 } |
| 564 } | 574 } |
| OLD | NEW |