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"; |
} |