1992-09-03 20:21:07 +00:00
|
|
|
/* 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.");
|
1997-12-29 19:52:29 +00:00
|
|
|
|
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.");
|
1997-12-29 19:52:29 +00:00
|
|
|
|
1996-07-23 18:12:39 +00:00
|
|
|
static PyObject *
|
2000-07-10 09:43:24 +00:00
|
|
|
strop_maketrans(PyObject *self, PyObject *args)
|
1996-07-23 18:12:39 +00:00
|
|
|
{
|
1997-01-06 16:50:09 +00:00
|
|
|
unsigned char *c, *from=NULL, *to=NULL;
|
2006-02-17 15:49:09 +00:00
|
|
|
Py_ssize_t i, fromlen=0, tolen=0;
|
1997-01-06 16:50:09 +00:00
|
|
|
PyObject *result;
|
1996-07-23 18:12:39 +00:00
|
|
|
|
2000-02-29 13:59:29 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen))
|
1997-01-03 22:45:34 +00:00
|
|
|
return NULL;
|
1996-07-23 18:12:39 +00:00
|
|
|
|
1997-01-03 22:45:34 +00:00
|
|
|
if (fromlen != tolen) {
|
1996-12-09 18:35:56 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
1996-07-23 18:12:39 +00:00
|
|
|
"maketrans arguments must have same length");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
1997-01-06 16:50:09 +00:00
|
|
|
|
|
|
|
|
result = PyString_FromStringAndSize((char *)NULL, 256);
|
|
|
|
|
if (result == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result);
|
1997-01-03 22:45:34 +00:00
|
|
|
for (i = 0; i < 256; i++)
|
1996-07-23 18:12:39 +00:00
|
|
|
c[i]=(unsigned char)i;
|
1997-01-03 22:45:34 +00:00
|
|
|
for (i = 0; i < fromlen; i++)
|
1996-07-23 18:12:39 +00:00
|
|
|
c[from[i]]=to[i];
|
1997-01-03 22:45:34 +00:00
|
|
|
|
1997-01-06 16:50:09 +00:00
|
|
|
return result;
|
1996-07-23 18:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
1992-09-03 20:21:07 +00:00
|
|
|
/* List of functions defined in the module */
|
|
|
|
|
|
1997-01-03 22:45:34 +00:00
|
|
|
static PyMethodDef
|
|
|
|
|
strop_methods[] = {
|
2001-05-09 22:15:03 +00:00
|
|
|
{"maketrans", strop_maketrans, METH_VARARGS, maketrans__doc__},
|
1992-09-03 20:21:07 +00:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2002-08-02 02:27:13 +00:00
|
|
|
PyMODINIT_FUNC
|
2000-07-21 06:00:07 +00:00
|
|
|
initstrop(void)
|
1992-09-03 20:21:07 +00:00
|
|
|
{
|
2007-04-17 08:48:32 +00:00
|
|
|
PyObject *m;
|
1997-12-29 19:52:29 +00:00
|
|
|
m = Py_InitModule4("strop", strop_methods, strop_module__doc__,
|
|
|
|
|
(PyObject*)NULL, PYTHON_API_VERSION);
|
2006-01-19 06:09:39 +00:00
|
|
|
if (m == NULL)
|
|
|
|
|
return;
|
1992-09-03 20:21:07 +00:00
|
|
|
}
|