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}
0 commit comments