Index: dart/utils/utf8/utf8.dart |
=================================================================== |
--- dart/utils/utf8/utf8.dart (revision 0) |
+++ dart/utils/utf8/utf8.dart (working copy) |
@@ -1,38 +1,23 @@ |
-// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
// for details. All rights reserved. Use of this source code is governed by a |
// BSD-style license that can be found in the LICENSE file. |
-/** |
- * An abstract string representation. |
- */ |
-class ByteString implements SourceString { |
+#library('utf8'); |
+ |
+class Utf8Decoder { |
final List<int> bytes; |
final int offset; |
final int length; |
- int _hashCode; |
- ByteString(List<int> this.bytes, int this.offset, int this.length); |
+ Utf8Decoder(List<int> this.bytes, int this.offset, int this.length); |
- 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)); |
+ return new String.fromCharCodes(decodeUtf8(bytes.getRange(offset, length))); |
} |
static int decodeTrailing(int byte) { |
if (byte < 0x80 || 0xBF < byte) { |
- throw new MalformedInputException('Cannot decode UTF-8 $byte'); |
+ throw new Exception('Cannot decode UTF-8 $byte'); |
} else { |
return byte & 0x3F; |
} |
@@ -44,12 +29,12 @@ |
if (bytes[i] < 0x80) { |
result.add(bytes[i]); |
} else if (bytes[i] < 0xC2) { |
- throw new MalformedInputException('Cannot decode UTF-8 @ $i'); |
+ throw new Exception('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}'); |
+ throw new Exception('Cannot decode UTF-8 @ ${i-1}'); |
} else { |
result.add(char); |
} |
@@ -59,7 +44,7 @@ |
char <<= 6; |
char += decodeTrailing(bytes[i]); |
if (char < 0x800 || (0xD800 <= char && char <= 0xDFFF)) { |
- throw new MalformedInputException('Cannot decode UTF-8 @ ${i-2}'); |
+ throw new Exception('Cannot decode UTF-8 @ ${i-2}'); |
} else { |
result.add(char); |
} |
@@ -71,88 +56,14 @@ |
char <<= 6; |
char += decodeTrailing(bytes[i]); |
if (char < 0x10000) { |
- throw new MalformedInputException('Cannot decode UTF-8 @ ${i-3}'); |
+ throw new Exception('Cannot decode UTF-8 @ ${i-3}'); |
} else { |
result.add(char); |
} |
} else { |
- throw new MalformedInputException('Cannot decode UTF-8 @ $i'); |
+ throw new Exception('Cannot decode UTF-8 @ $i'); |
} |
} |
return result; |
} |
- |
- bool operator ==(other) { |
- throw "should be overridden in subclass"; |
- } |
- |
- int hashCode() { |
- if (_hashCode === null) { |
- _hashCode = computeHashCode(); |
- } |
- return _hashCode; |
- } |
- |
- int computeHashCode() { |
- int code = 1; |
- int end = offset + length; |
- for (int i = offset; i < end; i++) { |
- code += 19 * code + bytes[i]; |
- } |
- return code; |
- } |
- |
- printOn(StringBuffer sb) { |
- sb.add(toString()); |
- } |
} |
- |
-/** |
- * A string that consists purely of 7bit ASCII characters. |
- */ |
-class AsciiString extends ByteString { |
- final String charset = "ASCII"; |
- |
- AsciiString(List<int> bytes, int offset, int length) |
- : super(bytes, offset, length); |
- |
- static AsciiString of(List<int> bytes, int offset, int length) { |
- AsciiString string = new AsciiString(bytes, offset, length); |
- return string; |
- } |
- |
- static AsciiString fromString(String string) { |
- List<int> bytes = string.charCodes(); |
- return AsciiString.of(bytes, 0, bytes.length); |
- } |
-} |
- |
-/** |
- * A string that consists of characters that can be encoded as UTF-8. |
- */ |
-class Utf8String extends ByteString { |
- final String charset = "UTF8"; |
- |
- Utf8String(List<int> bytes, int offset, int length) |
- : super(bytes, offset, length); |
- |
- static Utf8String of(List<int> bytes, int offset, int length) { |
- return new Utf8String(bytes, offset, length); |
- } |
- |
- static Utf8String fromString(String string) { |
- throw "not implemented yet"; |
- } |
-} |
- |
-/** |
- * A ByteString-valued token. |
- */ |
-class ByteStringToken extends Token { |
- final ByteString value; |
- |
- ByteStringToken(PrecedenceInfo info, ByteString this.value, int charOffset) |
- : super(info, charOffset); |
- |
- String toString() => value.toString(); |
-} |