Index: Objects/unicodeobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v retrieving revision 2.71 diff -u -r2.71 unicodeobject.c --- Objects/unicodeobject.c 2001年01月03日 21:29:14 2.71 +++ Objects/unicodeobject.c 2001年01月04日 17:34:13 @@ -2192,13 +2192,15 @@ { PyUnicodeObject *v; Py_UNICODE *p; + int cbAllocated = size; + int cbWritten = 0; if (mapping == NULL) { PyErr_BadArgument(); return NULL; } - /* Output will never be longer than input */ + /* Our first guess for the output size is the input size */ v = _PyUnicode_New(size); if (v == NULL) goto onError; @@ -2219,7 +2221,16 @@ if (PyErr_ExceptionMatches(PyExc_LookupError)) { /* No mapping found: default to 1-1 mapping */ PyErr_Clear(); - *p++ = ch; + if ((cbWritten+1)>= cbAllocated) { + /* We need more space */ + cbAllocated = 2*(cbAllocated+1); + if (_PyUnicode_Resize(v, cbAllocated)) { + Py_DECREF(x); + goto onError; + } + p = PyUnicode_AS_UNICODE(v); + } + p[cbWritten++] = ch; continue; } goto onError; @@ -2227,7 +2238,18 @@ /* Apply mapping */ if (PyInt_Check(x)) - *p++ = (Py_UNICODE)PyInt_AS_LONG(x); + { + if ((cbWritten+1)>= cbAllocated) { + /* We need more space */ + cbAllocated = 2*(cbAllocated+1); + if (_PyUnicode_Resize(v, cbAllocated)) { + Py_DECREF(x); + goto onError; + } + p = PyUnicode_AS_UNICODE(v); + } + p[cbWritten++] = (Py_UNICODE)PyInt_AS_LONG(x); + } else if (x == Py_None) { /* undefined mapping */ if (translate_error(&s, &p, errors, @@ -2237,26 +2259,29 @@ } } else if (PyUnicode_Check(x)) { - if (PyUnicode_GET_SIZE(x) != 1) { - /* 1-n mapping */ - PyErr_SetString(PyExc_NotImplementedError, - "1-n mappings are currently not implemented"); - Py_DECREF(x); - goto onError; + int replaceSize = PyUnicode_GET_SIZE(x); + if ((cbWritten+replaceSize)>= cbAllocated) { + /* We need more space */ + cbAllocated = 2*(cbAllocated+replaceSize); + if (_PyUnicode_Resize(v, cbAllocated)) { + Py_DECREF(x); + goto onError; + } + p = PyUnicode_AS_UNICODE(v); } - *p++ = *PyUnicode_AS_UNICODE(x); + Py_UNICODE_COPY(p + cbWritten, PyUnicode_AS_UNICODE(x), replaceSize); + cbWritten += replaceSize; } else { /* wrong return value */ PyErr_SetString(PyExc_TypeError, "translate mapping must return integer, None or unicode"); - Py_DECREF(x); goto onError; } Py_DECREF(x); } - if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) - if (_PyUnicode_Resize(v, (int)(p - PyUnicode_AS_UNICODE(v)))) + if (cbWritten

AltStyle によって変換されたページ (->オリジナル) /