pit_type as unsigned

Richard Kimberly Heck rikiheck at lyx.org
Sun May 3 04:24:29 UTC 2020


I've done some more work on this, and it seems to be working all right
now. See this branch:

https://git.lyx.org/?p=developers/rgheck/lyx.git;a=shortlog;h=refs/heads/cleanup/pit_type

I'm also attaching a diff.

There are still some conversion warnings, but I do not think they are
serious. (I actually see a lot of warnings about changing signedness
when ints are used as indices into vectors....)

Riki


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lyx.org/pipermail/lyx-devel/attachments/20200503/4f578841/attachment-0001.html>
-------------- next part --------------
diff --git a/src/BufferView.cpp b/src/BufferView.cpp
index 621b58ef26..5e00f99d86 100644
--- a/src/BufferView.cpp
+++ b/src/BufferView.cpp
@@ -607,7 +607,7 @@ void BufferView::updateScrollbar()
 	int top_pos = first.second->position() - first.second->ascent();
 	int bottom_pos = last.second->position() + last.second->descent();
 	bool first_visible = first.first == 0 && top_pos >= 0;
-	bool last_visible = last.first + 1 == int(parsize) && bottom_pos <= height_;
+	bool last_visible = last.first + 1 == parsize && bottom_pos <= height_;
 	if (first_visible && last_visible) {
 		d->scrollbarParameters_.min = 0;
 		d->scrollbarParameters_.max = 0;
@@ -704,7 +704,7 @@ void BufferView::scrollDocView(int const value, bool update)
 	// find paragraph at target position
 	int par_pos = d->scrollbarParameters_.min;
 	pit_type i = 0;
-	for (; i != int(d->par_height_.size()); ++i) {
+	for (; i != d->par_height_.size(); ++i) {
 		par_pos += d->par_height_[i];
 		if (par_pos >= value)
 			break;
@@ -862,7 +862,7 @@ bool BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos,
 	// restoration is inaccurate. If a bookmark was within an inset,
 	// it will be restored to the left of the outmost inset that contains
 	// the bookmark.
-	if (bottom_pit < int(buffer_.paragraphs().size())) {
+	if (bottom_pit < buffer_.paragraphs().size()) {
 		dit = doc_iterator_begin(&buffer_);
 
 		dit.pit() = bottom_pit;
@@ -946,8 +946,8 @@ bool BufferView::scrollToCursor(DocIterator const & dit, bool const recenter)
 	CursorSlice const & bot = dit.bottom();
 	TextMetrics & tm = d->text_metrics_[bot.text()];
 
-	pos_type const max_pit = pos_type(bot.text()->paragraphs().size() - 1);
-	int bot_pit = bot.pit();
+	pit_type const max_pit = bot.text()->paragraphs().size() - 1;
+	pit_type bot_pit = bot.pit();
 	if (bot_pit > max_pit) {
 		// FIXME: Why does this happen?
 		LYXERR0("bottom pit is greater that max pit: "
@@ -2454,7 +2454,7 @@ int BufferView::scrollDown(int offset)
 		int bottom_pos = last.second->position() + last.second->descent();
 		if (lyxrc.scroll_below_document)
 			bottom_pos += height_ - minVisiblePart();
-		if (last.first + 1 == int(text->paragraphs().size())) {
+		if (last.first + 1 == text->paragraphs().size()) {
 			if (bottom_pos <= height_)
 				return 0;
 			offset = min(offset, bottom_pos - height_);
@@ -2812,7 +2812,7 @@ void BufferView::updateMetrics(Update::flags & update_flags)
 		return;
 
 	Text & buftext = buffer_.text();
-	pit_type const npit = int(buftext.paragraphs().size());
+	pit_type const npit = buftext.paragraphs().size();
 
 	// Clear out the position cache in case of full screen redraw,
 	d->coord_cache_.clear();
@@ -2858,16 +2858,21 @@ void BufferView::updateMetrics(Update::flags & update_flags)
 
 	// Redo paragraphs above anchor if necessary.
 	int y1 = d->anchor_ypos_ - anchor_pm.ascent();
-	// We are now just above the anchor paragraph.
-	pit_type pit1 = d->anchor_pit_ - 1;
-	for (; pit1 >= 0 && y1 >= 0; --pit1) {
-		tm.redoParagraph(pit1);
-		ParagraphMetrics & pm = tm.par_metrics_[pit1];
-		y1 -= pm.descent();
-		// Save the paragraph position in the cache.
-		pm.setPosition(y1);
-		tm.updatePosCache(pit1);
-		y1 -= pm.ascent();
+	pit_type pit1 = d->anchor_pit_ ? d->anchor_pit_- 1 : lyxnpos;
+	if (pit1 != lyxnpos) {
+		// We are now just above the anchor paragraph.
+		while (y1 >= 0) {
+			tm.redoParagraph(pit1);
+			ParagraphMetrics & pm = tm.par_metrics_[pit1];
+			y1 -= pm.descent();
+			// Save the paragraph position in the cache.
+			pm.setPosition(y1);
+			tm.updatePosCache(pit1);
+			y1 -= pm.ascent();
+			if (pit1 == 0)
+				break;
+			--pit1;
+		}
 	}
 
 	// Redo paragraphs below the anchor if necessary.
diff --git a/src/Compare.cpp b/src/Compare.cpp
index 60a0b65bb1..931fe76faf 100644
--- a/src/Compare.cpp
+++ b/src/Compare.cpp
@@ -881,10 +881,10 @@ void Compare::Impl::writeToDestBuffer(DocRange const & range,
 
 void Compare::Impl::writeToDestBuffer(ParagraphList const & pars) const
 {
-	pit_type const pit = dest_pars_->size() - 1;
 	dest_pars_->insert(dest_pars_->end(), pars.begin(), pars.end());
-	if (pit >= 0)
-		mergeParagraph(dest_buf_->params(), *dest_pars_, pit);
+	pit_type pit = dest_pars_->size();
+	if (pit > 0)
+		mergeParagraph(dest_buf_->params(), *dest_pars_, pit - 1);
 }
 
 
diff --git a/src/Cursor.cpp b/src/Cursor.cpp
index 41cf26ec17..8300648afa 100644
--- a/src/Cursor.cpp
+++ b/src/Cursor.cpp
@@ -1404,7 +1404,7 @@ bool Cursor::atFirstOrLastRow(bool up)
 	TextMetrics const & tm = bv_->textMetrics(text());
 	ParagraphMetrics const & pm = tm.parMetrics(pit());
 
-	int row;
+	size_t row;
 	if (pos() && boundary())
 		row = pm.pos2row(pos() - 1);
 	else
@@ -1414,8 +1414,8 @@ bool Cursor::atFirstOrLastRow(bool up)
 		if (pit() == 0 && row == 0)
 			return true;
 	} else {
-		if (pit() + 1 >= int(text()->paragraphs().size()) &&
-			row + 1 >= int(pm.rows().size()))
+		if (pit() + 1 >= text()->paragraphs().size() &&
+			row + 1 >= pm.rows().size())
 			return true;
 	}
 	return false;
@@ -2218,7 +2218,7 @@ bool Cursor::upDownInText(bool up, bool & updateNeeded)
 		} else {
 			if (row + 1 < int(pm.rows().size())) {
 				++next_row;
-			} else if (pit() + 1 < int(text()->paragraphs().size())) {
+			} else if (pit() + 1 < text()->paragraphs().size()) {
 				++pit();
 				TextMetrics & tm2 = bv_->textMetrics(text());
 				if (!tm2.contains(pit()))
diff --git a/src/CursorSlice.cpp b/src/CursorSlice.cpp
index 62fd92f362..254a386484 100644
--- a/src/CursorSlice.cpp
+++ b/src/CursorSlice.cpp
@@ -94,7 +94,7 @@ CursorSlice::col_type CursorSlice::col() const
 
 void CursorSlice::setPitPos(pit_type pit, pos_type pos)
 {
-	LASSERT(pit != int(text()->paragraphs().size()), return);
+	LASSERT(pit != text()->paragraphs().size(), return);
 	pit_ = pit;
 	pos_ = pos;
 
diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp
index a6d61e6000..ac53c7710f 100644
--- a/src/Paragraph.cpp
+++ b/src/Paragraph.cpp
@@ -2726,7 +2726,7 @@ void Paragraph::latex(BufferParams const & bparams,
 						textinset->text().paragraphs();
 					pit_type const pit = pars.size() - 1;
 					Font const lastfont =
-						pit < 0 || pars[pit].empty()
+						pars[pit].empty()
 						? pars[pit].getLayoutFont(
 								bparams,
 								outerfont)
diff --git a/src/RowPainter.cpp b/src/RowPainter.cpp
index 6e4cf31ac1..a392c027fa 100644
--- a/src/RowPainter.cpp
+++ b/src/RowPainter.cpp
@@ -60,8 +60,7 @@ RowPainter::RowPainter(PainterInfo & pi,
 	//lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
 	//row_.dump();
 
-	LBUFERR(row.pit() >= 0);
-	LBUFERR(row.pit() < int(text.paragraphs().size()));
+	LBUFERR(row.pit() < text.paragraphs().size());
 }
 
 
diff --git a/src/Text.cpp b/src/Text.cpp
index 7c00a6aabe..6fcc690862 100644
--- a/src/Text.cpp
+++ b/src/Text.cpp
@@ -1426,14 +1426,14 @@ void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
 
 		// skip if this is not the last paragraph of the document
 		// note: the user should be able to accept/reject the par break of the last par!
-		if (pit == endPit && pit + 1 != int(pars_.size()))
+		if (pit == endPit && pit + 1 != pars_.size())
 			break; // last iteration anway
 
 		if (op == ACCEPT) {
 			if (pars_[pit].isInserted(pos)) {
 				pars_[pit].setChange(pos, Change(Change::UNCHANGED));
 			} else if (pars_[pit].isDeleted(pos)) {
-				if (pit + 1 == int(pars_.size())) {
+				if (pit + 1 == pit_type(pars_.size())) {
 					// we cannot remove a par break at the end of the last paragraph;
 					// instead, we mark it unchanged
 					pars_[pit].setChange(pos, Change(Change::UNCHANGED));
@@ -1447,7 +1447,7 @@ void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
 			if (pars_[pit].isDeleted(pos)) {
 				pars_[pit].setChange(pos, Change(Change::UNCHANGED));
 			} else if (pars_[pit].isInserted(pos)) {
-				if (pit + 1 == int(pars_.size())) {
+				if (pit + 1 == pit_type(pars_.size())) {
 					// we mark the par break at the end of the last paragraph unchanged
 					pars_[pit].setChange(pos, Change(Change::UNCHANGED));
 				} else {
diff --git a/src/Text2.cpp b/src/Text2.cpp
index 6adbb51553..b966aef115 100644
--- a/src/Text2.cpp
+++ b/src/Text2.cpp
@@ -943,7 +943,7 @@ bool Text::deleteEmptyParagraphMechanism(Cursor & cur,
 
 void Text::deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges)
 {
-	LASSERT(first >= 0 && first <= last && last < (int) pars_.size(), return);
+	LASSERT(first >= 0 && first <= last && last < pars_.size(), return);
 
 	for (pit_type pit = first; pit <= last; ++pit) {
 		Paragraph & par = pars_[pit];
diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp
index c92af02184..2cb7cbacbd 100644
--- a/src/TextMetrics.cpp
+++ b/src/TextMetrics.cpp
@@ -189,7 +189,7 @@ void TextMetrics::newParMetricsDown()
 {
 	pair<pit_type, ParagraphMetrics> const & last = *par_metrics_.rbegin();
 	pit_type const pit = last.first + 1;
-	if (pit == int(text_->paragraphs().size()))
+	if (pit == text_->paragraphs().size())
 		return;
 
 	// do it and update its position.
@@ -979,7 +979,7 @@ bool TextMetrics::breakRow(Row & row, int const right_margin) const
 		// - A newline inset
 		// - Before a display inset
 		// - After a display inset
-		Inset const * inset = 0;
+		Inset const * inset = nullptr;
 		if (par.isNewline(i) || par.isEnvSeparator(i)
 		    || (i + 1 < end && (inset = par.getInset(i + 1))
 		        && inset->display())
@@ -1275,12 +1275,12 @@ pos_type TextMetrics::x2pos(pit_type pit, int row, int x) const
 // y is screen coordinate
 pit_type TextMetrics::getPitNearY(int y)
 {
-	LASSERT(!text_->paragraphs().empty(), return -1);
-	LASSERT(!par_metrics_.empty(), return -1);
+	LASSERT(!text_->paragraphs().empty(), return lyxnpos);
+	LASSERT(!par_metrics_.empty(), return lyxnpos);
 	LYXERR(Debug::DEBUG, "y: " << y << " cache size: " << par_metrics_.size());
 
 	// look for highest numbered paragraph with y coordinate less than given y
-	pit_type pit = -1;
+	pit_type pit = lyxnpos;
 	int yy = -1;
 	ParMetricsCache::const_iterator it = par_metrics_.begin();
 	ParMetricsCache::const_iterator et = par_metrics_.end();
@@ -1289,7 +1289,7 @@ pit_type TextMetrics::getPitNearY(int y)
 
 	ParagraphMetrics const & pm = it->second;
 
-	if (y < it->second.position() - int(pm.ascent())) {
+	if (y < pm.position() - pm.ascent()) {
 		// We are looking for a position that is before the first paragraph in
 		// the cache (which is in priciple off-screen, that is before the
 		// visible part.
@@ -1309,7 +1309,7 @@ pit_type TextMetrics::getPitNearY(int y)
 		// the cache (which is in priciple off-screen), that is before the
 		// visible part.
 		pit = last->first + 1;
-		if (pit == int(text_->paragraphs().size()))
+		if (pit == text_->paragraphs().size())
 			//  We are already at the last paragraph in the inset.
 			return last->first;
 		// OK, this is the paragraph we are looking for.
@@ -1323,7 +1323,8 @@ pit_type TextMetrics::getPitNearY(int y)
 
 		ParagraphMetrics const & pm2 = par_metrics_[it->first];
 
-		if (it->first >= pit && int(it->second.position()) - int(pm2.ascent()) <= y) {
+		if ((it->first >= pit || pit == lyxnpos) &&
+				int(it->second.position()) - int(pm2.ascent()) <= y) {
 			pit = it->first;
 			yy = it->second.position();
 		}
@@ -1366,7 +1367,7 @@ Row const & TextMetrics::getPitAndRowNearY(int & y, pit_type & pit,
 			if (rit != rlast) {
 				y = yy + rit->height();
 				++rit;
-			} else if (pit < int(text_->paragraphs().size()) - 1) {
+			} else if (pit < text_->paragraphs().size() - 1) {
 				++pit;
 				newParMetricsDown();
 				ParagraphMetrics const & pm2 = par_metrics_[pit];
@@ -1389,7 +1390,7 @@ Inset * TextMetrics::editXY(Cursor & cur, int x, int y,
 		cur.bv().coordCache().dump();
 	}
 	pit_type pit = getPitNearY(y);
-	LASSERT(pit != -1, return 0);
+	LASSERT(pit != lyxnpos, return nullptr);
 	Row const & row = getPitAndRowNearY(y, pit, assert_in_view, up);
 	cur.pit() = pit;
 
@@ -1403,7 +1404,7 @@ Inset * TextMetrics::editXY(Cursor & cur, int x, int y,
 		cur.boundary(bound);
 		cur.setCurrentFont();
 		cur.setTargetX(x);
-		return 0;
+		return nullptr;
 	}
 
 	Inset * inset = e->inset;
@@ -1438,7 +1439,7 @@ void TextMetrics::setCursorFromCoordinates(Cursor & cur, int const x, int const
 {
 	LASSERT(text_ == cur.text(), return);
 	pit_type const pit = getPitNearY(y);
-	LASSERT(pit != -1, return);
+	LASSERT(pit != lyxnpos, return);
 
 	ParagraphMetrics const & pm = par_metrics_[pit];
 
@@ -1489,7 +1490,7 @@ InsetList::Element * TextMetrics::checkInsetHit(pit_type pit, int x, int y)
 	}
 
 	LYXERR(Debug::DEBUG, "No inset hit. ");
-	return 0;
+	return nullptr;
 }
 
 
@@ -1497,11 +1498,11 @@ InsetList::Element * TextMetrics::checkInsetHit(pit_type pit, int x, int y)
 Inset * TextMetrics::checkInsetHit(int x, int y)
 {
 	pit_type const pit = getPitNearY(y);
-	LASSERT(pit != -1, return 0);
+	LASSERT(pit != lyxnpos, return nullptr);
 	InsetList::Element * e = checkInsetHit(pit, x, y);
 
 	if (!e)
-		return 0;
+		return nullptr;
 
 	return e->inset;
 }
@@ -1537,7 +1538,7 @@ int TextMetrics::cursorY(CursorSlice const & sl, bool boundary) const
 	for (pit_type pit = 0; pit < sl.pit(); ++pit) {
 		h += parMetrics(pit).height();
 	}
-	int pos = sl.pos();
+	pos_type pos = sl.pos();
 	if (pos && boundary)
 		--pos;
 	size_t const rend = pm.pos2row(pos);
@@ -1615,8 +1616,7 @@ int TextMetrics::leftMargin(pit_type const pit, pos_type const pos) const
 {
 	ParagraphList const & pars = text_->paragraphs();
 
-	LASSERT(pit >= 0, return 0);
-	LASSERT(pit < int(pars.size()), return 0);
+	LASSERT(pit < pars.size(), return 0);
 	Paragraph const & par = pars[pit];
 	LASSERT(pos >= 0, return 0);
 	LASSERT(pos <= par.size(), return 0);
@@ -1635,7 +1635,7 @@ int TextMetrics::leftMargin(pit_type const pit, pos_type const pos) const
 		l_margin += bfm.signedWidth(tclass.leftmargin());
 	}
 
-	int depth = par.getDepth();
+	int depth = int(par.getDepth());
 	if (depth != 0) {
 		// find the next level paragraph
 		pit_type newpar = text_->outerHook(pit);
diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp
index 6dca350078..977495dc06 100644
--- a/src/insets/InsetInclude.cpp
+++ b/src/insets/InsetInclude.cpp
@@ -982,7 +982,7 @@ docstring InsetInclude::xhtml(XHTMLStream & xs, OutputParams const & rp) const
 	// are we generating only some paragraphs, or all of them?
 	bool const all_pars = !rp.dryrun ||
 			(rp.par_begin == 0 &&
-			 rp.par_end == (int)buffer().text().paragraphs().size());
+			 rp.par_end == buffer().text().paragraphs().size());
 
 	OutputParams op = rp;
 	if (all_pars) {
diff --git a/src/output_xhtml.cpp b/src/output_xhtml.cpp
index 080369f104..d4ac5f5ab3 100644
--- a/src/output_xhtml.cpp
+++ b/src/output_xhtml.cpp
@@ -885,7 +885,7 @@ ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
 						style.labelfont : style.font;
 			FontInfo const our_font =
 				par->getFont(buf.masterBuffer()->params(), 0,
-			               text.outerFont(distance(begin, par))).fontInfo();
+						   text.outerFont(pit_type(distance(begin, par)))).fontInfo();
 			if (first_font == our_font)
 				special_case = true;
 		}
@@ -913,7 +913,7 @@ ParagraphList::const_iterator makeParagraphs(Buffer const & buf,
 		}
 
 		docstring const deferred = par->simpleLyXHTMLOnePar(buf, xs,
-			runparams, text.outerFont(distance(begin, par)),
+			runparams, text.outerFont(pit_type(distance(begin, par))),
 			open_par, close_par);
 
 		if (close_par) {
@@ -1050,7 +1050,7 @@ ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
 					openItemTag(xs, style, par->params());
 
 				docstring deferred = par->simpleLyXHTMLOnePar(buf, xs, runparams,
-					text.outerFont(distance(begin, par)), true, true, sep);
+					text.outerFont(pit_type(distance(begin, par))), true, true, sep);
 				xs << XHTMLStream::ESCAPE_NONE << deferred;
 				++par;
 
@@ -1130,7 +1130,7 @@ void makeCommand(Buffer const & buf,
 
 	ParagraphList::const_iterator const begin = text.paragraphs().begin();
 	pbegin->simpleLyXHTMLOnePar(buf, xs, runparams,
-			text.outerFont(distance(begin, pbegin)));
+			text.outerFont(pit_type(distance(begin, pbegin))));
 	closeTag(xs, style);
 	xs << html::CR();
 }
@@ -1155,7 +1155,7 @@ void xhtmlParagraphs(Text const & text,
 
 	OutputParams ourparams = runparams;
 	ParagraphList::const_iterator const pend =
-		(epit == (int) paragraphs.size()) ?
+		(epit == paragraphs.size()) ?
 			paragraphs.end() : paragraphs.constIterator(epit);
 	while (bpit < epit) {
 		ParagraphList::const_iterator par = paragraphs.constIterator(bpit);
@@ -1208,7 +1208,7 @@ void xhtmlParagraphs(Text const & text,
 			par = makeParagraphs(buf, xs, ourparams, text, par, send);
 			break;
 		}
-		bpit += distance(lastpar, par);
+		bpit += pit_type(distance(lastpar, par));
 	}
 }
 
diff --git a/src/support/lstrings.cpp b/src/support/lstrings.cpp
index 55e66ce077..8c15d9bf09 100644
--- a/src/support/lstrings.cpp
+++ b/src/support/lstrings.cpp
@@ -1507,6 +1507,14 @@ docstring bformat(docstring const & fmt, unsigned int arg1)
 }
 
 
+docstring bformat(docstring const & fmt, pit_type arg1)
+{
+	LATTEST(contains(fmt, from_ascii("%1$d")));
+	docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
+	return subst(str, from_ascii("%%"), from_ascii("%"));
+}
+
+
 docstring bformat(docstring const & fmt, docstring const & arg1)
 {
 	LATTEST(contains(fmt, from_ascii("%1$s")));
diff --git a/src/support/lstrings.h b/src/support/lstrings.h
index f661fc5b85..0662769711 100644
--- a/src/support/lstrings.h
+++ b/src/support/lstrings.h
@@ -17,6 +17,7 @@
 #define LSTRINGS_H
 
 #include "support/docstring.h"
+#include "support/types.h"
 
 #include <vector>
 
@@ -369,6 +370,7 @@ docstring bformat(docstring const & fmt, long arg1);
 docstring bformat(docstring const & fmt, long long arg1);
 #endif
 docstring bformat(docstring const & fmt, unsigned int arg1);
+docstring bformat(docstring const & fmt, pit_type arg1);
 docstring bformat(docstring const & fmt, docstring const & arg1);
 docstring bformat(docstring const & fmt, char * arg1);
 docstring bformat(docstring const & fmt, docstring const & arg1, docstring const & arg2);
diff --git a/src/support/types.h b/src/support/types.h
index c9cf349592..fadd62a252 100644
--- a/src/support/types.h
+++ b/src/support/types.h
@@ -16,6 +16,7 @@
 #ifndef LYX_TYPES_H
 #define LYX_TYPES_H
 
+#include <climits>
 #include <cstddef>
 
 namespace lyx {
@@ -40,11 +41,12 @@ namespace lyx {
 	 */
 	// FIXME: should be unsigned as well.
 	// however, simply changing it breaks a downward loop somewhere...
-	typedef ptrdiff_t  pit_type;
+	typedef size_t     pit_type;
 
 	/// a type for the nesting depth of a paragraph
 	typedef size_t     depth_type;
 
+	static const size_t lyxnpos = pit_type(-1);
 // set this to '0' if you want to have really safe types
 #if 1
 


More information about the lyx-devel mailing list