[PATCH 1 of 4 foldmap-in-C] parsers: factor out most of asciilower into an internal function

Siddharth Agarwal sid0 at fb.com
Wed Apr 1 17:28:04 UTC 2015


# HG changeset patch
# User Siddharth Agarwal <sid0 at fb.com>
# Date 1427822729 25200
#      Tue Mar 31 10:25:29 2015 -0700
# Node ID 719010b9a74187eb3139b4e57562ecefde288cc4
# Parent  9a023f039ed86ed4694174223b5f6c3965e99723
parsers: factor out most of asciilower into an internal function

We're going to reuse this in upcoming patches.

diff --git a/mercurial/parsers.c b/mercurial/parsers.c
--- a/mercurial/parsers.c
+++ b/mercurial/parsers.c
@@ -93,14 +93,15 @@
 	return ret;
 }
 
-static PyObject *asciilower(PyObject *self, PyObject *args)
+static inline PyObject *_asciilower(PyObject *str_obj)
 {
 	char *str, *newstr;
-	int i, len;
+	Py_ssize_t i, len;
 	PyObject *newobj = NULL;
+	PyObject *ret = NULL;
 
-	if (!PyArg_ParseTuple(args, "s#", &str, &len))
-		goto quit;
+	str = PyBytes_AS_STRING(str_obj);
+	len = PyBytes_GET_SIZE(str_obj);
 
 	newobj = PyBytes_FromStringAndSize(NULL, len);
 	if (!newobj)
@@ -121,10 +122,19 @@
 		newstr[i] = lowertable[(unsigned char)c];
 	}
 
-	return newobj;
+	ret = newobj;
+	Py_INCREF(ret);
 quit:
 	Py_XDECREF(newobj);
-	return NULL;
+	return ret;
+}
+
+static PyObject *asciilower(PyObject *self, PyObject *args)
+{
+	PyObject *str_obj;
+	if (!PyArg_ParseTuple(args, "O!:asciilower", &PyBytes_Type, &str_obj))
+		return NULL;
+	return _asciilower(str_obj);
 }
 
 /*


More information about the Mercurial-devel mailing list