Repository navigation
fix: correct Unicode code point syntax in Qt6 regex for Chinese detection - #759
Merged
deepin-bot[bot] merged 1 commit intoDec 1, 2026
Conversation
dengzhongyuan365-dev
marked this pull request as draft
November 30, 2026 12:20
Reviewer's GuideImplementationDecode input explicitly as UTF-8 and require a lossless byte round trip before using Chinese characters as UTF-8 evidence, while correcting the Qt6 regex to match the intended Unicode range.
TestsAdd regression tests for short UTF-8 Chinese text and for GBK/GB18030 inputs, including a four-byte GB18030 sequence.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/encodes/detectcode.cpp" line_range="208" />
<code_context>
QRegExp reg("[\\x4e00-\\x9fa5]+");
#else
- QRegularExpression reg("[\\x4e00-\\x9fa5]+");
+ QRegularExpression reg("[\\x{4e00}-\\x{9fa5}]+");
#endif
bool bFlag = str.contains(reg);
</code_context>
<issue_to_address>
**GB18030 files become UTF-8**
When a GB18030/GBK byte sequence also decodes as UTF-8 to a character in the matched range, such as `D6D0` decoding to U+5B50, `QString str(content)` can decode legacy bytes that also form valid UTF-8 into characters in this range; `GetFileEncodingFormat()` then appends the UTF-8 suffix and biases detection toward UTF-8. `FileLoadThread` skips conversion for that result, so the legacy-encoded text displays as mojibake.
Do not use the decoded `QString` match alone to apply the UTF-8 confidence boost to ambiguous legacy-encoded input.
</issue_to_address>…tion
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
dengzhongyuan365-dev
force-pushed
the
agent/bugfix/384821
branch
from
December 1, 2026 03:14
6ae2b96 to
1d1817b
Compare
dengzhongyuan365-dev
marked this pull request as ready for review
December 1, 2026 03:28
lzwind
approved these changes
Dec 1, 2026
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: dengzhongyuan365-dev, lzwind The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Member
Author
|
/forcemerge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root Cause Analysis
When deepin-editor is built with Qt6,
QRegularExpressionuses the PCRE2 engine, which interprets\x4e00as byte0x4Efollowed by literal"00"rather than Unicode code point U+4E00. This causes the Chinese character regex"[\\x4e00-\\x9fa5]+"inDetectCode::GetFileEncodingFormat()(src/encodes/detectcode.cpp:210) to never match, preventing the confidence-boosting suffix from being appended. With chardet confidence stuck at 0.7525 (< 0.9 threshold),selectCoding()triggers the GB18030 fallback, resulting in garbled text for short UTF-8 files without BOM.Fix
Changed the Qt6
#elsebranch regex from"[\\x4e00-\\x9fa5]+"to"[\\x{4e00}-\\x{9fa5}]+", using PCRE2's correct\x{...}syntax for Unicode code points. The Qt5QRegExpbranch is untouched. Single-line change, one file.Change Safety Assessment
Code Safety
#elsebranch is modified; Qt5 path (QRegExp) is unchangedBusiness Impact Scope
Verification Suggestion
根因分析
deepin-editor 在 Qt6 构建下,
QRegularExpression使用 PCRE2 引擎,将\x4e00解析为字节0x4E加字面量"00",而非 Unicode 码点 U+4E00。这导致DetectCode::GetFileEncodingFormat()(src/encodes/detectcode.cpp:210)中的中文正则"[\\x4e00-\\x9fa5]+"永不匹配,无法追加提升置信度的后缀。chardet 置信度停留在 0.7525(< 0.9 阈值),selectCoding()触发 GB18030 兜底,导致无 BOM 的短 UTF-8 文件显示乱码。修复方案
将 Qt6
#else分支的正则从"[\\x4e00-\\x9fa5]+"改为"[\\x{4e00}-\\x{9fa5}]+",使用 PCRE2 正确的\x{...}Unicode 码点语法。Qt5QRegExp分支不动。单行修改,一个文件。改动安全评估
代码安全评估
#else分支;Qt5 路径(QRegExp)不受影响业务影响范围
验证建议
Summary by Sourcery
Correct Qt6 Chinese-character matching and require valid UTF-8 input before applying the UTF-8 confidence boost.
Bug Fixes:
Tests: