Skip to content

Commit 9401e01

Browse files
committed
Fix and improve the Addons::I2MProg
1 parent 238d6b8 commit 9401e01

2 files changed

Lines changed: 39 additions & 18 deletions

File tree

addons/src/i2mprog/utils-i2mprog.cpp

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

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

2526
#include "utils-i2mprog.h"
2627

2728

28-
int Utils::i2mprog(QString tx,
29+
#define APOSTROPHE_COMMENT_REGEXPR "\\'[^\\n\\r]*\\'"
30+
#define GCODE_COMMENT_REGEXPR "\\([^\\n\\r]*\\)"
31+
#define SINUMERIK_COMMENT_REGEXPR ";[^\\n\\r]*$"
32+
// |< sign >| |< digits-dot-digits >| |< digits-dot >| |< dot-digits >| |< digits >|
33+
#define WORD_REGEXPR "[%1]([ \\t]*[-+]?[ \\t]*((\\d[ \\t\\d]*\\.[ \\t\\d]*\\d)|(\\d[ \\t\\d]*\\.)|(\\.[ \\t\\d]*\\d)|([ \\t\\d]*\\d)))"
34+
35+
36+
int Utils::i2mprog(QString& tx,
2937
const QString& addr,
3038
bool toInch, int prec,
3139
const std::function<bool (int)>& interrupt)
3240
{
3341
int count = 0;
34-
QRegularExpression regex;
35-
36-
regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
37-
regex.setPattern(
38-
QString("[%1]{1,1}([+-]?\\d*\\.?\\d*)|\\([^\\n\\r]*\\)|\'[^\\n\\r]*\'|;[^\\n\\r]*$").arg(addr));
39-
auto match = regex.match(tx);
42+
QRegularExpression regex{
43+
QString(
44+
WORD_REGEXPR
45+
"|"
46+
GCODE_COMMENT_REGEXPR
47+
"|"
48+
APOSTROPHE_COMMENT_REGEXPR
49+
"|"
50+
SINUMERIK_COMMENT_REGEXPR
51+
).arg(addr),
52+
QRegularExpression::CaseInsensitiveOption
53+
};
54+
QRegularExpressionMatchIterator iterator = regex.globalMatch(tx);
4055

41-
while (match.hasMatch()) {
42-
int pos = match.capturedEnd();
56+
int pos = 0;
57+
QString result;
4358

59+
while (iterator.hasNext()) {
4460
if (interrupt(pos)) {
45-
return count;
61+
return 0;
4662
}
4763

64+
const QRegularExpressionMatch& match = iterator.next();
65+
4866
if (match.capturedLength(1) > 0) {
4967
QString f_tx = match.captured(1);
68+
f_tx.remove(' ');
69+
f_tx.remove('\t');
5070
bool ok;
5171
double it = f_tx.toDouble(&ok);
5272

@@ -58,14 +78,15 @@ int Utils::i2mprog(QString tx,
5878
}
5979

6080
QString conv = QString("%1").arg(it, 0, 'f', prec);
61-
tx.replace(match.capturedStart(), match.capturedLength(), conv);
62-
pos += conv.length() - match.capturedLength();
81+
result.append(tx.mid(pos, match.capturedStart(1) - pos));
82+
result.append(conv);
83+
pos = match.capturedEnd(1);
6384
count++;
6485
}
6586
}
66-
67-
match = regex.match(tx, pos);
6887
}
6988

89+
result.append(tx.mid(pos));
90+
tx = result;
7091
return count;
7192
}

addons/src/i2mprog/utils-i2mprog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class QString;
2727

2828

2929
namespace Utils {
30-
int i2mprog(QString tx,
30+
int i2mprog(QString& tx,
3131
const QString& addr,
3232
bool toInch, int prec,
3333
const std::function<bool(int)>& interrupt);

0 commit comments

Comments
 (0)