From a05116e6f1f87027eb90a24da4895577f596f3c9 Mon Sep 17 00:00:00 2001 From: Aaron Linville Date: Thu, 2 Feb 2017 22:13:32 -0500 Subject: Update QSyntaxHighlighter docs to use QRegularExpression Update the QSyntaxHighlighter examples to use the new QRegularExpression class in place of QRegExp. Fix typos. Remove duplicated snippet. Replace lengthy section of duplicate text in highlight(..) with a note to see the detailed description. Task-number: QTBUG-58494 Change-Id: Id8d94bddbed52e6e52feac107f6fc84e2fe4518a Reviewed-by: Samuel Gaist Reviewed-by: Sze Howe Koh --- .../code/src_gui_text_qsyntaxhighlighter.cpp | 43 ++++++---------------- 1 file changed, 12 insertions(+), 31 deletions(-) (limited to 'src/gui/doc') diff --git a/src/gui/doc/snippets/code/src_gui_text_qsyntaxhighlighter.cpp b/src/gui/doc/snippets/code/src_gui_text_qsyntaxhighlighter.cpp index da960088b7..bb27eb9612 100644 --- a/src/gui/doc/snippets/code/src_gui_text_qsyntaxhighlighter.cpp +++ b/src/gui/doc/snippets/code/src_gui_text_qsyntaxhighlighter.cpp @@ -60,14 +60,13 @@ void MyHighlighter::highlightBlock(const QString &text) QTextCharFormat myClassFormat; myClassFormat.setFontWeight(QFont::Bold); myClassFormat.setForeground(Qt::darkMagenta); - QString pattern = "\\bMy[A-Za-z]+\\b"; - QRegExp expression(pattern); - int index = text.indexOf(expression); - while (index >= 0) { - int length = expression.matchedLength(); - setFormat(index, length, myClassFormat); - index = text.indexOf(expression, index + length); + QRegularExpression expression("\\bMy[A-Za-z]+\\b"); + QRegularExpressionMatchIterator i = expression.globalMatch(text); + while (i.hasNext()) + { + QRegularExpressionMatch match = i.next(); + setFormat(match.capturedStart(), match.capturedLength(), myClassFormat); } } //! [1] @@ -77,8 +76,8 @@ void MyHighlighter::highlightBlock(const QString &text) QTextCharFormat multiLineCommentFormat; multiLineCommentFormat.setForeground(Qt::red); -QRegExp startExpression("/\\*"); -QRegExp endExpression("\\*/"); +QRegularExpression startExpression("/\\*"); +QRegularExpression endExpression("\\*/"); setCurrentBlockState(0); @@ -87,14 +86,15 @@ if (previousBlockState() != 1) startIndex = text.indexOf(startExpression); while (startIndex >= 0) { - int endIndex = text.indexOf(endExpression, startIndex); + QRegularExpressionMatch endMatch; + int endIndex = text.indexOf(endExpression, startIndex, &endMatch); int commentLength; if (endIndex == -1) { setCurrentBlockState(1); commentLength = text.length() - startIndex; } else { commentLength = endIndex - startIndex - + endExpression.matchedLength(); + + endMatch.capturedLength(); } setFormat(startIndex, commentLength, multiLineCommentFormat); startIndex = text.indexOf(startExpression, @@ -104,25 +104,6 @@ while (startIndex >= 0) { //! [3] -void MyHighlighter::highlightBlock(const QString &text) -{ - QTextCharFormat myClassFormat; - myClassFormat.setFontWeight(QFont::Bold); - myClassFormat.setForeground(Qt::darkMagenta); - QString pattern = "\\bMy[A-Za-z]+\\b"; - - QRegExp expression(pattern); - int index = text.indexOf(expression); - while (index >= 0) { - int length = expression.matchedLength(); - setFormat(index, length, myClassFormat); - index = text.indexOf(expression, index + length); - } - } -//! [3] - - -//! [4] struct ParenthesisInfo { QChar char; @@ -133,4 +114,4 @@ struct BlockData : public QTextBlockUserData { QVector parentheses; }; -//! [4] +//! [3] -- cgit v1.2.3