Use ASCII-compatible fallback if an encoder has no values defined for code points below U+0020. Bug 399257, r=jshin

This commit is contained in:
Simon Montagu 2008-06-04 22:53:33 +03:00
parent b249b550e9
commit 8f1bffa84b
2 changed files with 60 additions and 2 deletions

View File

@ -0,0 +1,53 @@
// Tests encoding of characters below U+0020
const Ci = Components.interfaces;
const Cc = Components.classes;
const inString = "Hello\u000aWorld";
const expectedString = "Hello\nWorld";
function run_test() {
var failures = false;
var ccManager = Cc["@mozilla.org/charset-converter-manager;1"]
.getService(Ci.nsICharsetConverterManager);
var encodingConverter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Ci.nsIScriptableUnicodeConverter);
var charsetList = ccManager.getDecoderList();
var counter = 0;
while (charsetList.hasMore()) {
++counter;
var charset = charsetList.getNext();
// exclude known non-ASCII compatible charsets
if (charset.substr(0, "UTF-16".length) == "UTF-16" ||
charset.substr(0, "UTF-32".length) == "UTF-32" ||
charset == "x-imap4-modified-utf7") {
dump("skipping " + counter + " " + charset + "\n");
continue;
}
dump("testing " + counter + " " + charset + "\n");
try {
encodingConverter.charset = charset;
} catch(e) {
dump("Warning: couldn't set encoder charset to " + charset + "\n");
continue;
}
var codepageString = encodingConverter.ConvertFromUnicode(inString) +
encodingConverter.Finish();
if (codepageString != expectedString) {
dump(charset + " encoding failed\n");
for (var i = 0; i < expectedString.length; ++i) {
if (codepageString.charAt(i) != expectedString.charAt(i)) {
dump(i.toString(16) + ": 0x" +
codepageString.charCodeAt(i).toString(16) + " != " +
expectedString.charCodeAt(i).toString(16) + "\n");
}
}
failures = true;
}
}
if (failures) {
do_throw("test failed\n");
}
}

View File

@ -62,8 +62,13 @@ nsresult nsUnicodeEncodeHelper::ConvertByTable(
while (src < srcEnd) {
if (!uMapCode((uTable*) aMappingTable, static_cast<PRUnichar>(*(src++)), reinterpret_cast<PRUint16*>(&med))) {
res = NS_ERROR_UENC_NOMAPPING;
break;
if (*(src - 1) < 0x20) {
// some tables are missing the 0x00 - 0x20 part
med = *(src - 1);
} else {
res = NS_ERROR_UENC_NOMAPPING;
break;
}
}
PRBool charFound;