QString::lastNIdexOf( QRegularExpression)

Discuss anything related to product development
Post Reply
seasoned_geek
Posts: 254
Joined: Thu Jun 11 2020 12:18 pm

QString::lastNIdexOf( QRegularExpression)

Post 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?
barbara
Posts: 447
Joined: Sat Apr 04 2015 2:32 am
Contact:

Re: QString::lastNIdexOf( QRegularExpression)

Post 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.
seasoned_geek
Posts: 254
Joined: Thu Jun 11 2020 12:18 pm

Re: QString::lastNIdexOf( QRegularExpression)

Post 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
Post Reply