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

Unified Diff: dart/frog/leg/scanner/byte_strings.dart

Issue 9185046: Move UTF-8 decoder to utils. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 8 years, 11 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | dart/frog/leg/scanner/node_scanner_bench.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dart/frog/leg/scanner/byte_strings.dart
===================================================================
--- dart/frog/leg/scanner/byte_strings.dart (revision 3230)
+++ dart/frog/leg/scanner/byte_strings.dart (working copy)
@@ -15,73 +15,8 @@
abstract String get charset();
- String toString() {
- var list;
- try {
- list = bytes.getRange(offset, length);
- } catch (var ignored) {
- // An exception may occur when running this on node. This is
- // because [bytes] really is a buffer (or typed array).
- list = new List<int>(length);
- for (int i = 0; i < length; i++) {
- list[i] = bytes[i + offset];
- }
- }
- return new String.fromCharCodes(decodeUtf8(list));
- }
+ String toString() => new Utf8Decoder(bytes, offset, length).toString();
- static int decodeTrailing(int byte) {
- if (byte < 0x80 || 0xBF < byte) {
- throw new MalformedInputException('Cannot decode UTF-8 $byte');
- } else {
- return byte & 0x3F;
- }
- }
-
- static List<int> decodeUtf8(List<int> bytes) {
- List<int> result = new List<int>();
- for (int i = 0; i < bytes.length; i++) {
- if (bytes[i] < 0x80) {
- result.add(bytes[i]);
- } else if (bytes[i] < 0xC2) {
- throw new MalformedInputException('Cannot decode UTF-8 @ $i');
- } else if (bytes[i] < 0xE0) {
- int char = (bytes[i++] & 0x1F) << 6;
- char += decodeTrailing(bytes[i]);
- if (char < 0x80) {
- throw new MalformedInputException('Cannot decode UTF-8 @ ${i-1}');
- } else {
- result.add(char);
- }
- } else if (bytes[i] < 0xF0) {
- int char = (bytes[i++] & 0x0F) << 6;
- char += decodeTrailing(bytes[i++]);
- char <<= 6;
- char += decodeTrailing(bytes[i]);
- if (char < 0x800 || (0xD800 <= char && char <= 0xDFFF)) {
- throw new MalformedInputException('Cannot decode UTF-8 @ ${i-2}');
- } else {
- result.add(char);
- }
- } else if (bytes[i] < 0xF8) {
- int char = (bytes[i++] & 0x07) << 6;
- char += decodeTrailing(bytes[i++]);
- char <<= 6;
- char += decodeTrailing(bytes[i++]);
- char <<= 6;
- char += decodeTrailing(bytes[i]);
- if (char < 0x10000) {
- throw new MalformedInputException('Cannot decode UTF-8 @ ${i-3}');
- } else {
- result.add(char);
- }
- } else {
- throw new MalformedInputException('Cannot decode UTF-8 @ $i');
- }
- }
- return result;
- }
-
bool operator ==(other) {
throw "should be overridden in subclass";
}
« no previous file with comments | « no previous file | dart/frog/leg/scanner/node_scanner_bench.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698