D4106: index: don't add 1 to length variables

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Sun Aug 5 06:33:57 UTC 2018


martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  A lot of "+ 1" and "-1" were mechanically added to ease the transition
  in https://phab.mercurial-scm.org/rHG781b2720d2ac005e2806b46d8cb91abacfe4b901 (index: don't include nullid in len(),
  2018-07-20). Let's clean it up now.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D4106

AFFECTED FILES
  mercurial/cext/revlog.c

CHANGE DETAILS

diff --git a/mercurial/cext/revlog.c b/mercurial/cext/revlog.c
--- a/mercurial/cext/revlog.c
+++ b/mercurial/cext/revlog.c
@@ -155,15 +155,15 @@
 	int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
 	const char *c_node_id;
 	const char *data;
-	Py_ssize_t length = index_length(self) + 1;
+	Py_ssize_t length = index_length(self);
 	PyObject *entry;
 
 	if (pos == -1) {
 		Py_INCREF(nullentry);
 		return nullentry;
 	}
 
-	if (pos < 0 || pos >= length - 1) {
+	if (pos < 0 || pos >= length) {
 		PyErr_SetString(PyExc_IndexError, "revlog index out of range");
 		return NULL;
 	}
@@ -225,13 +225,13 @@
  */
 static const char *index_node(indexObject *self, Py_ssize_t pos)
 {
-	Py_ssize_t length = index_length(self) + 1;
+	Py_ssize_t length = index_length(self);
 	const char *data;
 
 	if (pos == -1)
 		return nullid;
 
-	if (pos >= length - 1)
+	if (pos >= length)
 		return NULL;
 
 	if (pos >= self->length - 1) {
@@ -285,7 +285,7 @@
 	if (node_check(PyTuple_GET_ITEM(obj, 7), &node) == -1)
 		return NULL;
 
-	len = index_length(self) + 1;
+	len = index_length(self);
 
 	if (self->added == NULL) {
 		self->added = PyList_New(0);
@@ -297,7 +297,7 @@
 		return NULL;
 
 	if (self->nt)
-		nt_insert(self, node, len - 1);
+		nt_insert(self, node, len);
 
 	Py_CLEAR(self->headrevs);
 	Py_RETURN_NONE;
@@ -844,7 +844,7 @@
 	int stoprev, iterrev, baserev = -1;
 	int stopped;
 	PyObject *chain = NULL, *result = NULL;
-	const Py_ssize_t length = index_length(self) + 1;
+	const Py_ssize_t length = index_length(self);
 
 	if (!PyArg_ParseTuple(args, "iOi", &rev, &stoparg, &generaldelta)) {
 		return NULL;
@@ -865,7 +865,7 @@
 		return NULL;
 	}
 
-	if (rev < 0 || rev >= length - 1) {
+	if (rev < 0 || rev >= length) {
 		PyErr_SetString(PyExc_ValueError, "revlog index out of range");
 		return NULL;
 	}
@@ -908,7 +908,7 @@
 			break;
 		}
 
-		if (iterrev >= length - 1) {
+		if (iterrev >= length) {
 			PyErr_SetString(PyExc_IndexError, "revision outside index");
 			return NULL;
 		}



To: martinvonz, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list