mercurial/pathencode.c
changeset 18430 0459c6555f69
parent 17699 0696b1793f4b
child 18431 3aa9b2136593
equal deleted inserted replaced
18429:e9ea0f0f05e7 18430:0459c6555f69
    13  * handled either in a single pass with no allocations or two passes
    13  * handled either in a single pass with no allocations or two passes
    14  * with a single allocation.  For longer paths, multiple passes are
    14  * with a single allocation.  For longer paths, multiple passes are
    15  * required.
    15  * required.
    16  */
    16  */
    17 
    17 
       
    18 #define PY_SSIZE_T_CLEAN
    18 #include <Python.h>
    19 #include <Python.h>
    19 #include <assert.h>
    20 #include <assert.h>
    20 #include <ctype.h>
    21 #include <ctype.h>
    21 #include <stdlib.h>
    22 #include <stdlib.h>
    22 #include <string.h>
    23 #include <string.h>
   479 		       src, len, 1);
   480 		       src, len, 1);
   480 }
   481 }
   481 
   482 
   482 static const Py_ssize_t maxstorepathlen = 120;
   483 static const Py_ssize_t maxstorepathlen = 120;
   483 
   484 
       
   485 static Py_ssize_t _lowerencode(char *dest, size_t destsize,
       
   486 			       const char *src, Py_ssize_t len)
       
   487 {
       
   488 	static const uint32_t onebyte[8] = {
       
   489 		1, 0x2bfffbfb, 0xe8000001, 0x2fffffff
       
   490 	};
       
   491 
       
   492 	static const uint32_t lower[8] = { 0, 0, 0x7fffffe };
       
   493 
       
   494 	Py_ssize_t i, destlen = 0;
       
   495 
       
   496 	for (i = 0; i < len; i++) {
       
   497 		if (inset(onebyte, src[i]))
       
   498 			charcopy(dest, &destlen, destsize, src[i]);
       
   499 		else if (inset(lower, src[i]))
       
   500 			charcopy(dest, &destlen, destsize, src[i] + 32);
       
   501 		else
       
   502 			escape3(dest, &destlen, destsize, src[i]);
       
   503 	}
       
   504 
       
   505 	return destlen;
       
   506 }
       
   507 
       
   508 PyObject *lowerencode(PyObject *self, PyObject *args)
       
   509 {
       
   510 	char *path;
       
   511 	Py_ssize_t len, newlen;
       
   512 	PyObject *ret;
       
   513 
       
   514 	if (!PyArg_ParseTuple(args, "s#:lowerencode", &path, &len))
       
   515 		return NULL;
       
   516 
       
   517 	newlen = _lowerencode(NULL, 0, path, len);
       
   518 	ret = PyString_FromStringAndSize(NULL, newlen);
       
   519 	if (ret)
       
   520 		newlen = _lowerencode(PyString_AS_STRING(ret), newlen,
       
   521 				      path, len);
       
   522 
       
   523 	return ret;
       
   524 }
       
   525 
   484 /*
   526 /*
   485  * We currently implement only basic encoding.
   527  * We currently implement only basic encoding.
   486  *
   528  *
   487  * If a name is too long to encode due to Windows path name limits,
   529  * If a name is too long to encode due to Windows path name limits,
   488  * this function returns None.
   530  * this function returns None.