Solved QString::section() NO bug
-
Here I get QString looking like this:
title = "6 product feature key1:365 days key2:value"
To parse it to elements I use cycle:
for( int i = 0; i < MAXELEMS; i++ ) { QString tmp = title.section(QRegExp("\\s+"), i, i); qWarning()<<i<<tmp; //..... further processing }
Then suddenly see this:
0 6 1 product 2 feature 3 key1:35 //...ooops!!! not key1:365 4 days 5 key2:value
Ok, let's change to
title = "6 product feature key1:375 days key2:value"
Result:
0 6 1 product 2 feature 3 key1:375 //...right 4 days 5 key2:value
Then
title = "7 product feature key1:375 days key2:value"
Oops:
0 7 1 product 2 feature 3 key1:35 //...right 4 days 5 key2:value
And finally:
QString tmp = title.section(QRegExp("\\s+"), i, i); qWarning()<<title;
Prints of:
product feature key1:35 days key2:value // 8-()
That means QString::section cuts off all occurrences equal to required section from source string! This is a bug!
1st - There is nothing about source string change in QString::section() description.
2nd - Why it cuts all occurrences if I need only first one?I found this in my Android app build with Qt 5.12. Anybody can try check in other OSes.
-
@Gourmet said in QString::section() bug:
There is nothing about source string change in QString::section() description.
QString::section() is const - it can't change the source.
Why not simply use QString::split(QLatin1Char(' '))? Or even QString::splitRef()?btw: QRegExp is deprecated.
-
@Christian-Ehrlicher said in QString::section bug:
QString::section() is const - it can't change the source.
But it changes.
@Christian-Ehrlicher said in QString::section bug:
Why not simply use QString::split
The processing is more complex than just split. QString::section() is more useful in my case.
@Christian-Ehrlicher said in QString::section bug:
QRegExp is deprecated
There is nothing about this in Qt docs. As I see only some functions are obsolete but not entire class.
-
@Gourmet said in QString::section() bug:
But it changes.
Please show this to us by providing a small example - you just show snippets.
There is nothing about this in Qt docs.
Hmm: https://doc.qt.io/qt-5/qregexp.html#details
"Note: In Qt 5, the new QRegularExpression class provides a Perl compatible implementation of regular expressions and is recommended in place of QRegExp."
@SGaist: why is \obsolete missing here?
-
@Christian-Ehrlicher said in QString::section bug:
Please show this to us by providing a small example - you just show snippets.
Sorry, I have no time for experiments. I must fix and release product. May be some time later when I will be free.
-
@Christian-Ehrlicher said in QString::section() bug:
why is \obsolete missing here?
I swear last month it was there, because I explicitly looked up which one is obsolete!
-
@Gourmet said in QString::section() bug:
Why it cuts all occurrences if I need only first one?
You have to tell it that
https://doc.qt.io/qt-5/qregexp.html#setMinimal -
@Gourmet said in QString::section() bug:
Sorry, I have no time for experiments. I must fix and release product.
So we can't help you - QString::split() can not change the object - no matter what you tell us.
-
@Christian-Ehrlicher said in QString::section() bug:
@SGaist: why is \obsolete missing here?
Because not all APIs making use of it had replacements. However it's getting nuked in Qt 6.
-
Finally I have found error in my code. It was hard because of weird processing. QString::split() works fine.
But what about QRegExp - I do not remember why I used it instead of QRegularExpression. May be it looked simpler. This code was redesigned and regular expression was much more complex at beginning.