Thx, but sorry, I don't think this solves the problem.
Originally Posted by
anda_skoa
Basically you have the next match already retrieved from the QDirIterator at all times
Is that so? I think not.
Edit:
Ah, I think I can follow your train of thoughts at last!
Do you say you are ok with hasNext() altering dirIt's state, and have next() only return this altered state rather than advance dirIt itself?
This will break if next() is called without hasNext(). I'd not want to do this.
Unfortunately your pseudo code spares the relevant part:
Originally Posted by
anda_skoa
void findNext()
{
// iterate over dirIt until match found
// store in nextMatch
}
void findNext()
{
// iterate over dirIt until match found
// store in nextMatch
}
To copy to clipboard, switch view to plain text mode
How exactly do you do this?
"Iterate over dirIt" generally implies calling QDirIterator::hasNext() and QDirIterator::next() (for the regex match) in turns, the latter changing the iterator's state.
"Until match found" generally implies to run these cycles more than once unless the very first item returned by QDirIterator matches.
Also in your pseudo code hasNext() would have to call findNext(), too.
Let me try to clarify.
The general pseudo code for the caller using your syntax is
RegExIterator rxIt(dir, rx);
while ( rxIt.hasNext() ) {
//do something with fi
}
RegExIterator rxIt(dir, rx);
while ( rxIt.hasNext() ) {
QFileInfo fi = rxIt.next();
//do something with fi
}
To copy to clipboard, switch view to plain text mode
Now consider this directory structure
dir/
a
x
aa
y
dir/
a
x
aa
y
To copy to clipboard, switch view to plain text mode
and assume a regex of "a+". The expexted matches are {a, aa}, while {x, y} shall be skipped by the iterator.
Further assume the iterator's position (= state) to be aa. Expected result of hasNext() is false, and next() shall return an invalid result.
The hasNext() algorithm steps would be (loop serialized for clarity)
call dirIt.hasNext() //true
call dirIt.next() //necessary to (1)match regex, (2)prepare for 2nd hasNext(). This alters dirIt's position to y!
call dirIt.hasNext() again //false
revert dirIt position to aa //hasNext() must not alter state!
call dirIt.hasNext() //true
call dirIt.next() //necessary to (1)match regex, (2)prepare for 2nd hasNext(). This alters dirIt's position to y!
call dirIt.hasNext() again //false
revert dirIt position to aa //hasNext() must not alter state!
To copy to clipboard, switch view to plain text mode
Now, given QDirIterator's limitations I listed in this thread's first post, if I could perform the above algorithm on a dirIt copy, it would not change the original dirIt's state and I'd be safe.
In the above example, inside hasNext() I'd create another QDirIterator based on dir/, advance it to aa, then perform the algorithm on the "copy". Obviously this would only work if dirIt and copyDirIt return the items in the same order.
Bookmarks