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

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: remove notes to self 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..cf91dcda5f2b35ee7e156a1ef0cac0ddb1a401f5
--- /dev/null
+++ b/utils/string_encoding/Utf16.dart
@@ -0,0 +1,199 @@
+// 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.
+
+#library("UTF16");
Søren Gjesse 2012/02/01 11:25:25 Maybe utf16?
dcarlson 2012/02/01 22:18:46 Done.
+#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, [bool writeBOM = true]) =>
+ encodeAsUtf16be(str, writeBOM);
+
+/**
+ * Produce a sequence of UTF16-BE encoded bytes.
+ */
+List<int> encodeAsUtf16be(String str, [bool writeBOM = false]) {
+ List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str);
+ List<int> encoding = new List<int>(2 * utf16CodeUnits.length +
+ (writeBOM ? 2 : 0));
+ int i = 0;
+ if (writeBOM) {
+ encoding[i++] = UNICODE_UTF_BOM_HI;
+ encoding[i++] = UNICODE_UTF_BOM_LO;
+ }
+ for (int unit in utf16CodeUnits) {
+ encoding[i++] = (unit & UNICODE_BYTE_ONE_MASK) >> 8;
+ encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK;
+ }
+ return encoding;
+}
+
+/**
+ * Produce a sequence of UTF16-LE encoded bytes.
+ */
+List<int> encodeAsUtf16le(String str, [bool writeBOM = false]) {
+ List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str);
+ List<int> encoding = new List<int>(2 * utf16CodeUnits.length +
+ (writeBOM ? 2 : 0));
+ int i = 0;
+ if (writeBOM) {
+ encoding[i++] = UNICODE_UTF_BOM_LO;
+ encoding[i++] = UNICODE_UTF_BOM_HI;
+ }
+ for (int unit in utf16CodeUnits) {
+ encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK;
+ encoding[i++] = (unit & UNICODE_BYTE_ONE_MASK) >> 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 = null]) {
+ if (!(start >= 0)) {
+ throw new IllegalArgumentException("start");
+ }
+
+ if (!(length == null || length >= 0)) {
+ throw new IllegalArgumentException("length");
+ }
+
+ int end = length != null ?
+ Math.min(utf16beEncodedBytes.length, start + length) :
+ utf16beEncodedBytes.length;
+
+ List<int> codeUnits = <int>[];
+ int i = start;
+ int lastIndex = end - 1;
+ while (i < lastIndex) {
+ int hi = utf16beEncodedBytes[i++];
+ int lo = utf16beEncodedBytes[i++];
+ codeUnits.add((hi << 8) | 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 = null]) {
+ if (!(start >= 0)) {
+ throw new IllegalArgumentException("start");
+ }
+
+ if (!(length == null || length >= 0)) {
+ throw new IllegalArgumentException("length");
+ }
+
+ int end = length != null ?
+ Math.min(utf16leEncodedBytes.length, start + length) :
+ utf16leEncodedBytes.length;
+
+ List<int> codeUnits = <int>[];
+ int i = start;
+ int lastIndex = end - 1;
+ while (i < lastIndex) {
+ int lo = utf16leEncodedBytes[i++];
+ int hi = utf16leEncodedBytes[i++];
+ codeUnits.add((hi << 8) | 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 = null]) {
+ if (!(start >= 0)) {
+ throw new IllegalArgumentException("start");
+ }
+
+ if (!(length == null || length >= 0)) {
+ throw new IllegalArgumentException("length");
+ }
+
+ int end = length != null ?
+ Math.min(utf16EncodedBytes.length, start + length) :
+ utf16EncodedBytes.length;
+
+ if ((start + 1 < end) && utf16EncodedBytes[start] == UNICODE_UTF_BOM_HI &&
+ utf16EncodedBytes[start + 1] == UNICODE_UTF_BOM_LO) {
+ return _utf16beToUtf16CodeUnits(utf16EncodedBytes, start + 2,
+ end - (start + 2));
+ } else if ((start + 1 < end) &&
+ utf16EncodedBytes[start] == UNICODE_UTF_BOM_LO &&
+ utf16EncodedBytes[start + 1] == UNICODE_UTF_BOM_HI) {
+ return _utf16leToUtf16CodeUnits(utf16EncodedBytes, start + 2,
+ end - (start + 2));
+ } else {
+ return _utf16beToUtf16CodeUnits(utf16EncodedBytes, start, end - start);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698