commit 7180cb6492d8629d5285eb07bb054b00fa250a1c Author: Jean-Marc Lasgouttes <lasgout...@lyx.org> Date: Tue Mar 4 12:05:05 2025 +0100
Replace DocIterator::cutOff with resize This is the same thing, expcept that the count parameter is off-by-one. Using '- 1' on size_type-valued variables can upset Coverity Scan at times. --- src/Cursor.cpp | 6 +++--- src/DocIterator.cpp | 13 ++++--------- src/DocIterator.h | 12 +++++------- src/insets/InsetText.cpp | 2 +- src/mathed/InsetMathMacro.cpp | 2 +- src/mathed/InsetMathMacroTemplate.cpp | 4 ++-- src/mathed/InsetMathScript.cpp | 2 +- src/mathed/MathData.cpp | 8 ++++---- 8 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/Cursor.cpp b/src/Cursor.cpp index d9c7b0a73c..f4f410c97e 100644 --- a/src/Cursor.cpp +++ b/src/Cursor.cpp @@ -197,11 +197,11 @@ void CursorData::setCursorSelectionTo(DocIterator dit) // otherwise the cursor is already normal if (i == anchor_.depth()) // dit is a proper extension of the anchor_ - dit.cutOff(i - 1); + dit.resize(i); else if (i + 1 < dit.depth()) { // one has dit[i] != anchor_[i] but either dit[i-1] == anchor_[i-1] // or i == 0. Remove excess. - dit.cutOff(i); + dit.resize(i + 1); if (dit[i] > anchor_[i]) // place dit after the inset it was in ++dit.pos(); @@ -2448,7 +2448,7 @@ bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur) // but stop if the inset claims the cursor to be invalid now for (size_type j = i; j < old.depth(); ++j) { Cursor inset_pos = old; - inset_pos.cutOff(j); + inset_pos.resize(j + 1); if (old[j].inset().notifyCursorLeaves(inset_pos, cur)) return true; } diff --git a/src/DocIterator.cpp b/src/DocIterator.cpp index b8f43a19bb..d155e40c32 100644 --- a/src/DocIterator.cpp +++ b/src/DocIterator.cpp @@ -690,16 +690,11 @@ int DocIterator::find(Inset const * inset) const } -void DocIterator::cutOff(int above, vector<CursorSlice> & cut) +void DocIterator::resize(size_type count, vector<CursorSlice> & cut) { - cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end()); - slices_.resize(above + 1); -} - - -void DocIterator::cutOff(int above) -{ - slices_.resize(above + 1); + LASSERT(count <= depth(), return); + cut = vector<CursorSlice>(slices_.begin() + count, slices_.end()); + slices_.resize(count); } diff --git a/src/DocIterator.h b/src/DocIterator.h index 543f10a60b..f5bdd97fbc 100644 --- a/src/DocIterator.h +++ b/src/DocIterator.h @@ -57,8 +57,10 @@ public: CursorSlice const & operator[](size_t i) const { return slices_[i]; } /// access slice at position \p i CursorSlice & operator[](size_t i) { return slices_[i]; } - /// chop a few slices from the iterator - void resize(size_t i) { slices_.resize(i); } + /// Resize cursor to \c count <= depth() slices. + void resize(size_t count) { slices_.resize(count); } + /// Resize cursor to \c count <= depth() slices and store cut off slices in \c cut. + void resize(size_type count, std::vector<CursorSlice> & cut); /// is the iterator valid? explicit operator bool() const { return !empty(); } @@ -258,10 +260,6 @@ public: int find(MathData const & cell) const; /// find index of CursorSlice with inset() == inset (or -1 of not found) int find(Inset const * inset) const; - /// cut off CursorSlices with index > above and store cut off slices in cut. - void cutOff(int above, std::vector<CursorSlice> & cut); - /// cut off CursorSlices with index > above - void cutOff(int above); /// push CursorSlices on top void append(std::vector<CursorSlice> const & x); /// push one CursorSlice on top and set its index and position @@ -280,7 +278,7 @@ private: explicit DocIterator(Buffer * buf, Inset * inset) : inset_(inset), buffer_(buf) {} - + /** * Normally, when the cursor is at position i, it is painted *before* * the character at position i. However, what if we want the cursor diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp index 907530b22b..875430c957 100644 --- a/src/insets/InsetText.cpp +++ b/src/insets/InsetText.cpp @@ -1297,7 +1297,7 @@ bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur) // we can try to continue here. returning true means // the cursor is "now" invalid. which it was. LASSERT(scriptSlice != -1, return true); - insetCur.cutOff(scriptSlice); + insetCur.resize(scriptSlice + 1); LASSERT(&insetCur.inset() == this, return true); // update the old paragraph's words diff --git a/src/mathed/InsetMathMacro.cpp b/src/mathed/InsetMathMacro.cpp index 5513542365..27fadaf35d 100644 --- a/src/mathed/InsetMathMacro.cpp +++ b/src/mathed/InsetMathMacro.cpp @@ -1116,7 +1116,7 @@ bool InsetMathMacro::notifyCursorLeaves(Cursor const & old, Cursor & cur) // returning true means the cursor is "now" invalid, // which it was. LASSERT(macroSlice != -1, return true); - inset_cursor.cutOff(macroSlice); + inset_cursor.resize(macroSlice + 1); inset_cursor.recordUndoInset(); inset_cursor.pop(); inset_cursor.cell().erase(inset_cursor.pos()); diff --git a/src/mathed/InsetMathMacroTemplate.cpp b/src/mathed/InsetMathMacroTemplate.cpp index 94760d4060..daf3dd40c9 100644 --- a/src/mathed/InsetMathMacroTemplate.cpp +++ b/src/mathed/InsetMathMacroTemplate.cpp @@ -930,7 +930,7 @@ void InsetMathMacroTemplate::removeParameter(Cursor & cur, int macroSlice = cur.find(this); if (macroSlice != -1) { if (cur[macroSlice].idx() == optIdx(pos)) { - cur.cutOff(macroSlice); + cur.resize(macroSlice + 1); cur[macroSlice].idx() = 1; cur[macroSlice].pos() = 0; } else if (cur[macroSlice].idx() > optIdx(pos)) @@ -985,7 +985,7 @@ void InsetMathMacroTemplate::makeNonOptional(Cursor & cur, if (cur[macroSlice].idx() > optIdx(optionals_)) --cur[macroSlice].idx(); else if (cur[macroSlice].idx() == optIdx(optionals_)) { - cur.cutOff(macroSlice); + cur.resize(macroSlice + 1); cur[macroSlice].idx() = optIdx(optionals_); cur[macroSlice].pos() = 0; } diff --git a/src/mathed/InsetMathScript.cpp b/src/mathed/InsetMathScript.cpp index 6ccaaaa840..319ae0f40d 100644 --- a/src/mathed/InsetMathScript.cpp +++ b/src/mathed/InsetMathScript.cpp @@ -718,7 +718,7 @@ bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur) Cursor insetCur = old; int scriptSlice = insetCur.find(this); LASSERT(scriptSlice != -1, /**/); - insetCur.cutOff(scriptSlice); + insetCur.resize(scriptSlice + 1); insetCur.recordUndoInset(); // Let the script inset commit suicide. This is diff --git a/src/mathed/MathData.cpp b/src/mathed/MathData.cpp index 6d618adcf6..566eeba89e 100644 --- a/src/mathed/MathData.cpp +++ b/src/mathed/MathData.cpp @@ -521,7 +521,7 @@ void MathData::updateMacros(Cursor * cur, MacroContext const & mc, if (cur) { int macroSlice = cur->find(macroInset); if (macroSlice != -1) - cur->cutOff(macroSlice - 1); + cur->resize(macroSlice); } } @@ -588,7 +588,7 @@ void MathData::detachMacroParameters(DocIterator * cur, const size_type macroPos if (curMacroSlice != -1) { curMacroPos = (*cur)[curMacroSlice].pos(); curMacroIdx = (*cur)[curMacroSlice].idx(); - cur->cutOff(curMacroSlice, argSlices); + cur->resize(curMacroSlice + 1, argSlices); cur->pop_back(); } @@ -837,7 +837,7 @@ void MathData::collectOptionalParameters(Cursor * cur, && thisPos >= int(pos) && thisPos <= int(right)) { int paramPos = max(0, thisPos - int(pos) - 1); vector<CursorSlice> x; - cur->cutOff(thisSlice, x); + cur->resize(thisSlice + 1, x); (*cur)[thisSlice].pos() = macroPos; if (brace) { paramPos = x[0].pos(); @@ -877,7 +877,7 @@ void MathData::collectParameters(Cursor * cur, // (see caller), but making this explicit pleases coverity. if (cur && thisSlice != -1 && thisPos == int(pos)) - cur->cutOff(thisSlice, argSlices); + cur->resize(thisSlice + 1, argSlices); // which kind of parameter is it? In {}? With index x^n? InsetMathBrace const * brace = cell->asBraceInset(); -- lyx-cvs mailing list lyx-cvs@lists.lyx.org https://lists.lyx.org/mailman/listinfo/lyx-cvs