Page 1 of 1

QString::lastNIdexOf( QRegularExpression)

Posted: Sun Dec 13 2020 7:57 pm
by seasoned_geek
All,

Stumbled into this today. Qt 4.8 had

Code: Select all

int 	lastIndexOf(const QRegExp & rx, int from = -1) const
int 	lastIndexOf(QRegExp & rx, int from = -1) const
In porting some code I believed the method existed in CopperSpice but now used QRegularExpression. The compiler quickly proved that wrong. The doc appears to confirm the compiler as well.

Code: Select all

size_type 	indexOf (const QRegularExpression8 &regExp, size_type from=0) const
const_iterator 	indexOfFast (const QRegularExpression8 &regExp) const 
const_iterator 	indexOfFast (const QRegularExpression8 &regExp, const_iterator from) const


We have it going forward, just not backwards. Was this a conscious decision?

Re: QString::lastNIdexOf( QRegularExpression)

Posted: Mon Dec 14 2020 8:48 pm
by barbara
We have it going forward, just not backwards. Was this a conscious decision?
The QRegExp was removed and replaced with completely redesigned QRegularExpression class. The method "lastIndexOf()" was not implemented since searching in reverse in a regex is extremely slow and inefficient.

Very often the regex can be rewritten so it only matches at the end of the search string. If you can not do this then use QRegularExpression::globalMatch() which returns a list of all locations where a match occurs.

Re: QString::lastNIdexOf( QRegularExpression)

Posted: Thu Dec 17 2020 5:49 pm
by seasoned_geek
That's what I will end up having to do, but one should consider the potential size of an object which may or may not be in memory. If the object is memory mapped to disk one could have to parse many gigs to obtain something that is less than twenty bytes in from the end.

The proposed solution works well for an incredibly fast desktop. When you are running on a battery operated embedded system with horrific dynamic memory allocation times, dynamically building a list you are going to throw away can be very expensive.

Just my 0.002 cents