Chromium Code Reviews| 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 /** | 7 /** |
| 8 * An instance of the default implementation of the [Utf8Codec]. | 8 * An instance of the default implementation of the [Utf8Codec]. |
| 9 * | 9 * |
| 10 * This instance provides a convenient access to the most common UTF-8 | 10 * This instance provides a convenient access to the most common UTF-8 |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 448 value = _REPLACEMENT_CHARACTER; | 448 value = _REPLACEMENT_CHARACTER; |
| 449 } | 449 } |
| 450 if (!_isFirstCharacter || value != _BOM_CHARACTER) { | 450 if (!_isFirstCharacter || value != _BOM_CHARACTER) { |
| 451 _stringSink.writeCharCode(value); | 451 _stringSink.writeCharCode(value); |
| 452 } | 452 } |
| 453 _isFirstCharacter = false; | 453 _isFirstCharacter = false; |
| 454 } | 454 } |
| 455 | 455 |
| 456 while (i < endIndex) { | 456 while (i < endIndex) { |
| 457 int unit = codeUnits[i++]; | 457 int unit = codeUnits[i++]; |
| 458 if (unit <= _ONE_BYTE_LIMIT) { | 458 if (unit < 0) { |
|
Lasse Reichstein Nielsen
2013/08/22 06:10:42
Would it be cheaper/faster to do:
if ((unit & ~0
floitsch
2013/08/22 15:40:25
Rapidly tried it, but somehow introduced a bug. No
| |
| 459 // TODO(floitsch): should this be unit <= 0 ? | |
|
Lasse Reichstein Nielsen
2013/08/22 06:10:42
No, zero is a valid UTF-8 code unit encoding the U
floitsch
2013/08/22 15:40:25
Done.
| |
| 460 if (!_allowMalformed) { | |
| 461 throw new FormatException( | |
| 462 "Negative UTF-8 code unit: -0x${(-unit).toRadixString(16)}"); | |
| 463 } | |
| 464 _stringSink.writeCharCode(_REPLACEMENT_CHARACTER); | |
| 465 } else if (unit <= _ONE_BYTE_LIMIT) { | |
| 459 _isFirstCharacter = false; | 466 _isFirstCharacter = false; |
| 460 _stringSink.writeCharCode(unit); | 467 _stringSink.writeCharCode(unit); |
| 461 } else { | 468 } else { |
| 462 if ((unit & 0xE0) == 0xC0) { | 469 if ((unit & 0xE0) == 0xC0) { |
| 463 value = unit & 0x1F; | 470 value = unit & 0x1F; |
| 464 expectedUnits = extraUnits = 1; | 471 expectedUnits = extraUnits = 1; |
| 465 continue loop; | 472 continue loop; |
| 466 } | 473 } |
| 467 if ((unit & 0xF0) == 0xE0) { | 474 if ((unit & 0xF0) == 0xE0) { |
| 468 value = unit & 0x0F; | 475 value = unit & 0x0F; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 487 } | 494 } |
| 488 break loop; | 495 break loop; |
| 489 } | 496 } |
| 490 if (expectedUnits > 0) { | 497 if (expectedUnits > 0) { |
| 491 _value = value; | 498 _value = value; |
| 492 _expectedUnits = expectedUnits; | 499 _expectedUnits = expectedUnits; |
| 493 _extraUnits = extraUnits; | 500 _extraUnits = extraUnits; |
| 494 } | 501 } |
| 495 } | 502 } |
| 496 } | 503 } |
| OLD | NEW |