// Extension to testharness.js API which avoids logging enormous strings // on a coding failure. function assert_string_equals(actual, expected, description) { // short circuit success case if (actual === expected) {
assert_true(true, description + ": <actual> === <expected>"); return;
}
var i, a, b; for (i = 0; i < actual.length; i++) {
a = actual.charCodeAt(i);
b = expected.charCodeAt(i); if (a !== b) {
assert_true( false,
description + ": code unit " +
i.toString() + " unequal: " +
cpname(a) + " != " +
cpname(b)
);
} // doesn't return
}
// It should be impossible to get here, because the initial // comparison failed, so either the length comparison or the // codeunit-by-codeunit comparison should also fail.
assert_true(false, description + ": failed to detect string difference");
}
// Inspired by: // http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html function encode_utf8(string) { var utf8 = unescape(encodeURIComponent(string)); var octets = new Uint8Array(utf8.length),
i; for (i = 0; i < utf8.length; i += 1) {
octets[i] = utf8.charCodeAt(i);
} return octets;
}
function encode_utf16le(string) { var octets = new Uint8Array(string.length * 2); var di = 0; for (var i = 0; i < string.length; i++) { var code = string.charCodeAt(i);
octets[di++] = code & 0xff;
octets[di++] = code >> 8;
} return octets;
}
function encode_utf16be(string) { var octets = new Uint8Array(string.length * 2); var di = 0; for (var i = 0; i < string.length; i++) { var code = string.charCodeAt(i);
octets[di++] = code >> 8;
octets[di++] = code & 0xff;
} return octets;
}
function decode_utf8(octets) { var utf8 = String.fromCharCode.apply(null, octets); return decodeURIComponent(escape(utf8));
}
// Helpers for test_utf_roundtrip. function cpname(n) { if (n + 0 !== n) { return n.toString();
} var w = n <= 0xffff ? 4 : 6; return"U+" + ("000000" + n.toString(16).toUpperCase()).slice(-w);
}
function genblock(from, len) { var i, j, point, offset; var size, block;
// determine size required: // 1 unit for each point from U+000000 through U+00D7FF // 0 units U+00D800 through U+00DFFF // 1 unit U+00E000 through U+00FFFF // 2 units U+010000 through U+10FFFF function overlap(min1, max1, min2, max2) { return Math.max(0, Math.min(max1, max2) - Math.max(min1, min2));
}
size =
overlap(from, from + len, 0x000000, 0x00d800) +
overlap(from, from + len, 0x00e000, 0x010000) +
overlap(from, from + len, 0x010000, 0x110000) * 2;
block = new Uint16Array(size); for (i = 0, j = 0; i < len; i++) {
point = from + i; if (0xd800 <= point && point <= 0xdfff) { continue;
} elseif (point <= 0xffff) {
block[j++] = point;
} else {
offset = point - 0x10000;
block[j++] = 0xd800 + (offset >> 10);
block[j++] = 0xdc00 + (offset & 0x3ff);
}
} return String.fromCharCode.apply(null, block);
}
function test_utf_roundtrip() { var MIN_CODEPOINT = 0; var MAX_CODEPOINT = 0x10ffff; var BLOCK_SIZE = 0x1000;
var block, block_tag, i, encoded, decoded, exp_encoded, exp_decoded;
var TD_U16LE = new TextDecoder("UTF-16LE");
var TD_U16BE = new TextDecoder("UTF-16BE");
var TE_U8 = new TextEncoder(); var TD_U8 = new TextDecoder("UTF-8");
for (i = MIN_CODEPOINT; i < MAX_CODEPOINT; i += BLOCK_SIZE) {
block_tag = cpname(i) + " - " + cpname(i + BLOCK_SIZE - 1);
block = genblock(i, BLOCK_SIZE);
// test UTF-16LE, UTF-16BE, and UTF-8 encodings against themselves
encoded = encode_utf16le(block);
decoded = TD_U16LE.decode(encoded);
assert_string_equals(block, decoded, "UTF-16LE round trip " + block_tag);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.