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

Unified Diff: dart/utils/utf8/utf8.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 | « dart/frog/leg/scanner/vm_scanner_bench.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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();
-}
« no previous file with comments | « dart/frog/leg/scanner/vm_scanner_bench.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698