[PATCH 2 of 8] store: implement lowerencode in C
Bryan O'Sullivan
bos at serpentine.com
Tue Nov 13 16:00:47 CST 2012
# HG changeset patch
# User Bryan O'Sullivan <bryano at fb.com>
# Date 1352842383 28800
# Node ID e35d9835498989606a68a8950f5d521a4e35d7a0
# Parent 79bdf2869dd9a8f88cb0f5e1828b37ce1a448bdc
store: implement lowerencode in C
diff --git a/mercurial/parsers.c b/mercurial/parsers.c
--- a/mercurial/parsers.c
+++ b/mercurial/parsers.c
@@ -1508,6 +1508,7 @@ static char parsers_doc[] = "Efficient c
PyObject *encodedir(PyObject *self, PyObject *args);
PyObject *pathencode(PyObject *self, PyObject *args);
+PyObject *lowerencode(PyObject *self, PyObject *args);
static PyMethodDef methods[] = {
{"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"},
@@ -1516,6 +1517,7 @@ static PyMethodDef methods[] = {
{"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"},
{"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"},
{"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"},
+ {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"},
{NULL, NULL}
};
diff --git a/mercurial/pathencode.c b/mercurial/pathencode.c
--- a/mercurial/pathencode.c
+++ b/mercurial/pathencode.c
@@ -481,6 +481,47 @@ static Py_ssize_t basicencode(char *dest
static const Py_ssize_t maxstorepathlen = 120;
+static Py_ssize_t _lowerencode(char *dest, size_t destsize,
+ const char *src, Py_ssize_t len)
+{
+ static const uint32_t onebyte[8] = {
+ 1, 0x2bfffbfb, 0xe8000001, 0x2fffffff
+ };
+
+ static const uint32_t lower[8] = { 0, 0, 0x7fffffe };
+
+ Py_ssize_t i, destlen = 0;
+
+ for (i = 0; i < len; i++) {
+ if (inset(onebyte, src[i]))
+ charcopy(dest, &destlen, destsize, src[i]);
+ else if (inset(lower, src[i]))
+ charcopy(dest, &destlen, destsize, src[i] + 32);
+ else
+ escape3(dest, &destlen, destsize, src[i]);
+ }
+
+ return destlen;
+}
+
+PyObject *lowerencode(PyObject *self, PyObject *args)
+{
+ char *path;
+ Py_ssize_t len, newlen;
+ PyObject *ret;
+
+ if (!PyArg_ParseTuple(args, "s#:lowerencode", &path, &len))
+ return NULL;
+
+ newlen = _lowerencode(NULL, 0, path, len);
+ ret = PyString_FromStringAndSize(NULL, newlen);
+ if (ret)
+ newlen = _lowerencode(PyString_AS_STRING(ret), newlen,
+ path, len);
+
+ return ret;
+}
+
/*
* We currently implement only basic encoding.
*
diff --git a/mercurial/store.py b/mercurial/store.py
--- a/mercurial/store.py
+++ b/mercurial/store.py
@@ -132,7 +132,7 @@ def _buildlowerencodefun():
cmap[chr(x)] = chr(x).lower()
return lambda s: "".join([cmap[c] for c in s])
-lowerencode = _buildlowerencodefun()
+lowerencode = getattr(parsers, 'lowerencode', None) or _buildlowerencodefun()
# Windows reserved names: con, prn, aux, nul, com1..com9, lpt1..lpt9
_winres3 = ('aux', 'con', 'prn', 'nul') # length 3
More information about the Mercurial-devel
mailing list