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

Unified Diff: utils/string_encoding/Utf16.dart

Issue 9233041: String encoding utility methods and tests for Unicode, UTF-8, -16 and -32. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: updates from initial review. 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
Index: utils/string_encoding/Utf16.dart
diff --git a/utils/string_encoding/Utf16.dart b/utils/string_encoding/Utf16.dart
new file mode 100644
index 0000000000000000000000000000000000000000..853250b7e7a8a3fcd67e1e3a84caae53d6ae0eec
--- /dev/null
+++ b/utils/string_encoding/Utf16.dart
@@ -0,0 +1,159 @@
+// Copyright (c) 2011, 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.
+
+#library("UTF16");
+#import("UnicodeCore.dart");
+#import("Unicode.dart");
+
+/**
+ * Produce a String from a sequence of UTF16 encoded bytes.
+ */
+String decodeFromUtf16(List<int> bytes) {
+ List<int> codeUnits = _utf16ToUtf16CodeUnits(bytes);
+ // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc
+ // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
+ // removing after this issue is resolved.
+ if (is16BitCodeUnit()) {
+ return new String.fromCharCodes(codeUnits);
+ } else {
+ return new String.fromCharCodes(utf16CodeUnitsToCodepoints(codeUnits));
+ }
+}
+
+/**
+ * Produce a String from a sequence of UTF16-BE encoded bytes.
+ */
+String decodeFromUtf16be(List<int> bytes) {
+ List<int> codeUnits = _utf16beToUtf16CodeUnits(bytes);
+ // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc
+ // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
+ // removing after this issue is resolved.
+ if (is16BitCodeUnit()) {
+ return new String.fromCharCodes(codeUnits);
+ } else {
+ return new String.fromCharCodes(utf16CodeUnitsToCodepoints(codeUnits));
+ }
+}
+
+/**
+ * Produce a String from a sequence of UTF16-LE encoded bytes.
+ */
+String decodeFromUtf16le(List<int> bytes) {
+ List<int> codeUnits = _utf16leToUtf16CodeUnits(bytes);
+ // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc
+ // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
+ // removing after this issue is resolved.
+ if (is16BitCodeUnit()) {
+ return new String.fromCharCodes(codeUnits);
+ } else {
+ return new String.fromCharCodes(utf16CodeUnitsToCodepoints(codeUnits));
+ }
+}
+
+/**
+ * Produce a sequence of UTF16 encoded bytes.
+ */
+List<int> encodeAsUtf16(String str) => encodeAsUtf16be(str);
+
+/**
+ * Produce a sequence of UTF16-BE encoded bytes.
+ */
+List<int> encodeAsUtf16be(String str) {
+ List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str);
+ List<int> encoding = <int>[0xfe, 0xff];
+ for (int unit in utf16CodeUnits) {
+ encoding.add((unit & 0xff00) >> 8);
+ encoding.add(unit & 0xff);
+ }
+ return encoding;
+}
+
+/**
+ * Produce a sequence of UTF16-LE encoded bytes.
+ */
+List<int> encodeAsUtf16le(String str) {
+ List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str);
+ List<int> encoding = <int>[0xff, 0xfe];
+ for (int unit in utf16CodeUnits) {
+ encoding.add(unit & 0xff);
+ encoding.add((unit & 0xff00) >> 8);
+ }
+ return encoding;
+}
+
+List<int> _stringToUtf16CodeUnits(String str) {
+ List<int> codepoints = <int>[];
+ // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc
+ // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
+ // removing after this issue is resolved.
+ if (is16BitCodeUnit()) {
+ return str.charCodes();
+ } else {
+ return codepointsToUtf16CodeUnits(str.charCodes());
+ }
+}
+
+/**
+ * Convert UTF-16BE encoded bytes to utf16 code units by grouping 1-2 bytes
+ * to produce the code unit (0-(2^16)-1).
+ */
+List<int> _utf16beToUtf16CodeUnits(List<int> utf16beEncodedBytes,
+ [int start = 0, int length = - 1]) {
+ List<int> codeUnits = <int>[];
+ int end = length >= 0 ?
+ Math.min(utf16beEncodedBytes.length, start + length) :
+ utf16beEncodedBytes.length;
+ int i = start;
+ int lastIndex = end - 1;
+ while (i < lastIndex) {
+ int hi = utf16beEncodedBytes[i++];
+ int lo = utf16beEncodedBytes[i++];
+ codeUnits.add((hi * 256) + lo);
+ }
+ return codeUnits;
+}
+
+/**
+ * Convert UTF-16LE encoded bytes to utf16 code units by grouping 1-2 bytes
+ * to produce the code unit (0-(2^16)-1).
+ */
+List<int> _utf16leToUtf16CodeUnits(List<int> utf16leEncodedBytes,
+ [int start = 0, int length = - 1]) {
+ List<int> codeUnits = <int>[];
+ int end = length >= 0 ?
+ Math.min(utf16leEncodedBytes.length, start + length) :
+ utf16leEncodedBytes.length;
+ int i = start;
+ int lastIndex = end - 1;
+ while (i < lastIndex) {
+ int lo = utf16leEncodedBytes[i++];
+ int hi = utf16leEncodedBytes[i++];
+ codeUnits.add((hi * 256) + lo);
+ }
+ return codeUnits;
+}
+
+/**
+ * Convert UTF-16 encoded bytes to utf16 code units by grouping 1-2 bytes
+ * to produce the code unit (0-(2^16)-1). Relies on BOM to determine
+ * endian-ness, and defaults to BE.
+ */
+List<int> _utf16ToUtf16CodeUnits(List<int> utf16EncodedBytes,
+ [int start = 0, int length = - 1]) {
+ int end = length >= 0 ?
+ Math.min(utf16EncodedBytes.length, start + length) :
+ utf16EncodedBytes.length;
+
+ if((start + 1 < end) && utf16EncodedBytes[start] == 0xfe &&
+ utf16EncodedBytes[start + 1] == 0xff) {
+ return _utf16beToUtf16CodeUnits(utf16EncodedBytes, start + 2,
+ end - (start + 2));
+ } else if((start + 2 <= end) && utf16EncodedBytes[start] == 0xff &&
+ utf16EncodedBytes[start + 1] == 0xfe) {
+ return _utf16leToUtf16CodeUnits(utf16EncodedBytes, start + 2,
+ end - (start + 2));
+ } else {
+ return _utf16beToUtf16CodeUnits(utf16EncodedBytes, start, end - start);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698