Details
-
Bug
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
5.12.5
-
None
Description
The documentation here: https://doc.qt.io/qt-5/qtextboundaryfinder.html#position
...says: "The range is from 0 (the beginning of the string) to the length of the string inclusive."
After move past the last grapheme boundary, position() will always return -1.
See code here:
int QTextBoundaryFinder::toNextBoundary() { if (!d || pos < 0 || pos >= length) { pos = -1; return pos; } ...
Method toPreviousBoundary() has similar code.
Here are two possible solutions:
1) Update the docs. Maybe there is interesting history behind this design decision to be shared.
2) Change the code. Do not update pos; only return -1.
Ex: if (!d || pos < 0 || pos >= length) return -1;
Simple repro:
#include <QTextBoundaryFinder> int main(int argc, char *argv[]) { const QLatin1String lstr{"abc"}; const QString str{lstr}; QTextBoundaryFinder f{QTextBoundaryFinder::BoundaryType::Grapheme, str}; const int p1 = f.position(); // 0 const int b1 = f.toNextBoundary(); // 1 const int p2 = f.position(); // 1 const int b2 = f.toNextBoundary(); // 2 const int p3 = f.position(); // 2 const int b3 = f.toNextBoundary(); // 3 const int p4 = f.position(); // 3 const int b4 = f.toNextBoundary(); // -1 const int p5 = f.position(); // -1 const int b5 = f.toNextBoundary(); // -1 const int p6 = f.position(); // -1 const int b6 = f.toPreviousBoundary(); // -1 const int p7 = f.position(); // -1 const int b7 = f.toPreviousBoundary(); // -1 const int p8 = f.position(); // -1 const int b8 = f.toPreviousBoundary(); // -1 const int p9 = f.position(); // -1 // @DebugBreakpoint int dummy = 1; }