From 1d1817b5edede2378bb63ebfe4c58fb4deb46b6c Mon Sep 17 00:00:00 2001 From: dengzhongyuan365-dev Date: Mon, 30 Nov 2026 20:13:45 +0800 Subject: [PATCH] fix: correct Unicode code point syntax in Qt6 regex for Chinese detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Root cause: Qt6 QRegularExpression uses PCRE2 engine, where \x4e00 is parsed as byte 0x4E followed by literal '00', not Unicode code point U+4E00, causing the Chinese character regex to never match 2. Fix: change \x4e00-\x9fa5 to \x{4e00}-\x{9fa5} in the Qt6 branch (#else block) so PCRE2 correctly interprets the Unicode ranges 3. Impact: Chinese content is now properly detected, chardet confidence reaches the threshold, encoding returns UTF-8 instead of GB18030 fix: 修复Qt6正则中Unicode码点语法导致中文检测失败 1. 根因:Qt6 QRegularExpression 使用 PCRE2 引擎,\x4e00 被解析为字节 0x4E 加字面量'00',而非Unicode码点U+4E00,导致中文正则永不匹配 2. 方案:将Qt6分支(#else块)中的 \x4e00-\x9fa5 改为 \x{4e00}-\x{9fa5}, 使PCRE2正确解析Unicode码点范围 3. 影响:中文内容可被正确检测,chardet置信度达标,编码返回UTF-8而非GB18030 Log: Fixed Chinese character encoding detection failing due to incorrect PCRE2 Unicode syntax in Qt6 Influence: - Test opening a txt file with Chinese content, verify encoding is UTF-8 - Test opening a plain ASCII txt file, verify no regression - Test opening a mixed Chinese-English txt file, verify correct encoding Log: 修复Qt6下中文编码检测因PCRE2 Unicode语法错误而失败的问题 Influence: - 测试打开包含中文内容的txt文件,验证编码为UTF-8 - 测试打开纯ASCII文本文件,验证无回归 - 测试打开中英文混合文件,验证编码检测正确 PMS: BUG-384821 --- src/encodes/detectcode.cpp | 9 ++++++--- tests/common2/test_detectcode.cpp | 29 +++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/encodes/detectcode.cpp b/src/encodes/detectcode.cpp index 498390fb..25e818c5 100644 --- a/src/encodes/detectcode.cpp +++ b/src/encodes/detectcode.cpp @@ -200,14 +200,17 @@ QByteArray DetectCode::GetFileEncodingFormat(QString filepath, QByteArray conten 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)) QRegExp reg("[\\x4e00-\\x9fa5]+"); #else - QRegularExpression reg("[\\x4e00-\\x9fa5]+"); + QRegularExpression reg("[\\x{4e00}-\\x{9fa5}]+"); #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 = "为增加探测率保留的中文"; diff --git a/tests/common2/test_detectcode.cpp b/tests/common2/test_detectcode.cpp index 0722f903..c7e56f2e 100644 --- a/tests/common2/test_detectcode.cpp +++ b/tests/common2/test_detectcode.cpp @@ -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"); @@ -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 结果为空 → 默认编码