fix: normalize decimal point input#211
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideNormalizes decimal point and digit grouping input more robustly by routing text through pointFaultTolerance, improving handling of locale-specific symbols, multiple decimal points, and various operators, including pasted expressions. Sequence diagram for decimal and grouping normalization in InputEditsequenceDiagram
participant InputEdit
participant Settings
InputEdit->>InputEdit: handleTextChanged text
InputEdit->>InputEdit: pointFaultTolerance text
InputEdit->>Settings: Settings::instance
Settings-->>InputEdit: getSystemDecimalSymbol
Settings-->>InputEdit: getSystemDigitGroupingSymbol
InputEdit->>InputEdit: replace locale grouping with groupingPlaceholder
InputEdit->>InputEdit: replace locale decimal with decimalPlaceholder
InputEdit->>InputEdit: normalize operators and multiple decimal points
InputEdit->>InputEdit: restore groupingPlaceholder to grouping symbol
InputEdit->>InputEdit: restore decimalPlaceholder to decimal symbol
InputEdit-->>InputEdit: normalizedExpr
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
e4fbe95 to
78e150e
Compare
log: 修复小数点输入和粘贴容错异常 pms: bug-361443
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: JWWTSL, 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 |
deepin pr auto review★ 总体评分:92分■ 【总体评价】
■ 【详细分析】
■ 【改进建议代码示例】 QString InputEdit::pointFaultTolerance(const QString &text)
{
const auto sys = Settings::instance();
QString decSym = sys->getSystemDecimalSymbol();
QString grpSym = sys->getSystemDigitGroupingSymbol();
// 使用不可见控制字符作为占位符,避免与用户输入冲突
const QString decimalPlaceholder = QString(QChar(0x1D));
const QString groupingPlaceholder = QString(QChar(0x1C));
QString workingText = text;
// 统一系统符号为内部处理格式
if (!grpSym.isEmpty() && grpSym != QLatin1String(","))
workingText.replace(grpSym, groupingPlaceholder);
if (!decSym.isEmpty() && decSym != QLatin1String("."))
workingText.replace(decSym, decimalPlaceholder);
workingText.replace(groupingPlaceholder, ",");
workingText.replace(decimalPlaceholder, ".");
// 兼容全角小数点
workingText.replace(QString::fromUtf8("。"), ".");
QString exp = workingText;
const QRegularExpression separatorExpression("[+-×÷+*/()\\-]");
QStringList list = exp.split(separatorExpression);
QStringList separators;
QRegularExpressionMatchIterator matches = separatorExpression.globalMatch(exp);
while (matches.hasNext())
separators.append(matches.next().captured());
QString oldText;
// 逐段处理数字部分,并按原顺序拼接运算符
for (int i = 0; i < list.size(); ++i) {
QString item = list[i];
int firstPoint = item.indexOf(".");
if (firstPoint != -1) {
// 处理小数点在首位的情况,补0
if (firstPoint == 0) {
item.insert(firstPoint, "0");
++firstPoint;
}
// 处理右括号或百分号后跟小数点的情况,移除小数点
else if (item.at(firstPoint - 1) == QChar(')') || item.at(firstPoint - 1) == QChar('%')) {
item.remove(firstPoint, 1);
}
// 多小数点只保留第一个
if (item.count(".") > 1) {
item.remove(".");
item.insert(firstPoint, ".");
}
}
oldText.append(item);
// 追加对应的运算符
if (i < separators.size())
oldText.append(separators[i]);
}
// 将内部格式还原为系统显示格式
QString result = oldText;
if (!grpSym.isEmpty() && grpSym != QLatin1String(","))
result.replace(",", groupingPlaceholder);
if (!decSym.isEmpty() && decSym != QLatin1String(".")) {
result.replace(".", decimalPlaceholder);
}
if (!grpSym.isEmpty() && grpSym != QLatin1String(","))
result.replace(groupingPlaceholder, grpSym);
if (!decSym.isEmpty() && decSym != QLatin1String("."))
result.replace(decimalPlaceholder, decSym);
return result;
} |
|
/forcemerge |
log: 修复小数点输入和粘贴容错异常
pms: bug-361443
Summary by Sourcery
Normalize numeric input by applying decimal point fault tolerance before further processing and improving handling of locale-specific decimal and grouping symbols.
Bug Fixes: