@@ -57,7 +57,7 @@ static const QStringList s_Operators = {"=",
5757 " <<" };
5858
5959// Python braces
60- static const QStringList s_Braces = {" \\ {" , " \\ }" , " \\ (" , " \\ )" , R"( [)" , R"( ])" };
60+ static const QStringList s_Braces = {" \\ {" , " \\ }" , " \\ (" , " \\ )" , R"( \ [)" , R"( \ ])" };
6161
6262QString PythonHighlighter::CodeElementName (PythonHighlighter::CodeElement e)
6363{
@@ -145,7 +145,7 @@ void PythonHighlighter::initialize()
145145 m_highlightingRules += HighlightingRule (CodeElement::Definition, R"( \bclass\b\s*(\w+))" , 1 );
146146
147147 // From '#' until a newline
148- m_highlightingRules += HighlightingRule (CodeElement::Comment, " #[^\\ n]*" , 0 );
148+ m_highlightingRules += HighlightingRule (CodeElement::Comment, " #[^\r \n ]*" , 0 );
149149
150150 // Numeric literals
151151 m_highlightingRules += HighlightingRule (CodeElement::Numbers, " \\ b[+-]?[0-9]+[lL]?\\ b" , 0 );
@@ -160,22 +160,18 @@ void PythonHighlighter::highlightPythonBlock(const QString &text)
160160 if (text.isEmpty ())
161161 return ;
162162
163- int index = -1 ;
164-
165163 // Do other syntax formatting
166164 for (const auto &rule : m_highlightingRules)
167165 {
168- index = rule.pattern .indexIn (text, 0 );
169-
170- // We actually want the index of the nth match
171- while (index >= 0 )
166+ QRegularExpressionMatchIterator i = rule.pattern .globalMatch (text);
167+ while (i.hasNext ())
172168 {
173- index = rule.pattern .pos (rule.matchIndex );
174- int length = rule.pattern .cap (rule.matchIndex ).length ();
169+ QRegularExpressionMatch match = i.next ();
170+ const int index = match.capturedStart (rule.matchIndex );
171+ const int length = match.captured (rule.matchIndex ).length ();
175172 if (length > 0 )
176173 {
177174 setFormat (index, length, rule.format );
178- index = rule.pattern .indexIn (text, index + length);
179175 }
180176 }
181177 }
@@ -196,7 +192,7 @@ inside a multi-line string when this function is finished.
196192*/
197193bool PythonHighlighter::matchMultiLine (const QString &text, const HighlightingRule &rule)
198194{
199- int start, add, end, length;
195+ int start, add, length;
200196
201197 // If inside triple-single quotes, start at 0
202198 if (previousBlockState () == rule.matchIndex )
@@ -207,36 +203,42 @@ bool PythonHighlighter::matchMultiLine(const QString &text, const HighlightingRu
207203 // Otherwise, look for the delimiter on this line
208204 else
209205 {
210- start = rule.pattern .indexIn (text);
211- // Move past this match
212- add = rule.pattern .matchedLength ();
206+ QRegularExpressionMatch match = rule.pattern .match (text);
207+ if (match.hasMatch ()) {
208+ start = match.capturedStart (0 );
209+ add = match.capturedLength (0 );
210+ } else {
211+ start = -1 ;
212+ add = 0 ;
213+ }
213214 }
214215
215- // As long as there's a delimiter match on this line...
216216 while (start >= 0 )
217217 {
218218 // Look for the ending delimiter
219- end = rule.pattern .indexIn (text, start + add);
220- // Ending delimiter on this line?
219+ QRegularExpressionMatch endMatch = rule.pattern .match (text, start + add);
220+ int end = endMatch.hasMatch () ? endMatch.capturedStart (0 ) : -1 ;
221+
221222 if (end >= add)
222223 {
223- length = end - start + add + rule.pattern .matchedLength ();
224+ // Ending delimiter on this line
225+ length = end - start + add + endMatch.capturedLength (0 );
224226 setCurrentBlockState (0 );
225227 }
226- // No; multi-line string
227228 else
228229 {
230+ // No; multi-line string
229231 setCurrentBlockState (rule.matchIndex );
230232 length = text.length () - start + add;
231233 }
232234
233- // Apply formatting
234235 setFormat (start, length, rule.format );
235236
236237 // Look for the next match
237- start = rule.pattern .indexIn (text, start + length);
238+ QRegularExpressionMatch nextMatch = rule.pattern .match (text, start + length);
239+ start = nextMatch.hasMatch () ? nextMatch.capturedStart (0 ) : -1 ;
240+ add = nextMatch.hasMatch () ? nextMatch.capturedLength (0 ) : 0 ;
238241 }
239242
240- // Return True if still inside a multi-line string, False otherwise
241243 return currentBlockState () == rule.matchIndex ;
242- }
244+ }
0 commit comments