[KoCo-CVS] [Commit] cjkcodecs/src multibytecodec.c
Brought to you by:
perky
From: Hye-Shik C. <pe...@us...> - 2003-05-19 22:37:15
|
perky 03/05/19 15:37:14 Modified: src multibytecodec.c Log: Inline iencode, idecode function. They are just 2 strikes; decode & StreamReader, and the other couple. Revision Changes Path 1.6 +41 -76 cjkcodecs/src/multibytecodec.c Index: multibytecodec.c =================================================================== RCS file: /cvsroot/koco/cjkcodecs/src/multibytecodec.c,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- multibytecodec.c 19 May 2003 10:38:08 -0000 1.5 +++ multibytecodec.c 19 May 2003 22:37:13 -0000 1.6 @@ -26,7 +26,7 @@ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Id: multibytecodec.c,v 1.5 2003/05/19 10:38:08 perky Exp $ + * $Id: multibytecodec.c,v 1.6 2003/05/19 22:37:13 perky Exp $ */ #include "Python.h" @@ -386,47 +386,6 @@ return -1; } -static int -multibytecodec_iencode(PyMultibyteCodec *codec, - PyMultibyteCodec_State *state, - MultibyteEncodeBuffer *buf, - PyObject *errors) -{ - for (;;) { - int r; - size_t inleft, outleft; - - /* we don't reuse inleft and outleft here. - * error callbacks can relocate the cursor anywhere on buffer */ - inleft = (size_t)(buf->inbuf_end - buf->inbuf); - if (inleft == 0) return 0; - outleft = (size_t)(buf->outbuf_end - buf->outbuf); - r = codec->encode(state, &buf->inbuf, inleft, &buf->outbuf, outleft); - if (r == 0) - return 0; - else if (multibytecodec_encerror(codec, state, buf, errors, r)) - return -1; - else if (buf->inbuf >= buf->inbuf_end) - return 0; - } -} - -static int -multibytecodec_prepencoderbuf(MultibyteEncodeBuffer *buf, - const Py_UNICODE *data, int datalen) -{ - buf->excobj = NULL; - buf->inbuf = buf->inbuf_top = data; - buf->inbuf_end = buf->inbuf_top + datalen; - buf->outobj = PyString_FromStringAndSize(NULL, datalen * 2 + 16); - if (buf->outobj == NULL) - return -1; - buf->outbuf = (unsigned char *)PyString_AS_STRING(buf->outobj); - buf->outbuf_end = buf->outbuf + PyString_GET_SIZE(buf->outobj); - - return 0; -} - static PyObject * multibytecodec_encode(PyMultibyteCodec *codec, PyMultibyteCodec_State *state, @@ -439,12 +398,32 @@ if (datalen == 0) return PyString_FromString(""); - if (multibytecodec_prepencoderbuf(&buf, data, datalen) == -1) + buf.excobj = NULL; + buf.inbuf = buf.inbuf_top = data; + buf.inbuf_end = buf.inbuf_top + datalen; + buf.outobj = PyString_FromStringAndSize(NULL, datalen * 2 + 16); + if (buf.outobj == NULL) goto errorexit; + buf.outbuf = (unsigned char *)PyString_AS_STRING(buf.outobj); + buf.outbuf_end = buf.outbuf + PyString_GET_SIZE(buf.outobj); - if (multibytecodec_iencode(codec, state, &buf, errors) == -1) - goto errorexit; - /* XXX: FLUSH IT! */ + for (;;) { + int r; + size_t inleft, outleft; + + /* we don't reuse inleft and outleft here. + * error callbacks can relocate the cursor anywhere on buffer */ + inleft = (size_t)(buf.inbuf_end - buf.inbuf); + if (inleft == 0) break; + outleft = (size_t)(buf.outbuf_end - buf.outbuf); + r = codec->encode(state, &buf.inbuf, inleft, &buf.outbuf, outleft); + if (r == 0) + break; + else if (multibytecodec_encerror(codec, state, &buf, errors, r)) + goto errorexit; + else if (buf.inbuf >= buf.inbuf_end) + break; + } finalsize = (int)((char*)buf.outbuf - PyString_AS_STRING(buf.outobj)); @@ -496,32 +475,6 @@ return NULL; } -static int -multibytecodec_idecode(PyMultibyteCodec *codec, - PyMultibyteCodec_State *state, - MultibyteDecodeBuffer *buf, - PyObject *errors) -{ - for (;;) { - size_t inleft, outleft; - int r; - - inleft = (size_t)(buf->inbuf_end - buf->inbuf); - if (inleft == 0) return 0; - outleft = (size_t)(buf->outbuf_end - buf->outbuf); - - r = codec->decode(state, &buf->inbuf, inleft, &buf->outbuf, outleft); - if (r == 0) - return 0; - else if (multibytecodec_decerror(codec, state, buf, errors, r)) - return -1; - else if (buf->inbuf >= buf->inbuf_end) - return 0; - } - - return 0; -} - static PyObject * MultibyteCodec_Decode(PyMultibyteCodecObject *self, PyObject *args, PyObject *kwargs) @@ -556,10 +509,22 @@ buf.outbuf_end = buf.outbuf + PyUnicode_GET_SIZE(buf.outobj); state.p = NULL; - r = multibytecodec_idecode(self->codec, &state, &buf, errorcb); - /* XXX: FLUSH IT! */ - if (r != 0) - goto errorexit; + for (;;) { + size_t inleft, outleft; + int r; + + inleft = (size_t)(bufinbuf_end - bufinbuf); + if (inleft == 0) break; + outleft = (size_t)(bufoutbuf_end - bufoutbuf); + + r = codec->decode(&state, &bufinbuf, inleft, &bufoutbuf, outleft); + if (r == 0) + break; + else if (multibytecodec_decerror(codec, &state, &buf, errors, r)) + goto errorexit; + else if (bufinbuf >= bufinbuf_end) + break; + } finalsize = (int)(buf.outbuf - PyUnicode_AS_UNICODE(buf.outobj)); |