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: 6 additions & 3 deletions src/encodes/detectcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,17 @@
float chardetconfidence = 0.0f;

/* chardet识别编码 */
QString str(content);
const QString str = QString::fromUtf8(content);
const bool isStrictUtf8 = str.toUtf8() == content;
// 匹配的是中文(仅在UTF-8编码下)
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))

Check warning on line 206 in src/encodes/detectcode.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

failed to evaluate #if condition, undefined function-like macro invocation: QT_VERSION_CHECK( ... )
QRegExp reg("[\\x4e00-\\x9fa5]+");
#else
QRegularExpression reg("[\\x4e00-\\x9fa5]+");
QRegularExpression reg("[\\x{4e00}-\\x{9fa5}]+");
Comment thread
dengzhongyuan365-dev marked this conversation as resolved.
Resolved
#endif
bool bFlag = str.contains(reg);
// 只有原始字节可无损往返为 UTF-8 时,中文特征才可触发 UTF-8 置信度增强。
// 非法 UTF-8 的 GBK/GB18030 字节经有损解码后不能作为 UTF-8 中文证据。
bool bFlag = isStrictUtf8 && str.contains(reg);
if (bFlag) {
qCDebug(appLog) << "Content contains Chinese characters";
const QByteArray suffix = "为增加探测率保留的中文";
Expand Down
29 changes: 27 additions & 2 deletions tests/common2/test_detectcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,21 @@ TEST_F(DetectCodeTest, GetFileEncodingFormat_Utf8ChineseFile_ReturnsUtf8)
EXPECT_EQ(ret, ret.toUpper()); // 返回值为大写规范形式
}

TEST_F(DetectCodeTest, GetFileEncodingFormat_Gb18030File_ReturnsGbFamily)
TEST_F(DetectCodeTest, GetFileEncodingFormat_ShortUtf8ChineseFile_ReturnsUtf8)
{
// Arrange: GB18030 字节序列("中文测试" ×3)
// Arrange: 两个汉字是当前 Bug 的短文本场景
const QByteArray raw = QStringLiteral("你好").toUtf8();
const QString path = writeTempFile(QStringLiteral("gf_short_zh.txt"), raw);
ASSERT_FALSE(path.isEmpty());
// Act
const QByteArray ret = DetectCode::GetFileEncodingFormat(path, raw);
// Assert
EXPECT_EQ(ret, QByteArrayLiteral("UTF-8"));
}

TEST_F(DetectCodeTest, GetFileEncodingFormat_GbkFile_ReturnsGbFamily)
{
// Arrange: GBK 字节序列;D6D0 是“中”,但不是合法 UTF-8
QByteArray gb;
for (int i = 0; i < 3; ++i)
gb += QByteArray::fromHex("D6D0CEC4B2E2CAD4");
Expand All @@ -662,6 +674,19 @@ TEST_F(DetectCodeTest, GetFileEncodingFormat_Gb18030File_ReturnsGbFamily)
EXPECT_FALSE(ret.isEmpty());
}

TEST_F(DetectCodeTest, GetFileEncodingFormat_Gb18030FourByteSequence_ReturnsGb18030)
{
// Arrange: U+1F600 的 GB18030 四字节序列,后接中文样本提高检测稳定性
const QByteArray raw = QByteArray::fromHex("9439FC36")
+ QByteArray::fromHex("D6D0CEC4B2E2CAD4").repeated(8);
const QString path = writeTempFile(QStringLiteral("gf_gb18030_4byte.txt"), raw);
ASSERT_FALSE(path.isEmpty());
// Act
const QByteArray ret = DetectCode::GetFileEncodingFormat(path, raw);
// Assert
EXPECT_EQ(ret, QByteArrayLiteral("GB18030")) << "actual: " << ret.constData();
}

TEST_F(DetectCodeTest, GetFileEncodingFormat_EmptyFile_ReturnsDefaultEncoding)
{
// Arrange: G6 结果为空 → 默认编码
Expand Down
Loading