escape_encode(PyObject *self,
PyObject *args)
{
- static const char *hexdigits = "0123456789abcdef";
- PyObject *str;
- Py_ssize_t size;
- Py_ssize_t newsize;
- const char *errors = NULL;
- PyObject *v;
-
- if (!PyArg_ParseTuple(args, "O!|z:escape_encode",
- &PyBytes_Type, &str, &errors))
- return NULL;
-
- size = PyBytes_GET_SIZE(str);
- newsize = 4*size;
- if (newsize > PY_SSIZE_T_MAX || newsize / 4 != size) {
- PyErr_SetString(PyExc_OverflowError,
- "string is too large to encode");
- return NULL;
- }
- v = PyBytes_FromStringAndSize(NULL, newsize);
+ static const char *hexdigits = "0123456789abcdef";
+ PyObject *str;
+ Py_ssize_t size;
+ Py_ssize_t newsize;
+ const char *errors = NULL;
+ PyObject *v;
+
+ if (!PyArg_ParseTuple(args, "O!|z:escape_encode",
+ &PyBytes_Type, &str, &errors))
+ return NULL;
+
+ size = PyBytes_GET_SIZE(str);
+ newsize = 4*size;
+ if (newsize > PY_SSIZE_T_MAX || newsize / 4 != size) {
+ PyErr_SetString(PyExc_OverflowError,
+ "string is too large to encode");
+ return NULL;
+ }
+ v = PyBytes_FromStringAndSize(NULL, newsize);
- if (v == NULL) {
- return NULL;
+ if (v == NULL) {
+ return NULL;
+ }
+ else {
+ register Py_ssize_t i;
+ register char c;
+ register char *p = PyBytes_AS_STRING(v);
+
+ for (i = 0; i < size; i++) {
+ /* There's at least enough room for a hex escape */
+ assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4);
+ c = PyBytes_AS_STRING(str)[i];
+ if (c == '\'' || c == '\\')
+ *p++ = '\\', *p++ = c;
+ else if (c == '\t')
+ *p++ = '\\', *p++ = 't';
+ else if (c == '\n')
+ *p++ = '\\', *p++ = 'n';
+ else if (c == '\r')
+ *p++ = '\\', *p++ = 'r';
+ else if (c < ' ' || c >= 0x7f) {
+ *p++ = '\\';
+ *p++ = 'x';
+ *p++ = hexdigits[(c & 0xf0) >> 4];
+ *p++ = hexdigits[c & 0xf];
+ }
+ else
+ *p++ = c;
}
- else {
- register Py_ssize_t i;
- register char c;
- register char *p = PyBytes_AS_STRING(v);
-
- for (i = 0; i < size; i++) {
- /* There's at least enough room for a hex escape */
- assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4);
- c = PyBytes_AS_STRING(str)[i];
- if (c == '\'' || c == '\\')
- *p++ = '\\', *p++ = c;
- else if (c == '\t')
- *p++ = '\\', *p++ = 't';
- else if (c == '\n')
- *p++ = '\\', *p++ = 'n';
- else if (c == '\r')
- *p++ = '\\', *p++ = 'r';
- else if (c < ' ' || c >= 0x7f) {
- *p++ = '\\';
- *p++ = 'x';
- *p++ = hexdigits[(c & 0xf0) >> 4];
- *p++ = hexdigits[c & 0xf];
- }
- else
- *p++ = c;
- }
- *p = '\0';
- if (_PyBytes_Resize(&v, (p - PyBytes_AS_STRING(v)))) {
- return NULL;
- }
+ *p = '\0';
+ if (_PyBytes_Resize(&v, (p - PyBytes_AS_STRING(v)))) {
+ return NULL;
}
+ }
- return codec_tuple(v, PyBytes_Size(v));
+ return codec_tuple(v, PyBytes_Size(v));
}
/* --- Decoder ------------------------------------------------------------ */
utf_7_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int final = 0;
Py_ssize_t consumed;
decoded = PyUnicode_DecodeUTF7Stateful(pbuf.buf, pbuf.len, errors,
final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
return NULL;
return codec_tuple(decoded, consumed);
utf_8_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int final = 0;
Py_ssize_t consumed;
decoded = PyUnicode_DecodeUTF8Stateful(pbuf.buf, pbuf.len, errors,
final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
return NULL;
return codec_tuple(decoded, consumed);
utf_16_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = 0;
int final = 0;
consumed = pbuf.len; /* This is overwritten unless final is true. */
decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
&byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
return NULL;
return codec_tuple(decoded, consumed);
utf_16_le_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = -1;
int final = 0;
consumed = pbuf.len; /* This is overwritten unless final is true. */
decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
&byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
return NULL;
return codec_tuple(decoded, consumed);
utf_16_be_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = 1;
int final = 0;
consumed = pbuf.len; /* This is overwritten unless final is true. */
decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
&byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
return NULL;
return codec_tuple(decoded, consumed);
utf_16_ex_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = 0;
PyObject *unicode, *tuple;
consumed = pbuf.len; /* This is overwritten unless final is true. */
unicode = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
&byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ PyBuffer_Release(&pbuf);
if (unicode == NULL)
return NULL;
tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);
utf_32_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = 0;
int final = 0;
consumed = pbuf.len; /* This is overwritten unless final is true. */
decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
&byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
return NULL;
return codec_tuple(decoded, consumed);
utf_32_le_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = -1;
int final = 0;
consumed = pbuf.len; /* This is overwritten unless final is true. */
decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
&byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
return NULL;
return codec_tuple(decoded, consumed);
utf_32_be_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = 1;
int final = 0;
consumed = pbuf.len; /* This is overwritten unless final is true. */
decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
&byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
return NULL;
return codec_tuple(decoded, consumed);
utf_32_ex_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = 0;
PyObject *unicode, *tuple;
consumed = pbuf.len; /* This is overwritten unless final is true. */
unicode = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
&byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ PyBuffer_Release(&pbuf);
if (unicode == NULL)
return NULL;
tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);
unicode_escape_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
PyObject *unicode;
&pbuf, &errors))
return NULL;
- unicode = PyUnicode_DecodeUnicodeEscape(pbuf.buf, pbuf.len, errors);
- PyBuffer_Release(&pbuf);
- return codec_tuple(unicode, pbuf.len);
+ unicode = PyUnicode_DecodeUnicodeEscape(pbuf.buf, pbuf.len, errors);
+ PyBuffer_Release(&pbuf);
+ return codec_tuple(unicode, pbuf.len);
}
static PyObject *
raw_unicode_escape_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
- PyObject *unicode;
+ PyObject *unicode;
if (!PyArg_ParseTuple(args, "s*|z:raw_unicode_escape_decode",
&pbuf, &errors))
return NULL;
- unicode = PyUnicode_DecodeRawUnicodeEscape(pbuf.buf, pbuf.len, errors);
- PyBuffer_Release(&pbuf);
- return codec_tuple(unicode, pbuf.len);
+ unicode = PyUnicode_DecodeRawUnicodeEscape(pbuf.buf, pbuf.len, errors);
+ PyBuffer_Release(&pbuf);
+ return codec_tuple(unicode, pbuf.len);
}
static PyObject *
latin_1_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
- PyObject *unicode;
+ Py_buffer pbuf;
+ PyObject *unicode;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y*|z:latin_1_decode",
&pbuf, &errors))
return NULL;
- unicode = PyUnicode_DecodeLatin1(pbuf.buf, pbuf.len, errors);
- PyBuffer_Release(&pbuf);
- return codec_tuple(unicode, pbuf.len);
+ unicode = PyUnicode_DecodeLatin1(pbuf.buf, pbuf.len, errors);
+ PyBuffer_Release(&pbuf);
+ return codec_tuple(unicode, pbuf.len);
}
static PyObject *
ascii_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
- PyObject *unicode;
+ Py_buffer pbuf;
+ PyObject *unicode;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y*|z:ascii_decode",
&pbuf, &errors))
return NULL;
- unicode = PyUnicode_DecodeASCII(pbuf.buf, pbuf.len, errors);
- PyBuffer_Release(&pbuf);
- return codec_tuple(unicode, pbuf.len);
+ unicode = PyUnicode_DecodeASCII(pbuf.buf, pbuf.len, errors);
+ PyBuffer_Release(&pbuf);
+ return codec_tuple(unicode, pbuf.len);
}
static PyObject *
charmap_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
- PyObject *unicode;
+ Py_buffer pbuf;
+ PyObject *unicode;
const char *errors = NULL;
PyObject *mapping = NULL;
if (mapping == Py_None)
mapping = NULL;
- unicode = PyUnicode_DecodeCharmap(pbuf.buf, pbuf.len, mapping, errors);
- PyBuffer_Release(&pbuf);
- return codec_tuple(unicode, pbuf.len);
+ unicode = PyUnicode_DecodeCharmap(pbuf.buf, pbuf.len, mapping, errors);
+ PyBuffer_Release(&pbuf);
+ return codec_tuple(unicode, pbuf.len);
}
#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
mbcs_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int final = 0;
Py_ssize_t consumed;
decoded = PyUnicode_DecodeMBCSStateful(pbuf.buf, pbuf.len, errors,
final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
return NULL;
return codec_tuple(decoded, consumed);