Skip to content

Commit 18d7e50

Browse files
committed
move to QRegularExpression for all searches
1 parent b9030f5 commit 18d7e50

File tree

7 files changed

+27
-124
lines changed

7 files changed

+27
-124
lines changed

src/buildmanager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,7 @@ void BuildManager::preview(const QString &preamble, const PreviewSource &source,
17721772
QStringList addPaths;
17731773
addPaths << masterDir;
17741774
if (preamble_mod.contains("\\usepackage{import}")) {
1775-
QStringList imports = regExpFindAllMatches(preamble_mod, QRegExp("\\\\subimport\\{([^}\n]*)\\}\\{[^}\n]*\\}"), 1);
1775+
QStringList imports = regularExpressionFindAllMatches(preamble_mod, QRegularExpression("\\\\subimport\\{([^}\n]*)\\}\\{[^}\n]*\\}"), 1);
17761776
imports.sort();
17771777
for (int i = imports.size() - 1; i > 0; i--)
17781778
if (imports[i] == imports[i - 1]) imports.removeAt(i);

src/qcodeedit/lib/document/qdocument.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,10 +1608,11 @@ int QDocument::findLineContaining(const QString &searchText, const int& startLi
16081608

16091609
int QDocument::findLineRegExp(const QString &searchText, const int& startLine, const Qt::CaseSensitivity cs, const bool wholeWord, const bool useRegExp) const{
16101610

1611-
QRegExp m_regexp=generateRegExp(searchText,cs==Qt::CaseSensitive,wholeWord,useRegExp);
1611+
QRegularExpression m_regexp=generateRegularExpression(searchText,cs==Qt::CaseSensitive,wholeWord,useRegExp);
16121612

16131613
for (int i=startLine;i<lines();i++){
1614-
if(m_regexp.indexIn(line(i).text(),0)>-1)
1614+
QRegularExpressionMatch match=m_regexp.match(line(i).text());
1615+
if(match.hasMatch())
16151616
return i;
16161617
}
16171618

src/qcodeedit/lib/document/qdocumentsearch.cpp

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,10 @@ void QDocumentSearch::searchMatches(const QDocumentCursor& subHighlightScope, bo
156156
l.setFlag(QDocumentLine::FormatsApplied, false);
157157

158158
const QString &s = boundaries.endLine != ln ? l.text() : l.text().left(boundaries.end);
159-
#if QT_VERSION > 0x050500
159+
160160
QRegularExpressionMatch match=m_regularExpression.match(s, hc.columnNumber());
161161
int column = match.capturedStart();
162162
int length = match.capturedLength();
163-
#else
164-
int column=m_regexp.indexIn(s, hc.columnNumber());
165-
int length=m_regexp.matchedLength();
166-
#endif
167163

168164
/*
169165
qDebug("searching %s in %s => %i",
@@ -209,18 +205,13 @@ void QDocumentSearch::searchMatches(const QDocumentCursor& subHighlightScope, bo
209205
if((endOffset>=0)&&(i+1==endLine)){
210206
txt=txt.left(endOffset);
211207
}
212-
#if QT_VERSION > 0x050500
208+
213209
QRegularExpressionMatch match=m_regularExpression.match(txt,offset);
214210
if(match.hasMatch()){
215211
m_editor->addMarkDelayed(i,Qt::darkYellow,"search");
216212
needsUpdate=true;
217213
}
218-
#else
219-
if(m_regexp.indexIn(txt,offset)>-1){
220-
m_editor->addMarkDelayed(i,Qt::darkYellow,"search");
221-
needsUpdate=true;
222-
}
223-
#endif
214+
224215
offset=0;
225216
}
226217
if(needsUpdate)
@@ -290,7 +281,6 @@ void QDocumentSearch::recreateRegExp(){
290281
:
291282
Qt::CaseInsensitive;
292283

293-
#if QT_VERSION >= 0x050500
294284
QRegularExpression::PatternOptions patternOption= cs==Qt::CaseInsensitive ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption ;
295285
patternOption |= QRegularExpression::UseUnicodePropertiesOption;
296286
if ( hasOption(RegExp) )
@@ -303,23 +293,6 @@ void QDocumentSearch::recreateRegExp(){
303293
} else {
304294
m_regularExpression = QRegularExpression(QRegularExpression::escape(m_string), patternOption);
305295
}
306-
#else
307-
if ( hasOption(RegExp) )
308-
{
309-
m_regexp = QRegExp(m_string, cs, QRegExp::RegExp);
310-
} else if ( hasOption(WholeWords) ) {
311-
//todo: screw this? it prevents searching of "world!" and similar things
312-
//(qtextdocument just checks the surrounding character when searching for whole words, this would also allow wholewords|regexp search)
313-
m_regexp = QRegExp(
314-
QString("\\b%1\\b").arg(QRegExp::escape(m_string)),
315-
cs,
316-
QRegExp::RegExp
317-
);
318-
} else {
319-
m_regexp = QRegExp(m_string, cs, QRegExp::FixedString);
320-
}
321-
m_regexp.setMinimal( m_option & QDocumentSearch::NonGreedy); // allow greedy or non-greedy capture
322-
#endif
323296
}
324297

325298

@@ -492,15 +465,9 @@ QString QDocumentSearch::replaceTextExpanded() const
492465
QString replacement = hasOption(EscapeSeq)?escapeCpp(m_replace):m_replace;
493466

494467
if (hasOption(RegExp))
495-
#if QT_VERSION>0x050500
496468
for( int i=m_match.lastCapturedIndex();i >=0; --i )
497469
replacement.replace(QString("\\") + QString::number(i),
498470
m_match.captured(i));
499-
#else
500-
for ( int i = m_regexp.captureCount(); i >= 0; --i )
501-
replacement.replace(QString("\\") + QString::number(i),
502-
m_regexp.cap(i));
503-
#endif
504471

505472
return replacement;
506473
}
@@ -614,8 +581,6 @@ int QDocumentSearch::next(bool backward, bool all, bool again, bool allowWrapAro
614581
// replace
615582
if (hasOption(Replace) && again && !all) {
616583
bool replaceSelectedText = false;
617-
#if QT_VERSION > 0x050500
618-
qDebug()<<m_match.hasMatch() << m_match.captured();
619584
if (m_match.hasMatch() && m_match.captured()==m_cursor.selectedText()) {
620585
replaceSelectedText = true;
621586
} else if (m_regularExpression.pattern().contains("(?=") || m_regularExpression.pattern().contains("(?!")) {
@@ -637,28 +602,6 @@ int QDocumentSearch::next(bool backward, bool all, bool again, bool allowWrapAro
637602
start = end;
638603
}
639604
}
640-
#else
641-
if (m_regexp.exactMatch(m_cursor.selectedText())) {
642-
replaceSelectedText = true;
643-
} else if (m_regexp.pattern().contains("(?=") || m_regexp.pattern().contains("(?!")) {
644-
// special handling for lookahead: The selected text is not enough to match the regexp
645-
// because the lookahead context is missing. Therefore we have to find matches to the
646-
// whole line until we find the original selection. Only then, we know that the original
647-
// selection is match and should be replaced.
648-
int start = 0;
649-
while (true) {
650-
start = m_regexp.indexIn(m_cursor.line().text(), start);
651-
if (start < 0)
652-
break;
653-
int end = start + m_regexp.matchedLength();
654-
if ((start == m_cursor.startColumnNumber() && end == m_cursor.endColumnNumber()) ||
655-
(end == m_cursor.startColumnNumber() && start == m_cursor.endColumnNumber())) {
656-
replaceSelectedText = true;
657-
}
658-
start = end;
659-
}
660-
}
661-
#endif
662605
if (replaceSelectedText) {
663606
replaceCursorText(backward);
664607
updateReplacementOverlays();
@@ -759,7 +702,7 @@ int QDocumentSearch::next(bool backward, bool all, bool again, bool allowWrapAro
759702

760703
int column;
761704
int length;
762-
#if QT_VERSION > 0x050500
705+
763706
if (backward) {
764707
column=s.lastIndexOf(m_regularExpression,m_cursor.columnNumber()-coloffset,&m_match);
765708
length=m_match.capturedLength();
@@ -769,13 +712,6 @@ int QDocumentSearch::next(bool backward, bool all, bool again, bool allowWrapAro
769712
length=m_match.capturedLength();
770713
}
771714

772-
773-
#else
774-
if (backward) column=m_regexp.lastIndexIn(s,m_cursor.columnNumber()-coloffset);
775-
else column=m_regexp.indexIn(s, m_cursor.columnNumber());
776-
length=m_regexp.matchedLength();
777-
#endif
778-
779715
QDocumentCursor::MoveOperation nextMove = move;
780716

781717
bool matches = column != -1 && (backward || column >= m_cursor.columnNumber() );

src/qcodeedit/lib/document/qdocumentsearch.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525

2626
#include <QString>
27-
#include <QRegExp>
2827
#include <QRegularExpression>
2928
#include <QPointer>
3029
#include <QAbstractScrollArea>
@@ -105,7 +104,6 @@ class QCE_EXPORT QDocumentSearch: public QObject
105104

106105
void recreateRegExp();
107106

108-
// int m_index;
109107
Options m_option;
110108
QString m_string;
111109
QRegularExpression m_regularExpression;

src/searchresultmodel.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -313,21 +313,19 @@ QList<SearchMatch> SearchResultModel::getSearchMatches(const QDocumentLine &docl
313313
{
314314
if (!docline.isValid() || mExpression.isEmpty()) return QList<SearchMatch>();
315315

316-
QRegExp regexp = generateRegExp(mExpression, mIsCaseSensitive, mIsWord, mIsRegExp);
316+
QRegularExpression regexp = generateRegularExpression(mExpression, mIsCaseSensitive, mIsWord, mIsRegExp);
317317
QString text = docline.text();
318318

319-
int i = 0;
320319
QList<SearchMatch> result;
321-
while (i < text.length()) {
322-
i = regexp.indexIn(text, i);
323-
if (i < 0) break;
324-
325-
SearchMatch match;
326-
match.pos = i;
327-
match.length = regexp.matchedLength();
328-
result << match;
329-
i+=match.length;
330-
}
320+
QRegularExpressionMatch re_match = regexp.match(text);
321+
int offset=re_match.capturedStart();
322+
while (offset > -1) {
323+
SearchMatch match;
324+
match.pos = offset;
325+
match.length = re_match.capturedLength();
326+
result << match;
327+
}
328+
331329
return result;
332330
}
333331

@@ -367,12 +365,13 @@ QVariant SearchResultModel::headerData(int section, Qt::Orientation orientation,
367365

368366
int SearchResultModel::getNextSearchResultColumn(const QString &text, int col)
369367
{
370-
QRegExp m_regexp = generateRegExp(mExpression, mIsCaseSensitive, mIsWord, mIsRegExp);
368+
QRegularExpression m_regexp = generateRegularExpression(mExpression, mIsCaseSensitive, mIsWord, mIsRegExp);
371369

372370
int i = 0;
373371
int i_old = 0;
374372
while (i <= col && i > -1) {
375-
i = m_regexp.indexIn(text, i);
373+
QRegularExpressionMatch match = m_regexp.match(text,i);
374+
i = match.capturedStart();
376375
if (i > -1) {
377376
i_old = i;
378377
i++;

src/smallUsefulFunctions.cpp

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ QString findToken(const QString &line, const QString &token, int &start)
464464
* \param token regexp to search
465465
* \return text after token
466466
*/
467-
QString findToken(const QString &line, QRegExp &token)
467+
/*
468+
QString findToken(const QString &line, QRegularExpression &token)
468469
{
469470
//ATTENTION: token is not const because, you can't call cap on const qregexp in qt < 4.5
470471
int tagStart = 0;
@@ -476,7 +477,7 @@ QString findToken(const QString &line, QRegExp &token)
476477
return s;
477478
}
478479
return "";
479-
}
480+
}*/
480481

481482
bool findTokenWithArg(const QString &line, const QString &token, QString &outName, QString &outArg)
482483
{
@@ -607,26 +608,6 @@ QString getParamItem(const QString &line, int pos, bool stopAtWhiteSpace)
607608
return line.mid(start, end - start);
608609
}
609610

610-
QRegExp generateRegExp(const QString &text, const bool isCase, const bool isWord, const bool isRegExp)
611-
{
612-
Qt::CaseSensitivity cs = isCase ? Qt::CaseSensitive : Qt::CaseInsensitive;
613-
QRegExp m_regexp;
614-
if ( isRegExp ) {
615-
m_regexp = QRegExp(text, cs, QRegExp::RegExp);
616-
} else if ( isWord ) {
617-
//todo: screw this? it prevents searching of "world!" and similar things
618-
//(qtextdocument just checks the surrounding character when searching for whole words, this would also allow wholewords|regexp search)
619-
m_regexp = QRegExp(
620-
QString("\\b%1\\b").arg(QRegExp::escape(text)),
621-
cs,
622-
QRegExp::RegExp
623-
);
624-
} else {
625-
m_regexp = QRegExp(text, cs, QRegExp::FixedString);
626-
}
627-
return m_regexp;
628-
}
629-
630611
QRegularExpression generateRegularExpression(const QString &text, const bool isCase, const bool isWord, const bool isRegExp)
631612
{
632613
QRegularExpression::PatternOption po = isCase ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption;
@@ -646,18 +627,6 @@ QRegularExpression generateRegularExpression(const QString &text, const bool isC
646627
return m_regexp;
647628
}
648629

649-
QStringList regExpFindAllMatches(const QString &searchIn, const QRegExp &regexp, int cap)
650-
{
651-
int offset = regexp.indexIn(searchIn);
652-
QStringList res;
653-
while (offset > -1) {
654-
res << regexp.cap(cap);
655-
offset = regexp.indexIn(searchIn, offset + regexp.matchedLength());
656-
}
657-
return res;
658-
}
659-
660-
661630
QStringList regularExpressionFindAllMatches(const QString &searchIn, const QRegularExpression &regexp, int cap)
662631
{
663632
QRegularExpressionMatch match = regexp.match(searchIn);

src/smallUsefulFunctions.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ QString trimRight(const QString &s);
8484
/// find token (e.g. \label \input \section and return content (\section{content})
8585
QString findToken(const QString &line, const QString &token);
8686
QString findToken(const QString &line, const QString &token, int &start);
87-
QString findToken(const QString &line, QRegExp &token);
87+
//QString findToken(const QString &line, QRegularExpression &token);
8888
/// find token (e.g. \label \input \section and return content (\newcommand{name}[arg]), returns true if outName!=""
8989
bool findTokenWithArg(const QString &line, const QString &token, QString &outName, QString &outArg);
9090

9191

9292
/// generate multiple times used regexpression
93-
QRegExp generateRegExp(const QString &text, const bool isCase, const bool isWord, const bool isRegExp);
93+
QRegularExpression generateRegularExpression(const QString &text, const bool isCase, const bool isWord, const bool isRegExp);
9494

95-
QStringList regExpFindAllMatches(const QString &searchIn, const QRegExp &regexp, int cap = 0);
95+
QStringList regularExpressionFindAllMatches(const QString &searchIn, const QRegularExpression &regexp, int cap = 0);
9696
QList<int> indicesOf(const QString &line, const QString &word, Qt::CaseSensitivity cs = Qt::CaseSensitive);
9797
QList<int> indicesOf(const QString &line, const QRegularExpression &rx);
9898

0 commit comments

Comments
 (0)