Skip to content

Commit ac97e45

Browse files
committed
Improve the Addons::InsertDot
1 parent 88742b8 commit ac97e45

1 file changed

Lines changed: 44 additions & 26 deletions

File tree

addons/src/dot/utils-insertdot.cpp

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@
1818
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
*/
2020

21-
#include <QLatin1Char> // for QLatin1Char
22-
#include <QRegularExpression> // for QRegularExpression, QRegularExpression::CaseInsensitiveOption
23-
#include <QRegularExpressionMatch> // for QRegularExpressionMatch
24-
#include <QString> // for QString
21+
#include <QLatin1Char> // for QLatin1Char
22+
#include <QRegularExpression> // for QRegularExpression, QRegularExpression::CaseInsensitiveOption
23+
#include <QRegularExpressionMatch> // for QRegularExpressionMatch
24+
#include <QRegularExpressionMatchIterator> // for QRegularExpressionMatchIterator
25+
#include <QString> // for QString
2526

2627
#include "utils-insertdot.h"
2728

2829

30+
#define APOSTROPHE_COMMENT_REGEXPR "\\'[^\\n\\r]*\\'"
31+
#define GCODE_COMMENT_REGEXPR "\\([^\\n\\r]*\\)"
32+
#define SINUMERIK_COMMENT_REGEXPR ";[^\\n\\r]*$"
33+
// |< sign >| |< digits-dot-digits >| |< digits-dot >| |< dot-digits >| |< digits >|
34+
#define WORD_REGEXPR "[%1]([ \\t]*[-+]?[ \\t]*((\\d[ \\t\\d]*\\.[ \\t\\d]*\\d)|(\\d[ \\t\\d]*\\.)|(\\.[ \\t\\d]*\\d)|([ \\t\\d]*\\d)))"
35+
36+
2937
int Utils::insertDot(QString& tx,
3038
const QString& addr,
3139
bool convert,
@@ -34,47 +42,57 @@ int Utils::insertDot(QString& tx,
3442
{
3543
int count = 0;
3644
int pos = 0;
37-
QString f_tx;
38-
QRegularExpression regex;
39-
40-
regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
41-
regex.setPattern(QString("[%1]{1,1}[-.+0-9]+|\\([^\\n\\r]*\\)|\'[^\\n\\r]*\'|;[^\\n\\r]*$").arg(addr));
42-
auto match = regex.match(tx);
43-
44-
while (match.hasMatch()) {
45-
f_tx = match.captured();
46-
pos = match.capturedEnd();
45+
QRegularExpression regex{
46+
QString(
47+
WORD_REGEXPR
48+
"|"
49+
GCODE_COMMENT_REGEXPR
50+
"|"
51+
APOSTROPHE_COMMENT_REGEXPR
52+
"|"
53+
SINUMERIK_COMMENT_REGEXPR
54+
).arg(addr),
55+
QRegularExpression::CaseInsensitiveOption
56+
};
57+
QRegularExpressionMatchIterator iterator = regex.globalMatch(tx);
58+
QString result;
4759

60+
while (iterator.hasNext()) {
4861
if (interrupt(pos)) {
49-
return count;
62+
return 0;
5063
}
5164

52-
if (!f_tx.contains(QLatin1Char('(')) && !f_tx.contains(QLatin1Char('\''))
53-
&& !f_tx.contains(QLatin1Char(';'))) {
54-
if (convert && !f_tx.contains(QLatin1Char('.'))) {
55-
f_tx.remove(0, 1);
65+
QRegularExpressionMatch match = iterator.next();
66+
67+
if (match.capturedLength(1) > 0) {
68+
QString f_tx = match.captured(1);
69+
f_tx.remove(' ');
70+
f_tx.remove('\t');
5671

57-
//f_tx.remove('+');
72+
if (convert && !f_tx.contains(QLatin1Char('.'))) {
5873
bool ok;
5974
double it = f_tx.toDouble(&ok);
6075

6176
if (ok) {
6277
it = it / divider;
63-
tx.replace(match.capturedStart() + 1, match.capturedLength() - 1,
64-
QString("%1").arg(it, 0, 'f', 3));
78+
QString conv = QString("%1").arg(it, 0, 'f', 3);
79+
result.append(tx.mid(pos, match.capturedStart(1) - pos));
80+
result.append(conv);
81+
pos = match.capturedEnd(1);
6582
count++;
6683
}
6784
}
6885

6986
if (!convert && !f_tx.contains(QLatin1Char('.'))) {
70-
tx.insert(match.capturedEnd(), QLatin1Char('.'));
71-
pos++;
87+
result.append(tx.mid(pos, match.capturedEnd(1) - pos));
88+
result.append(QLatin1Char('.'));
89+
pos = match.capturedEnd(1);
7290
count++;
7391
}
7492
}
75-
76-
match = regex.match(tx, pos);
7793
}
7894

95+
result.append(tx.mid(pos));
96+
tx = result;
7997
return count;
8098
}

0 commit comments

Comments
 (0)