Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/encodes/detectcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,15 @@ QByteArray DetectCode::selectCoding(QByteArray ucharDetectdRet, QByteArrayList i
return icuDetectRetList[0];
}
qCDebug(appLog) << "Returning uchardetDetectRet as fallback";
// uchardet 与 ICU[0] 完全无包含关系时的交叉校验:
// 若 ICU[0] 为 UTF-8 而 uchardet 结果非 UTF 变体(UTF-16/UTF-32 等),
// 优先信任 ICU,避免 UTF-8 文件被 uchardet 误判为 TIS-620 等编码
if (icuDetectRetList[0].contains("UTF-8") &&
!ucharDetectdRet.contains("UTF-16") &&
!ucharDetectdRet.contains("UTF-32")) {
qCDebug(appLog) << "ICU[0] is UTF-8 and uchardet is not UTF variant, trusting ICU";
return icuDetectRetList[0];
}
return ucharDetectdRet;
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/common2/test_detectcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ INSTANTIATE_TEST_SUITE_P(
SelectCase{QByteArrayLiteral("BIG5"), {QByteArrayLiteral("GB18030")}, 0.99f, false, true, QByteArrayLiteral("GB18030")},// S4 含 GB18030
SelectCase{QByteArrayLiteral("UTF-16"), {QByteArrayLiteral("UTF-16BE")}, 0.99f, false, true, QByteArrayLiteral("UTF-16BE")}, // S5 icu[0] 包含
SelectCase{QByteArrayLiteral("EUC-JP"), {QByteArrayLiteral("SHIFT-JIS")}, 0.99f, false, true, QByteArrayLiteral("EUC-JP")}, // S6 兜底
SelectCase{QByteArrayLiteral("TIS-620"), {QByteArrayLiteral("UTF-8")}, 0.99f, false, true, QByteArrayLiteral("UTF-8")}, // S6 交叉校验:ICU[0]=UTF-8, uchardet=TIS-620 → 信任 ICU
SelectCase{QByteArrayLiteral(""), {QByteArrayLiteral("GB18030")}, 0.99f, false, true, QByteArrayLiteral("GB18030")}, // S7 空 uchar+GB
SelectCase{QByteArrayLiteral(""), {QByteArrayLiteral("UTF-16LE")}, 0.99f, false, true, QByteArrayLiteral("UTF-16LE")})); // S7 空 uchar+首项

Expand Down
Loading