Skip to content

feat: add handling for special characters.#14

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
deepin-mozart:master
Jan 28, 2026
Merged

feat: add handling for special characters.#14
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
deepin-mozart:master

Conversation

@deepin-mozart
Copy link
Contributor

add cleanString function to process input strings and improve text handling

Log:

add cleanString function to process input strings and improve text handling

Log:
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: deepin-mozart

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-ci-robot
Copy link

deepin pr auto review

这段代码主要添加了一个 cleanString 函数,用于在将 std::string 转换为 QString 时去除其中的空字符(\0),并在 nlp_demotts_demo 两个函数中应用了该修改。

以下是对这段代码的审查意见,包括语法逻辑、代码质量、代码性能和代码安全方面的建议:

1. 语法逻辑

  • 正确性:逻辑上是正确的。QString::fromStdString 会转换字符串,QString::remove(QChar('\0')) 会移除所有的空字符。这解决了可能存在的包含空终止符的 C 风格字符串或二进制数据被错误转换为 QString 导致截断的问题。
  • 参数传递cleanString 函数的参数 std::string &stdString 使用了引用传递。由于函数内部并未修改 stdString,建议将其改为 const std::string &stdString,以明确表达该函数不会修改输入数据的意图,同时也允许传入临时对象或 const 对象。

2. 代码质量

  • 函数命名与封装cleanString 这个名字略显通用。如果这个函数的目的是专门处理 "null characters",建议改名为 removeNullCharssanitizeString,这样更具自解释性。
  • 代码复用:将转换和清洗逻辑封装成函数是一个好的做法,避免了在多处重复编写 QString::fromStdString.remove 逻辑,符合 DRY(Don't Repeat Yourself)原则。
  • 头文件包含:请确保文件顶部包含了必要的 Qt 头文件(如 <QString>),否则编译会报错。

3. 代码性能

  • 拷贝开销:当前的实现涉及两次主要的拷贝/转换:
    1. QString::fromStdString(stdString):将 std::string 拷贝并转换为 QString(涉及内存分配和字符编码转换)。
    2. strText.remove(...)remove 函数通常会返回一个新的 QString 对象(或者在 Qt5/6 中视具体实现可能修改自身并返回引用),这可能会引起二次内存分配或数据移动。
  • 优化建议:虽然对于短文本(如 Demo 中的输入)性能影响可以忽略不计,但如果用于高频或大文本处理,可以考虑直接在 std::string 阶段使用 erase + remove 惯用法去除 \0,然后再转换,或者确保 remove 操作是原地进行的(Qt 的 remove 通常会修改自身并返回引用,这里写法没问题,但需注意 fromStdString 的开销不可避免)。

4. 代码安全

  • 输入验证cleanString 函数没有检查输入字符串是否为空或是否有效。虽然 QString 的构造函数能很好地处理空字符串,但显式的检查(如果业务逻辑需要)能提高代码的健壮性。
  • 编码安全QString::fromStdString 依赖于 std::string 的内容。如果 std::string 包含的不是有效的 UTF-8 序列(假设源码是 UTF-8 环境),转换可能会产生替换字符(�)。如果这是一个潜在风险,应考虑使用 QString::fromUtf8 并配合错误处理机制。
  • 空字符处理:去除 \0 是一个很好的安全/健壮性实践,防止了因 C 风格字符串处理函数遇到 \0 提前截断而导致的逻辑错误。

改进后的代码建议

// 建议修改函数签名,添加 const
QString removeNullChars(const std::string &stdString)
{
    // 直接转换,Qt 的 remove 会修改自身并返回引用
    QString strText = QString::fromStdString(stdString);
    // 移除空字符
    strText.remove(QChar('\0'));
    return strText;
}

或者在 nlp_demotts_demo 的调用处:

// 由于参数是 const 引用,如果 text 是 std::string,可以直接传
QString strText = removeNullChars(text);

总结

这段修改主要是为了修复潜在的字符串截断问题,逻辑正确且有效。主要改进点在于将函数参数设为 const 引用以提高接口设计的严谨性,并优化函数命名以增加可读性。

@deepin-mozart
Copy link
Contributor Author

/forcemerge

@deepin-bot
Copy link

deepin-bot bot commented Jan 28, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit 3750d83 into linuxdeepin:master Jan 28, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants