cpython/Modules/stropmodule.c

65 lines
1.5 KiB
C
Raw Normal View History

/* strop module */
2006-02-17 15:49:09 +00:00
#define PY_SSIZE_T_CLEAN
2002-06-13 20:33:02 +00:00
#include "Python.h"
PyDoc_STRVAR(strop_module__doc__,
2001-05-09 22:15:03 +00:00
"Common string manipulations, optimized for speed.\n"
"\n"
"Always use \"import string\" rather than referencing\n"
2002-06-13 20:33:02 +00:00
"this module directly.");
2002-06-13 20:33:02 +00:00
PyDoc_STRVAR(maketrans__doc__,
2001-05-09 22:15:03 +00:00
"maketrans(frm, to) -> string\n"
"\n"
"Return a translation table (a string of 256 bytes long)\n"
"suitable for use in string.translate. The strings frm and to\n"
2002-06-13 20:33:02 +00:00
"must be of the same length.");
static PyObject *
2000-07-10 09:43:24 +00:00
strop_maketrans(PyObject *self, PyObject *args)
{
unsigned char *c, *from=NULL, *to=NULL;
2006-02-17 15:49:09 +00:00
Py_ssize_t i, fromlen=0, tolen=0;
PyObject *result;
if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen))
return NULL;
if (fromlen != tolen) {
1996-12-09 18:35:56 +00:00
PyErr_SetString(PyExc_ValueError,
"maketrans arguments must have same length");
return NULL;
}
result = PyString_FromStringAndSize((char *)NULL, 256);
if (result == NULL)
return NULL;
c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result);
for (i = 0; i < 256; i++)
c[i]=(unsigned char)i;
for (i = 0; i < fromlen; i++)
c[from[i]]=to[i];
return result;
}
/* List of functions defined in the module */
static PyMethodDef
strop_methods[] = {
2001-05-09 22:15:03 +00:00
{"maketrans", strop_maketrans, METH_VARARGS, maketrans__doc__},
{NULL, NULL} /* sentinel */
};
PyMODINIT_FUNC
initstrop(void)
{
PyObject *m;
m = Py_InitModule4("strop", strop_methods, strop_module__doc__,
(PyObject*)NULL, PYTHON_API_VERSION);
if (m == NULL)
return;
}