static PyObject* escape_unicode(PyUnicodeObject *in) { PyUnicodeObject *out; Py_UNICODE *inp = in->str; const Py_UNICODE *inp_end = in->str + in->length; Py_UNICODE *next_escp; Py_UNICODE *outp; Py_ssize_t delta=0, erepl=0, delta_len=0; /* First we need to figure out how long the escaped string will be */ while (*(inp) || inp < inp_end) { if (*inp < ESCAPED_CHARS_TABLE_SIZE && escaped_chars_delta_len[*inp]) { delta += escaped_chars_delta_len[*inp]; ++erepl; } ++inp; } /* Do we need to escape anything at all? */ if (!erepl) { Py_INCREF(in); return (PyObject*)in; } out = (PyUnicodeObject*)PyUnicode_FromUnicode(NULL, in->length + delta); if (!out) return NULL; outp = out->str; inp = in->str; while (erepl-- > 0) { /* look for the next substitution */ next_escp = inp; while (next_escp < inp_end) { if (*next_escp < ESCAPED_CHARS_TABLE_SIZE && (delta_len = escaped_chars_delta_len[*next_escp])) { ++delta_len; break; } ++next_escp; } if (next_escp > inp) { /* copy unescaped chars between inp and next_escp */ Py_UNICODE_COPY(outp, inp, next_escp-inp); outp += next_escp - inp; } /* escape 'next_escp' */ Py_UNICODE_COPY(outp, escaped_chars_repl[*next_escp], delta_len); outp += delta_len; inp = next_escp + 1; } if (inp < inp_end) Py_UNICODE_COPY(outp, inp, in->length - (inp - in->str)); return (PyObject*)out; }
static PyObject * Splitter_split(Splitter *self, PyObject *args) { PyObject *doc; char *encoding = "iso-8859-15"; Py_XDECREF(self->list); self->list = PyList_New(0); if (! (PyArg_ParseTuple(args,"O|s",&doc, &encoding))) return NULL; if (PyBytes_Check(doc)) { if (strlen(encoding) == 0 || !strcmp(encoding,"ascii")) splitString(self, doc); else { PyObject *doc1; if (! (doc1 = PyUnicode_FromEncodedObject(doc, encoding, "strict"))) { PyErr_SetString(PyExc_UnicodeError,"unicode conversion failed (maybe wrong encoding parameter)"); return NULL; } splitUnicodeString(self, doc1); Py_XDECREF(doc1); } } else if (PyUnicode_Check(doc)) { PyObject *doc1; // create a *real* copy since we need to modify the string doc1 = PyUnicode_FromUnicode(NULL, PyUnicode_GET_SIZE(doc)); Py_UNICODE_COPY(PyUnicode_AS_UNICODE(doc1), PyUnicode_AS_UNICODE(doc), PyUnicode_GET_SIZE(doc)); splitUnicodeString(self, doc1); Py_DECREF(doc1); } else { PyErr_SetString(PyExc_TypeError, "first argument must be string or unicode"); return NULL; } Py_XINCREF(self->list); return self->list; }
static PyObject* getenvironment(PyObject* environment) { int i; Py_ssize_t envsize; PyObject* out = NULL; PyObject* keys; PyObject* values; Py_UNICODE* p; /* convert environment dictionary to windows enviroment string */ if (! PyMapping_Check(environment)) { PyErr_SetString( PyExc_TypeError, "environment must be dictionary or None"); return NULL; } envsize = PyMapping_Length(environment); keys = PyMapping_Keys(environment); values = PyMapping_Values(environment); if (!keys || !values) goto error; out = PyUnicode_FromUnicode(NULL, 2048); if (! out) goto error; p = PyUnicode_AS_UNICODE(out); for (i = 0; i < envsize; i++) { Py_ssize_t ksize, vsize, totalsize; PyObject* key = PyList_GET_ITEM(keys, i); PyObject* value = PyList_GET_ITEM(values, i); if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) { PyErr_SetString(PyExc_TypeError, "environment can only contain strings"); goto error; } ksize = PyUnicode_GET_SIZE(key); vsize = PyUnicode_GET_SIZE(value); totalsize = (p - PyUnicode_AS_UNICODE(out)) + ksize + 1 + vsize + 1 + 1; if (totalsize > PyUnicode_GET_SIZE(out)) { Py_ssize_t offset = p - PyUnicode_AS_UNICODE(out); PyUnicode_Resize(&out, totalsize + 1024); p = PyUnicode_AS_UNICODE(out) + offset; } Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(key), ksize); p += ksize; *p++ = '='; Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(value), vsize); p += vsize; *p++ = '\0'; } /* add trailing null byte */ *p++ = '\0'; PyUnicode_Resize(&out, p - PyUnicode_AS_UNICODE(out)); /* PyObject_Print(out, stdout, 0); */ Py_XDECREF(keys); Py_XDECREF(values); return out; error: Py_XDECREF(out); Py_XDECREF(keys); Py_XDECREF(values); return NULL; }