mercurial/cext/charencode.c
changeset 41361 763b45bc4483
parent 36649 186c6df3a373
child 46819 d4ba4d51f85f
equal deleted inserted replaced
41360:b81ca9a3f4e4 41361:763b45bc4483
   112 	char *d;
   112 	char *d;
   113 	Py_ssize_t i;
   113 	Py_ssize_t i;
   114 
   114 
   115 	ret = PyBytes_FromStringAndSize(NULL, len / 2);
   115 	ret = PyBytes_FromStringAndSize(NULL, len / 2);
   116 
   116 
   117 	if (!ret)
   117 	if (!ret) {
   118 		return NULL;
   118 		return NULL;
       
   119 	}
   119 
   120 
   120 	d = PyBytes_AsString(ret);
   121 	d = PyBytes_AsString(ret);
   121 
   122 
   122 	for (i = 0; i < len;) {
   123 	for (i = 0; i < len;) {
   123 		int hi = hexdigit(str, i++);
   124 		int hi = hexdigit(str, i++);
   131 PyObject *isasciistr(PyObject *self, PyObject *args)
   132 PyObject *isasciistr(PyObject *self, PyObject *args)
   132 {
   133 {
   133 	const char *buf;
   134 	const char *buf;
   134 	Py_ssize_t i, len;
   135 	Py_ssize_t i, len;
   135 	if (!PyArg_ParseTuple(args, PY23("s#:isasciistr", "y#:isasciistr"),
   136 	if (!PyArg_ParseTuple(args, PY23("s#:isasciistr", "y#:isasciistr"),
   136 	                      &buf, &len))
   137 	                      &buf, &len)) {
   137 		return NULL;
   138 		return NULL;
       
   139 	}
   138 	i = 0;
   140 	i = 0;
   139 	/* char array in PyStringObject should be at least 4-byte aligned */
   141 	/* char array in PyStringObject should be at least 4-byte aligned */
   140 	if (((uintptr_t)buf & 3) == 0) {
   142 	if (((uintptr_t)buf & 3) == 0) {
   141 		const uint32_t *p = (const uint32_t *)buf;
   143 		const uint32_t *p = (const uint32_t *)buf;
   142 		for (; i < len / 4; i++) {
   144 		for (; i < len / 4; i++) {
   143 			if (p[i] & 0x80808080U)
   145 			if (p[i] & 0x80808080U) {
   144 				Py_RETURN_FALSE;
   146 				Py_RETURN_FALSE;
       
   147 			}
   145 		}
   148 		}
   146 		i *= 4;
   149 		i *= 4;
   147 	}
   150 	}
   148 	for (; i < len; i++) {
   151 	for (; i < len; i++) {
   149 		if (buf[i] & 0x80)
   152 		if (buf[i] & 0x80) {
   150 			Py_RETURN_FALSE;
   153 			Py_RETURN_FALSE;
       
   154 		}
   151 	}
   155 	}
   152 	Py_RETURN_TRUE;
   156 	Py_RETURN_TRUE;
   153 }
   157 }
   154 
   158 
   155 static inline PyObject *
   159 static inline PyObject *
   162 
   166 
   163 	str = PyBytes_AS_STRING(str_obj);
   167 	str = PyBytes_AS_STRING(str_obj);
   164 	len = PyBytes_GET_SIZE(str_obj);
   168 	len = PyBytes_GET_SIZE(str_obj);
   165 
   169 
   166 	newobj = PyBytes_FromStringAndSize(NULL, len);
   170 	newobj = PyBytes_FromStringAndSize(NULL, len);
   167 	if (!newobj)
   171 	if (!newobj) {
   168 		goto quit;
   172 		goto quit;
       
   173 	}
   169 
   174 
   170 	newstr = PyBytes_AS_STRING(newobj);
   175 	newstr = PyBytes_AS_STRING(newobj);
   171 
   176 
   172 	for (i = 0; i < len; i++) {
   177 	for (i = 0; i < len; i++) {
   173 		char c = str[i];
   178 		char c = str[i];
   195 }
   200 }
   196 
   201 
   197 PyObject *asciilower(PyObject *self, PyObject *args)
   202 PyObject *asciilower(PyObject *self, PyObject *args)
   198 {
   203 {
   199 	PyObject *str_obj;
   204 	PyObject *str_obj;
   200 	if (!PyArg_ParseTuple(args, "O!:asciilower", &PyBytes_Type, &str_obj))
   205 	if (!PyArg_ParseTuple(args, "O!:asciilower", &PyBytes_Type, &str_obj)) {
   201 		return NULL;
   206 		return NULL;
       
   207 	}
   202 	return _asciitransform(str_obj, lowertable, NULL);
   208 	return _asciitransform(str_obj, lowertable, NULL);
   203 }
   209 }
   204 
   210 
   205 PyObject *asciiupper(PyObject *self, PyObject *args)
   211 PyObject *asciiupper(PyObject *self, PyObject *args)
   206 {
   212 {
   207 	PyObject *str_obj;
   213 	PyObject *str_obj;
   208 	if (!PyArg_ParseTuple(args, "O!:asciiupper", &PyBytes_Type, &str_obj))
   214 	if (!PyArg_ParseTuple(args, "O!:asciiupper", &PyBytes_Type, &str_obj)) {
   209 		return NULL;
   215 		return NULL;
       
   216 	}
   210 	return _asciitransform(str_obj, uppertable, NULL);
   217 	return _asciitransform(str_obj, uppertable, NULL);
   211 }
   218 }
   212 
   219 
   213 PyObject *make_file_foldmap(PyObject *self, PyObject *args)
   220 PyObject *make_file_foldmap(PyObject *self, PyObject *args)
   214 {
   221 {
   220 	Py_ssize_t pos = 0;
   227 	Py_ssize_t pos = 0;
   221 	const char *table;
   228 	const char *table;
   222 
   229 
   223 	if (!PyArg_ParseTuple(args, "O!O!O!:make_file_foldmap", &PyDict_Type,
   230 	if (!PyArg_ParseTuple(args, "O!O!O!:make_file_foldmap", &PyDict_Type,
   224 	                      &dmap, &PyInt_Type, &spec_obj, &PyFunction_Type,
   231 	                      &dmap, &PyInt_Type, &spec_obj, &PyFunction_Type,
   225 	                      &normcase_fallback))
   232 	                      &normcase_fallback)) {
   226 		goto quit;
   233 		goto quit;
       
   234 	}
   227 
   235 
   228 	spec = (int)PyInt_AS_LONG(spec_obj);
   236 	spec = (int)PyInt_AS_LONG(spec_obj);
   229 	switch (spec) {
   237 	switch (spec) {
   230 	case NORMCASE_LOWER:
   238 	case NORMCASE_LOWER:
   231 		table = lowertable;
   239 		table = lowertable;
   242 	}
   250 	}
   243 
   251 
   244 	/* Add some more entries to deal with additions outside this
   252 	/* Add some more entries to deal with additions outside this
   245 	   function. */
   253 	   function. */
   246 	file_foldmap = _dict_new_presized((PyDict_Size(dmap) / 10) * 11);
   254 	file_foldmap = _dict_new_presized((PyDict_Size(dmap) / 10) * 11);
   247 	if (file_foldmap == NULL)
   255 	if (file_foldmap == NULL) {
   248 		goto quit;
   256 		goto quit;
       
   257 	}
   249 
   258 
   250 	while (PyDict_Next(dmap, &pos, &k, &v)) {
   259 	while (PyDict_Next(dmap, &pos, &k, &v)) {
   251 		if (!dirstate_tuple_check(v)) {
   260 		if (!dirstate_tuple_check(v)) {
   252 			PyErr_SetString(PyExc_TypeError,
   261 			PyErr_SetString(PyExc_TypeError,
   253 			                "expected a dirstate tuple");
   262 			                "expected a dirstate tuple");
   263 			} else {
   272 			} else {
   264 				normed = PyObject_CallFunctionObjArgs(
   273 				normed = PyObject_CallFunctionObjArgs(
   265 				    normcase_fallback, k, NULL);
   274 				    normcase_fallback, k, NULL);
   266 			}
   275 			}
   267 
   276 
   268 			if (normed == NULL)
   277 			if (normed == NULL) {
   269 				goto quit;
   278 				goto quit;
       
   279 			}
   270 			if (PyDict_SetItem(file_foldmap, normed, k) == -1) {
   280 			if (PyDict_SetItem(file_foldmap, normed, k) == -1) {
   271 				Py_DECREF(normed);
   281 				Py_DECREF(normed);
   272 				goto quit;
   282 				goto quit;
   273 			}
   283 			}
   274 			Py_DECREF(normed);
   284 			Py_DECREF(normed);
   375 	PyObject *origstr, *escstr;
   385 	PyObject *origstr, *escstr;
   376 	const char *origbuf;
   386 	const char *origbuf;
   377 	Py_ssize_t origlen, esclen;
   387 	Py_ssize_t origlen, esclen;
   378 	int paranoid;
   388 	int paranoid;
   379 	if (!PyArg_ParseTuple(args, "O!i:jsonescapeu8fast", &PyBytes_Type,
   389 	if (!PyArg_ParseTuple(args, "O!i:jsonescapeu8fast", &PyBytes_Type,
   380 	                      &origstr, &paranoid))
   390 	                      &origstr, &paranoid)) {
   381 		return NULL;
   391 		return NULL;
       
   392 	}
   382 
   393 
   383 	origbuf = PyBytes_AS_STRING(origstr);
   394 	origbuf = PyBytes_AS_STRING(origstr);
   384 	origlen = PyBytes_GET_SIZE(origstr);
   395 	origlen = PyBytes_GET_SIZE(origstr);
   385 	esclen = jsonescapelen(origbuf, origlen, paranoid);
   396 	esclen = jsonescapelen(origbuf, origlen, paranoid);
   386 	if (esclen < 0)
   397 	if (esclen < 0) {
   387 		return NULL; /* unsupported char found or overflow */
   398 		return NULL; /* unsupported char found or overflow */
       
   399 	}
   388 	if (origlen == esclen) {
   400 	if (origlen == esclen) {
   389 		Py_INCREF(origstr);
   401 		Py_INCREF(origstr);
   390 		return origstr;
   402 		return origstr;
   391 	}
   403 	}
   392 
   404 
   393 	escstr = PyBytes_FromStringAndSize(NULL, esclen);
   405 	escstr = PyBytes_FromStringAndSize(NULL, esclen);
   394 	if (!escstr)
   406 	if (!escstr) {
   395 		return NULL;
   407 		return NULL;
       
   408 	}
   396 	encodejsonescape(PyBytes_AS_STRING(escstr), esclen, origbuf, origlen,
   409 	encodejsonescape(PyBytes_AS_STRING(escstr), esclen, origbuf, origlen,
   397 	                 paranoid);
   410 	                 paranoid);
   398 
   411 
   399 	return escstr;
   412 	return escstr;
   400 }
   413 }