commit 3c203c2f78e786b50d8324da771487e0688b7d0c Author: Jean-Marc Lasgouttes <lasgout...@lyx.org> Date: Tue Mar 4 14:52:24 2025 +0100
Try to improve signedness of variables Introduce lyx::npos, which is the same as basic_string::npos. Modify DocIterator to return size_type, and lyx::npos when not found. Change type of variables accordingly. --- src/DocIterator.cpp | 8 ++--- src/DocIterator.h | 8 ++--- src/Text.cpp | 4 +-- src/insets/InsetText.cpp | 4 +-- src/mathed/InsetMathMacro.cpp | 4 +-- src/mathed/InsetMathMacroTemplate.cpp | 16 +++++----- src/mathed/InsetMathScript.cpp | 4 +-- src/mathed/MathData.cpp | 55 ++++++++++++++++++----------------- src/mathed/MathData.h | 20 ++++++------- src/support/types.h | 3 ++ 10 files changed, 65 insertions(+), 61 deletions(-) diff --git a/src/DocIterator.cpp b/src/DocIterator.cpp index d155e40c32..1e3d5c56b1 100644 --- a/src/DocIterator.cpp +++ b/src/DocIterator.cpp @@ -670,23 +670,23 @@ void DocIterator::leaveInset(Inset const & inset) } -int DocIterator::find(MathData const & cell) const +size_type DocIterator::find(MathData const & cell) const { for (size_t l = 0; l != slices_.size(); ++l) { if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell) return l; } - return -1; + return lyx::npos; } -int DocIterator::find(Inset const * inset) const +size_type DocIterator::find(Inset const * inset) const { for (size_t l = 0; l != slices_.size(); ++l) { if (&slices_[l].inset() == inset) return l; } - return -1; + return lyx::npos; } diff --git a/src/DocIterator.h b/src/DocIterator.h index f5bdd97fbc..06be32fba1 100644 --- a/src/DocIterator.h +++ b/src/DocIterator.h @@ -256,10 +256,10 @@ public: /// make sure we are outside of given inset void leaveInset(Inset const & inset); - /// find index of CursorSlice with &cell() == &cell (or -1 if not found) - int find(MathData const & cell) const; - /// find index of CursorSlice with inset() == inset (or -1 of not found) - int find(Inset const * inset) const; + /// find index of CursorSlice with &cell() == &cell (or lyx::npos if not found) + size_type find(MathData const & cell) const; + /// find index of CursorSlice with inset() == inset (or lyx::npos of not found) + size_type find(Inset const * inset) const; /// push CursorSlices on top void append(std::vector<CursorSlice> const & x); /// push one CursorSlice on top and set its index and position diff --git a/src/Text.cpp b/src/Text.cpp index 7f4c10b7df..ecc994db64 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -3010,7 +3010,7 @@ bool Text::checkAndActivateInset(Cursor & cur, bool front) Inset * inset = front ? cur.nextInset() : cur.prevInset(); if (!inset || !inset->editable()) return false; - if (cur.selection() && cur.realAnchor().find(inset) == -1) + if (cur.selection() && cur.realAnchor().find(inset) == lyx::npos) return false; /* * Apparently, when entering an inset we are expected to be positioned @@ -3038,7 +3038,7 @@ bool Text::checkAndActivateInsetVisual(Cursor & cur, bool movingForward, bool mo Inset * inset = par.isInset(cur.pos()) ? par.getInset(cur.pos()) : nullptr; if (!inset || !inset->editable()) return false; - if (cur.selection() && cur.realAnchor().find(inset) == -1) + if (cur.selection() && cur.realAnchor().find(inset) == lyx::npos) return false; inset->edit(cur, movingForward, movingLeft ? Inset::ENTRY_DIRECTION_RIGHT : Inset::ENTRY_DIRECTION_LEFT); diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp index 875430c957..c44bca6573 100644 --- a/src/insets/InsetText.cpp +++ b/src/insets/InsetText.cpp @@ -1293,10 +1293,10 @@ bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur) // find text inset in old cursor Cursor insetCur = old; - int scriptSlice = insetCur.find(this); + size_type scriptSlice = insetCur.find(this); // we can try to continue here. returning true means // the cursor is "now" invalid. which it was. - LASSERT(scriptSlice != -1, return true); + LASSERT(scriptSlice != lyx::npos, return true); insetCur.resize(scriptSlice + 1); LASSERT(&insetCur.inset() == this, return true); diff --git a/src/mathed/InsetMathMacro.cpp b/src/mathed/InsetMathMacro.cpp index 27fadaf35d..c771207ca0 100644 --- a/src/mathed/InsetMathMacro.cpp +++ b/src/mathed/InsetMathMacro.cpp @@ -1112,10 +1112,10 @@ bool InsetMathMacro::notifyCursorLeaves(Cursor const & old, Cursor & cur) if (unfolded_name != d->name_) { // The macro name was changed Cursor inset_cursor = old; - int macroSlice = inset_cursor.find(this); + size_type macroSlice = inset_cursor.find(this); // returning true means the cursor is "now" invalid, // which it was. - LASSERT(macroSlice != -1, return true); + LASSERT(macroSlice != lyx::npos, return true); inset_cursor.resize(macroSlice + 1); inset_cursor.recordUndoInset(); inset_cursor.pop(); diff --git a/src/mathed/InsetMathMacroTemplate.cpp b/src/mathed/InsetMathMacroTemplate.cpp index daf3dd40c9..f16c825afd 100644 --- a/src/mathed/InsetMathMacroTemplate.cpp +++ b/src/mathed/InsetMathMacroTemplate.cpp @@ -657,8 +657,8 @@ void InsetMathMacroTemplate::removeArguments(Cursor & cur, InsetMathMacroArgument * arg = static_cast<InsetMathMacroArgument*>(it.nextInset()); int n = arg->number() - 1; if (from <= n && n <= to) { - int cellSlice = cur.find(it.cell()); - if (cellSlice != -1 && cur[cellSlice].pos() > it.pos()) + size_type cellSlice = cur.find(it.cell()); + if (cellSlice != lyx::npos && cur[cellSlice].pos() > it.pos()) --cur[cellSlice].pos(); it.cell().erase(it.pos()); @@ -927,8 +927,8 @@ void InsetMathMacroTemplate::removeParameter(Cursor & cur, cells_.erase(cells_.begin() + optIdx(pos)); // fix cursor - int macroSlice = cur.find(this); - if (macroSlice != -1) { + size_type macroSlice = cur.find(this); + if (macroSlice != lyx::npos) { if (cur[macroSlice].idx() == optIdx(pos)) { cur.resize(macroSlice + 1); cur[macroSlice].idx() = 1; @@ -956,8 +956,8 @@ void InsetMathMacroTemplate::makeOptional(Cursor & cur, ++optionals_; cells_.insert(cells_.begin() + optIdx(optionals_ - 1), optionalValues_[optionals_ - 1]); // fix cursor - int macroSlice = cur.find(this); - if (macroSlice != -1 && cur[macroSlice].idx() >= optIdx(optionals_ - 1)) + size_type macroSlice = cur.find(this); + if (macroSlice != lyx::npos && cur[macroSlice].idx() >= optIdx(optionals_ - 1)) ++cur[macroSlice].idx(); // fix macro instances @@ -980,8 +980,8 @@ void InsetMathMacroTemplate::makeNonOptional(Cursor & cur, cells_.erase(cells_.begin() + optIdx(optionals_)); // fix cursor - int macroSlice = cur.find(this); - if (macroSlice != -1) { + size_type macroSlice = cur.find(this); + if (macroSlice != lyx::npos) { if (cur[macroSlice].idx() > optIdx(optionals_)) --cur[macroSlice].idx(); else if (cur[macroSlice].idx() == optIdx(optionals_)) { diff --git a/src/mathed/InsetMathScript.cpp b/src/mathed/InsetMathScript.cpp index 319ae0f40d..167462c711 100644 --- a/src/mathed/InsetMathScript.cpp +++ b/src/mathed/InsetMathScript.cpp @@ -716,8 +716,8 @@ bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur) // it does not necessarily point to us anymore. But we // should be on top of the cursor old. Cursor insetCur = old; - int scriptSlice = insetCur.find(this); - LASSERT(scriptSlice != -1, /**/); + size_type scriptSlice = insetCur.find(this); + LASSERT(scriptSlice != lyx::npos, /**/); insetCur.resize(scriptSlice + 1); insetCur.recordUndoInset(); diff --git a/src/mathed/MathData.cpp b/src/mathed/MathData.cpp index 566eeba89e..34e4a0be1c 100644 --- a/src/mathed/MathData.cpp +++ b/src/mathed/MathData.cpp @@ -519,8 +519,8 @@ void MathData::updateMacros(Cursor * cur, MacroContext const & mc, && oldDisplayMode == InsetMathMacro::DISPLAY_UNFOLDED) { // put cursor in front of macro if (cur) { - int macroSlice = cur->find(macroInset); - if (macroSlice != -1) + size_type macroSlice = cur->find(macroInset); + if (macroSlice != lyx::npos) cur->resize(macroSlice); } } @@ -579,13 +579,13 @@ void MathData::detachMacroParameters(DocIterator * cur, const size_type macroPos macroInset->detachArguments(detachedArgs, false); // find cursor slice - int curMacroSlice = -1; + size_type curMacroSlice = lyx::npos; if (cur) curMacroSlice = cur->find(macroInset); - idx_type curMacroIdx = -1; - pos_type curMacroPos = -1; + idx_type curMacroIdx = lyx::npos; + pos_type curMacroPos = lyx::npos; vector<CursorSlice> argSlices; - if (curMacroSlice != -1) { + if (curMacroSlice != lyx::npos) { curMacroPos = (*cur)[curMacroSlice].pos(); curMacroIdx = (*cur)[curMacroSlice].idx(); cur->resize(curMacroSlice + 1, argSlices); @@ -643,7 +643,7 @@ void MathData::detachMacroParameters(DocIterator * cur, const size_type macroPos p += optarg.size(); // cursor in macro? - if (curMacroSlice == -1) + if (curMacroSlice == lyx::npos) continue; // cursor in optional argument of macro? @@ -670,7 +670,7 @@ void MathData::detachMacroParameters(DocIterator * cur, const size_type macroPos insert(p, MathAtom(new InsetMathBrace(buffer_, arg))); // cursor in macro? - if (curMacroSlice == -1) + if (curMacroSlice == lyx::npos) continue; // cursor in j-th argument of macro? @@ -693,7 +693,7 @@ void MathData::detachMacroParameters(DocIterator * cur, const size_type macroPos void MathData::attachMacroParameters(Cursor * cur, - const size_type macroPos, const size_type macroNumArgs, + const pos_type macroPos, const size_type macroNumArgs, const int macroOptionals, const bool fromInitToNormalMode, const bool interactiveInit, const size_t appetite) { @@ -706,11 +706,11 @@ void MathData::attachMacroParameters(Cursor * cur, MathAtom scriptToPutAround; // find cursor slice again of this MathData - int thisSlice = -1; + size_type thisSlice = lyx::npos; if (cur) thisSlice = cur->find(*this); - int thisPos = -1; - if (thisSlice != -1) + pos_type thisPos = lyx::npos; + if (thisSlice != lyx::npos) thisPos = (*cur)[thisSlice].pos(); // find arguments behind the macro @@ -740,7 +740,7 @@ void MathData::attachMacroParameters(Cursor * cur, operator[](macroPos) = scriptToPutAround; // go into the script inset nucleus - if (cur && thisPos == int(macroPos)) + if (cur && thisPos == macroPos) cur->append(0, 0); // get pointer to "deep" copied macro inset @@ -752,20 +752,22 @@ void MathData::attachMacroParameters(Cursor * cur, erase(macroPos + 1, p); // cursor outside this MathData? - if (thisSlice == -1) + if (thisSlice == lyx::npos) return; // fix cursor if right of p - if (thisPos >= int(p)) + if (thisPos >= p) (*cur)[thisSlice].pos() -= p - (macroPos + 1); // was the macro inset just inserted interactively and was now folded // and the cursor is just behind? - if ((*cur)[thisSlice].pos() == int(macroPos + 1) + // FIXME: MathData::pos_type is unsigned, whereas lyx::pos_type is signed. + // This is not good. + if ((*cur)[thisSlice].pos() == lyx::pos_type(macroPos + 1) && interactiveInit && fromInitToNormalMode && macroInset->arity() > 0 - && thisSlice + 1 == int(cur->depth())) { + && thisSlice + 1 == cur->depth()) { // then enter it if the cursor was just behind (*cur)[thisSlice].pos() = macroPos; cur->push_back(CursorSlice(*macroInset)); @@ -777,7 +779,7 @@ void MathData::attachMacroParameters(Cursor * cur, void MathData::collectOptionalParameters(Cursor * cur, const size_type numOptionalParams, vector<MathData> & params, size_t & pos, MathAtom & scriptToPutAround, - const pos_type macroPos, const int thisPos, const int thisSlice) + const pos_type macroPos, const pos_type thisPos, const size_type thisSlice) { Buffer * buf = cur ? cur->buffer() : 0; // insert optional arguments? @@ -833,9 +835,9 @@ void MathData::collectOptionalParameters(Cursor * cur, // place cursor in optional argument of macro // Note: The two expressions on the first line are equivalent // (see caller), but making this explicit pleases coverity. - if (cur && thisSlice != -1 - && thisPos >= int(pos) && thisPos <= int(right)) { - int paramPos = max(0, thisPos - int(pos) - 1); + if (cur && thisSlice != lyx::npos + && thisPos >= pos && thisPos <= right) { + int paramPos = max(0, int(thisPos - pos - 1)); vector<CursorSlice> x; cur->resize(thisSlice + 1, x); (*cur)[thisSlice].pos() = macroPos; @@ -858,7 +860,7 @@ void MathData::collectOptionalParameters(Cursor * cur, void MathData::collectParameters(Cursor * cur, const size_type numParams, vector<MathData> & params, size_t & pos, MathAtom & scriptToPutAround, - const pos_type macroPos, const int thisPos, const int thisSlice, + const pos_type macroPos, const pos_type thisPos, const size_type thisSlice, const size_t appetite) { size_t startSize = params.size(); @@ -875,8 +877,7 @@ void MathData::collectParameters(Cursor * cur, int argPos = 0; // Note: The two expressions on the first line are equivalent // (see caller), but making this explicit pleases coverity. - if (cur && thisSlice != -1 - && thisPos == int(pos)) + if (cur && thisSlice != lyx::npos && thisPos == pos) cur->resize(thisSlice + 1, argSlices); // which kind of parameter is it? In {}? With index x^n? @@ -886,7 +887,7 @@ void MathData::collectParameters(Cursor * cur, params.push_back(brace->cell(0)); // cursor inside of the brace or just in front of? - if (thisPos == int(pos) && !argSlices.empty()) { + if (thisPos == pos && !argSlices.empty()) { argPos = argSlices[0].pos(); argSlices.erase(argSlices.begin()); } @@ -904,7 +905,7 @@ void MathData::collectParameters(Cursor * cur, scriptToPutAround = cell; // this should only happen after loading, so make cursor handling simple - if (thisPos >= int(macroPos) && thisPos <= int(macroPos + numParams)) { + if (thisPos >= macroPos && thisPos <= macroPos + numParams) { argSlices.clear(); if (cur) cur->append(0, 0); @@ -920,7 +921,7 @@ void MathData::collectParameters(Cursor * cur, // Note: The first two expressions on the first line are // equivalent (see caller), but making this explicit pleases // coverity. - if (cur && thisSlice != -1 && thisPos == int(pos)) { + if (cur && thisSlice != lyx::npos && thisPos == pos) { cur->append(params.size() - 1, argPos); cur->append(argSlices); (*cur)[thisSlice].pos() = macroPos; diff --git a/src/mathed/MathData.h b/src/mathed/MathData.h index ca0f08c8cf..6d85000658 100644 --- a/src/mathed/MathData.h +++ b/src/mathed/MathData.h @@ -205,23 +205,23 @@ private: bool find1(MathData const & ar, size_type pos) const; /// - void detachMacroParameters(DocIterator * dit, const size_type macroPos); + void detachMacroParameters(DocIterator * dit, size_type macroPos); /// - void attachMacroParameters(Cursor * cur, const size_type macroPos, - const size_type macroNumArgs, const int macroOptionals, - const bool fromInitToNormalMode, const bool interactiveInit, - const size_t appetite); + void attachMacroParameters(Cursor * cur, size_type macroPos, + size_type macroNumArgs, int macroOptionals, + bool fromInitToNormalMode, bool interactiveInit, + size_t appetite); /// void collectOptionalParameters(Cursor * cur, - const size_type numOptionalParams, std::vector<MathData> & params, + size_type numOptionalParams, std::vector<MathData> & params, size_t & pos, MathAtom & scriptToPutAround, - const pos_type macroPos, const int thisPos, const int thisSlice); + pos_type macroPos, pos_type thisPos, size_type thisSlice); /// void collectParameters(Cursor * cur, - const size_type numParams, std::vector<MathData> & params, + size_type numParams, std::vector<MathData> & params, size_t & pos, MathAtom & scriptToPutAround, - const pos_type macroPos, const int thisPos, const int thisSlice, - const size_t appetite); + pos_type macroPos, pos_type thisPos, size_type thisSlice, + size_t appetite); }; /// diff --git a/src/support/types.h b/src/support/types.h index 99f53c8f1b..b3f1efaf8b 100644 --- a/src/support/types.h +++ b/src/support/types.h @@ -81,6 +81,9 @@ namespace lyx { #endif + /// a 'not found' value + inline const size_type npos = static_cast<size_type>(-1); + /// enum word_location { /// the word around the cursor, only if the cursor is -- lyx-cvs mailing list lyx-cvs@lists.lyx.org https://lists.lyx.org/mailman/listinfo/lyx-cvs