summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2011-09-28 05:41:54 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2011-09-28 05:41:54 (GMT)
commitd63a3b8beb4a0841cb59fb3515347ccaab34b733 (patch)
tree3b4e3cc63151c5a5a910c3550a190aefaea96ad4 /Objects
parent48d49497c50e79d14e9df9527d766ca3a0a38be5 (diff)
downloadcpython-d63a3b8beb4a0841cb59fb3515347ccaab34b733.zip
cpython-d63a3b8beb4a0841cb59fb3515347ccaab34b733.tar.gz
cpython-d63a3b8beb4a0841cb59fb3515347ccaab34b733.tar.bz2
Implement PEP 393.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/abstract.c4
-rw-r--r--Objects/bytearrayobject.c145
-rw-r--r--Objects/bytesobject.c127
-rw-r--r--Objects/codeobject.c15
-rw-r--r--Objects/complexobject.c19
-rw-r--r--Objects/dictobject.c20
-rw-r--r--Objects/exceptions.c26
-rw-r--r--Objects/fileobject.c17
-rw-r--r--Objects/floatobject.c19
-rw-r--r--Objects/longobject.c82
-rw-r--r--Objects/moduleobject.c9
-rw-r--r--Objects/object.c10
-rw-r--r--Objects/setobject.c40
-rw-r--r--Objects/stringlib/count.h9
-rw-r--r--Objects/stringlib/eq.h23
-rw-r--r--Objects/stringlib/fastsearch.h4
-rw-r--r--Objects/stringlib/find.h31
-rw-r--r--Objects/stringlib/formatter.h1516
-rw-r--r--Objects/stringlib/localeutil.h27
-rw-r--r--Objects/stringlib/partition.h12
-rw-r--r--Objects/stringlib/split.h26
-rw-r--r--Objects/stringlib/stringdefs.h2
-rw-r--r--Objects/stringlib/ucs1lib.h35
-rw-r--r--Objects/stringlib/ucs2lib.h34
-rw-r--r--Objects/stringlib/ucs4lib.h34
-rw-r--r--Objects/stringlib/undef.h10
-rw-r--r--Objects/stringlib/unicode_format.h (renamed from Objects/stringlib/string_format.h)385
-rw-r--r--Objects/stringlib/unicodedefs.h2
-rw-r--r--Objects/typeobject.c18
-rw-r--r--Objects/unicodeobject.c5796
-rw-r--r--Objects/uniops.h91
31 files changed, 4790 insertions, 3798 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 8298fd4..9105769 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -1379,9 +1379,7 @@ PyNumber_Long(PyObject *o)
PyBytes_GET_SIZE(o));
if (PyUnicode_Check(o))
/* The above check is done in PyLong_FromUnicode(). */
- return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
- PyUnicode_GET_SIZE(o),
- 10);
+ return PyLong_FromUnicodeObject(o, 10);
if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
return long_from_string(buffer, buffer_len);
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 11a0101..d294cd6 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -854,83 +854,79 @@ bytearray_repr(PyByteArrayObject *self)
const char *quote_prefix = "bytearray(b";
const char *quote_postfix = ")";
Py_ssize_t length = Py_SIZE(self);
- /* 14 == strlen(quote_prefix) + 2 + strlen(quote_postfix) */
+ /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
size_t newsize;
PyObject *v;
- if (length > (PY_SSIZE_T_MAX - 14) / 4) {
+ register Py_ssize_t i;
+ register char c;
+ register char *p;
+ int quote;
+ char *test, *start;
+ char *buffer;
+
+ if (length > (PY_SSIZE_T_MAX - 15) / 4) {
PyErr_SetString(PyExc_OverflowError,
"bytearray object is too large to make repr");
return NULL;
}
- newsize = 14 + 4 * length;
- v = PyUnicode_FromUnicode(NULL, newsize);
- if (v == NULL) {
+
+ newsize = 15 + length * 4;
+ buffer = PyMem_Malloc(newsize);
+ if (buffer == NULL) {
+ PyErr_NoMemory();
return NULL;
}
- else {
- register Py_ssize_t i;
- register Py_UNICODE c;
- register Py_UNICODE *p;
- int quote;
-
- /* Figure out which quote to use; single is preferred */
- quote = '\'';
- {
- char *test, *start;
- start = PyByteArray_AS_STRING(self);
- for (test = start; test < start+length; ++test) {
- if (*test == '"') {
- quote = '\''; /* back to single */
- goto decided;
- }
- else if (*test == '\'')
- quote = '"';
- }
- decided:
- ;
- }
- p = PyUnicode_AS_UNICODE(v);
- while (*quote_prefix)
- *p++ = *quote_prefix++;
- *p++ = quote;
-
- for (i = 0; i < length; i++) {
- /* There's at least enough room for a hex escape
- and a closing quote. */
- assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5);
- c = self->ob_bytes[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 == 0)
- *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
- else if (c < ' ' || c >= 0x7f) {
- *p++ = '\\';
- *p++ = 'x';
- *p++ = hexdigits[(c & 0xf0) >> 4];
- *p++ = hexdigits[c & 0xf];
- }
- else
- *p++ = c;
- }
- assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1);
- *p++ = quote;
- while (*quote_postfix) {
- *p++ = *quote_postfix++;
+ /* Figure out which quote to use; single is preferred */
+ quote = '\'';
+ start = PyByteArray_AS_STRING(self);
+ for (test = start; test < start+length; ++test) {
+ if (*test == '"') {
+ quote = '\''; /* back to single */
+ break;
}
- *p = '\0';
- if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) {
- Py_DECREF(v);
- return NULL;
+ else if (*test == '\'')
+ quote = '"';
+ }
+
+ p = buffer;
+ while (*quote_prefix)
+ *p++ = *quote_prefix++;
+ *p++ = quote;
+
+ for (i = 0; i < length; i++) {
+ /* There's at least enough room for a hex escape
+ and a closing quote. */
+ assert(newsize - (p - buffer) >= 5);
+ c = self->ob_bytes[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 == 0)
+ *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
+ else if (c < ' ' || c >= 0x7f) {
+ *p++ = '\\';
+ *p++ = 'x';
+ *p++ = hexdigits[(c & 0xf0) >> 4];
+ *p++ = hexdigits[c & 0xf];
}
- return v;
+ else
+ *p++ = c;
+ }
+ assert(newsize - (p - buffer) >= 1);
+ *p++ = quote;
+ while (*quote_postfix) {
+ *p++ = *quote_postfix++;
}
+
+ v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
+ PyMem_Free(buffer);
+ return v;
}
static PyObject *
@@ -1034,6 +1030,8 @@ bytearray_dealloc(PyByteArrayObject *self)
/* -------------------------------------------------------------------- */
/* Methods */
+#define FASTSEARCH fastsearch
+#define STRINGLIB(F) stringlib_##F
#define STRINGLIB_CHAR char
#define STRINGLIB_LEN PyByteArray_GET_SIZE
#define STRINGLIB_STR PyByteArray_AS_STRING
@@ -2651,15 +2649,20 @@ bytearray_fromhex(PyObject *cls, PyObject *args)
{
PyObject *newbytes, *hexobj;
char *buf;
- Py_UNICODE *hex;
Py_ssize_t hexlen, byteslen, i, j;
int top, bot;
+ void *data;
+ unsigned int kind;
if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj))
return NULL;
assert(PyUnicode_Check(hexobj));
- hexlen = PyUnicode_GET_SIZE(hexobj);
- hex = PyUnicode_AS_UNICODE(hexobj);
+ if (PyUnicode_READY(hexobj))
+ return NULL;
+ kind = PyUnicode_KIND(hexobj);
+ data = PyUnicode_DATA(hexobj);
+ hexlen = PyUnicode_GET_LENGTH(hexobj);
+
byteslen = hexlen/2; /* This overestimates if there are spaces */
newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
if (!newbytes)
@@ -2667,12 +2670,12 @@ bytearray_fromhex(PyObject *cls, PyObject *args)
buf = PyByteArray_AS_STRING(newbytes);
for (i = j = 0; i < hexlen; i += 2) {
/* skip over spaces in the input */
- while (hex[i] == ' ')
+ while (PyUnicode_READ(kind, data, i) == ' ')
i++;
if (i >= hexlen)
break;
- top = hex_digit_to_int(hex[i]);
- bot = hex_digit_to_int(hex[i+1]);
+ top = hex_digit_to_int(PyUnicode_READ(kind, data, i));
+ bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1));
if (top == -1 || bot == -1) {
PyErr_Format(PyExc_ValueError,
"non-hexadecimal number found in "
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index d7f9981..b77d693 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -566,74 +566,68 @@ PyBytes_Repr(PyObject *obj, int smartquotes)
{
static const char *hexdigits = "0123456789abcdef";
register PyBytesObject* op = (PyBytesObject*) obj;
- Py_ssize_t length = Py_SIZE(op);
- size_t newsize;
+ Py_ssize_t i, length = Py_SIZE(op);
+ size_t newsize, squotes, dquotes;
PyObject *v;
- if (length > (PY_SSIZE_T_MAX - 3) / 4) {
+ unsigned char quote, *s, *p;
+
+ /* Compute size of output string */
+ squotes = dquotes = 0;
+ newsize = 3; /* b'' */
+ s = (unsigned char*)op->ob_sval;
+ for (i = 0; i < length; i++) {
+ switch(s[i]) {
+ case '\'': squotes++; newsize++; break;
+ case '"': dquotes++; newsize++; break;
+ case '\\': case '\t': case '\n': case '\r':
+ newsize += 2; break; /* \C */
+ default:
+ if (s[i] < ' ' || s[i] >= 0x7f)
+ newsize += 4; /* \xHH */
+ else
+ newsize++;
+ }
+ }
+ quote = '\'';
+ if (smartquotes && squotes && !dquotes)
+ quote = '"';
+ if (squotes && quote == '\'')
+ newsize += squotes;
+
+ if (newsize > (PY_SSIZE_T_MAX - sizeof(PyUnicodeObject) - 1)) {
PyErr_SetString(PyExc_OverflowError,
"bytes object is too large to make repr");
return NULL;
}
- newsize = 3 + 4 * length;
- v = PyUnicode_FromUnicode(NULL, newsize);
+
+ v = PyUnicode_New(newsize, 127);
if (v == NULL) {
return NULL;
}
- else {
- register Py_ssize_t i;
- register Py_UNICODE c;
- register Py_UNICODE *p = PyUnicode_AS_UNICODE(v);
- int quote;
-
- /* Figure out which quote to use; single is preferred */
- quote = '\'';
- if (smartquotes) {
- char *test, *start;
- start = PyBytes_AS_STRING(op);
- for (test = start; test < start+length; ++test) {
- if (*test == '"') {
- quote = '\''; /* back to single */
- goto decided;
- }
- else if (*test == '\'')
- quote = '"';
- }
- decided:
- ;
- }
-
- *p++ = 'b', *p++ = quote;
- for (i = 0; i < length; i++) {
- /* There's at least enough room for a hex escape
- and a closing quote. */
- assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5);
- c = op->ob_sval[i];
- if (c == quote || 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;
- }
- assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1);
- *p++ = quote;
- *p = '\0';
- if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) {
- Py_DECREF(v);
- return NULL;
+ p = PyUnicode_1BYTE_DATA(v);
+
+ *p++ = 'b', *p++ = quote;
+ for (i = 0; i < length; i++) {
+ unsigned char c = op->ob_sval[i];
+ if (c == quote || 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];
}
- return v;
+ else
+ *p++ = c;
}
+ *p++ = quote;
+ return v;
}
static PyObject *
@@ -2356,15 +2350,20 @@ bytes_fromhex(PyObject *cls, PyObject *args)
{
PyObject *newstring, *hexobj;
char *buf;
- Py_UNICODE *hex;
Py_ssize_t hexlen, byteslen, i, j;
int top, bot;
+ void *data;
+ unsigned int kind;
if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj))
return NULL;
assert(PyUnicode_Check(hexobj));
- hexlen = PyUnicode_GET_SIZE(hexobj);
- hex = PyUnicode_AS_UNICODE(hexobj);
+ if (PyUnicode_READY(hexobj))
+ return NULL;
+ kind = PyUnicode_KIND(hexobj);
+ data = PyUnicode_DATA(hexobj);
+ hexlen = PyUnicode_GET_LENGTH(hexobj);
+
byteslen = hexlen/2; /* This overestimates if there are spaces */
newstring = PyBytes_FromStringAndSize(NULL, byteslen);
if (!newstring)
@@ -2372,12 +2371,12 @@ bytes_fromhex(PyObject *cls, PyObject *args)
buf = PyBytes_AS_STRING(newstring);
for (i = j = 0; i < hexlen; i += 2) {
/* skip over spaces in the input */
- while (hex[i] == ' ')
+ while (PyUnicode_READ(kind, data, i) == ' ')
i++;
if (i >= hexlen)
break;
- top = hex_digit_to_int(hex[i]);
- bot = hex_digit_to_int(hex[i+1]);
+ top = hex_digit_to_int(PyUnicode_READ(kind, data, i));
+ bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1));
if (top == -1 || bot == -1) {
PyErr_Format(PyExc_ValueError,
"non-hexadecimal number found in "
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 3f77718..0489c7b 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -8,19 +8,24 @@
/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
static int
-all_name_chars(Py_UNICODE *s)
+all_name_chars(PyObject *o)
{
static char ok_name_char[256];
static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
+ PyUnicodeObject *u = (PyUnicodeObject *)o;
+ const unsigned char *s;
+
+ if (!PyUnicode_Check(o) || PyUnicode_READY(u) == -1 ||
+ PyUnicode_MAX_CHAR_VALUE(u) >= 128)
+ return 0;
if (ok_name_char[*name_chars] == 0) {
unsigned char *p;
for (p = name_chars; *p; p++)
ok_name_char[*p] = 1;
}
+ s = PyUnicode_1BYTE_DATA(u);
while (*s) {
- if (*s >= 128)
- return 0;
if (ok_name_char[*s++] == 0)
return 0;
}
@@ -77,9 +82,7 @@ PyCode_New(int argcount, int kwonlyargcount,
/* Intern selected string constants */
for (i = PyTuple_GET_SIZE(consts); --i >= 0; ) {
PyObject *v = PyTuple_GetItem(consts, i);
- if (!PyUnicode_Check(v))
- continue;
- if (!all_name_chars(PyUnicode_AS_UNICODE(v)))
+ if (!all_name_chars(v))
continue;
PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
}
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 85c1d22..1e61b96 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -702,9 +702,8 @@ complex__format__(PyObject* self, PyObject* args)
if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
return NULL;
- return _PyComplex_FormatAdvanced(self,
- PyUnicode_AS_UNICODE(format_spec),
- PyUnicode_GET_SIZE(format_spec));
+ return _PyComplex_FormatAdvanced(self, format_spec, 0,
+ PyUnicode_GET_LENGTH(format_spec));
}
#if 0
@@ -755,20 +754,10 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
Py_ssize_t len;
if (PyUnicode_Check(v)) {
- Py_ssize_t i, buflen = PyUnicode_GET_SIZE(v);
- Py_UNICODE *bufptr;
- s_buffer = PyUnicode_TransformDecimalToASCII(
- PyUnicode_AS_UNICODE(v), buflen);
+ s_buffer = _PyUnicode_TransformDecimalAndSpaceToASCII(v);
if (s_buffer == NULL)
return NULL;
- /* Replace non-ASCII whitespace with ' ' */
- bufptr = PyUnicode_AS_UNICODE(s_buffer);
- for (i = 0; i < buflen; i++) {
- Py_UNICODE ch = bufptr[i];
- if (ch > 127 && Py_UNICODE_ISSPACE(ch))
- bufptr[i] = ' ';
- }
- s = _PyUnicode_AsStringAndSize(s_buffer, &len);
+ s = PyUnicode_AsUTF8AndSize(s_buffer, &len);
if (s == NULL)
goto error;
}
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index e76e508..c4265da 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -710,7 +710,7 @@ PyDict_GetItem(PyObject *op, PyObject *key)
if (!PyDict_Check(op))
return NULL;
if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1)
+ (hash = ((PyASCIIObject *) key)->hash) == -1)
{
hash = PyObject_Hash(key);
if (hash == -1) {
@@ -762,7 +762,7 @@ PyDict_GetItemWithError(PyObject *op, PyObject *key)
return NULL;
}
if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1)
+ (hash = ((PyASCIIObject *) key)->hash) == -1)
{
hash = PyObject_Hash(key);
if (hash == -1) {
@@ -797,7 +797,7 @@ PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
assert(value);
mp = (PyDictObject *)op;
if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1)
+ (hash = ((PyASCIIObject *) key)->hash) == -1)
{
hash = PyObject_Hash(key);
if (hash == -1)
@@ -842,7 +842,7 @@ PyDict_DelItem(PyObject *op, PyObject *key)
}
assert(key);
if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ (hash = ((PyASCIIObject *) key)->hash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return -1;
@@ -1122,7 +1122,7 @@ dict_subscript(PyDictObject *mp, register PyObject *key)
PyDictEntry *ep;
assert(mp->ma_table != NULL);
if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ (hash = ((PyASCIIObject *) key)->hash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return NULL;
@@ -1726,7 +1726,7 @@ dict_contains(register PyDictObject *mp, PyObject *key)
PyDictEntry *ep;
if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ (hash = ((PyASCIIObject *) key)->hash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return NULL;
@@ -1750,7 +1750,7 @@ dict_get(register PyDictObject *mp, PyObject *args)
return NULL;
if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ (hash = ((PyASCIIObject *) key)->hash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return NULL;
@@ -1779,7 +1779,7 @@ dict_setdefault(register PyDictObject *mp, PyObject *args)
return NULL;
if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ (hash = ((PyASCIIObject *) key)->hash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return NULL;
@@ -1824,7 +1824,7 @@ dict_pop(PyDictObject *mp, PyObject *args)
return NULL;
}
if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ (hash = ((PyASCIIObject *) key)->hash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return NULL;
@@ -2033,7 +2033,7 @@ PyDict_Contains(PyObject *op, PyObject *key)
PyDictEntry *ep;
if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ (hash = ((PyASCIIObject *) key)->hash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return -1;
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index fb7864f..703e72e 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -962,21 +962,18 @@ SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
static PyObject*
my_basename(PyObject *name)
{
- Py_UNICODE *unicode;
Py_ssize_t i, size, offset;
-
- unicode = PyUnicode_AS_UNICODE(name);
- size = PyUnicode_GET_SIZE(name);
+ int kind = PyUnicode_KIND(name);
+ void *data = PyUnicode_DATA(name);
+ size = PyUnicode_GET_LENGTH(name);
offset = 0;
for(i=0; i < size; i++) {
- if (unicode[i] == SEP)
+ if (PyUnicode_READ(kind, data, i) == SEP)
offset = i + 1;
}
- if (offset != 0) {
- return PyUnicode_FromUnicode(
- PyUnicode_AS_UNICODE(name) + offset,
- size - offset);
- } else {
+ if (offset != 0)
+ return PyUnicode_Substring(name, offset, size);
+ else {
Py_INCREF(name);
return name;
}
@@ -1712,6 +1709,7 @@ static PyTypeObject _PyExc_UnicodeTranslateError = {
};
PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
+/* Deprecated. */
PyObject *
PyUnicodeTranslateError_Create(
const Py_UNICODE *object, Py_ssize_t length,
@@ -1721,6 +1719,14 @@ PyUnicodeTranslateError_Create(
object, length, start, end, reason);
}
+PyObject *
+_PyUnicodeTranslateError_Create(
+ PyObject *object,
+ Py_ssize_t start, Py_ssize_t end, const char *reason)
+{
+ return PyObject_CallFunction(PyExc_UnicodeTranslateError, "Ons",
+ object, start, end, reason);
+}
/*
* AssertionError extends Exception
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index cffa5de..6421543 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -103,23 +103,18 @@ PyFile_GetLine(PyObject *f, int n)
}
}
if (n < 0 && result != NULL && PyUnicode_Check(result)) {
- Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
- Py_ssize_t len = PyUnicode_GET_SIZE(result);
+ Py_ssize_t len = PyUnicode_GET_LENGTH(result);
if (len == 0) {
Py_DECREF(result);
result = NULL;
PyErr_SetString(PyExc_EOFError,
"EOF when reading a line");
}
- else if (s[len-1] == '\n') {
- if (result->ob_refcnt == 1)
- PyUnicode_Resize(&result, len-1);
- else {
- PyObject *v;
- v = PyUnicode_FromUnicode(s, len-1);
- Py_DECREF(result);
- result = v;
- }
+ else if (PyUnicode_READ_CHAR(result, len-1) == '\n') {
+ PyObject *v;
+ v = PyUnicode_Substring(result, 0, len-1);
+ Py_DECREF(result);
+ result = v;
}
}
return result;
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index a031d1b..1c8a6a3 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -174,20 +174,10 @@ PyFloat_FromString(PyObject *v)
PyObject *result = NULL;
if (PyUnicode_Check(v)) {
- Py_ssize_t i, buflen = PyUnicode_GET_SIZE(v);
- Py_UNICODE *bufptr;
- s_buffer = PyUnicode_TransformDecimalToASCII(
- PyUnicode_AS_UNICODE(v), buflen);
+ s_buffer = _PyUnicode_TransformDecimalAndSpaceToASCII(v);
if (s_buffer == NULL)
return NULL;
- /* Replace non-ASCII whitespace with ' ' */
- bufptr = PyUnicode_AS_UNICODE(s_buffer);
- for (i = 0; i < buflen; i++) {
- Py_UNICODE ch = bufptr[i];
- if (ch > 127 && Py_UNICODE_ISSPACE(ch))
- bufptr[i] = ' ';
- }
- s = _PyUnicode_AsStringAndSize(s_buffer, &len);
+ s = PyUnicode_AsUTF8AndSize(s_buffer, &len);
if (s == NULL) {
Py_DECREF(s_buffer);
return NULL;
@@ -1741,9 +1731,8 @@ float__format__(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
return NULL;
- return _PyFloat_FormatAdvanced(self,
- PyUnicode_AS_UNICODE(format_spec),
- PyUnicode_GET_SIZE(format_spec));
+ return _PyFloat_FormatAdvanced(self, format_spec, 0,
+ PyUnicode_GET_LENGTH(format_spec));
}
PyDoc_STRVAR(float__format__doc,
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 5a4259d..85b3353 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -1551,7 +1551,7 @@ long_to_decimal_string(PyObject *aa)
PyObject *str;
Py_ssize_t size, strlen, size_a, i, j;
digit *pout, *pin, rem, tenpow;
- Py_UNICODE *p;
+ unsigned char *p;
int negative;
a = (PyLongObject *)aa;
@@ -1619,14 +1619,15 @@ long_to_decimal_string(PyObject *aa)
tenpow *= 10;
strlen++;
}
- str = PyUnicode_FromUnicode(NULL, strlen);
+ str = PyUnicode_New(strlen, '9');
if (str == NULL) {
Py_DECREF(scratch);
return NULL;
}
/* fill the string right-to-left */
- p = PyUnicode_AS_UNICODE(str) + strlen;
+ assert(PyUnicode_KIND(str) == PyUnicode_1BYTE_KIND);
+ p = PyUnicode_1BYTE_DATA(str) + strlen;
*p = '\0';
/* pout[0] through pout[size-2] contribute exactly
_PyLong_DECIMAL_SHIFT digits each */
@@ -1649,7 +1650,7 @@ long_to_decimal_string(PyObject *aa)
*--p = '-';
/* check we've counted correctly */
- assert(p == PyUnicode_AS_UNICODE(str));
+ assert(p == PyUnicode_1BYTE_DATA(str));
Py_DECREF(scratch);
return (PyObject *)str;
}
@@ -1662,10 +1663,12 @@ PyObject *
_PyLong_Format(PyObject *aa, int base)
{
register PyLongObject *a = (PyLongObject *)aa;
- PyObject *str;
+ PyObject *v;
Py_ssize_t i, sz;
Py_ssize_t size_a;
- Py_UNICODE *p, sign = '\0';
+ char *p;
+ char sign = '\0';
+ char *buffer;
int bits;
assert(base == 2 || base == 8 || base == 10 || base == 16);
@@ -1695,7 +1698,7 @@ _PyLong_Format(PyObject *aa, int base)
}
/* compute length of output string: allow 2 characters for prefix and
1 for possible '-' sign. */
- if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT) {
+ if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT / sizeof(Py_UCS4)) {
PyErr_SetString(PyExc_OverflowError,
"int is too large to format");
return NULL;
@@ -1704,11 +1707,12 @@ _PyLong_Format(PyObject *aa, int base)
is safe from overflow */
sz = 3 + (size_a * PyLong_SHIFT + (bits - 1)) / bits;
assert(sz >= 0);
- str = PyUnicode_FromUnicode(NULL, sz);
- if (str == NULL)
+ buffer = PyMem_Malloc(sz);
+ if (buffer == NULL) {
+ PyErr_NoMemory();
return NULL;
- p = PyUnicode_AS_UNICODE(str) + sz;
- *p = '\0';
+ }
+ p = &buffer[sz];
if (Py_SIZE(a) < 0)
sign = '-';
@@ -1724,10 +1728,10 @@ _PyLong_Format(PyObject *aa, int base)
accumbits += PyLong_SHIFT;
assert(accumbits >= bits);
do {
- Py_UNICODE cdigit;
- cdigit = (Py_UNICODE)(accum & (base - 1));
+ char cdigit;
+ cdigit = (char)(accum & (base - 1));
cdigit += (cdigit < 10) ? '0' : 'a'-10;
- assert(p > PyUnicode_AS_UNICODE(str));
+ assert(p > buffer);
*--p = cdigit;
accumbits -= bits;
accum >>= bits;
@@ -1744,19 +1748,9 @@ _PyLong_Format(PyObject *aa, int base)
*--p = '0';
if (sign)
*--p = sign;
- if (p != PyUnicode_AS_UNICODE(str)) {
- Py_UNICODE *q = PyUnicode_AS_UNICODE(str);
- assert(p > q);
- do {
- } while ((*q++ = *p++) != '\0');
- q--;
- if (PyUnicode_Resize(&str,(Py_ssize_t) (q -
- PyUnicode_AS_UNICODE(str)))) {
- Py_DECREF(str);
- return NULL;
- }
- }
- return (PyObject *)str;
+ v = PyUnicode_DecodeASCII(p, &buffer[sz] - p, NULL);
+ PyMem_Free(buffer);
+ return v;
}
/* Table of digit values for 8-bit string -> integer conversion.
@@ -2134,23 +2128,26 @@ digit beyond the first.
PyObject *
PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
{
+ PyObject *v, *unicode = PyUnicode_FromUnicode(u, length);
+ if (unicode == NULL)
+ return NULL;
+ v = PyLong_FromUnicodeObject(unicode, base);
+ Py_DECREF(unicode);
+ return v;
+}
+
+PyObject *
+PyLong_FromUnicodeObject(PyObject *u, int base)
+{
PyObject *result;
PyObject *asciidig;
char *buffer, *end;
- Py_ssize_t i, buflen;
- Py_UNICODE *ptr;
+ Py_ssize_t buflen;
- asciidig = PyUnicode_TransformDecimalToASCII(u, length);
+ asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII(u);
if (asciidig == NULL)
return NULL;
- /* Replace non-ASCII whitespace with ' ' */
- ptr = PyUnicode_AS_UNICODE(asciidig);
- for (i = 0; i < length; i++) {
- Py_UNICODE ch = ptr[i];
- if (ch > 127 && Py_UNICODE_ISSPACE(ch))
- ptr[i] = ' ';
- }
- buffer = _PyUnicode_AsStringAndSize(asciidig, &buflen);
+ buffer = PyUnicode_AsUTF8AndSize(asciidig, &buflen);
if (buffer == NULL) {
Py_DECREF(asciidig);
return NULL;
@@ -4144,9 +4141,7 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
if (PyUnicode_Check(x))
- return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
- PyUnicode_GET_SIZE(x),
- (int)base);
+ return PyLong_FromUnicodeObject(x, (int)base);
else if (PyByteArray_Check(x) || PyBytes_Check(x)) {
/* Since PyLong_FromString doesn't have a length parameter,
* check here for possible NULs in the string. */
@@ -4228,9 +4223,8 @@ long__format__(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
return NULL;
- return _PyLong_FormatAdvanced(self,
- PyUnicode_AS_UNICODE(format_spec),
- PyUnicode_GET_SIZE(format_spec));
+ return _PyLong_FormatAdvanced(self, format_spec, 0,
+ PyUnicode_GET_LENGTH(format_spec));
}
/* Return a pair (q, r) such that a = b * q + r, and
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index 3817ef3..4b4f9d1 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -285,8 +285,8 @@ _PyModule_Clear(PyObject *m)
pos = 0;
while (PyDict_Next(d, &pos, &key, &value)) {
if (value != Py_None && PyUnicode_Check(key)) {
- Py_UNICODE *u = PyUnicode_AS_UNICODE(key);
- if (u[0] == '_' && u[1] != '_') {
+ if (PyUnicode_READ_CHAR(key, 0) == '_' &&
+ PyUnicode_READ_CHAR(key, 1) != '_') {
if (Py_VerboseFlag > 1) {
const char *s = _PyUnicode_AsString(key);
if (s != NULL)
@@ -303,9 +303,8 @@ _PyModule_Clear(PyObject *m)
pos = 0;
while (PyDict_Next(d, &pos, &key, &value)) {
if (value != Py_None && PyUnicode_Check(key)) {
- Py_UNICODE *u = PyUnicode_AS_UNICODE(key);
- if (u[0] != '_'
- || PyUnicode_CompareWithASCIIString(key, "__builtins__") != 0)
+ if (PyUnicode_READ_CHAR(key, 0) != '_' ||
+ PyUnicode_CompareWithASCIIString(key, "__builtins__") != 0)
{
if (Py_VerboseFlag > 1) {
const char *s = _PyUnicode_AsString(key);
diff --git a/Objects/object.c b/Objects/object.c
index 52acf12..aeaa4b5 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -295,9 +295,7 @@ PyObject_Print(PyObject *op, FILE *fp, int flags)
}
else if (PyUnicode_Check(s)) {
PyObject *t;
- t = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(s),
- PyUnicode_GET_SIZE(s),
- "backslashreplace");
+ t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
if (t == NULL)
ret = 0;
else {
@@ -439,11 +437,7 @@ PyObject_ASCII(PyObject *v)
return NULL;
/* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
- ascii = PyUnicode_EncodeASCII(
- PyUnicode_AS_UNICODE(repr),
- PyUnicode_GET_SIZE(repr),
- "backslashreplace");
-
+ ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Py_DECREF(repr);
if (ascii == NULL)
return NULL;
diff --git a/Objects/setobject.c b/Objects/setobject.c
index 41df24d..34d8204 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -386,7 +386,7 @@ set_add_key(register PySetObject *so, PyObject *key)
register Py_ssize_t n_used;
if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ (hash = ((PyASCIIObject *) key)->hash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return -1;
@@ -434,7 +434,7 @@ set_discard_key(PySetObject *so, PyObject *key)
assert (PyAnySet_Check(so));
if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ (hash = ((PyASCIIObject *) key)->hash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return -1;
@@ -579,11 +579,8 @@ set_dealloc(PySetObject *so)
static PyObject *
set_repr(PySetObject *so)
{
- PyObject *keys, *result=NULL;
- Py_UNICODE *u;
+ PyObject *result=NULL, *keys, *listrepr, *tmp;
int status = Py_ReprEnter((PyObject*)so);
- PyObject *listrepr;
- Py_ssize_t newsize;
if (status != 0) {
if (status < 0)
@@ -601,31 +598,24 @@ set_repr(PySetObject *so)
if (keys == NULL)
goto done;
+ /* repr(keys)[1:-1] */
listrepr = PyObject_Repr(keys);
Py_DECREF(keys);
if (listrepr == NULL)
goto done;
- newsize = PyUnicode_GET_SIZE(listrepr);
- result = PyUnicode_FromUnicode(NULL, newsize);
- if (result == NULL)
+ tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
+ Py_DECREF(listrepr);
+ if (tmp == NULL)
goto done;
+ listrepr = tmp;
- u = PyUnicode_AS_UNICODE(result);
- *u++ = '{';
- /* Omit the brackets from the listrepr */
- Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
- newsize-2);
- u += newsize-2;
- *u++ = '}';
+ if (Py_TYPE(so) != &PySet_Type)
+ result = PyUnicode_FromFormat("%s({%U})",
+ Py_TYPE(so)->tp_name,
+ listrepr);
+ else
+ result = PyUnicode_FromFormat("{%U}", listrepr);
Py_DECREF(listrepr);
-
- if (Py_TYPE(so) != &PySet_Type) {
- PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
- Py_TYPE(so)->tp_name,
- result);
- Py_DECREF(result);
- result = tmp;
- }
done:
Py_ReprLeave((PyObject*)so);
return result;
@@ -684,7 +674,7 @@ set_contains_key(PySetObject *so, PyObject *key)
setentry *entry;
if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ (hash = ((PyASCIIObject *) key)->hash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return -1;
diff --git a/Objects/stringlib/count.h b/Objects/stringlib/count.h
index de34f96..f48500b 100644
--- a/Objects/stringlib/count.h
+++ b/Objects/stringlib/count.h
@@ -1,14 +1,11 @@
/* stringlib: count implementation */
-#ifndef STRINGLIB_COUNT_H
-#define STRINGLIB_COUNT_H
-
#ifndef STRINGLIB_FASTSEARCH_H
#error must include "stringlib/fastsearch.h" before including this module
#endif
Py_LOCAL_INLINE(Py_ssize_t)
-stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
+STRINGLIB(count)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
Py_ssize_t maxcount)
{
@@ -19,7 +16,7 @@ stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
if (sub_len == 0)
return (str_len < maxcount) ? str_len + 1 : maxcount;
- count = fastsearch(str, str_len, sub, sub_len, maxcount, FAST_COUNT);
+ count = FASTSEARCH(str, str_len, sub, sub_len, maxcount, FAST_COUNT);
if (count < 0)
return 0; /* no match */
@@ -27,4 +24,4 @@ stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
return count;
}
-#endif
+
diff --git a/Objects/stringlib/eq.h b/Objects/stringlib/eq.h
index 3e7f5e8..dd67128 100644
--- a/Objects/stringlib/eq.h
+++ b/Objects/stringlib/eq.h
@@ -9,13 +9,26 @@ unicode_eq(PyObject *aa, PyObject *bb)
register PyUnicodeObject *a = (PyUnicodeObject *)aa;
register PyUnicodeObject *b = (PyUnicodeObject *)bb;
- if (a->length != b->length)
+ if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
+ assert(0 && "unicode_eq ready fail");
return 0;
- if (a->length == 0)
+ }
+
+ if (PyUnicode_GET_LENGTH(a) != PyUnicode_GET_LENGTH(b))
+ return 0;
+ if (PyUnicode_GET_LENGTH(a) == 0)
return 1;
- if (a->str[0] != b->str[0])
+ if (PyUnicode_KIND(a) != PyUnicode_KIND(b))
+ return 0;
+ /* Just comparing the first byte is enough to see if a and b differ.
+ * If they are 2 byte or 4 byte character most differences will happen in
+ * the lower bytes anyways.
+ */
+ if (PyUnicode_1BYTE_DATA(a)[0] != PyUnicode_1BYTE_DATA(b)[0])
return 0;
- if (a->length == 1)
+ if (PyUnicode_KIND(a) == PyUnicode_1BYTE_KIND &&
+ PyUnicode_GET_LENGTH(a) == 1)
return 1;
- return memcmp(a->str, b->str, a->length * sizeof(Py_UNICODE)) == 0;
+ return memcmp(PyUnicode_1BYTE_DATA(a), PyUnicode_1BYTE_DATA(b),
+ PyUnicode_GET_LENGTH(a) * PyUnicode_CHARACTER_SIZE(a)) == 0;
}
diff --git a/Objects/stringlib/fastsearch.h b/Objects/stringlib/fastsearch.h
index e231c58..d35cba3 100644
--- a/Objects/stringlib/fastsearch.h
+++ b/Objects/stringlib/fastsearch.h
@@ -1,6 +1,5 @@
/* stringlib: fastsearch implementation */
-#ifndef STRINGLIB_FASTSEARCH_H
#define STRINGLIB_FASTSEARCH_H
/* fast search/count implementation, based on a mix between boyer-
@@ -34,7 +33,7 @@
((mask & (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1)))))
Py_LOCAL_INLINE(Py_ssize_t)
-fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n,
+FASTSEARCH(const STRINGLIB_CHAR* s, Py_ssize_t n,
const STRINGLIB_CHAR* p, Py_ssize_t m,
Py_ssize_t maxcount, int mode)
{
@@ -157,4 +156,3 @@ fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n,
return count;
}
-#endif
diff --git a/Objects/stringlib/find.h b/Objects/stringlib/find.h
index ce615dc..7cce156 100644
--- a/Objects/stringlib/find.h
+++ b/Objects/stringlib/find.h
@@ -1,14 +1,11 @@
/* stringlib: find/index implementation */
-#ifndef STRINGLIB_FIND_H
-#define STRINGLIB_FIND_H
-
#ifndef STRINGLIB_FASTSEARCH_H
#error must include "stringlib/fastsearch.h" before including this module
#endif
Py_LOCAL_INLINE(Py_ssize_t)
-stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
+STRINGLIB(find)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
Py_ssize_t offset)
{
@@ -19,7 +16,7 @@ stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
if (sub_len == 0)
return offset;
- pos = fastsearch(str, str_len, sub, sub_len, -1, FAST_SEARCH);
+ pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_SEARCH);
if (pos >= 0)
pos += offset;
@@ -28,7 +25,7 @@ stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
}
Py_LOCAL_INLINE(Py_ssize_t)
-stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
+STRINGLIB(rfind)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
Py_ssize_t offset)
{
@@ -39,7 +36,7 @@ stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
if (sub_len == 0)
return str_len + offset;
- pos = fastsearch(str, str_len, sub, sub_len, -1, FAST_RSEARCH);
+ pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_RSEARCH);
if (pos >= 0)
pos += offset;
@@ -63,29 +60,29 @@ stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
}
Py_LOCAL_INLINE(Py_ssize_t)
-stringlib_find_slice(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
+STRINGLIB(find_slice)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
Py_ssize_t start, Py_ssize_t end)
{
ADJUST_INDICES(start, end, str_len);
- return stringlib_find(str + start, end - start, sub, sub_len, start);
+ return STRINGLIB(find)(str + start, end - start, sub, sub_len, start);
}
Py_LOCAL_INLINE(Py_ssize_t)
-stringlib_rfind_slice(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
+STRINGLIB(rfind_slice)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
Py_ssize_t start, Py_ssize_t end)
{
ADJUST_INDICES(start, end, str_len);
- return stringlib_rfind(str + start, end - start, sub, sub_len, start);
+ return STRINGLIB(rfind)(str + start, end - start, sub, sub_len, start);
}
#ifdef STRINGLIB_WANT_CONTAINS_OBJ
Py_LOCAL_INLINE(int)
-stringlib_contains_obj(PyObject* str, PyObject* sub)
+STRINGLIB(contains_obj)(PyObject* str, PyObject* sub)
{
- return stringlib_find(
+ return STRINGLIB(find)(
STRINGLIB_STR(str), STRINGLIB_LEN(str),
STRINGLIB_STR(sub), STRINGLIB_LEN(sub), 0
) != -1;
@@ -105,7 +102,7 @@ is ok.
#define FORMAT_BUFFER_SIZE 50
Py_LOCAL_INLINE(int)
-stringlib_parse_args_finds(const char * function_name, PyObject *args,
+STRINGLIB(parse_args_finds)(const char * function_name, PyObject *args,
PyObject **subobj,
Py_ssize_t *start, Py_ssize_t *end)
{
@@ -153,13 +150,13 @@ after finishing using the substring, must DECREF it).
*/
Py_LOCAL_INLINE(int)
-stringlib_parse_args_finds_unicode(const char * function_name, PyObject *args,
+STRINGLIB(parse_args_finds_unicode)(const char * function_name, PyObject *args,
PyUnicodeObject **substring,
Py_ssize_t *start, Py_ssize_t *end)
{
PyObject *tmp_substring;
- if(stringlib_parse_args_finds(function_name, args, &tmp_substring,
+ if(STRINGLIB(parse_args_finds)(function_name, args, &tmp_substring,
start, end)) {
tmp_substring = PyUnicode_FromObject(tmp_substring);
if (!tmp_substring)
@@ -171,5 +168,3 @@ stringlib_parse_args_finds_unicode(const char * function_name, PyObject *args,
}
#endif /* STRINGLIB_IS_UNICODE */
-
-#endif /* STRINGLIB_FIND_H */
diff --git a/Objects/stringlib/formatter.h b/Objects/stringlib/formatter.h
deleted file mode 100644
index 139b56c..0000000
--- a/Objects/stringlib/formatter.h
+++ /dev/null
@@ -1,1516 +0,0 @@
-/* implements the string, long, and float formatters. that is,
- string.__format__, etc. */
-
-#include <locale.h>
-
-/* Before including this, you must include either:
- stringlib/unicodedefs.h
- stringlib/stringdefs.h
-
- Also, you should define the names:
- FORMAT_STRING
- FORMAT_LONG
- FORMAT_FLOAT
- FORMAT_COMPLEX
- to be whatever you want the public names of these functions to
- be. These are the only non-static functions defined here.
-*/
-
-/* Raises an exception about an unknown presentation type for this
- * type. */
-
-static void
-unknown_presentation_type(STRINGLIB_CHAR presentation_type,
- const char* type_name)
-{
-#if STRINGLIB_IS_UNICODE
- /* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range,
- hence the two cases. If it is char, gcc complains that the
- condition below is always true, hence the ifdef. */
- if (presentation_type > 32 && presentation_type < 128)
-#endif
- PyErr_Format(PyExc_ValueError,
- "Unknown format code '%c' "
- "for object of type '%.200s'",
- (char)presentation_type,
- type_name);
-#if STRINGLIB_IS_UNICODE
- else
- PyErr_Format(PyExc_ValueError,
- "Unknown format code '\\x%x' "
- "for object of type '%.200s'",
- (unsigned int)presentation_type,
- type_name);
-#endif
-}
-
-static void
-invalid_comma_type(STRINGLIB_CHAR presentation_type)
-{
-#if STRINGLIB_IS_UNICODE
- /* See comment in unknown_presentation_type */
- if (presentation_type > 32 && presentation_type < 128)
-#endif
- PyErr_Format(PyExc_ValueError,
- "Cannot specify ',' with '%c'.",
- (char)presentation_type);
-#if STRINGLIB_IS_UNICODE
- else
- PyErr_Format(PyExc_ValueError,
- "Cannot specify ',' with '\\x%x'.",
- (unsigned int)presentation_type);
-#endif
-}
-
-/*
- get_integer consumes 0 or more decimal digit characters from an
- input string, updates *result with the corresponding positive
- integer, and returns the number of digits consumed.
-
- returns -1 on error.
-*/
-static int
-get_integer(STRINGLIB_CHAR **ptr, STRINGLIB_CHAR *end,
- Py_ssize_t *result)
-{
- Py_ssize_t accumulator, digitval;
- int numdigits;
- accumulator = numdigits = 0;
- for (;;(*ptr)++, numdigits++) {
- if (*ptr >= end)
- break;
- digitval = STRINGLIB_TODECIMAL(**ptr);
- if (digitval < 0)
- break;
- /*
- Detect possible overflow before it happens:
-
- accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if
- accumulator > (PY_SSIZE_T_MAX - digitval) / 10.
- */
- if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) {
- PyErr_Format(PyExc_ValueError,
- "Too many decimal digits in format string");
- return -1;
- }
- accumulator = accumulator * 10 + digitval;
- }
- *result = accumulator;
- return numdigits;
-}
-
-/************************************************************************/
-/*********** standard format specifier parsing **************************/
-/************************************************************************/
-
-/* returns true if this character is a specifier alignment token */
-Py_LOCAL_INLINE(int)
-is_alignment_token(STRINGLIB_CHAR c)
-{
- switch (c) {
- case '<': case '>': case '=': case '^':
- return 1;
- default:
- return 0;
- }
-}
-
-/* returns true if this character is a sign element */
-Py_LOCAL_INLINE(int)
-is_sign_element(STRINGLIB_CHAR c)
-{
- switch (c) {
- case ' ': case '+': case '-':
- return 1;
- default:
- return 0;
- }
-}
-
-
-typedef struct {
- STRINGLIB_CHAR fill_char;
- STRINGLIB_CHAR align;
- int alternate;
- STRINGLIB_CHAR sign;
- Py_ssize_t width;
- int thousands_separators;
- Py_ssize_t precision;
- STRINGLIB_CHAR type;
-} InternalFormatSpec;
-
-
-#if 0
-/* Occassionally useful for debugging. Should normally be commented out. */
-static void
-DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
-{
- printf("internal format spec: fill_char %d\n", format->fill_char);
- printf("internal format spec: align %d\n", format->align);
- printf("internal format spec: alternate %d\n", format->alternate);
- printf("internal format spec: sign %d\n", format->sign);
- printf("internal format spec: width %zd\n", format->width);
- printf("internal format spec: thousands_separators %d\n",
- format->thousands_separators);
- printf("internal format spec: precision %zd\n", format->precision);
- printf("internal format spec: type %c\n", format->type);
- printf("\n");
-}
-#endif
-
-
-/*
- ptr points to the start of the format_spec, end points just past its end.
- fills in format with the parsed information.
- returns 1 on success, 0 on failure.
- if failure, sets the exception
-*/
-static int
-parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
- Py_ssize_t format_spec_len,
- InternalFormatSpec *format,
- char default_type,
- char default_align)
-{
- STRINGLIB_CHAR *ptr = format_spec;
- STRINGLIB_CHAR *end = format_spec + format_spec_len;
-
- /* end-ptr is used throughout this code to specify the length of
- the input string */
-
- Py_ssize_t consumed;
- int align_specified = 0;
-
- format->fill_char = '\0';
- format->align = default_align;
- format->alternate = 0;
- format->sign = '\0';
- format->width = -1;
- format->thousands_separators = 0;
- format->precision = -1;
- format->type = default_type;
-
- /* If the second char is an alignment token,
- then parse the fill char */
- if (end-ptr >= 2 && is_alignment_token(ptr[1])) {
- format->align = ptr[1];
- format->fill_char = ptr[0];
- align_specified = 1;
- ptr += 2;
- }
- else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {
- format->align = ptr[0];
- align_specified = 1;
- ++ptr;
- }
-
- /* Parse the various sign options */
- if (end-ptr >= 1 && is_sign_element(ptr[0])) {
- format->sign = ptr[0];
- ++ptr;
- }
-
- /* If the next character is #, we're in alternate mode. This only
- applies to integers. */
- if (end-ptr >= 1 && ptr[0] == '#') {
- format->alternate = 1;
- ++ptr;
- }
-
- /* The special case for 0-padding (backwards compat) */
- if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
- format->fill_char = '0';
- if (!align_specified) {
- format->align = '=';
- }
- ++ptr;
- }
-
- consumed = get_integer(&ptr, end, &format->width);
- if (consumed == -1)
- /* Overflow error. Exception already set. */
- return 0;
-
- /* If consumed is 0, we didn't consume any characters for the
- width. In that case, reset the width to -1, because
- get_integer() will have set it to zero. -1 is how we record
- that the width wasn't specified. */
- if (consumed == 0)
- format->width = -1;
-
- /* Comma signifies add thousands separators */
- if (end-ptr && ptr[0] == ',') {
- format->thousands_separators = 1;
- ++ptr;
- }
-
- /* Parse field precision */
- if (end-ptr && ptr[0] == '.') {
- ++ptr;
-
- consumed = get_integer(&ptr, end, &format->precision);
- if (consumed == -1)
- /* Overflow error. Exception already set. */
- return 0;
-
- /* Not having a precision after a dot is an error. */
- if (consumed == 0) {
- PyErr_Format(PyExc_ValueError,
- "Format specifier missing precision");
- return 0;
- }
-
- }
-
- /* Finally, parse the type field. */
-
- if (end-ptr > 1) {
- /* More than one char remain, invalid conversion spec. */
- PyErr_Format(PyExc_ValueError, "Invalid conversion specification");
- return 0;
- }
-
- if (end-ptr == 1) {
- format->type = ptr[0];
- ++ptr;
- }
-
- /* Do as much validating as we can, just by looking at the format
- specifier. Do not take into account what type of formatting
- we're doing (int, float, string). */
-
- if (format->thousands_separators) {
- switch (format->type) {
- case 'd':
- case 'e':
- case 'f':
- case 'g':
- case 'E':
- case 'G':
- case '%':
- case 'F':
- case '\0':
- /* These are allowed. See PEP 378.*/
- break;
- default:
- invalid_comma_type(format->type);
- return 0;
- }
- }
-
- return 1;
-}
-
-/* Calculate the padding needed. */
-static void
-calc_padding(Py_ssize_t nchars, Py_ssize_t width, STRINGLIB_CHAR align,
- Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
- Py_ssize_t *n_total)
-{
- if (width >= 0) {
- if (nchars > width)
- *n_total = nchars;
- else
- *n_total = width;
- }
- else {
- /* not specified, use all of the chars and no more */
- *n_total = nchars;
- }
-
- /* Figure out how much leading space we need, based on the
- aligning */
- if (align == '>')
- *n_lpadding = *n_total - nchars;
- else if (align == '^')
- *n_lpadding = (*n_total - nchars) / 2;
- else if (align == '<' || align == '=')
- *n_lpadding = 0;
- else {
- /* We should never have an unspecified alignment. */
- *n_lpadding = 0;
- assert(0);
- }
-
- *n_rpadding = *n_total - nchars - *n_lpadding;
-}
-
-/* Do the padding, and return a pointer to where the caller-supplied
- content goes. */
-static STRINGLIB_CHAR *
-fill_padding(STRINGLIB_CHAR *p, Py_ssize_t nchars, STRINGLIB_CHAR fill_char,
- Py_ssize_t n_lpadding, Py_ssize_t n_rpadding)
-{
- /* Pad on left. */
- if (n_lpadding)
- STRINGLIB_FILL(p, fill_char, n_lpadding);
-
- /* Pad on right. */
- if (n_rpadding)
- STRINGLIB_FILL(p + nchars + n_lpadding, fill_char, n_rpadding);
-
- /* Pointer to the user content. */
- return p + n_lpadding;
-}
-
-#if defined FORMAT_FLOAT || defined FORMAT_LONG || defined FORMAT_COMPLEX
-/************************************************************************/
-/*********** common routines for numeric formatting *********************/
-/************************************************************************/
-
-/* Locale type codes. */
-#define LT_CURRENT_LOCALE 0
-#define LT_DEFAULT_LOCALE 1
-#define LT_NO_LOCALE 2
-
-/* Locale info needed for formatting integers and the part of floats
- before and including the decimal. Note that locales only support
- 8-bit chars, not unicode. */
-typedef struct {
- char *decimal_point;
- char *thousands_sep;
- char *grouping;
-} LocaleInfo;
-
-/* describes the layout for an integer, see the comment in
- calc_number_widths() for details */
-typedef struct {
- Py_ssize_t n_lpadding;
- Py_ssize_t n_prefix;
- Py_ssize_t n_spadding;
- Py_ssize_t n_rpadding;
- char sign;
- Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
- Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
- any grouping chars. */
- Py_ssize_t n_decimal; /* 0 if only an integer */
- Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
- excluding the decimal itself, if
- present. */
-
- /* These 2 are not the widths of fields, but are needed by
- STRINGLIB_GROUPING. */
- Py_ssize_t n_digits; /* The number of digits before a decimal
- or exponent. */
- Py_ssize_t n_min_width; /* The min_width we used when we computed
- the n_grouped_digits width. */
-} NumberFieldWidths;
-
-
-/* Given a number of the form:
- digits[remainder]
- where ptr points to the start and end points to the end, find where
- the integer part ends. This could be a decimal, an exponent, both,
- or neither.
- If a decimal point is present, set *has_decimal and increment
- remainder beyond it.
- Results are undefined (but shouldn't crash) for improperly
- formatted strings.
-*/
-static void
-parse_number(STRINGLIB_CHAR *ptr, Py_ssize_t len,
- Py_ssize_t *n_remainder, int *has_decimal)
-{
- STRINGLIB_CHAR *end = ptr + len;
- STRINGLIB_CHAR *remainder;
-
- while (ptr<end && isdigit(*ptr))
- ++ptr;
- remainder = ptr;
-
- /* Does remainder start with a decimal point? */
- *has_decimal = ptr<end && *remainder == '.';
-
- /* Skip the decimal point. */
- if (*has_decimal)
- remainder++;
-
- *n_remainder = end - remainder;
-}
-
-/* not all fields of format are used. for example, precision is
- unused. should this take discrete params in order to be more clear
- about what it does? or is passing a single format parameter easier
- and more efficient enough to justify a little obfuscation? */
-static Py_ssize_t
-calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
- STRINGLIB_CHAR sign_char, STRINGLIB_CHAR *number,
- Py_ssize_t n_number, Py_ssize_t n_remainder,
- int has_decimal, const LocaleInfo *locale,
- const InternalFormatSpec *format)
-{
- Py_ssize_t n_non_digit_non_padding;
- Py_ssize_t n_padding;
-
- spec->n_digits = n_number - n_remainder - (has_decimal?1:0);
- spec->n_lpadding = 0;
- spec->n_prefix = n_prefix;
- spec->n_decimal = has_decimal ? strlen(locale->decimal_point) : 0;
- spec->n_remainder = n_remainder;
- spec->n_spadding = 0;
- spec->n_rpadding = 0;
- spec->sign = '\0';
- spec->n_sign = 0;
-
- /* the output will look like:
- | |
- | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
- | |
-
- sign is computed from format->sign and the actual
- sign of the number
-
- prefix is given (it's for the '0x' prefix)
-
- digits is already known
-
- the total width is either given, or computed from the
- actual digits
-
- only one of lpadding, spadding, and rpadding can be non-zero,
- and it's calculated from the width and other fields
- */
-
- /* compute the various parts we're going to write */
- switch (format->sign) {
- case '+':
- /* always put a + or - */
- spec->n_sign = 1;
- spec->sign = (sign_char == '-' ? '-' : '+');
- break;
- case ' ':
- spec->n_sign = 1;
- spec->sign = (sign_char == '-' ? '-' : ' ');
- break;
- default:
- /* Not specified, or the default (-) */
- if (sign_char == '-') {
- spec->n_sign = 1;
- spec->sign = '-';
- }
- }
-
- /* The number of chars used for non-digits and non-padding. */
- n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
- spec->n_remainder;
-
- /* min_width can go negative, that's okay. format->width == -1 means
- we don't care. */
- if (format->fill_char == '0' && format->align == '=')
- spec->n_min_width = format->width - n_non_digit_non_padding;
- else
- spec->n_min_width = 0;
-
- if (spec->n_digits == 0)
- /* This case only occurs when using 'c' formatting, we need
- to special case it because the grouping code always wants
- to have at least one character. */
- spec->n_grouped_digits = 0;
- else
- spec->n_grouped_digits = STRINGLIB_GROUPING(NULL, 0, NULL,
- spec->n_digits,
- spec->n_min_width,
- locale->grouping,
- locale->thousands_sep);
-
- /* Given the desired width and the total of digit and non-digit
- space we consume, see if we need any padding. format->width can
- be negative (meaning no padding), but this code still works in
- that case. */
- n_padding = format->width -
- (n_non_digit_non_padding + spec->n_grouped_digits);
- if (n_padding > 0) {
- /* Some padding is needed. Determine if it's left, space, or right. */
- switch (format->align) {
- case '<':
- spec->n_rpadding = n_padding;
- break;
- case '^':
- spec->n_lpadding = n_padding / 2;
- spec->n_rpadding = n_padding - spec->n_lpadding;
- break;
- case '=':
- spec->n_spadding = n_padding;
- break;
- case '>':
- spec->n_lpadding = n_padding;
- break;
- default:
- /* Shouldn't get here, but treat it as '>' */
- spec->n_lpadding = n_padding;
- assert(0);
- break;
- }
- }
- return spec->n_lpadding + spec->n_sign + spec->n_prefix +
- spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
- spec->n_remainder + spec->n_rpadding;
-}
-
-/* Fill in the digit parts of a numbers's string representation,
- as determined in calc_number_widths().
- No error checking, since we know the buffer is the correct size. */
-static void
-fill_number(STRINGLIB_CHAR *buf, const NumberFieldWidths *spec,
- STRINGLIB_CHAR *digits, Py_ssize_t n_digits,
- STRINGLIB_CHAR *prefix, STRINGLIB_CHAR fill_char,
- LocaleInfo *locale, int toupper)
-{
- /* Used to keep track of digits, decimal, and remainder. */
- STRINGLIB_CHAR *p = digits;
-
-#ifndef NDEBUG
- Py_ssize_t r;
-#endif
-
- if (spec->n_lpadding) {
- STRINGLIB_FILL(buf, fill_char, spec->n_lpadding);
- buf += spec->n_lpadding;
- }
- if (spec->n_sign == 1) {
- *buf++ = spec->sign;
- }
- if (spec->n_prefix) {
- memmove(buf,
- prefix,
- spec->n_prefix * sizeof(STRINGLIB_CHAR));
- if (toupper) {
- Py_ssize_t t;
- for (t = 0; t < spec->n_prefix; ++t)
- buf[t] = STRINGLIB_TOUPPER(buf[t]);
- }
- buf += spec->n_prefix;
- }
- if (spec->n_spadding) {
- STRINGLIB_FILL(buf, fill_char, spec->n_spadding);
- buf += spec->n_spadding;
- }
-
- /* Only for type 'c' special case, it has no digits. */
- if (spec->n_digits != 0) {
- /* Fill the digits with InsertThousandsGrouping. */
-#ifndef NDEBUG
- r =
-#endif
- STRINGLIB_GROUPING(buf, spec->n_grouped_digits, digits,
- spec->n_digits, spec->n_min_width,
- locale->grouping, locale->thousands_sep);
-#ifndef NDEBUG
- assert(r == spec->n_grouped_digits);
-#endif
- p += spec->n_digits;
- }
- if (toupper) {
- Py_ssize_t t;
- for (t = 0; t < spec->n_grouped_digits; ++t)
- buf[t] = STRINGLIB_TOUPPER(buf[t]);
- }
- buf += spec->n_grouped_digits;
-
- if (spec->n_decimal) {
- Py_ssize_t t;
- for (t = 0; t < spec->n_decimal; ++t)
- buf[t] = locale->decimal_point[t];
- buf += spec->n_decimal;
- p += 1;
- }
-
- if (spec->n_remainder) {
- memcpy(buf, p, spec->n_remainder * sizeof(STRINGLIB_CHAR));
- buf += spec->n_remainder;
- p += spec->n_remainder;
- }
-
- if (spec->n_rpadding) {
- STRINGLIB_FILL(buf, fill_char, spec->n_rpadding);
- buf += spec->n_rpadding;
- }
-}
-
-static char no_grouping[1] = {CHAR_MAX};
-
-/* Find the decimal point character(s?), thousands_separator(s?), and
- grouping description, either for the current locale if type is
- LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
- none if LT_NO_LOCALE. */
-static void
-get_locale_info(int type, LocaleInfo *locale_info)
-{
- switch (type) {
- case LT_CURRENT_LOCALE: {
- struct lconv *locale_data = localeconv();
- locale_info->decimal_point = locale_data->decimal_point;
- locale_info->thousands_sep = locale_data->thousands_sep;
- locale_info->grouping = locale_data->grouping;
- break;
- }
- case LT_DEFAULT_LOCALE:
- locale_info->decimal_point = ".";
- locale_info->thousands_sep = ",";
- locale_info->grouping = "\3"; /* Group every 3 characters. The
- (implicit) trailing 0 means repeat
- infinitely. */
- break;
- case LT_NO_LOCALE:
- locale_info->decimal_point = ".";
- locale_info->thousands_sep = "";
- locale_info->grouping = no_grouping;
- break;
- default:
- assert(0);
- }
-}
-
-#endif /* FORMAT_FLOAT || FORMAT_LONG || FORMAT_COMPLEX */
-
-/************************************************************************/
-/*********** string formatting ******************************************/
-/************************************************************************/
-
-static PyObject *
-format_string_internal(PyObject *value, const InternalFormatSpec *format)
-{
- Py_ssize_t lpad;
- Py_ssize_t rpad;
- Py_ssize_t total;
- STRINGLIB_CHAR *p;
- Py_ssize_t len = STRINGLIB_LEN(value);
- PyObject *result = NULL;
-
- /* sign is not allowed on strings */
- if (format->sign != '\0') {
- PyErr_SetString(PyExc_ValueError,
- "Sign not allowed in string format specifier");
- goto done;
- }
-
- /* alternate is not allowed on strings */
- if (format->alternate) {
- PyErr_SetString(PyExc_ValueError,
- "Alternate form (#) not allowed in string format "
- "specifier");
- goto done;
- }
-
- /* '=' alignment not allowed on strings */
- if (format->align == '=') {
- PyErr_SetString(PyExc_ValueError,
- "'=' alignment not allowed "
- "in string format specifier");
- goto done;
- }
-
- /* if precision is specified, output no more that format.precision
- characters */
- if (format->precision >= 0 && len >= format->precision) {
- len = format->precision;
- }
-
- calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
-
- /* allocate the resulting string */
- result = STRINGLIB_NEW(NULL, total);
- if (result == NULL)
- goto done;
-
- /* Write into that space. First the padding. */
- p = fill_padding(STRINGLIB_STR(result), len,
- format->fill_char=='\0'?' ':format->fill_char,
- lpad, rpad);
-
- /* Then the source string. */
- memcpy(p, STRINGLIB_STR(value), len * sizeof(STRINGLIB_CHAR));
-
-done:
- return result;
-}
-
-
-/************************************************************************/
-/*********** long formatting ********************************************/
-/************************************************************************/
-
-#if defined FORMAT_LONG || defined FORMAT_INT
-typedef PyObject*
-(*IntOrLongToString)(PyObject *value, int base);
-
-static PyObject *
-format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
- IntOrLongToString tostring)
-{
- PyObject *result = NULL;
- PyObject *tmp = NULL;
- STRINGLIB_CHAR *pnumeric_chars;
- STRINGLIB_CHAR numeric_char;
- STRINGLIB_CHAR sign_char = '\0';
- Py_ssize_t n_digits; /* count of digits need from the computed
- string */
- Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
- produces non-digits */
- Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
- Py_ssize_t n_total;
- STRINGLIB_CHAR *prefix = NULL;
- NumberFieldWidths spec;
- long x;
-
- /* Locale settings, either from the actual locale or
- from a hard-code pseudo-locale */
- LocaleInfo locale;
-
- /* no precision allowed on integers */
- if (format->precision != -1) {
- PyErr_SetString(PyExc_ValueError,
- "Precision not allowed in integer format specifier");
- goto done;
- }
-
- /* special case for character formatting */
- if (format->type == 'c') {
- /* error to specify a sign */
- if (format->sign != '\0') {
- PyErr_SetString(PyExc_ValueError,
- "Sign not allowed with integer"
- " format specifier 'c'");
- goto done;
- }
-
- /* taken from unicodeobject.c formatchar() */
- /* Integer input truncated to a character */
-/* XXX: won't work for int */
- x = PyLong_AsLong(value);
- if (x == -1 && PyErr_Occurred())
- goto done;
-#ifdef Py_UNICODE_WIDE
- if (x < 0 || x > 0x10ffff) {
- PyErr_SetString(PyExc_OverflowError,
- "%c arg not in range(0x110000) "
- "(wide Python build)");
- goto done;
- }
-#else
- if (x < 0 || x > 0xffff) {
- PyErr_SetString(PyExc_OverflowError,
- "%c arg not in range(0x10000) "
- "(narrow Python build)");
- goto done;
- }
-#endif
- numeric_char = (STRINGLIB_CHAR)x;
- pnumeric_chars = &numeric_char;
- n_digits = 1;
-
- /* As a sort-of hack, we tell calc_number_widths that we only
- have "remainder" characters. calc_number_widths thinks
- these are characters that don't get formatted, only copied
- into the output string. We do this for 'c' formatting,
- because the characters are likely to be non-digits. */
- n_remainder = 1;
- }
- else {
- int base;
- int leading_chars_to_skip = 0; /* Number of characters added by
- PyNumber_ToBase that we want to
- skip over. */
-
- /* Compute the base and how many characters will be added by
- PyNumber_ToBase */
- switch (format->type) {
- case 'b':
- base = 2;
- leading_chars_to_skip = 2; /* 0b */
- break;
- case 'o':
- base = 8;
- leading_chars_to_skip = 2; /* 0o */
- break;
- case 'x':
- case 'X':
- base = 16;
- leading_chars_to_skip = 2; /* 0x */
- break;
- default: /* shouldn't be needed, but stops a compiler warning */
- case 'd':
- case 'n':
- base = 10;
- break;
- }
-
- /* The number of prefix chars is the same as the leading
- chars to skip */
- if (format->alternate)
- n_prefix = leading_chars_to_skip;
-
- /* Do the hard part, converting to a string in a given base */
- tmp = tostring(value, base);
- if (tmp == NULL)
- goto done;
-
- pnumeric_chars = STRINGLIB_STR(tmp);
- n_digits = STRINGLIB_LEN(tmp);
-
- prefix = pnumeric_chars;
-
- /* Remember not to modify what pnumeric_chars points to. it
- might be interned. Only modify it after we copy it into a
- newly allocated output buffer. */
-
- /* Is a sign character present in the output? If so, remember it
- and skip it */
- if (pnumeric_chars[0] == '-') {
- sign_char = pnumeric_chars[0];
- ++prefix;
- ++leading_chars_to_skip;
- }
-
- /* Skip over the leading chars (0x, 0b, etc.) */
- n_digits -= leading_chars_to_skip;
- pnumeric_chars += leading_chars_to_skip;
- }
-
- /* Determine the grouping, separator, and decimal point, if any. */
- get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
- (format->thousands_separators ?
- LT_DEFAULT_LOCALE :
- LT_NO_LOCALE),
- &locale);
-
- /* Calculate how much memory we'll need. */
- n_total = calc_number_widths(&spec, n_prefix, sign_char, pnumeric_chars,
- n_digits, n_remainder, 0, &locale, format);
-
- /* Allocate the memory. */
- result = STRINGLIB_NEW(NULL, n_total);
- if (!result)
- goto done;
-
- /* Populate the memory. */
- fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits,
- prefix, format->fill_char == '\0' ? ' ' : format->fill_char,
- &locale, format->type == 'X');
-
-done:
- Py_XDECREF(tmp);
- return result;
-}
-#endif /* defined FORMAT_LONG || defined FORMAT_INT */
-
-/************************************************************************/
-/*********** float formatting *******************************************/
-/************************************************************************/
-
-#ifdef FORMAT_FLOAT
-#if STRINGLIB_IS_UNICODE
-static void
-strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len)
-{
- Py_ssize_t i;
- for (i = 0; i < len; ++i)
- buffer[i] = (Py_UNICODE)charbuffer[i];
-}
-#endif
-
-/* much of this is taken from unicodeobject.c */
-static PyObject *
-format_float_internal(PyObject *value,
- const InternalFormatSpec *format)
-{
- char *buf = NULL; /* buffer returned from PyOS_double_to_string */
- Py_ssize_t n_digits;
- Py_ssize_t n_remainder;
- Py_ssize_t n_total;
- int has_decimal;
- double val;
- Py_ssize_t precision = format->precision;
- Py_ssize_t default_precision = 6;
- STRINGLIB_CHAR type = format->type;
- int add_pct = 0;
- STRINGLIB_CHAR *p;
- NumberFieldWidths spec;
- int flags = 0;
- PyObject *result = NULL;
- STRINGLIB_CHAR sign_char = '\0';
- int float_type; /* Used to see if we have a nan, inf, or regular float. */
-
-#if STRINGLIB_IS_UNICODE
- Py_UNICODE *unicode_tmp = NULL;
-#endif
-
- /* Locale settings, either from the actual locale or
- from a hard-code pseudo-locale */
- LocaleInfo locale;
-
- if (format->alternate)
- flags |= Py_DTSF_ALT;
-
- if (type == '\0') {
- /* Omitted type specifier. Behaves in the same way as repr(x)
- and str(x) if no precision is given, else like 'g', but with
- at least one digit after the decimal point. */
- flags |= Py_DTSF_ADD_DOT_0;
- type = 'r';
- default_precision = 0;
- }
-
- if (type == 'n')
- /* 'n' is the same as 'g', except for the locale used to
- format the result. We take care of that later. */
- type = 'g';
-
- val = PyFloat_AsDouble(value);
- if (val == -1.0 && PyErr_Occurred())
- goto done;
-
- if (type == '%') {
- type = 'f';
- val *= 100;
- add_pct = 1;
- }
-
- if (precision < 0)
- precision = default_precision;
- else if (type == 'r')
- type = 'g';
-
- /* Cast "type", because if we're in unicode we need to pass a
- 8-bit char. This is safe, because we've restricted what "type"
- can be. */
- buf = PyOS_double_to_string(val, (char)type, precision, flags,
- &float_type);
- if (buf == NULL)
- goto done;
- n_digits = strlen(buf);
-
- if (add_pct) {
- /* We know that buf has a trailing zero (since we just called
- strlen() on it), and we don't use that fact any more. So we
- can just write over the trailing zero. */
- buf[n_digits] = '%';
- n_digits += 1;
- }
-
- /* Since there is no unicode version of PyOS_double_to_string,
- just use the 8 bit version and then convert to unicode. */
-#if STRINGLIB_IS_UNICODE
- unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_digits)*sizeof(Py_UNICODE));
- if (unicode_tmp == NULL) {
- PyErr_NoMemory();
- goto done;
- }
- strtounicode(unicode_tmp, buf, n_digits);
- p = unicode_tmp;
-#else
- p = buf;
-#endif
-
- /* Is a sign character present in the output? If so, remember it
- and skip it */
- if (*p == '-') {
- sign_char = *p;
- ++p;
- --n_digits;
- }
-
- /* Determine if we have any "remainder" (after the digits, might include
- decimal or exponent or both (or neither)) */
- parse_number(p, n_digits, &n_remainder, &has_decimal);
-
- /* Determine the grouping, separator, and decimal point, if any. */
- get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
- (format->thousands_separators ?
- LT_DEFAULT_LOCALE :
- LT_NO_LOCALE),
- &locale);
-
- /* Calculate how much memory we'll need. */
- n_total = calc_number_widths(&spec, 0, sign_char, p, n_digits,
- n_remainder, has_decimal, &locale, format);
-
- /* Allocate the memory. */
- result = STRINGLIB_NEW(NULL, n_total);
- if (result == NULL)
- goto done;
-
- /* Populate the memory. */
- fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL,
- format->fill_char == '\0' ? ' ' : format->fill_char, &locale,
- 0);
-
-done:
- PyMem_Free(buf);
-#if STRINGLIB_IS_UNICODE
- PyMem_Free(unicode_tmp);
-#endif
- return result;
-}
-#endif /* FORMAT_FLOAT */
-
-/************************************************************************/
-/*********** complex formatting *****************************************/
-/************************************************************************/
-
-#ifdef FORMAT_COMPLEX
-
-static PyObject *
-format_complex_internal(PyObject *value,
- const InternalFormatSpec *format)
-{
- double re;
- double im;
- char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */
- char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */
-
- InternalFormatSpec tmp_format = *format;
- Py_ssize_t n_re_digits;
- Py_ssize_t n_im_digits;
- Py_ssize_t n_re_remainder;
- Py_ssize_t n_im_remainder;
- Py_ssize_t n_re_total;
- Py_ssize_t n_im_total;
- int re_has_decimal;
- int im_has_decimal;
- Py_ssize_t precision = format->precision;
- Py_ssize_t default_precision = 6;
- STRINGLIB_CHAR type = format->type;
- STRINGLIB_CHAR *p_re;
- STRINGLIB_CHAR *p_im;
- NumberFieldWidths re_spec;
- NumberFieldWidths im_spec;
- int flags = 0;
- PyObject *result = NULL;
- STRINGLIB_CHAR *p;
- STRINGLIB_CHAR re_sign_char = '\0';
- STRINGLIB_CHAR im_sign_char = '\0';
- int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
- int im_float_type;
- int add_parens = 0;
- int skip_re = 0;
- Py_ssize_t lpad;
- Py_ssize_t rpad;
- Py_ssize_t total;
-
-#if STRINGLIB_IS_UNICODE
- Py_UNICODE *re_unicode_tmp = NULL;
- Py_UNICODE *im_unicode_tmp = NULL;
-#endif
-
- /* Locale settings, either from the actual locale or
- from a hard-code pseudo-locale */
- LocaleInfo locale;
-
- /* Zero padding is not allowed. */
- if (format->fill_char == '0') {
- PyErr_SetString(PyExc_ValueError,
- "Zero padding is not allowed in complex format "
- "specifier");
- goto done;
- }
-
- /* Neither is '=' alignment . */
- if (format->align == '=') {
- PyErr_SetString(PyExc_ValueError,
- "'=' alignment flag is not allowed in complex format "
- "specifier");
- goto done;
- }
-
- re = PyComplex_RealAsDouble(value);
- if (re == -1.0 && PyErr_Occurred())
- goto done;
- im = PyComplex_ImagAsDouble(value);
- if (im == -1.0 && PyErr_Occurred())
- goto done;
-
- if (format->alternate)
- flags |= Py_DTSF_ALT;
-
- if (type == '\0') {
- /* Omitted type specifier. Should be like str(self). */
- type = 'r';
- default_precision = 0;
- if (re == 0.0 && copysign(1.0, re) == 1.0)
- skip_re = 1;
- else
- add_parens = 1;
- }
-
- if (type == 'n')
- /* 'n' is the same as 'g', except for the locale used to
- format the result. We take care of that later. */
- type = 'g';
-
- if (precision < 0)
- precision = default_precision;
- else if (type == 'r')
- type = 'g';
-
- /* Cast "type", because if we're in unicode we need to pass a
- 8-bit char. This is safe, because we've restricted what "type"
- can be. */
- re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
- &re_float_type);
- if (re_buf == NULL)
- goto done;
- im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
- &im_float_type);
- if (im_buf == NULL)
- goto done;
-
- n_re_digits = strlen(re_buf);
- n_im_digits = strlen(im_buf);
-
- /* Since there is no unicode version of PyOS_double_to_string,
- just use the 8 bit version and then convert to unicode. */
-#if STRINGLIB_IS_UNICODE
- re_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_re_digits)*sizeof(Py_UNICODE));
- if (re_unicode_tmp == NULL) {
- PyErr_NoMemory();
- goto done;
- }
- strtounicode(re_unicode_tmp, re_buf, n_re_digits);
- p_re = re_unicode_tmp;
-
- im_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_im_digits)*sizeof(Py_UNICODE));
- if (im_unicode_tmp == NULL) {
- PyErr_NoMemory();
- goto done;
- }
- strtounicode(im_unicode_tmp, im_buf, n_im_digits);
- p_im = im_unicode_tmp;
-#else
- p_re = re_buf;
- p_im = im_buf;
-#endif
-
- /* Is a sign character present in the output? If so, remember it
- and skip it */
- if (*p_re == '-') {
- re_sign_char = *p_re;
- ++p_re;
- --n_re_digits;
- }
- if (*p_im == '-') {
- im_sign_char = *p_im;
- ++p_im;
- --n_im_digits;
- }
-
- /* Determine if we have any "remainder" (after the digits, might include
- decimal or exponent or both (or neither)) */
- parse_number(p_re, n_re_digits, &n_re_remainder, &re_has_decimal);
- parse_number(p_im, n_im_digits, &n_im_remainder, &im_has_decimal);
-
- /* Determine the grouping, separator, and decimal point, if any. */
- get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
- (format->thousands_separators ?
- LT_DEFAULT_LOCALE :
- LT_NO_LOCALE),
- &locale);
-
- /* Turn off any padding. We'll do it later after we've composed
- the numbers without padding. */
- tmp_format.fill_char = '\0';
- tmp_format.align = '<';
- tmp_format.width = -1;
-
- /* Calculate how much memory we'll need. */
- n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, p_re,
- n_re_digits, n_re_remainder,
- re_has_decimal, &locale, &tmp_format);
-
- /* Same formatting, but always include a sign, unless the real part is
- * going to be omitted, in which case we use whatever sign convention was
- * requested by the original format. */
- if (!skip_re)
- tmp_format.sign = '+';
- n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,
- n_im_digits, n_im_remainder,
- im_has_decimal, &locale, &tmp_format);
-
- if (skip_re)
- n_re_total = 0;
-
- /* Add 1 for the 'j', and optionally 2 for parens. */
- calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
- format->width, format->align, &lpad, &rpad, &total);
-
- result = STRINGLIB_NEW(NULL, total);
- if (result == NULL)
- goto done;
-
- /* Populate the memory. First, the padding. */
- p = fill_padding(STRINGLIB_STR(result),
- n_re_total + n_im_total + 1 + add_parens * 2,
- format->fill_char=='\0' ? ' ' : format->fill_char,
- lpad, rpad);
-
- if (add_parens)
- *p++ = '(';
-
- if (!skip_re) {
- fill_number(p, &re_spec, p_re, n_re_digits, NULL, 0, &locale, 0);
- p += n_re_total;
- }
- fill_number(p, &im_spec, p_im, n_im_digits, NULL, 0, &locale, 0);
- p += n_im_total;
- *p++ = 'j';
-
- if (add_parens)
- *p++ = ')';
-
-done:
- PyMem_Free(re_buf);
- PyMem_Free(im_buf);
-#if STRINGLIB_IS_UNICODE
- PyMem_Free(re_unicode_tmp);
- PyMem_Free(im_unicode_tmp);
-#endif
- return result;
-}
-#endif /* FORMAT_COMPLEX */
-
-/************************************************************************/
-/*********** built in formatters ****************************************/
-/************************************************************************/
-PyObject *
-FORMAT_STRING(PyObject *obj,
- STRINGLIB_CHAR *format_spec,
- Py_ssize_t format_spec_len)
-{
- InternalFormatSpec format;
- PyObject *result = NULL;
-
- /* check for the special case of zero length format spec, make
- it equivalent to str(obj) */
- if (format_spec_len == 0) {
- result = STRINGLIB_TOSTR(obj);
- goto done;
- }
-
- /* parse the format_spec */
- if (!parse_internal_render_format_spec(format_spec, format_spec_len,
- &format, 's', '<'))
- goto done;
-
- /* type conversion? */
- switch (format.type) {
- case 's':
- /* no type conversion needed, already a string. do the formatting */
- result = format_string_internal(obj, &format);
- break;
- default:
- /* unknown */
- unknown_presentation_type(format.type, obj->ob_type->tp_name);
- goto done;
- }
-
-done:
- return result;
-}
-
-#if defined FORMAT_LONG || defined FORMAT_INT
-static PyObject*
-format_int_or_long(PyObject* obj,
- STRINGLIB_CHAR *format_spec,
- Py_ssize_t format_spec_len,
- IntOrLongToString tostring)
-{
- PyObject *result = NULL;
- PyObject *tmp = NULL;
- InternalFormatSpec format;
-
- /* check for the special case of zero length format spec, make
- it equivalent to str(obj) */
- if (format_spec_len == 0) {
- result = STRINGLIB_TOSTR(obj);
- goto done;
- }
-
- /* parse the format_spec */
- if (!parse_internal_render_format_spec(format_spec,
- format_spec_len,
- &format, 'd', '>'))
- goto done;
-
- /* type conversion? */
- switch (format.type) {
- case 'b':
- case 'c':
- case 'd':
- case 'o':
- case 'x':
- case 'X':
- case 'n':
- /* no type conversion needed, already an int (or long). do
- the formatting */
- result = format_int_or_long_internal(obj, &format, tostring);
- break;
-
- case 'e':
- case 'E':
- case 'f':
- case 'F':
- case 'g':
- case 'G':
- case '%':
- /* convert to float */
- tmp = PyNumber_Float(obj);
- if (tmp == NULL)
- goto done;
- result = format_float_internal(tmp, &format);
- break;
-
- default:
- /* unknown */
- unknown_presentation_type(format.type, obj->ob_type->tp_name);
- goto done;
- }
-
-done:
- Py_XDECREF(tmp);
- return result;
-}
-#endif /* FORMAT_LONG || defined FORMAT_INT */
-
-#ifdef FORMAT_LONG
-/* Need to define long_format as a function that will convert a long
- to a string. In 3.0, _PyLong_Format has the correct signature. In
- 2.x, we need to fudge a few parameters */
-#if PY_VERSION_HEX >= 0x03000000
-#define long_format _PyLong_Format
-#else
-static PyObject*
-long_format(PyObject* value, int base)
-{
- /* Convert to base, don't add trailing 'L', and use the new octal
- format. We already know this is a long object */
- assert(PyLong_Check(value));
- /* convert to base, don't add 'L', and use the new octal format */
- return _PyLong_Format(value, base, 0, 1);
-}
-#endif
-
-PyObject *
-FORMAT_LONG(PyObject *obj,
- STRINGLIB_CHAR *format_spec,
- Py_ssize_t format_spec_len)
-{
- return format_int_or_long(obj, format_spec, format_spec_len,
- long_format);
-}
-#endif /* FORMAT_LONG */
-
-#ifdef FORMAT_INT
-/* this is only used for 2.x, not 3.0 */
-static PyObject*
-int_format(PyObject* value, int base)
-{
- /* Convert to base, and use the new octal format. We already
- know this is an int object */
- assert(PyInt_Check(value));
- return _PyInt_Format((PyIntObject*)value, base, 1);
-}
-
-PyObject *
-FORMAT_INT(PyObject *obj,
- STRINGLIB_CHAR *format_spec,
- Py_ssize_t format_spec_len)
-{
- return format_int_or_long(obj, format_spec, format_spec_len,
- int_format);
-}
-#endif /* FORMAT_INT */
-
-#ifdef FORMAT_FLOAT
-PyObject *
-FORMAT_FLOAT(PyObject *obj,
- STRINGLIB_CHAR *format_spec,
- Py_ssize_t format_spec_len)
-{
- PyObject *result = NULL;
- InternalFormatSpec format;
-
- /* check for the special case of zero length format spec, make
- it equivalent to str(obj) */
- if (format_spec_len == 0) {
- result = STRINGLIB_TOSTR(obj);
- goto done;
- }
-
- /* parse the format_spec */
- if (!parse_internal_render_format_spec(format_spec,
- format_spec_len,
- &format, '\0', '>'))
- goto done;
-
- /* type conversion? */
- switch (format.type) {
- case '\0': /* No format code: like 'g', but with at least one decimal. */
- case 'e':
- case 'E':
- case 'f':
- case 'F':
- case 'g':
- case 'G':
- case 'n':
- case '%':
- /* no conversion, already a float. do the formatting */
- result = format_float_internal(obj, &format);
- break;
-
- default:
- /* unknown */
- unknown_presentation_type(format.type, obj->ob_type->tp_name);
- goto done;
- }
-
-done:
- return result;
-}
-#endif /* FORMAT_FLOAT */
-
-#ifdef FORMAT_COMPLEX
-PyObject *
-FORMAT_COMPLEX(PyObject *obj,
- STRINGLIB_CHAR *format_spec,
- Py_ssize_t format_spec_len)
-{
- PyObject *result = NULL;
- InternalFormatSpec format;
-
- /* check for the special case of zero length format spec, make
- it equivalent to str(obj) */
- if (format_spec_len == 0) {
- result = STRINGLIB_TOSTR(obj);
- goto done;
- }
-
- /* parse the format_spec */
- if (!parse_internal_render_format_spec(format_spec,
- format_spec_len,
- &format, '\0', '>'))
- goto done;
-
- /* type conversion? */
- switch (format.type) {
- case '\0': /* No format code: like 'g', but with at least one decimal. */
- case 'e':
- case 'E':
- case 'f':
- case 'F':
- case 'g':
- case 'G':
- case 'n':
- /* no conversion, already a complex. do the formatting */
- result = format_complex_internal(obj, &format);
- break;
-
- default:
- /* unknown */
- unknown_presentation_type(format.type, obj->ob_type->tp_name);
- goto done;
- }
-
-done:
- return result;
-}
-#endif /* FORMAT_COMPLEX */
diff --git a/Objects/stringlib/localeutil.h b/Objects/stringlib/localeutil.h
index f548133..ddce69d 100644
--- a/Objects/stringlib/localeutil.h
+++ b/Objects/stringlib/localeutil.h
@@ -1,8 +1,5 @@
/* stringlib: locale related helpers implementation */
-#ifndef STRINGLIB_LOCALEUTIL_H
-#define STRINGLIB_LOCALEUTIL_H
-
#include <locale.h>
#define MAX(x, y) ((x) < (y) ? (y) : (x))
@@ -12,10 +9,10 @@ typedef struct {
const char *grouping;
char previous;
Py_ssize_t i; /* Where we're currently pointing in grouping. */
-} GroupGenerator;
+} STRINGLIB(GroupGenerator);
static void
-_GroupGenerator_init(GroupGenerator *self, const char *grouping)
+STRINGLIB(GroupGenerator_init)(STRINGLIB(GroupGenerator) *self, const char *grouping)
{
self->grouping = grouping;
self->i = 0;
@@ -24,7 +21,7 @@ _GroupGenerator_init(GroupGenerator *self, const char *grouping)
/* Returns the next grouping, or 0 to signify end. */
static Py_ssize_t
-_GroupGenerator_next(GroupGenerator *self)
+STRINGLIB(GroupGenerator_next)(STRINGLIB(GroupGenerator) *self)
{
/* Note that we don't really do much error checking here. If a
grouping string contains just CHAR_MAX, for example, then just
@@ -48,13 +45,11 @@ _GroupGenerator_next(GroupGenerator *self)
/* Fill in some digits, leading zeros, and thousands separator. All
are optional, depending on when we're called. */
static void
-fill(STRINGLIB_CHAR **digits_end, STRINGLIB_CHAR **buffer_end,
+STRINGLIB(fill)(STRINGLIB_CHAR **digits_end, STRINGLIB_CHAR **buffer_end,
Py_ssize_t n_chars, Py_ssize_t n_zeros, const char* thousands_sep,
Py_ssize_t thousands_sep_len)
{
-#if STRINGLIB_IS_UNICODE
Py_ssize_t i;
-#endif
if (thousands_sep) {
*buffer_end -= thousands_sep_len;
@@ -76,7 +71,8 @@ fill(STRINGLIB_CHAR **digits_end, STRINGLIB_CHAR **buffer_end,
memcpy(*buffer_end, *digits_end, n_chars * sizeof(STRINGLIB_CHAR));
*buffer_end -= n_zeros;
- STRINGLIB_FILL(*buffer_end, '0', n_zeros);
+ for (i = 0; i < n_zeros; i++)
+ (*buffer_end)[i] = '0';
}
/**
@@ -133,15 +129,15 @@ _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
be looked at */
/* A generator that returns all of the grouping widths, until it
returns 0. */
- GroupGenerator groupgen;
- _GroupGenerator_init(&groupgen, grouping);
+ STRINGLIB(GroupGenerator) groupgen;
+ STRINGLIB(GroupGenerator_init)(&groupgen, grouping);
if (buffer) {
buffer_end = buffer + n_buffer;
digits_end = digits + n_digits;
}
- while ((l = _GroupGenerator_next(&groupgen)) > 0) {
+ while ((l = STRINGLIB(GroupGenerator_next)(&groupgen)) > 0) {
l = MIN(l, MAX(MAX(remaining, min_width), 1));
n_zeros = MAX(0, l - remaining);
n_chars = MAX(0, MIN(remaining, l));
@@ -153,7 +149,7 @@ _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
if (buffer) {
/* Copy into the output buffer. */
- fill(&digits_end, &buffer_end, n_chars, n_zeros,
+ STRINGLIB(fill)(&digits_end, &buffer_end, n_chars, n_zeros,
use_separator ? thousands_sep : NULL, thousands_sep_len);
}
@@ -180,7 +176,7 @@ _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
count += (use_separator ? thousands_sep_len : 0) + n_zeros + n_chars;
if (buffer) {
/* Copy into the output buffer. */
- fill(&digits_end, &buffer_end, n_chars, n_zeros,
+ STRINGLIB(fill)(&digits_end, &buffer_end, n_chars, n_zeros,
use_separator ? thousands_sep : NULL, thousands_sep_len);
}
}
@@ -209,4 +205,3 @@ _Py_InsertThousandsGroupingLocale(STRINGLIB_CHAR *buffer,
return _Py_InsertThousandsGrouping(buffer, n_buffer, digits, n_digits,
min_width, grouping, thousands_sep);
}
-#endif /* STRINGLIB_LOCALEUTIL_H */
diff --git a/Objects/stringlib/partition.h b/Objects/stringlib/partition.h
index 0170bdd..40cb512 100644
--- a/Objects/stringlib/partition.h
+++ b/Objects/stringlib/partition.h
@@ -1,14 +1,11 @@
/* stringlib: partition implementation */
-#ifndef STRINGLIB_PARTITION_H
-#define STRINGLIB_PARTITION_H
-
#ifndef STRINGLIB_FASTSEARCH_H
#error must include "stringlib/fastsearch.h" before including this module
#endif
Py_LOCAL_INLINE(PyObject*)
-stringlib_partition(PyObject* str_obj,
+STRINGLIB(partition)(PyObject* str_obj,
const STRINGLIB_CHAR* str, Py_ssize_t str_len,
PyObject* sep_obj,
const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
@@ -25,7 +22,7 @@ stringlib_partition(PyObject* str_obj,
if (!out)
return NULL;
- pos = fastsearch(str, str_len, sep, sep_len, -1, FAST_SEARCH);
+ pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_SEARCH);
if (pos < 0) {
#if STRINGLIB_MUTABLE
@@ -58,7 +55,7 @@ stringlib_partition(PyObject* str_obj,
}
Py_LOCAL_INLINE(PyObject*)
-stringlib_rpartition(PyObject* str_obj,
+STRINGLIB(rpartition)(PyObject* str_obj,
const STRINGLIB_CHAR* str, Py_ssize_t str_len,
PyObject* sep_obj,
const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
@@ -75,7 +72,7 @@ stringlib_rpartition(PyObject* str_obj,
if (!out)
return NULL;
- pos = fastsearch(str, str_len, sep, sep_len, -1, FAST_RSEARCH);
+ pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_RSEARCH);
if (pos < 0) {
#if STRINGLIB_MUTABLE
@@ -107,4 +104,3 @@ stringlib_rpartition(PyObject* str_obj,
return out;
}
-#endif
diff --git a/Objects/stringlib/split.h b/Objects/stringlib/split.h
index 60e7767..947dd28 100644
--- a/Objects/stringlib/split.h
+++ b/Objects/stringlib/split.h
@@ -1,8 +1,5 @@
/* stringlib: split implementation */
-#ifndef STRINGLIB_SPLIT_H
-#define STRINGLIB_SPLIT_H
-
#ifndef STRINGLIB_FASTSEARCH_H
#error must include "stringlib/fastsearch.h" before including this module
#endif
@@ -54,7 +51,7 @@
#define FIX_PREALLOC_SIZE(list) Py_SIZE(list) = count
Py_LOCAL_INLINE(PyObject *)
-stringlib_split_whitespace(PyObject* str_obj,
+STRINGLIB(split_whitespace)(PyObject* str_obj,
const STRINGLIB_CHAR* str, Py_ssize_t str_len,
Py_ssize_t maxcount)
{
@@ -102,7 +99,7 @@ stringlib_split_whitespace(PyObject* str_obj,
}
Py_LOCAL_INLINE(PyObject *)
-stringlib_split_char(PyObject* str_obj,
+STRINGLIB(split_char)(PyObject* str_obj,
const STRINGLIB_CHAR* str, Py_ssize_t str_len,
const STRINGLIB_CHAR ch,
Py_ssize_t maxcount)
@@ -145,7 +142,7 @@ stringlib_split_char(PyObject* str_obj,
}
Py_LOCAL_INLINE(PyObject *)
-stringlib_split(PyObject* str_obj,
+STRINGLIB(split)(PyObject* str_obj,
const STRINGLIB_CHAR* str, Py_ssize_t str_len,
const STRINGLIB_CHAR* sep, Py_ssize_t sep_len,
Py_ssize_t maxcount)
@@ -158,7 +155,7 @@ stringlib_split(PyObject* str_obj,
return NULL;
}
else if (sep_len == 1)
- return stringlib_split_char(str_obj, str, str_len, sep[0], maxcount);
+ return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
list = PyList_New(PREALLOC_SIZE(maxcount));
if (list == NULL)
@@ -166,7 +163,7 @@ stringlib_split(PyObject* str_obj,
i = j = 0;
while (maxcount-- > 0) {
- pos = fastsearch(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
+ pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
if (pos < 0)
break;
j = i + pos;
@@ -193,7 +190,7 @@ stringlib_split(PyObject* str_obj,
}
Py_LOCAL_INLINE(PyObject *)
-stringlib_rsplit_whitespace(PyObject* str_obj,
+STRINGLIB(rsplit_whitespace)(PyObject* str_obj,
const STRINGLIB_CHAR* str, Py_ssize_t str_len,
Py_ssize_t maxcount)
{
@@ -243,7 +240,7 @@ stringlib_rsplit_whitespace(PyObject* str_obj,
}
Py_LOCAL_INLINE(PyObject *)
-stringlib_rsplit_char(PyObject* str_obj,
+STRINGLIB(rsplit_char)(PyObject* str_obj,
const STRINGLIB_CHAR* str, Py_ssize_t str_len,
const STRINGLIB_CHAR ch,
Py_ssize_t maxcount)
@@ -287,7 +284,7 @@ stringlib_rsplit_char(PyObject* str_obj,
}
Py_LOCAL_INLINE(PyObject *)
-stringlib_rsplit(PyObject* str_obj,
+STRINGLIB(rsplit)(PyObject* str_obj,
const STRINGLIB_CHAR* str, Py_ssize_t str_len,
const STRINGLIB_CHAR* sep, Py_ssize_t sep_len,
Py_ssize_t maxcount)
@@ -300,7 +297,7 @@ stringlib_rsplit(PyObject* str_obj,
return NULL;
}
else if (sep_len == 1)
- return stringlib_rsplit_char(str_obj, str, str_len, sep[0], maxcount);
+ return STRINGLIB(rsplit_char)(str_obj, str, str_len, sep[0], maxcount);
list = PyList_New(PREALLOC_SIZE(maxcount));
if (list == NULL)
@@ -308,7 +305,7 @@ stringlib_rsplit(PyObject* str_obj,
j = str_len;
while (maxcount-- > 0) {
- pos = fastsearch(str, j, sep, sep_len, -1, FAST_RSEARCH);
+ pos = FASTSEARCH(str, j, sep, sep_len, -1, FAST_RSEARCH);
if (pos < 0)
break;
SPLIT_ADD(str, pos + sep_len, j);
@@ -336,7 +333,7 @@ stringlib_rsplit(PyObject* str_obj,
}
Py_LOCAL_INLINE(PyObject *)
-stringlib_splitlines(PyObject* str_obj,
+STRINGLIB(splitlines)(PyObject* str_obj,
const STRINGLIB_CHAR* str, Py_ssize_t str_len,
int keepends)
{
@@ -391,4 +388,3 @@ stringlib_splitlines(PyObject* str_obj,
return NULL;
}
-#endif
diff --git a/Objects/stringlib/stringdefs.h b/Objects/stringlib/stringdefs.h
index 1c49426..9619332 100644
--- a/Objects/stringlib/stringdefs.h
+++ b/Objects/stringlib/stringdefs.h
@@ -6,6 +6,8 @@
compiled as unicode. */
#define STRINGLIB_IS_UNICODE 0
+#define FASTSEARCH fastsearch
+#define STRINGLIB(F) stringlib_##F
#define STRINGLIB_OBJECT PyBytesObject
#define STRINGLIB_CHAR char
#define STRINGLIB_TYPE_NAME "string"
diff --git a/Objects/stringlib/ucs1lib.h b/Objects/stringlib/ucs1lib.h
new file mode 100644
index 0000000..4685c17
--- /dev/null
+++ b/Objects/stringlib/ucs1lib.h
@@ -0,0 +1,35 @@
+/* this is sort of a hack. there's at least one place (formatting
+ floats) where some stringlib code takes a different path if it's
+ compiled as unicode. */
+#define STRINGLIB_IS_UNICODE 1
+
+#define FASTSEARCH ucs1lib_fastsearch
+#define STRINGLIB(F) ucs1lib_##F
+#define STRINGLIB_OBJECT PyUnicodeObject
+#define STRINGLIB_CHAR Py_UCS1
+#define STRINGLIB_TYPE_NAME "unicode"
+#define STRINGLIB_PARSE_CODE "U"
+#define STRINGLIB_EMPTY unicode_empty
+#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE
+#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
+#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
+#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL
+#define STRINGLIB_TOUPPER Py_UNICODE_TOUPPER
+#define STRINGLIB_TOLOWER Py_UNICODE_TOLOWER
+#define STRINGLIB_FILL Py_UNICODE_FILL
+#define STRINGLIB_STR PyUnicode_1BYTE_DATA
+#define STRINGLIB_LEN PyUnicode_GET_LENGTH
+#define STRINGLIB_NEW PyUnicode_FromUCS1
+#define STRINGLIB_RESIZE not_supported
+#define STRINGLIB_CHECK PyUnicode_Check
+#define STRINGLIB_CHECK_EXACT PyUnicode_CheckExact
+#define STRINGLIB_GROUPING _PyUnicode_InsertThousandsGrouping
+#define STRINGLIB_GROUPING_LOCALE _PyUnicode_InsertThousandsGroupingLocale
+
+#define STRINGLIB_TOSTR PyObject_Str
+#define STRINGLIB_TOASCII PyObject_ASCII
+
+#define _Py_InsertThousandsGrouping _PyUnicode_ucs1_InsertThousandsGrouping
+#define _Py_InsertThousandsGroupingLocale _PyUnicode_ucs1_InsertThousandsGroupingLocale
+
+
diff --git a/Objects/stringlib/ucs2lib.h b/Objects/stringlib/ucs2lib.h
new file mode 100644
index 0000000..1bb9c27
--- /dev/null
+++ b/Objects/stringlib/ucs2lib.h
@@ -0,0 +1,34 @@
+/* this is sort of a hack. there's at least one place (formatting
+ floats) where some stringlib code takes a different path if it's
+ compiled as unicode. */
+#define STRINGLIB_IS_UNICODE 1
+
+#define FASTSEARCH ucs2lib_fastsearch
+#define STRINGLIB(F) ucs2lib_##F
+#define STRINGLIB_OBJECT PyUnicodeObject
+#define STRINGLIB_CHAR Py_UCS2
+#define STRINGLIB_TYPE_NAME "unicode"
+#define STRINGLIB_PARSE_CODE "U"
+#define STRINGLIB_EMPTY unicode_empty
+#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE
+#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
+#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
+#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL
+#define STRINGLIB_TOUPPER Py_UNICODE_TOUPPER
+#define STRINGLIB_TOLOWER Py_UNICODE_TOLOWER
+#define STRINGLIB_FILL Py_UNICODE_FILL
+#define STRINGLIB_STR PyUnicode_1BYTE_DATA
+#define STRINGLIB_LEN PyUnicode_GET_LENGTH
+#define STRINGLIB_NEW PyUnicode_FromUCS2
+#define STRINGLIB_RESIZE not_supported
+#define STRINGLIB_CHECK PyUnicode_Check
+#define STRINGLIB_CHECK_EXACT PyUnicode_CheckExact
+#define STRINGLIB_GROUPING _PyUnicode_InsertThousandsGrouping
+#define STRINGLIB_GROUPING_LOCALE _PyUnicode_InsertThousandsGroupingLocale
+
+#define STRINGLIB_TOSTR PyObject_Str
+#define STRINGLIB_TOASCII PyObject_ASCII
+
+#define _Py_InsertThousandsGrouping _PyUnicode_ucs2_InsertThousandsGrouping
+#define _Py_InsertThousandsGroupingLocale _PyUnicode_ucs2_InsertThousandsGroupingLocale
+
diff --git a/Objects/stringlib/ucs4lib.h b/Objects/stringlib/ucs4lib.h
new file mode 100644
index 0000000..776d65f
--- /dev/null
+++ b/Objects/stringlib/ucs4lib.h
@@ -0,0 +1,34 @@
+/* this is sort of a hack. there's at least one place (formatting
+ floats) where some stringlib code takes a different path if it's
+ compiled as unicode. */
+#define STRINGLIB_IS_UNICODE 1
+
+#define FASTSEARCH ucs4lib_fastsearch
+#define STRINGLIB(F) ucs4lib_##F
+#define STRINGLIB_OBJECT PyUnicodeObject
+#define STRINGLIB_CHAR Py_UCS4
+#define STRINGLIB_TYPE_NAME "unicode"
+#define STRINGLIB_PARSE_CODE "U"
+#define STRINGLIB_EMPTY unicode_empty
+#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE
+#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
+#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
+#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL
+#define STRINGLIB_TOUPPER Py_UNICODE_TOUPPER
+#define STRINGLIB_TOLOWER Py_UNICODE_TOLOWER
+#define STRINGLIB_FILL Py_UNICODE_FILL
+#define STRINGLIB_STR PyUnicode_1BYTE_DATA
+#define STRINGLIB_LEN PyUnicode_GET_LENGTH
+#define STRINGLIB_NEW PyUnicode_FromUCS4
+#define STRINGLIB_RESIZE not_supported
+#define STRINGLIB_CHECK PyUnicode_Check
+#define STRINGLIB_CHECK_EXACT PyUnicode_CheckExact
+#define STRINGLIB_GROUPING _PyUnicode_InsertThousandsGrouping
+#define STRINGLIB_GROUPING_LOCALE _PyUnicode_InsertThousandsGroupingLocale
+
+#define STRINGLIB_TOSTR PyObject_Str
+#define STRINGLIB_TOASCII PyObject_ASCII
+
+#define _Py_InsertThousandsGrouping _PyUnicode_ucs4_InsertThousandsGrouping
+#define _Py_InsertThousandsGroupingLocale _PyUnicode_ucs4_InsertThousandsGroupingLocale
+
diff --git a/Objects/stringlib/undef.h b/Objects/stringlib/undef.h
new file mode 100644
index 0000000..40b4391
--- /dev/null
+++ b/Objects/stringlib/undef.h
@@ -0,0 +1,10 @@
+#undef FASTSEARCH
+#undef STRINGLIB
+#undef STRINGLIB_CHAR
+#undef STRINGLIB_STR
+#undef STRINGLIB_LEN
+#undef STRINGLIB_NEW
+#undef STRINGLIB_RESIZE
+#undef _Py_InsertThousandsGrouping
+#undef _Py_InsertThousandsGroupingLocale
+
diff --git a/Objects/stringlib/string_format.h b/Objects/stringlib/unicode_format.h
index d992b6f..81a1ff2 100644
--- a/Objects/stringlib/string_format.h
+++ b/Objects/stringlib/unicode_format.h
@@ -1,16 +1,8 @@
/*
- string_format.h -- implementation of string.format().
-
- It uses the Objects/stringlib conventions, so that it can be
- compiled for both unicode and string objects.
+ unicode_format.h -- implementation of str.format().
*/
-/* Defines for Python 2.6 compatibility */
-#if PY_VERSION_HEX < 0x03000000
-#define PyLong_FromSsize_t _PyLong_FromSsize_t
-#endif
-
/* Defines for more efficiently reallocating the string buffer */
#define INITIAL_SIZE_INCREMENT 100
#define SIZE_MULTIPLIER 2
@@ -26,8 +18,8 @@
unicode pointers.
*/
typedef struct {
- STRINGLIB_CHAR *ptr;
- STRINGLIB_CHAR *end;
+ PyObject *str; /* borrowed reference */
+ Py_ssize_t start, end;
} SubString;
@@ -64,34 +56,32 @@ AutoNumber_Init(AutoNumber *auto_number)
/* fill in a SubString from a pointer and length */
Py_LOCAL_INLINE(void)
-SubString_init(SubString *str, STRINGLIB_CHAR *p, Py_ssize_t len)
+SubString_init(SubString *str, PyObject *s, int start, int end)
{
- str->ptr = p;
- if (p == NULL)
- str->end = NULL;
- else
- str->end = str->ptr + len;
+ str->str = s;
+ str->start = start;
+ str->end = end;
}
-/* return a new string. if str->ptr is NULL, return None */
+/* return a new string. if str->str is NULL, return None */
Py_LOCAL_INLINE(PyObject *)
SubString_new_object(SubString *str)
{
- if (str->ptr == NULL) {
+ if (str->str == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
- return STRINGLIB_NEW(str->ptr, str->end - str->ptr);
+ return PyUnicode_Substring(str->str, str->start, str->end);
}
-/* return a new string. if str->ptr is NULL, return None */
+/* return a new string. if str->str is NULL, return None */
Py_LOCAL_INLINE(PyObject *)
SubString_new_object_or_empty(SubString *str)
{
- if (str->ptr == NULL) {
- return STRINGLIB_NEW(NULL, 0);
+ if (str->str == NULL) {
+ return PyUnicode_FromUnicode(NULL, 0);
}
- return STRINGLIB_NEW(str->ptr, str->end - str->ptr);
+ return SubString_new_object(str);
}
/* Return 1 if an error has been detected switching between automatic
@@ -125,9 +115,10 @@ autonumber_state_error(AutoNumberState state, int field_name_is_empty)
/************************************************************************/
typedef struct {
- STRINGLIB_CHAR *ptr;
- STRINGLIB_CHAR *end;
- PyObject *obj;
+ char *data;
+ Py_UCS4 maxchar;
+ unsigned int kind;
+ Py_ssize_t pos, size;
Py_ssize_t size_increment;
} OutputString;
@@ -135,12 +126,16 @@ typedef struct {
static int
output_initialize(OutputString *output, Py_ssize_t size)
{
- output->obj = STRINGLIB_NEW(NULL, size);
- if (output->obj == NULL)
+ output->data = PyMem_Malloc(size);
+ if (output->data == NULL) {
+ PyErr_NoMemory();
return 0;
+ }
- output->ptr = STRINGLIB_STR(output->obj);
- output->end = STRINGLIB_LEN(output->obj) + output->ptr;
+ output->maxchar = 127;
+ output->kind = PyUnicode_1BYTE_KIND;
+ output->pos = 0;
+ output->size = size;
output->size_increment = INITIAL_SIZE_INCREMENT;
return 1;
@@ -155,20 +150,51 @@ output_initialize(OutputString *output, Py_ssize_t size)
static int
output_extend(OutputString *output, Py_ssize_t count)
{
- STRINGLIB_CHAR *startptr = STRINGLIB_STR(output->obj);
- Py_ssize_t curlen = output->ptr - startptr;
- Py_ssize_t maxlen = curlen + count + output->size_increment;
+ Py_ssize_t maxlen = output->size + count + output->size_increment;
- if (STRINGLIB_RESIZE(&output->obj, maxlen) < 0)
+ output->data = PyMem_Realloc(output->data, maxlen << (output->kind-1));
+ output->size = maxlen;
+ if (output->data == 0) {
+ PyErr_NoMemory();
return 0;
- startptr = STRINGLIB_STR(output->obj);
- output->ptr = startptr + curlen;
- output->end = startptr + maxlen;
+ }
if (output->size_increment < MAX_SIZE_INCREMENT)
output->size_increment *= SIZE_MULTIPLIER;
return 1;
}
+static int
+output_widen(OutputString *output, Py_UCS4 maxchar)
+{
+ int kind;
+ void *data;
+ Py_ssize_t i;
+ if (maxchar <= output->maxchar)
+ return 1;
+ if (maxchar < 256) {
+ output->maxchar = 255;
+ return 1;
+ }
+ if (maxchar < 65536) {
+ output->maxchar = 65535;
+ kind = 2;
+ }
+ else {
+ output->maxchar = 1<<21;
+ kind = 3;
+ }
+ data = PyMem_Malloc(output->size << (kind-1));
+ if (data == 0)
+ return 0;
+ for (i = 0; i < output->size; i++)
+ PyUnicode_WRITE(kind, data, i,
+ PyUnicode_READ(output->kind, output->data, i));
+ PyMem_Free(output->data);
+ output->data = data;
+ output->kind = kind;
+ return 1;
+}
+
/*
output_data dumps characters into our output string
buffer.
@@ -179,12 +205,25 @@ output_extend(OutputString *output, Py_ssize_t count)
1 for success.
*/
static int
-output_data(OutputString *output, const STRINGLIB_CHAR *s, Py_ssize_t count)
+output_data(OutputString *output, PyObject *s, Py_ssize_t start, Py_ssize_t end)
{
- if ((count > output->end - output->ptr) && !output_extend(output, count))
+ Py_ssize_t i;
+ int kind;
+ if ((output->pos + end - start > output->size) &&
+ !output_extend(output, end - start))
return 0;
- memcpy(output->ptr, s, count * sizeof(STRINGLIB_CHAR));
- output->ptr += count;
+ kind = PyUnicode_KIND(s);
+ if (PyUnicode_MAX_CHAR_VALUE(s) > output->maxchar) {
+ Py_UCS4 maxchar = output->maxchar;
+ for (i = start; i < end; i++)
+ if (PyUnicode_READ(kind, PyUnicode_DATA(s), i) > maxchar)
+ maxchar = PyUnicode_READ(kind, PyUnicode_DATA(s), i);
+ if (!output_widen(output, maxchar))
+ return 0;
+ }
+ for (i = start; i < end; i++)
+ PyUnicode_WRITE(output->kind, output->data, output->pos++,
+ PyUnicode_READ(kind, PyUnicode_DATA(s), i));
return 1;
}
@@ -197,15 +236,14 @@ get_integer(const SubString *str)
{
Py_ssize_t accumulator = 0;
Py_ssize_t digitval;
- Py_ssize_t oldaccumulator;
- STRINGLIB_CHAR *p;
+ Py_ssize_t i;
/* empty string is an error */
- if (str->ptr >= str->end)
+ if (str->start >= str->end)
return -1;
- for (p = str->ptr; p < str->end; p++) {
- digitval = STRINGLIB_TODECIMAL(*p);
+ for (i = str->start; i < str->end; i++) {
+ digitval = Py_UNICODE_TODECIMAL(PyUnicode_READ_CHAR(str->str, i));
if (digitval < 0)
return -1;
/*
@@ -280,34 +318,36 @@ typedef struct {
lifetime of the iterator. can be empty */
SubString str;
- /* pointer to where we are inside field_name */
- STRINGLIB_CHAR *ptr;
+ /* index to where we are inside field_name */
+ Py_ssize_t index;
} FieldNameIterator;
static int
-FieldNameIterator_init(FieldNameIterator *self, STRINGLIB_CHAR *ptr,
- Py_ssize_t len)
+FieldNameIterator_init(FieldNameIterator *self, PyObject *s,
+ Py_ssize_t start, Py_ssize_t end)
{
- SubString_init(&self->str, ptr, len);
- self->ptr = self->str.ptr;
+ SubString_init(&self->str, s, start, end);
+ self->index = start;
return 1;
}
static int
_FieldNameIterator_attr(FieldNameIterator *self, SubString *name)
{
- STRINGLIB_CHAR c;
+ Py_UCS4 c;
- name->ptr = self->ptr;
+ name->str = self->str.str;
+ name->start = self->index;
/* return everything until '.' or '[' */
- while (self->ptr < self->str.end) {
- switch (c = *self->ptr++) {
+ while (self->index < self->str.end) {
+ c = PyUnicode_READ_CHAR(self->str.str, self->index++);
+ switch (c) {
case '[':
case '.':
/* backup so that we this character will be seen next time */
- self->ptr--;
+ self->index--;
break;
default:
continue;
@@ -315,7 +355,7 @@ _FieldNameIterator_attr(FieldNameIterator *self, SubString *name)
break;
}
/* end of string is okay */
- name->end = self->ptr;
+ name->end = self->index;
return 1;
}
@@ -323,13 +363,15 @@ static int
_FieldNameIterator_item(FieldNameIterator *self, SubString *name)
{
int bracket_seen = 0;
- STRINGLIB_CHAR c;
+ Py_UCS4 c;
- name->ptr = self->ptr;
+ name->str = self->str.str;
+ name->start = self->index;
/* return everything until ']' */
- while (self->ptr < self->str.end) {
- switch (c = *self->ptr++) {
+ while (self->index < self->str.end) {
+ c = PyUnicode_READ_CHAR(self->str.str, self->index++);
+ switch (c) {
case ']':
bracket_seen = 1;
break;
@@ -346,7 +388,7 @@ _FieldNameIterator_item(FieldNameIterator *self, SubString *name)
/* end of string is okay */
/* don't include the ']' */
- name->end = self->ptr-1;
+ name->end = self->index-1;
return 1;
}
@@ -356,10 +398,10 @@ FieldNameIterator_next(FieldNameIterator *self, int *is_attribute,
Py_ssize_t *name_idx, SubString *name)
{
/* check at end of input */
- if (self->ptr >= self->str.end)
+ if (self->index >= self->str.end)
return 1;
- switch (*self->ptr++) {
+ switch (PyUnicode_READ_CHAR(self->str.str, self->index++)) {
case '.':
*is_attribute = 1;
if (_FieldNameIterator_attr(self, name) == 0)
@@ -382,7 +424,7 @@ FieldNameIterator_next(FieldNameIterator *self, int *is_attribute,
}
/* empty string is an error */
- if (name->ptr == name->end) {
+ if (name->start == name->end) {
PyErr_SetString(PyExc_ValueError, "Empty attribute in format string");
return 0;
}
@@ -398,24 +440,23 @@ FieldNameIterator_next(FieldNameIterator *self, int *is_attribute,
'rest' is an iterator to return the rest
*/
static int
-field_name_split(STRINGLIB_CHAR *ptr, Py_ssize_t len, SubString *first,
+field_name_split(PyObject *str, Py_ssize_t start, Py_ssize_t end, SubString *first,
Py_ssize_t *first_idx, FieldNameIterator *rest,
AutoNumber *auto_number)
{
- STRINGLIB_CHAR c;
- STRINGLIB_CHAR *p = ptr;
- STRINGLIB_CHAR *end = ptr + len;
+ Py_UCS4 c;
+ Py_ssize_t i = start;
int field_name_is_empty;
int using_numeric_index;
/* find the part up until the first '.' or '[' */
- while (p < end) {
- switch (c = *p++) {
+ while (i < end) {
+ switch (c = PyUnicode_READ_CHAR(str, i++)) {
case '[':
case '.':
/* backup so that we this character is available to the
"rest" iterator */
- p--;
+ i--;
break;
default:
continue;
@@ -424,15 +465,15 @@ field_name_split(STRINGLIB_CHAR *ptr, Py_ssize_t len, SubString *first,
}
/* set up the return values */
- SubString_init(first, ptr, p - ptr);
- FieldNameIterator_init(rest, p, end - p);
+ SubString_init(first, str, start, i);
+ FieldNameIterator_init(rest, str, i, end);
/* see if "first" is an integer, in which case it's used as an index */
*first_idx = get_integer(first);
if (*first_idx == -1 && PyErr_Occurred())
return 0;
- field_name_is_empty = first->ptr >= first->end;
+ field_name_is_empty = first->start >= first->end;
/* If the field name is omitted or if we have a numeric index
specified, then we're doing numeric indexing into args. */
@@ -487,7 +528,7 @@ get_field_object(SubString *input, PyObject *args, PyObject *kwargs,
Py_ssize_t index;
FieldNameIterator rest;
- if (!field_name_split(input->ptr, input->end - input->ptr, &first,
+ if (!field_name_split(input->str, input->start, input->end, &first,
&index, &rest, auto_number)) {
goto error;
}
@@ -576,12 +617,8 @@ render_field(PyObject *fieldobj, SubString *format_spec, OutputString *output)
int ok = 0;
PyObject *result = NULL;
PyObject *format_spec_object = NULL;
- PyObject *(*formatter)(PyObject *, STRINGLIB_CHAR *, Py_ssize_t) = NULL;
- STRINGLIB_CHAR* format_spec_start = format_spec->ptr ?
- format_spec->ptr : NULL;
- Py_ssize_t format_spec_len = format_spec->ptr ?
- format_spec->end - format_spec->ptr : 0;
-
+ PyObject *(*formatter)(PyObject *, PyObject *, Py_ssize_t, Py_ssize_t) = NULL;
+
/* If we know the type exactly, skip the lookup of __format__ and just
call the formatter directly. */
if (PyUnicode_CheckExact(fieldobj))
@@ -597,39 +634,28 @@ render_field(PyObject *fieldobj, SubString *format_spec, OutputString *output)
if (formatter) {
/* we know exactly which formatter will be called when __format__ is
looked up, so call it directly, instead. */
- result = formatter(fieldobj, format_spec_start, format_spec_len);
+ result = formatter(fieldobj, format_spec->str,
+ format_spec->start, format_spec->end);
}
else {
/* We need to create an object out of the pointers we have, because
__format__ takes a string/unicode object for format_spec. */
- format_spec_object = STRINGLIB_NEW(format_spec_start,
- format_spec_len);
+ if (format_spec->str)
+ format_spec_object = PyUnicode_Substring(format_spec->str,
+ format_spec->start,
+ format_spec->end);
+ else
+ format_spec_object = PyUnicode_New(0, 0);
if (format_spec_object == NULL)
goto done;
result = PyObject_Format(fieldobj, format_spec_object);
}
- if (result == NULL)
+ if (result == NULL || PyUnicode_READY(result) == -1)
goto done;
-#if PY_VERSION_HEX >= 0x03000000
assert(PyUnicode_Check(result));
-#else
- assert(PyBytes_Check(result) || PyUnicode_Check(result));
-
- /* Convert result to our type. We could be str, and result could
- be unicode */
- {
- PyObject *tmp = STRINGLIB_TOSTR(result);
- if (tmp == NULL)
- goto done;
- Py_DECREF(result);
- result = tmp;
- }
-#endif
-
- ok = output_data(output,
- STRINGLIB_STR(result), STRINGLIB_LEN(result));
+ ok = output_data(output, result, 0, PyUnicode_GET_LENGTH(result));
done:
Py_XDECREF(format_spec_object);
Py_XDECREF(result);
@@ -638,23 +664,24 @@ done:
static int
parse_field(SubString *str, SubString *field_name, SubString *format_spec,
- STRINGLIB_CHAR *conversion)
+ Py_UCS4 *conversion)
{
/* Note this function works if the field name is zero length,
which is good. Zero length field names are handled later, in
field_name_split. */
- STRINGLIB_CHAR c = 0;
+ Py_UCS4 c = 0;
/* initialize these, as they may be empty */
*conversion = '\0';
- SubString_init(format_spec, NULL, 0);
+ SubString_init(format_spec, NULL, 0, 0);
/* Search for the field name. it's terminated by the end of
the string, or a ':' or '!' */
- field_name->ptr = str->ptr;
- while (str->ptr < str->end) {
- switch (c = *(str->ptr++)) {
+ field_name->str = str->str;
+ field_name->start = str->start;
+ while (str->start < str->end) {
+ switch ((c = PyUnicode_READ_CHAR(str->str, str->start++))) {
case ':':
case '!':
break;
@@ -667,26 +694,27 @@ parse_field(SubString *str, SubString *field_name, SubString *format_spec,
if (c == '!' || c == ':') {
/* we have a format specifier and/or a conversion */
/* don't include the last character */
- field_name->end = str->ptr-1;
+ field_name->end = str->start-1;
/* the format specifier is the rest of the string */
- format_spec->ptr = str->ptr;
+ format_spec->str = str->str;
+ format_spec->start = str->start;
format_spec->end = str->end;
/* see if there's a conversion specifier */
if (c == '!') {
/* there must be another character present */
- if (format_spec->ptr >= format_spec->end) {
+ if (format_spec->start >= format_spec->end) {
PyErr_SetString(PyExc_ValueError,
"end of format while looking for conversion "
"specifier");
return 0;
}
- *conversion = *(format_spec->ptr++);
+ *conversion = PyUnicode_READ_CHAR(format_spec->str, format_spec->start++);
/* if there is another character, it must be a colon */
- if (format_spec->ptr < format_spec->end) {
- c = *(format_spec->ptr++);
+ if (format_spec->start < format_spec->end) {
+ c = PyUnicode_READ_CHAR(format_spec->str, format_spec->start++);
if (c != ':') {
PyErr_SetString(PyExc_ValueError,
"expected ':' after format specifier");
@@ -697,7 +725,7 @@ parse_field(SubString *str, SubString *field_name, SubString *format_spec,
}
else
/* end of string, there's no format_spec or conversion */
- field_name->end = str->ptr;
+ field_name->end = str->start;
return 1;
}
@@ -716,9 +744,10 @@ typedef struct {
} MarkupIterator;
static int
-MarkupIterator_init(MarkupIterator *self, STRINGLIB_CHAR *ptr, Py_ssize_t len)
+MarkupIterator_init(MarkupIterator *self, PyObject *str,
+ Py_ssize_t start, Py_ssize_t end)
{
- SubString_init(&self->str, ptr, len);
+ SubString_init(&self->str, str, start, end);
return 1;
}
@@ -727,30 +756,30 @@ MarkupIterator_init(MarkupIterator *self, STRINGLIB_CHAR *ptr, Py_ssize_t len)
static int
MarkupIterator_next(MarkupIterator *self, SubString *literal,
int *field_present, SubString *field_name,
- SubString *format_spec, STRINGLIB_CHAR *conversion,
+ SubString *format_spec, Py_UCS4 *conversion,
int *format_spec_needs_expanding)
{
int at_end;
- STRINGLIB_CHAR c = 0;
- STRINGLIB_CHAR *start;
+ Py_UCS4 c = 0;
+ Py_ssize_t start;
int count;
Py_ssize_t len;
int markup_follows = 0;
/* initialize all of the output variables */
- SubString_init(literal, NULL, 0);
- SubString_init(field_name, NULL, 0);
- SubString_init(format_spec, NULL, 0);
+ SubString_init(literal, NULL, 0, 0);
+ SubString_init(field_name, NULL, 0, 0);
+ SubString_init(format_spec, NULL, 0, 0);
*conversion = '\0';
*format_spec_needs_expanding = 0;
*field_present = 0;
/* No more input, end of iterator. This is the normal exit
path. */
- if (self->str.ptr >= self->str.end)
+ if (self->str.start >= self->str.end)
return 1;
- start = self->str.ptr;
+ start = self->str.start;
/* First read any literal text. Read until the end of string, an
escaped '{' or '}', or an unescaped '{'. In order to never
@@ -759,8 +788,8 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal,
including the brace, but no format object. The next time
through, we'll return the rest of the literal, skipping past
the second consecutive brace. */
- while (self->str.ptr < self->str.end) {
- switch (c = *(self->str.ptr++)) {
+ while (self->str.start < self->str.end) {
+ switch (c = PyUnicode_READ_CHAR(self->str.str, self->str.start++)) {
case '{':
case '}':
markup_follows = 1;
@@ -771,10 +800,12 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal,
break;
}
- at_end = self->str.ptr >= self->str.end;
- len = self->str.ptr - start;
+ at_end = self->str.start >= self->str.end;
+ len = self->str.start - start;
- if ((c == '}') && (at_end || (c != *self->str.ptr))) {
+ if ((c == '}') && (at_end ||
+ (c != PyUnicode_READ_CHAR(self->str.str,
+ self->str.start)))) {
PyErr_SetString(PyExc_ValueError, "Single '}' encountered "
"in format string");
return 0;
@@ -785,10 +816,10 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal,
return 0;
}
if (!at_end) {
- if (c == *self->str.ptr) {
+ if (c == PyUnicode_READ_CHAR(self->str.str, self->str.start)) {
/* escaped } or {, skip it in the input. there is no
markup object following us, just this literal text */
- self->str.ptr++;
+ self->str.start++;
markup_follows = 0;
}
else
@@ -796,7 +827,8 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal,
}
/* record the literal text */
- literal->ptr = start;
+ literal->str = self->str.str;
+ literal->start = start;
literal->end = start + len;
if (!markup_follows)
@@ -808,12 +840,12 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal,
*field_present = 1;
count = 1;
- start = self->str.ptr;
+ start = self->str.start;
/* we know we can't have a zero length string, so don't worry
about that case */
- while (self->str.ptr < self->str.end) {
- switch (c = *(self->str.ptr++)) {
+ while (self->str.start < self->str.end) {
+ switch (c = PyUnicode_READ_CHAR(self->str.str, self->str.start++)) {
case '{':
/* the format spec needs to be recursively expanded.
this is an optimization, and not strictly needed */
@@ -826,7 +858,7 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal,
/* we're done. parse and get out */
SubString s;
- SubString_init(&s, start, self->str.ptr - 1 - start);
+ SubString_init(&s, self->str.str, start, self->str.start - 1);
if (parse_field(&s, field_name, format_spec, conversion) == 0)
return 0;
@@ -845,7 +877,7 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal,
/* do the !r or !s conversion on obj */
static PyObject *
-do_conversion(PyObject *obj, STRINGLIB_CHAR conversion)
+do_conversion(PyObject *obj, Py_UCS4 conversion)
{
/* XXX in pre-3.0, do we need to convert this to unicode, since it
might have returned a string? */
@@ -853,11 +885,9 @@ do_conversion(PyObject *obj, STRINGLIB_CHAR conversion)
case 'r':
return PyObject_Repr(obj);
case 's':
- return STRINGLIB_TOSTR(obj);
-#if PY_VERSION_HEX >= 0x03000000
+ return PyObject_Str(obj);
case 'a':
- return STRINGLIB_TOASCII(obj);
-#endif
+ return PyObject_ASCII(obj);
default:
if (conversion > 32 && conversion < 127) {
/* It's the ASCII subrange; casting to char is safe
@@ -889,7 +919,7 @@ do_conversion(PyObject *obj, STRINGLIB_CHAR conversion)
static int
output_markup(SubString *field_name, SubString *format_spec,
- int format_spec_needs_expanding, STRINGLIB_CHAR conversion,
+ int format_spec_needs_expanding, Py_UCS4 conversion,
OutputString *output, PyObject *args, PyObject *kwargs,
int recursion_depth, AutoNumber *auto_number)
{
@@ -906,7 +936,7 @@ output_markup(SubString *field_name, SubString *format_spec,
if (conversion != '\0') {
tmp = do_conversion(fieldobj, conversion);
- if (tmp == NULL)
+ if (tmp == NULL || PyUnicode_READY(tmp) == -1)
goto done;
/* do the assignment, transferring ownership: fieldobj = tmp */
@@ -919,14 +949,13 @@ output_markup(SubString *field_name, SubString *format_spec,
if (format_spec_needs_expanding) {
tmp = build_string(format_spec, args, kwargs, recursion_depth-1,
auto_number);
- if (tmp == NULL)
+ if (tmp == NULL || PyUnicode_READY(tmp) == -1)
goto done;
/* note that in the case we're expanding the format string,
tmp must be kept around until after the call to
render_field. */
- SubString_init(&expanded_format_spec,
- STRINGLIB_STR(tmp), STRINGLIB_LEN(tmp));
+ SubString_init(&expanded_format_spec, tmp, 0, PyUnicode_GET_LENGTH(tmp));
actual_format_spec = &expanded_format_spec;
}
else
@@ -961,14 +990,14 @@ do_markup(SubString *input, PyObject *args, PyObject *kwargs,
SubString literal;
SubString field_name;
SubString format_spec;
- STRINGLIB_CHAR conversion;
+ Py_UCS4 conversion;
- MarkupIterator_init(&iter, input->ptr, input->end - input->ptr);
+ MarkupIterator_init(&iter, input->str, input->start, input->end);
while ((result = MarkupIterator_next(&iter, &literal, &field_present,
&field_name, &format_spec,
&conversion,
&format_spec_needs_expanding)) == 2) {
- if (!output_data(output, literal.ptr, literal.end - literal.ptr))
+ if (!output_data(output, literal.str, literal.start, literal.end))
return 0;
if (field_present)
if (!output_markup(&field_name, &format_spec,
@@ -990,9 +1019,8 @@ build_string(SubString *input, PyObject *args, PyObject *kwargs,
{
OutputString output;
PyObject *result = NULL;
- Py_ssize_t count;
- output.obj = NULL; /* needed so cleanup code always works */
+ output.data = NULL; /* needed so cleanup code always works */
/* check the recursion level */
if (recursion_depth <= 0) {
@@ -1004,7 +1032,7 @@ build_string(SubString *input, PyObject *args, PyObject *kwargs,
/* initial size is the length of the format string, plus the size
increment. seems like a reasonable default */
if (!output_initialize(&output,
- input->end - input->ptr +
+ input->end - input->start +
INITIAL_SIZE_INCREMENT))
goto done;
@@ -1013,17 +1041,14 @@ build_string(SubString *input, PyObject *args, PyObject *kwargs,
goto done;
}
- count = output.ptr - STRINGLIB_STR(output.obj);
- if (STRINGLIB_RESIZE(&output.obj, count) < 0) {
+ result = PyUnicode_New(output.pos, output.maxchar);
+ if (!result)
goto done;
- }
-
- /* transfer ownership to result */
- result = output.obj;
- output.obj = NULL;
+ memcpy(PyUnicode_DATA(result), output.data, output.pos << (output.kind-1));
done:
- Py_XDECREF(output.obj);
+ if (output.data)
+ PyMem_Free(output.data);
return result;
}
@@ -1045,8 +1070,11 @@ do_string_format(PyObject *self, PyObject *args, PyObject *kwargs)
AutoNumber auto_number;
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+
AutoNumber_Init(&auto_number);
- SubString_init(&input, STRINGLIB_STR(self), STRINGLIB_LEN(self));
+ SubString_init(&input, self, 0, PyUnicode_GET_LENGTH(self));
return build_string(&input, args, kwargs, recursion_depth, &auto_number);
}
@@ -1069,7 +1097,7 @@ do_string_format_map(PyObject *self, PyObject *obj)
typedef struct {
PyObject_HEAD
- STRINGLIB_OBJECT *str;
+ PyUnicodeObject *str;
MarkupIterator it_markup;
} formatteriterobject;
@@ -1095,7 +1123,7 @@ formatteriter_next(formatteriterobject *it)
SubString literal;
SubString field_name;
SubString format_spec;
- STRINGLIB_CHAR conversion;
+ Py_UCS4 conversion;
int format_spec_needs_expanding;
int field_present;
int result = MarkupIterator_next(&it->it_markup, &literal, &field_present,
@@ -1139,7 +1167,8 @@ formatteriter_next(formatteriterobject *it)
Py_INCREF(conversion_str);
}
else
- conversion_str = STRINGLIB_NEW(&conversion, 1);
+ conversion_str = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
+ &conversion, 1);
if (conversion_str == NULL)
goto done;
@@ -1196,7 +1225,7 @@ static PyTypeObject PyFormatterIter_Type = {
describing the parsed elements. It's a wrapper around
stringlib/string_format.h's MarkupIterator */
static PyObject *
-formatter_parser(PyObject *ignored, STRINGLIB_OBJECT *self)
+formatter_parser(PyObject *ignored, PyUnicodeObject *self)
{
formatteriterobject *it;
@@ -1205,6 +1234,9 @@ formatter_parser(PyObject *ignored, STRINGLIB_OBJECT *self)
return NULL;
}
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+
it = PyObject_New(formatteriterobject, &PyFormatterIter_Type);
if (it == NULL)
return NULL;
@@ -1214,10 +1246,7 @@ formatter_parser(PyObject *ignored, STRINGLIB_OBJECT *self)
it->str = self;
/* initialize the contained MarkupIterator */
- MarkupIterator_init(&it->it_markup,
- STRINGLIB_STR(self),
- STRINGLIB_LEN(self));
-
+ MarkupIterator_init(&it->it_markup, (PyObject*)self, 0, PyUnicode_GET_LENGTH(self));
return (PyObject *)it;
}
@@ -1234,7 +1263,7 @@ formatter_parser(PyObject *ignored, STRINGLIB_OBJECT *self)
typedef struct {
PyObject_HEAD
- STRINGLIB_OBJECT *str;
+ PyUnicodeObject *str;
FieldNameIterator it_field;
} fieldnameiterobject;
@@ -1336,7 +1365,7 @@ static PyTypeObject PyFieldNameIter_Type = {
field_name_split. The iterator it returns is a
FieldNameIterator */
static PyObject *
-formatter_field_name_split(PyObject *ignored, STRINGLIB_OBJECT *self)
+formatter_field_name_split(PyObject *ignored, PyUnicodeObject *self)
{
SubString first;
Py_ssize_t first_idx;
@@ -1350,6 +1379,9 @@ formatter_field_name_split(PyObject *ignored, STRINGLIB_OBJECT *self)
return NULL;
}
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+
it = PyObject_New(fieldnameiterobject, &PyFieldNameIter_Type);
if (it == NULL)
return NULL;
@@ -1361,8 +1393,7 @@ formatter_field_name_split(PyObject *ignored, STRINGLIB_OBJECT *self)
/* Pass in auto_number = NULL. We'll return an empty string for
first_obj in that case. */
- if (!field_name_split(STRINGLIB_STR(self),
- STRINGLIB_LEN(self),
+ if (!field_name_split((PyObject*)self, 0, PyUnicode_GET_LENGTH(self),
&first, &first_idx, &it->it_field, NULL))
goto done;
diff --git a/Objects/stringlib/unicodedefs.h b/Objects/stringlib/unicodedefs.h
index 09dae6d..0c40b80 100644
--- a/Objects/stringlib/unicodedefs.h
+++ b/Objects/stringlib/unicodedefs.h
@@ -6,6 +6,8 @@
compiled as unicode. */
#define STRINGLIB_IS_UNICODE 1
+#define FASTSEARCH fastsearch
+#define STRINGLIB(F) stringlib_##F
#define STRINGLIB_OBJECT PyUnicodeObject
#define STRINGLIB_CHAR Py_UNICODE
#define STRINGLIB_TYPE_NAME "unicode"
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 640d14f..4bec4b6 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -20,10 +20,11 @@
>> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP))
#define MCACHE_HASH_METHOD(type, name) \
MCACHE_HASH((type)->tp_version_tag, \
- ((PyUnicodeObject *)(name))->hash)
+ ((PyASCIIObject *)(name))->hash)
#define MCACHE_CACHEABLE_NAME(name) \
PyUnicode_CheckExact(name) && \
- PyUnicode_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE
+ PyUnicode_READY(name) != -1 && \
+ PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE
struct method_cache_entry {
unsigned int version;
@@ -3489,7 +3490,7 @@ object_format(PyObject *self, PyObject *args)
if (self_as_str != NULL) {
/* Issue 7994: If we're converting to a string, we
should reject format specifications */
- if (PyUnicode_GET_SIZE(format_spec) > 0) {
+ if (PyUnicode_GET_LENGTH(format_spec) > 0) {
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"object.__format__ with a non-empty format "
"string is deprecated", 1) < 0) {
@@ -5122,14 +5123,21 @@ slot_tp_str(PyObject *self)
return res;
}
else {
- PyObject *ress;
+ /* PyObject *ress; */
PyErr_Clear();
res = slot_tp_repr(self);
if (!res)
return NULL;
+ /* XXX this is non-sensical. Why should we return
+ a bytes object from __str__. Is this code even
+ used? - mvl */
+ assert(0);
+ return res;
+ /*
ress = _PyUnicode_AsDefaultEncodedString(res);
Py_DECREF(res);
return ress;
+ */
}
}
@@ -6206,7 +6214,7 @@ super_getattro(PyObject *self, PyObject *name)
/* We want __class__ to return the class of the super object
(i.e. super, or a subclass), not the class of su->obj. */
skip = (PyUnicode_Check(name) &&
- PyUnicode_GET_SIZE(name) == 9 &&
+ PyUnicode_GET_LENGTH(name) == 9 &&
PyUnicode_CompareWithASCIIString(name, "__class__") == 0);
}
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index a85bac8..2ae1947 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -90,6 +90,19 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
extern "C" {
#endif
+#define _PyUnicode_WSTR(op) (((PyASCIIObject*)(op))->wstr)
+#define _PyUnicode_WSTR_LENGTH(op) (((PyCompactUnicodeObject*)(op))->wstr_length)
+#define _PyUnicode_LENGTH(op) (((PyASCIIObject *)(op))->length)
+#define _PyUnicode_STATE(op) (((PyASCIIObject *)(op))->state)
+#define _PyUnicode_HASH(op) (((PyASCIIObject *)(op))->hash)
+#define _PyUnicode_KIND(op) \
+ (assert(PyUnicode_Check(op)), \
+ ((PyASCIIObject *)(op))->state.kind)
+#define _PyUnicode_GET_LENGTH(op) \
+ (assert(PyUnicode_Check(op)), \
+ ((PyASCIIObject *)(op))->length)
+
+
/* This dictionary holds all interned unicode strings. Note that references
to strings in this dictionary are *not* counted in the string's ob_refcnt.
When the interned string reaches a refcnt of 0 the string deallocation
@@ -100,10 +113,6 @@ extern "C" {
*/
static PyObject *interned;
-/* Free list for Unicode objects */
-static PyUnicodeObject *free_list;
-static int numfree;
-
/* The empty Unicode object is shared to improve performance. */
static PyUnicodeObject *unicode_empty;
@@ -226,7 +235,7 @@ static BLOOM_MASK bloom_linebreak;
(BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch)))
Py_LOCAL_INLINE(BLOOM_MASK)
-make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len)
+make_bloom_mask(int kind, void* ptr, Py_ssize_t len)
{
/* calculate simple bloom-style bitmask for a given unicode string */
@@ -235,36 +244,58 @@ make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len)
mask = 0;
for (i = 0; i < len; i++)
- BLOOM_ADD(mask, ptr[i]);
+ BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i));
return mask;
}
-Py_LOCAL_INLINE(int)
-unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen)
-{
- Py_ssize_t i;
+#define BLOOM_MEMBER(mask, chr, str) \
+ (BLOOM(mask, chr) \
+ && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0))
- for (i = 0; i < setlen; i++)
- if (set[i] == chr)
- return 1;
+/* --- Unicode Object ----------------------------------------------------- */
- return 0;
-}
+static PyObject *
+substring(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t len);
-#define BLOOM_MEMBER(mask, chr, set, setlen) \
- BLOOM(mask, chr) && unicode_member(chr, set, setlen)
+static PyObject *
+fixup(PyUnicodeObject *self, Py_UCS4 (*fixfct)(PyUnicodeObject *s));
-/* --- Unicode Object ----------------------------------------------------- */
+Py_LOCAL_INLINE(char *) findchar(void *s, int kind,
+ Py_ssize_t size, Py_UCS4 ch,
+ int direction)
+{
+ /* like wcschr, but doesn't stop at NULL characters */
+ Py_ssize_t i;
+ if (direction == 1) {
+ for(i = 0; i < size; i++)
+ if (PyUnicode_READ(kind, s, i) == ch)
+ return (char*)s + PyUnicode_KIND_SIZE(kind, i);
+ }
+ else {
+ for(i = size-1; i >= 0; i--)
+ if (PyUnicode_READ(kind, s, i) == ch)
+ return (char*)s + PyUnicode_KIND_SIZE(kind, i);
+ }
+ return NULL;
+}
static int
unicode_resize(register PyUnicodeObject *unicode,
- Py_ssize_t length)
+ Py_ssize_t length)
{
void *oldstr;
+ /* Resizing is only supported for old unicode objects. */
+ assert(!PyUnicode_IS_COMPACT(unicode));
+ assert(_PyUnicode_WSTR(unicode) != NULL);
+
+ /* ... and only if they have not been readied yet, because
+ callees usually rely on the wstr representation when resizing. */
+ assert(unicode->data.any == NULL);
+
/* Shortcut if there's nothing much to do. */
- if (unicode->length == length)
+ if (_PyUnicode_WSTR_LENGTH(unicode) == length)
goto reset;
/* Resizing shared object (unicode_empty or single character
@@ -272,9 +303,9 @@ unicode_resize(register PyUnicodeObject *unicode,
instead ! */
if (unicode == unicode_empty ||
- (unicode->length == 1 &&
- unicode->str[0] < 256U &&
- unicode_latin1[unicode->str[0]] == unicode)) {
+ (_PyUnicode_WSTR_LENGTH(unicode) == 1 &&
+ _PyUnicode_WSTR(unicode)[0] < 256U &&
+ unicode_latin1[_PyUnicode_WSTR(unicode)[0]] == unicode)) {
PyErr_SetString(PyExc_SystemError,
"can't resize shared str objects");
return -1;
@@ -285,23 +316,31 @@ unicode_resize(register PyUnicodeObject *unicode,
safe to look at str[length] (without making any assumptions about what
it contains). */
- oldstr = unicode->str;
- unicode->str = PyObject_REALLOC(unicode->str,
- sizeof(Py_UNICODE) * (length + 1));
- if (!unicode->str) {
- unicode->str = (Py_UNICODE *)oldstr;
+ oldstr = _PyUnicode_WSTR(unicode);
+ _PyUnicode_WSTR(unicode) = PyObject_REALLOC(_PyUnicode_WSTR(unicode),
+ sizeof(Py_UNICODE) * (length + 1));
+ if (!_PyUnicode_WSTR(unicode)) {
+ _PyUnicode_WSTR(unicode) = (Py_UNICODE *)oldstr;
PyErr_NoMemory();
return -1;
}
- unicode->str[length] = 0;
- unicode->length = length;
+ _PyUnicode_WSTR(unicode)[length] = 0;
+ _PyUnicode_WSTR_LENGTH(unicode) = length;
reset:
- /* Reset the object caches */
- if (unicode->defenc) {
- Py_CLEAR(unicode->defenc);
+ if (unicode->data.any != NULL) {
+ PyObject_FREE(unicode->data.any);
+ if (unicode->_base.utf8 && unicode->_base.utf8 != unicode->data.any) {
+ PyObject_FREE(unicode->_base.utf8);
+ }
+ unicode->_base.utf8 = NULL;
+ unicode->_base.utf8_length = 0;
+ unicode->data.any = NULL;
+ _PyUnicode_LENGTH(unicode) = 0;
+ _PyUnicode_STATE(unicode).interned = _PyUnicode_STATE(unicode).interned;
+ _PyUnicode_STATE(unicode).kind = PyUnicode_WCHAR_KIND;
}
- unicode->hash = -1;
+ _PyUnicode_HASH(unicode) = -1;
return 0;
}
@@ -315,10 +354,15 @@ unicode_resize(register PyUnicodeObject *unicode,
*/
+#ifdef Py_DEBUG
+int unicode_old_new_calls = 0;
+#endif
+
static PyUnicodeObject *
_PyUnicode_New(Py_ssize_t length)
{
register PyUnicodeObject *unicode;
+ size_t new_size;
/* Optimization for empty strings */
if (length == 0 && unicode_empty != NULL) {
@@ -330,40 +374,26 @@ _PyUnicode_New(Py_ssize_t length)
if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) {
return (PyUnicodeObject *)PyErr_NoMemory();
}
-
- /* Unicode freelist & memory allocation */
- if (free_list) {
- unicode = free_list;
- free_list = *(PyUnicodeObject **)unicode;
- numfree--;
- if (unicode->str) {
- /* Keep-Alive optimization: we only upsize the buffer,
- never downsize it. */
- if ((unicode->length < length) &&
- unicode_resize(unicode, length) < 0) {
- PyObject_DEL(unicode->str);
- unicode->str = NULL;
- }
- }
- else {
- size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);
- unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size);
- }
- PyObject_INIT(unicode, &PyUnicode_Type);
- }
- else {
- size_t new_size;
- unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type);
- if (unicode == NULL)
- return NULL;
- new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);
- unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size);
+ if (length < 0) {
+ PyErr_SetString(PyExc_SystemError,
+ "Negative size passed to _PyUnicode_New");
+ return NULL;
}
- if (!unicode->str) {
+#ifdef Py_DEBUG
+ ++unicode_old_new_calls;
+#endif
+
+ unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type);
+ if (unicode == NULL)
+ return NULL;
+ new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);
+ _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size);
+ if (!_PyUnicode_WSTR(unicode)) {
PyErr_NoMemory();
goto onError;
}
+
/* Initialize the first element to guard against cases where
* the caller fails before initializing str -- unicode_resize()
* reads str[0], and the Keep-Alive optimization can keep memory
@@ -371,12 +401,19 @@ _PyUnicode_New(Py_ssize_t length)
* We don't want unicode_resize to read uninitialized memory in
* that case.
*/
- unicode->str[0] = 0;
- unicode->str[length] = 0;
- unicode->length = length;
- unicode->hash = -1;
- unicode->state = 0;
- unicode->defenc = NULL;
+ _PyUnicode_WSTR(unicode)[0] = 0;
+ _PyUnicode_WSTR(unicode)[length] = 0;
+ _PyUnicode_WSTR_LENGTH(unicode) = length;
+ _PyUnicode_HASH(unicode) = -1;
+ _PyUnicode_STATE(unicode).interned = 0;
+ _PyUnicode_STATE(unicode).kind = 0;
+ _PyUnicode_STATE(unicode).compact = 0;
+ _PyUnicode_STATE(unicode).ready = 0;
+ _PyUnicode_STATE(unicode).ascii = 0;
+ unicode->data.any = NULL;
+ _PyUnicode_LENGTH(unicode) = 0;
+ unicode->_base.utf8 = NULL;
+ unicode->_base.utf8_length = 0;
return unicode;
onError:
@@ -387,6 +424,467 @@ _PyUnicode_New(Py_ssize_t length)
return NULL;
}
+#ifdef Py_DEBUG
+int unicode_new_new_calls = 0;
+
+/* Functions wrapping macros for use in debugger */
+char *_PyUnicode_utf8(void *unicode){
+ return _PyUnicode_UTF8(unicode);
+}
+
+void *_PyUnicode_compact_data(void *unicode) {
+ return _PyUnicode_COMPACT_DATA(unicode);
+}
+void *_PyUnicode_data(void *unicode){
+ printf("obj %p\n", unicode);
+ printf("compact %d\n", PyUnicode_IS_COMPACT(unicode));
+ printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode));
+ printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1)));
+ printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1)));
+ printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode));
+ return PyUnicode_DATA(unicode);
+}
+#endif
+
+PyObject *
+PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar)
+{
+ PyObject *obj;
+ PyCompactUnicodeObject *unicode;
+ void *data;
+ int kind_state;
+ int is_sharing = 0, is_ascii = 0;
+ Py_ssize_t char_size;
+ Py_ssize_t struct_size;
+
+ /* Optimization for empty strings */
+ if (size == 0 && unicode_empty != NULL) {
+ Py_INCREF(unicode_empty);
+ return (PyObject *)unicode_empty;
+ }
+
+#ifdef Py_DEBUG
+ ++unicode_new_new_calls;
+#endif
+
+ struct_size = sizeof(PyCompactUnicodeObject);
+ if (maxchar < 128) {
+ kind_state = PyUnicode_1BYTE_KIND;
+ char_size = 1;
+ is_ascii = 1;
+ struct_size = sizeof(PyASCIIObject);
+ }
+ else if (maxchar < 256) {
+ kind_state = PyUnicode_1BYTE_KIND;
+ char_size = 1;
+ }
+ else if (maxchar < 65536) {
+ kind_state = PyUnicode_2BYTE_KIND;
+ char_size = 2;
+ if (sizeof(wchar_t) == 2)
+ is_sharing = 1;
+ }
+ else {
+ kind_state = PyUnicode_4BYTE_KIND;
+ char_size = 4;
+ if (sizeof(wchar_t) == 4)
+ is_sharing = 1;
+ }
+
+ /* Ensure we won't overflow the size. */
+ if (size < 0) {
+ PyErr_SetString(PyExc_SystemError,
+ "Negative size passed to PyUnicode_New");
+ return NULL;
+ }
+ if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1))
+ return PyErr_NoMemory();
+
+ /* Duplicated allocation code from _PyObject_New() instead of a call to
+ * PyObject_New() so we are able to allocate space for the object and
+ * it's data buffer.
+ */
+ obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size);
+ if (obj == NULL)
+ return PyErr_NoMemory();
+ obj = PyObject_INIT(obj, &PyUnicode_Type);
+ if (obj == NULL)
+ return NULL;
+
+ unicode = (PyCompactUnicodeObject *)obj;
+ if (is_ascii)
+ data = ((PyASCIIObject*)obj) + 1;
+ else
+ data = unicode + 1;
+ _PyUnicode_LENGTH(unicode) = size;
+ _PyUnicode_HASH(unicode) = -1;
+ _PyUnicode_STATE(unicode).interned = 0;
+ _PyUnicode_STATE(unicode).kind = kind_state;
+ _PyUnicode_STATE(unicode).compact = 1;
+ _PyUnicode_STATE(unicode).ready = 1;
+ _PyUnicode_STATE(unicode).ascii = is_ascii;
+ if (is_ascii) {
+ ((char*)data)[size] = 0;
+ _PyUnicode_WSTR(unicode) = NULL;
+ }
+ else if (kind_state == PyUnicode_1BYTE_KIND) {
+ ((char*)data)[size] = 0;
+ _PyUnicode_WSTR(unicode) = NULL;
+ _PyUnicode_WSTR_LENGTH(unicode) = 0;
+ unicode->utf8_length = 0;
+ unicode->utf8 = NULL;
+ }
+ else {
+ unicode->utf8 = NULL;
+ if (kind_state == PyUnicode_2BYTE_KIND)
+ ((Py_UCS2*)data)[size] = 0;
+ else /* kind_state == PyUnicode_4BYTE_KIND */
+ ((Py_UCS4*)data)[size] = 0;
+ if (is_sharing) {
+ _PyUnicode_WSTR_LENGTH(unicode) = size;
+ _PyUnicode_WSTR(unicode) = (wchar_t *)data;
+ }
+ else {
+ _PyUnicode_WSTR_LENGTH(unicode) = 0;
+ _PyUnicode_WSTR(unicode) = NULL;
+ }
+ }
+ return obj;
+}
+
+#if SIZEOF_WCHAR_T == 2
+/* Helper function to convert a 16-bits wchar_t representation to UCS4, this
+ will decode surrogate pairs, the other conversions are implemented as macros
+ for efficency.
+
+ This function assumes that unicode can hold one more code point than wstr
+ characters for a terminating null character. */
+static int
+unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end,
+ PyUnicodeObject *unicode)
+{
+ const wchar_t *iter;
+ Py_UCS4 *ucs4_out;
+
+ assert(unicode && PyUnicode_Check(unicode));
+ assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND);
+ ucs4_out = PyUnicode_4BYTE_DATA(unicode);
+
+ for (iter = begin; iter < end; ) {
+ assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) +
+ _PyUnicode_GET_LENGTH(unicode)));
+ if (*iter >= 0xD800 && *iter <= 0xDBFF
+ && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF)
+ {
+ *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000;
+ iter += 2;
+ }
+ else {
+ *ucs4_out++ = *iter;
+ iter++;
+ }
+ }
+ assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) +
+ _PyUnicode_GET_LENGTH(unicode)));
+
+ return 0;
+}
+#endif
+
+int
+PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start,
+ PyObject *from, Py_ssize_t from_start,
+ Py_ssize_t how_many)
+{
+ int from_kind;
+ int to_kind;
+
+ assert(PyUnicode_Check(from));
+ assert(PyUnicode_Check(to));
+
+ if (PyUnicode_READY(from))
+ return -1;
+ if (PyUnicode_READY(to))
+ return -1;
+
+ from_kind = PyUnicode_KIND(from);
+ to_kind = PyUnicode_KIND(to);
+
+ if (from_kind == to_kind) {
+ const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(to);
+ Py_MEMCPY(PyUnicode_1BYTE_DATA(to) + (to_start * char_size),
+ PyUnicode_1BYTE_DATA(from) + (from_start * char_size),
+ how_many * char_size);
+ return 0;
+ }
+
+ switch (from_kind) {
+ case PyUnicode_1BYTE_KIND:
+ switch (to_kind) {
+ case PyUnicode_2BYTE_KIND:
+ PyUnicode_CONVERT_BYTES(
+ unsigned char, Py_UCS2,
+ PyUnicode_1BYTE_DATA(from) + from_start,
+ PyUnicode_1BYTE_DATA(from) + from_start + how_many,
+ PyUnicode_2BYTE_DATA(to) + to_start
+ );
+ break;
+ case PyUnicode_4BYTE_KIND:
+ PyUnicode_CONVERT_BYTES(
+ unsigned char, Py_UCS4,
+ PyUnicode_1BYTE_DATA(from) + from_start,
+ PyUnicode_1BYTE_DATA(from) + from_start + how_many,
+ PyUnicode_4BYTE_DATA(to) + to_start
+ );
+ break;
+ default:
+ goto invalid_state;
+ }
+ break;
+ case PyUnicode_2BYTE_KIND:
+ switch (to_kind) {
+ case PyUnicode_1BYTE_KIND:
+ PyUnicode_CONVERT_BYTES(
+ Py_UCS2, unsigned char,
+ PyUnicode_2BYTE_DATA(from) + from_start,
+ PyUnicode_2BYTE_DATA(from) + from_start + how_many,
+ PyUnicode_1BYTE_DATA(to) + to_start
+ );
+ break;
+ case PyUnicode_4BYTE_KIND:
+ PyUnicode_CONVERT_BYTES(
+ Py_UCS2, Py_UCS4,
+ PyUnicode_2BYTE_DATA(from) + from_start,
+ PyUnicode_2BYTE_DATA(from) + from_start + how_many,
+ PyUnicode_4BYTE_DATA(to) + to_start
+ );
+ break;
+ default:
+ goto invalid_state;
+ }
+ break;
+ case PyUnicode_4BYTE_KIND:
+ switch (to_kind) {
+ case PyUnicode_1BYTE_KIND:
+ PyUnicode_CONVERT_BYTES(
+ Py_UCS4, unsigned char,
+ PyUnicode_4BYTE_DATA(from) + from_start,
+ PyUnicode_4BYTE_DATA(from) + from_start + how_many,
+ PyUnicode_1BYTE_DATA(to) + to_start
+ );
+ break;
+ case PyUnicode_2BYTE_KIND:
+ PyUnicode_CONVERT_BYTES(
+ Py_UCS4, Py_UCS2,
+ PyUnicode_4BYTE_DATA(from) + from_start,
+ PyUnicode_4BYTE_DATA(from) + from_start + how_many,
+ PyUnicode_2BYTE_DATA(to) + to_start
+ );
+ break;
+ default:
+ goto invalid_state;
+ }
+ break;
+ default:
+ goto invalid_state;
+ }
+ return 0;
+
+invalid_state:
+ PyErr_Format(PyExc_ValueError,
+ "Impossible kind state (from=%i, to=%i) "
+ "in PyUnicode_CopyCharacters",
+ from_kind, to_kind);
+ return -1;
+}
+
+int
+_PyUnicode_FindMaxCharAndNumSurrogatePairs(const wchar_t *begin,
+ const wchar_t *end,
+ Py_UCS4 *maxchar,
+ Py_ssize_t *num_surrogates)
+{
+ const wchar_t *iter;
+
+ if (num_surrogates == NULL || maxchar == NULL) {
+ PyErr_SetString(PyExc_SystemError,
+ "unexpected NULL arguments to "
+ "PyUnicode_FindMaxCharAndNumSurrogatePairs");
+ return -1;
+ }
+
+ *num_surrogates = 0;
+ *maxchar = 0;
+
+ for (iter = begin; iter < end; ) {
+ if (*iter > *maxchar)
+ *maxchar = *iter;
+#if SIZEOF_WCHAR_T == 2
+ if (*iter >= 0xD800 && *iter <= 0xDBFF
+ && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF)
+ {
+ Py_UCS4 surrogate_val;
+ surrogate_val = (((iter[0] & 0x3FF)<<10)
+ | (iter[1] & 0x3FF)) + 0x10000;
+ ++(*num_surrogates);
+ if (surrogate_val > *maxchar)
+ *maxchar = surrogate_val;
+ iter += 2;
+ }
+ else
+ iter++;
+#else
+ iter++;
+#endif
+ }
+ return 0;
+}
+
+#ifdef Py_DEBUG
+int unicode_ready_calls = 0;
+#endif
+
+int
+_PyUnicode_Ready(PyUnicodeObject *unicode)
+{
+ wchar_t *end;
+ Py_UCS4 maxchar = 0;
+ Py_ssize_t num_surrogates;
+#if SIZEOF_WCHAR_T == 2
+ Py_ssize_t length_wo_surrogates;
+#endif
+
+ assert(PyUnicode_Check(unicode));
+
+ if (unicode->data.any != NULL) {
+ assert(PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND);
+ return 0;
+ }
+
+ /* _PyUnicode_Ready() is only intented for old-style API usage where
+ * strings were created using _PyObject_New() and where no canonical
+ * representation (the str field) has been set yet aka strings
+ * which are not yet ready.
+ */
+ assert(_PyUnicode_WSTR(unicode) != NULL);
+ assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND);
+ assert(!PyUnicode_IS_COMPACT(unicode));
+ assert(!PyUnicode_IS_READY(unicode));
+ /* Actually, it should neither be interned nor be anything else: */
+ assert(_PyUnicode_STATE(unicode).interned == 0);
+ assert(unicode->_base.utf8 == NULL);
+
+#ifdef Py_DEBUG
+ ++unicode_ready_calls;
+#endif
+
+ end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode);
+ if (_PyUnicode_FindMaxCharAndNumSurrogatePairs(_PyUnicode_WSTR(unicode), end,
+ &maxchar,
+ &num_surrogates) == -1) {
+ assert(0 && "PyUnicode_FindMaxCharAndNumSurrogatePairs failed");
+ return -1;
+ }
+
+ if (maxchar < 256) {
+ unicode->data.any = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1);
+ if (!unicode->data.any) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ PyUnicode_CONVERT_BYTES(wchar_t, unsigned char,
+ _PyUnicode_WSTR(unicode), end,
+ PyUnicode_1BYTE_DATA(unicode));
+ PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0';
+ _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode);
+ _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND;
+ if (maxchar < 128) {
+ unicode->_base.utf8 = unicode->data.any;
+ unicode->_base.utf8_length = _PyUnicode_WSTR_LENGTH(unicode);
+ }
+ else {
+ unicode->_base.utf8 = NULL;
+ unicode->_base.utf8_length = 0;
+ }
+ PyObject_FREE(_PyUnicode_WSTR(unicode));
+ _PyUnicode_WSTR(unicode) = NULL;
+ _PyUnicode_WSTR_LENGTH(unicode) = 0;
+ }
+ /* In this case we might have to convert down from 4-byte native
+ wchar_t to 2-byte unicode. */
+ else if (maxchar < 65536) {
+ assert(num_surrogates == 0 &&
+ "FindMaxCharAndNumSurrogatePairs() messed up");
+
+ if (sizeof(wchar_t) == 2) {
+ /* We can share representations and are done. */
+ unicode->data.any = _PyUnicode_WSTR(unicode);
+ PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0';
+ _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode);
+ _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND;
+ unicode->_base.utf8 = NULL;
+ unicode->_base.utf8_length = 0;
+ }
+ else {
+ assert(sizeof(wchar_t) == 4);
+
+ unicode->data.any = PyObject_MALLOC(
+ 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1));
+ if (!unicode->data.any) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2,
+ _PyUnicode_WSTR(unicode), end,
+ PyUnicode_2BYTE_DATA(unicode));
+ PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0';
+ _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode);
+ _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND;
+ unicode->_base.utf8 = NULL;
+ unicode->_base.utf8_length = 0;
+ PyObject_FREE(_PyUnicode_WSTR(unicode));
+ _PyUnicode_WSTR(unicode) = NULL;
+ _PyUnicode_WSTR_LENGTH(unicode) = 0;
+ }
+ }
+ /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */
+ else {
+#if SIZEOF_WCHAR_T == 2
+ /* in case the native representation is 2-bytes, we need to allocate a
+ new normalized 4-byte version. */
+ length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates;
+ unicode->data.any = PyObject_MALLOC(4 * (length_wo_surrogates + 1));
+ if (!unicode->data.any) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ _PyUnicode_LENGTH(unicode) = length_wo_surrogates;
+ _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND;
+ unicode->_base.utf8 = NULL;
+ unicode->_base.utf8_length = 0;
+ if (unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end,
+ unicode) < 0) {
+ assert(0 && "ConvertWideCharToUCS4 failed");
+ return -1;
+ }
+ PyObject_FREE(_PyUnicode_WSTR(unicode));
+ _PyUnicode_WSTR(unicode) = NULL;
+ _PyUnicode_WSTR_LENGTH(unicode) = 0;
+#else
+ assert(num_surrogates == 0);
+
+ unicode->data.any = _PyUnicode_WSTR(unicode);
+ _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode);
+ unicode->_base.utf8 = NULL;
+ unicode->_base.utf8_length = 0;
+ _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND;
+#endif
+ PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0';
+ }
+ _PyUnicode_STATE(unicode).ready = 1;
+ return 0;
+}
+
static void
unicode_dealloc(register PyUnicodeObject *unicode)
{
@@ -409,25 +907,19 @@ unicode_dealloc(register PyUnicodeObject *unicode)
Py_FatalError("Inconsistent interned string state.");
}
- if (PyUnicode_CheckExact(unicode) &&
- numfree < PyUnicode_MAXFREELIST) {
- /* Keep-Alive optimization */
- if (unicode->length >= KEEPALIVE_SIZE_LIMIT) {
- PyObject_DEL(unicode->str);
- unicode->str = NULL;
- unicode->length = 0;
- }
- if (unicode->defenc) {
- Py_CLEAR(unicode->defenc);
- }
- /* Add to free list */
- *(PyUnicodeObject **)unicode = free_list;
- free_list = unicode;
- numfree++;
+ if (_PyUnicode_WSTR(unicode) &&
+ (!PyUnicode_IS_READY(unicode) ||
+ _PyUnicode_WSTR(unicode) != PyUnicode_DATA(unicode)))
+ PyObject_DEL(_PyUnicode_WSTR(unicode));
+ if (_PyUnicode_UTF8(unicode) && _PyUnicode_UTF8(unicode) != PyUnicode_DATA(unicode))
+ PyObject_DEL(unicode->_base.utf8);
+
+ if (PyUnicode_IS_COMPACT(unicode)) {
+ Py_TYPE(unicode)->tp_free((PyObject *)unicode);
}
else {
- PyObject_DEL(unicode->str);
- Py_XDECREF(unicode->defenc);
+ if (unicode->data.any)
+ PyObject_DEL(unicode->data.any);
Py_TYPE(unicode)->tp_free((PyObject *)unicode);
}
}
@@ -443,21 +935,26 @@ _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length)
return -1;
}
v = *unicode;
- if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) {
+ if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0 ||
+ PyUnicode_IS_COMPACT(v) || _PyUnicode_WSTR(v) == NULL) {
PyErr_BadInternalCall();
return -1;
}
/* Resizing unicode_empty and single character objects is not
- possible since these are being shared. We simply return a fresh
- copy with the same Unicode content. */
- if (v->length != length &&
- (v == unicode_empty || v->length == 1)) {
+ possible since these are being shared.
+ The same goes for new-representation unicode objects or objects which
+ have already been readied.
+ For these, we simply return a fresh copy with the same Unicode content.
+ */
+ if ((_PyUnicode_WSTR_LENGTH(v) != length &&
+ (v == unicode_empty || _PyUnicode_WSTR_LENGTH(v) == 1)) ||
+ PyUnicode_IS_COMPACT(v) || v->data.any) {
PyUnicodeObject *w = _PyUnicode_New(length);
if (w == NULL)
return -1;
- Py_UNICODE_COPY(w->str, v->str,
- length < v->length ? length : v->length);
+ Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(v),
+ length < _PyUnicode_WSTR_LENGTH(v) ? length : _PyUnicode_WSTR_LENGTH(v));
Py_DECREF(*unicode);
*unicode = w;
return 0;
@@ -474,44 +971,85 @@ PyUnicode_Resize(PyObject **unicode, Py_ssize_t length)
return _PyUnicode_Resize((PyUnicodeObject **)unicode, length);
}
+static PyObject*
+get_latin1_char(unsigned char ch)
+{
+ PyUnicodeObject *unicode = unicode_latin1[ch];
+ if (!unicode) {
+ unicode = (PyUnicodeObject *)PyUnicode_New(1, ch);
+ if (!unicode)
+ return NULL;
+ PyUnicode_1BYTE_DATA(unicode)[0] = ch;
+ unicode_latin1[ch] = unicode;
+ }
+ Py_INCREF(unicode);
+ return (PyObject *)unicode;
+}
+
PyObject *
PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)
{
PyUnicodeObject *unicode;
+ Py_UCS4 maxchar = 0;
+ Py_ssize_t num_surrogates;
+
+ if (u == NULL)
+ return (PyObject*)_PyUnicode_New(size);
/* If the Unicode data is known at construction time, we can apply
some optimizations which share commonly used objects. */
- if (u != NULL) {
- /* Optimization for empty strings */
- if (size == 0 && unicode_empty != NULL) {
- Py_INCREF(unicode_empty);
- return (PyObject *)unicode_empty;
- }
-
- /* Single character Unicode objects in the Latin-1 range are
- shared when using this constructor */
- if (size == 1 && *u < 256) {
- unicode = unicode_latin1[*u];
- if (!unicode) {
- unicode = _PyUnicode_New(1);
- if (!unicode)
- return NULL;
- unicode->str[0] = *u;
- unicode_latin1[*u] = unicode;
- }
- Py_INCREF(unicode);
- return (PyObject *)unicode;
- }
+ /* Optimization for empty strings */
+ if (size == 0 && unicode_empty != NULL) {
+ Py_INCREF(unicode_empty);
+ return (PyObject *)unicode_empty;
}
- unicode = _PyUnicode_New(size);
+ /* Single character Unicode objects in the Latin-1 range are
+ shared when using this constructor */
+ if (size == 1 && *u < 256)
+ return get_latin1_char((unsigned char)*u);
+
+ /* If not empty and not single character, copy the Unicode data
+ into the new object */
+ if (_PyUnicode_FindMaxCharAndNumSurrogatePairs(u, u + size, &maxchar,
+ &num_surrogates) == -1)
+ return NULL;
+
+ unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates,
+ maxchar);
if (!unicode)
return NULL;
- /* Copy the Unicode data into the new object */
- if (u != NULL)
- Py_UNICODE_COPY(unicode->str, u, size);
+ switch (PyUnicode_KIND(unicode)) {
+ case PyUnicode_1BYTE_KIND:
+ PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char,
+ u, u + size, PyUnicode_1BYTE_DATA(unicode));
+ break;
+ case PyUnicode_2BYTE_KIND:
+#if Py_UNICODE_SIZE == 2
+ Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2);
+#else
+ PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2,
+ u, u + size, PyUnicode_2BYTE_DATA(unicode));
+#endif
+ break;
+ case PyUnicode_4BYTE_KIND:
+#if SIZEOF_WCHAR_T == 2
+ /* This is the only case which has to process surrogates, thus
+ a simple copy loop is not enough and we need a function. */
+ if (unicode_convert_wchar_to_ucs4(u, u + size, unicode) < 0) {
+ Py_DECREF(unicode);
+ return NULL;
+ }
+#else
+ assert(num_surrogates == 0);
+ Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4);
+#endif
+ break;
+ default:
+ assert(0 && "Impossible state");
+ }
return (PyObject *)unicode;
}
@@ -541,18 +1079,8 @@ PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
/* Single characters are shared when using this constructor.
Restrict to ASCII, since the input must be UTF-8. */
- if (size == 1 && Py_CHARMASK(*u) < 128) {
- unicode = unicode_latin1[Py_CHARMASK(*u)];
- if (!unicode) {
- unicode = _PyUnicode_New(1);
- if (!unicode)
- return NULL;
- unicode->str[0] = Py_CHARMASK(*u);
- unicode_latin1[Py_CHARMASK(*u)] = unicode;
- }
- Py_INCREF(unicode);
- return (PyObject *)unicode;
- }
+ if (size == 1 && Py_CHARMASK(*u) < 128)
+ return get_latin1_char(Py_CHARMASK(*u));
return PyUnicode_DecodeUTF8(u, size, NULL);
}
@@ -576,76 +1104,196 @@ PyUnicode_FromString(const char *u)
return PyUnicode_FromStringAndSize(u, size);
}
-#ifdef HAVE_WCHAR_H
-
-#if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
-# define CONVERT_WCHAR_TO_SURROGATES
-#endif
-
-#ifdef CONVERT_WCHAR_TO_SURROGATES
-
-/* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need
- to convert from UTF32 to UTF16. */
+PyObject*
+PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size)
+{
+ PyObject *res;
+ unsigned char max = 127;
+ Py_ssize_t i;
+ for (i = 0; i < size; i++) {
+ if (u[i] & 0x80) {
+ max = 255;
+ break;
+ }
+ }
+ res = PyUnicode_New(size, max);
+ if (!res)
+ return NULL;
+ memcpy(PyUnicode_1BYTE_DATA(res), u, size);
+ return res;
+}
-PyObject *
-PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size)
+PyObject*
+PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size)
{
- PyUnicodeObject *unicode;
- register Py_ssize_t i;
- Py_ssize_t alloc;
- const wchar_t *orig_w;
+ PyObject *res;
+ Py_UCS2 max = 0;
+ Py_ssize_t i;
+ for (i = 0; i < size; i++)
+ if (u[i] > max)
+ max = u[i];
+ res = PyUnicode_New(size, max);
+ if (!res)
+ return NULL;
+ if (max >= 256)
+ memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size);
+ else
+ for (i = 0; i < size; i++)
+ PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i];
+ return res;
+}
- if (w == NULL) {
- if (size == 0)
- return PyUnicode_FromStringAndSize(NULL, 0);
- PyErr_BadInternalCall();
+PyObject*
+PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size)
+{
+ PyObject *res;
+ Py_UCS4 max = 0;
+ Py_ssize_t i;
+ for (i = 0; i < size; i++)
+ if (u[i] > max)
+ max = u[i];
+ res = PyUnicode_New(size, max);
+ if (!res)
return NULL;
+ if (max >= 0x10000)
+ memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size);
+ else {
+ int kind = PyUnicode_KIND(res);
+ void *data = PyUnicode_DATA(res);
+ for (i = 0; i < size; i++)
+ PyUnicode_WRITE(kind, data, i, u[i]);
}
+ return res;
+}
- if (size == -1) {
- size = wcslen(w);
+PyObject*
+PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size)
+{
+ switch(kind) {
+ case PyUnicode_1BYTE_KIND:
+ return PyUnicode_FromUCS1(buffer, size);
+ case PyUnicode_2BYTE_KIND:
+ return PyUnicode_FromUCS2(buffer, size);
+ case PyUnicode_4BYTE_KIND:
+ return PyUnicode_FromUCS4(buffer, size);
}
+ assert(0);
+ return NULL;
+}
- alloc = size;
- orig_w = w;
- for (i = size; i > 0; i--) {
- if (*w > 0xFFFF)
- alloc++;
- w++;
- }
- w = orig_w;
- unicode = _PyUnicode_New(alloc);
- if (!unicode)
+
+/* Widen Unicode objects to larger buffers.
+ Return NULL if the string is too wide already. */
+
+void*
+_PyUnicode_AsKind(PyObject *s, unsigned int kind)
+{
+ Py_ssize_t i;
+ Py_ssize_t len = PyUnicode_GET_LENGTH(s);
+ void *d = PyUnicode_DATA(s);
+ unsigned int skind = PyUnicode_KIND(s);
+ if (PyUnicode_KIND(s) >= kind) {
+ PyErr_SetString(PyExc_RuntimeError, "invalid widening attempt");
return NULL;
+ }
+ switch(kind) {
+ case PyUnicode_2BYTE_KIND: {
+ Py_UCS2 *result = PyMem_Malloc(PyUnicode_GET_LENGTH(s) * sizeof(Py_UCS2));
+ if (!result) {
+ PyErr_NoMemory();
+ return 0;
+ }
+ for (i = 0; i < len; i++)
+ result[i] = ((Py_UCS1*)d)[i];
+ return result;
+ }
+ case PyUnicode_4BYTE_KIND: {
+ Py_UCS4 *result = PyMem_Malloc(PyUnicode_GET_LENGTH(s) * sizeof(Py_UCS4));
+ if (!result) {
+ PyErr_NoMemory();
+ return 0;
+ }
+ for (i = 0; i < len; i++)
+ result[i] = PyUnicode_READ(skind, d, i);
+ return result;
+ }
+ }
+ Py_FatalError("invalid kind");
+ return NULL;
+}
- /* Copy the wchar_t data into the new object */
- {
- register Py_UNICODE *u;
- u = PyUnicode_AS_UNICODE(unicode);
- for (i = size; i > 0; i--) {
- if (*w > 0xFFFF) {
- wchar_t ordinal = *w++;
- ordinal -= 0x10000;
- *u++ = 0xD800 | (ordinal >> 10);
- *u++ = 0xDC00 | (ordinal & 0x3FF);
- }
- else
- *u++ = *w++;
+static Py_UCS4*
+as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize,
+ int copy_null)
+{
+ int kind;
+ void *data;
+ Py_ssize_t len, targetlen;
+ if (PyUnicode_READY(string) == -1)
+ return NULL;
+ kind = PyUnicode_KIND(string);
+ data = PyUnicode_DATA(string);
+ len = PyUnicode_GET_LENGTH(string);
+ targetlen = len;
+ if (copy_null)
+ targetlen++;
+ if (!target) {
+ if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ target = PyMem_Malloc(targetlen * sizeof(Py_UCS4));
+ if (!target) {
+ PyErr_NoMemory();
+ return NULL;
}
}
- return (PyObject *)unicode;
+ else {
+ if (targetsize < targetlen) {
+ PyErr_Format(PyExc_SystemError,
+ "string is longer than the buffer");
+ if (copy_null && 0 < targetsize)
+ target[0] = 0;
+ return NULL;
+ }
+ }
+ if (kind != PyUnicode_4BYTE_KIND) {
+ Py_ssize_t i;
+ for (i = 0; i < len; i++)
+ target[i] = PyUnicode_READ(kind, data, i);
+ }
+ else
+ Py_MEMCPY(target, data, len * sizeof(Py_UCS4));
+ if (copy_null)
+ target[len] = 0;
+ return target;
}
-#else
+Py_UCS4*
+PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize,
+ int copy_null)
+{
+ if (target == NULL || targetsize < 1) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return as_ucs4(string, target, targetsize, copy_null);
+}
+
+Py_UCS4*
+PyUnicode_AsUCS4Copy(PyObject *string)
+{
+ return as_ucs4(string, NULL, 0, 1);
+}
+
+#ifdef HAVE_WCHAR_H
PyObject *
PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size)
{
- PyUnicodeObject *unicode;
-
if (w == NULL) {
if (size == 0)
- return PyUnicode_FromStringAndSize(NULL, 0);
+ return PyUnicode_New(0, 0);
PyErr_BadInternalCall();
return NULL;
}
@@ -654,29 +1302,10 @@ PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size)
size = wcslen(w);
}
- unicode = _PyUnicode_New(size);
- if (!unicode)
- return NULL;
-
- /* Copy the wchar_t data into the new object */
-#if Py_UNICODE_SIZE == SIZEOF_WCHAR_T
- memcpy(unicode->str, w, size * sizeof(wchar_t));
-#else
- {
- register Py_UNICODE *u;
- register Py_ssize_t i;
- u = PyUnicode_AS_UNICODE(unicode);
- for (i = size; i > 0; i--)
- *u++ = *w++;
- }
-#endif
-
- return (PyObject *)unicode;
+ return PyUnicode_FromUnicode(w, size);
}
-#endif /* CONVERT_WCHAR_TO_SURROGATES */
-
-#undef CONVERT_WCHAR_TO_SURROGATES
+#endif /* HAVE_WCHAR_H */
static void
makefmt(char *fmt, int longflag, int longlongflag, int size_tflag,
@@ -779,10 +1408,6 @@ parse_format_flags(const char *f,
return f;
}
-#define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;}
-
-/* size of fixed-size buffer for formatting single arguments */
-#define ITEM_BUFFER_LEN 21
/* maximum number of characters required for output of %ld. 21 characters
allows for 64-bit integers (in decimal) and an optional sign. */
#define MAX_LONG_CHARS 21
@@ -803,97 +1428,184 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
int precision = 0;
int zeropad;
const char* f;
- Py_UNICODE *s;
- PyObject *string;
+ PyUnicodeObject *string;
/* used by sprintf */
- char buffer[ITEM_BUFFER_LEN+1];
- /* use abuffer instead of buffer, if we need more space
- * (which can happen if there's a format specifier with width). */
- char *abuffer = NULL;
- char *realbuffer;
- Py_ssize_t abuffersize = 0;
char fmt[61]; /* should be enough for %0width.precisionlld */
- const char *copy;
+ Py_UCS4 maxchar = 127; /* result is ASCII by default */
+ Py_UCS4 argmaxchar;
+ Py_ssize_t numbersize = 0;
+ char *numberresults = NULL;
+ char *numberresult = NULL;
+ Py_ssize_t i;
+ int kind;
+ void *data;
Py_VA_COPY(count, vargs);
/* step 1: count the number of %S/%R/%A/%s format specifications
* (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/
* PyUnicode_DecodeUTF8() for these objects once during step 3 and put the
- * result in an array) */
+ * result in an array)
+ * also esimate a upper bound for all the number formats in the string,
+ * numbers will be formated in step 3 and be keept in a '\0'-separated
+ * buffer before putting everything together. */
for (f = format; *f; f++) {
- if (*f == '%') {
- /* skip width or width.precision (eg. "1.2" of "%1.2f") */
- f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL);
- if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V')
- ++callcount;
- }
- else if (128 <= (unsigned char)*f) {
- PyErr_Format(PyExc_ValueError,
+ if (*f == '%') {
+ int longlongflag;
+ /* skip width or width.precision (eg. "1.2" of "%1.2f") */
+ f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL);
+ if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V')
+ ++callcount;
+
+ else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') {
+#ifdef HAVE_LONG_LONG
+ if (longlongflag) {
+ if (width < MAX_LONG_LONG_CHARS)
+ width = MAX_LONG_LONG_CHARS;
+ }
+ else
+#endif
+ /* MAX_LONG_CHARS is enough to hold a 64-bit integer,
+ including sign. Decimal takes the most space. This
+ isn't enough for octal. If a width is specified we
+ need more (which we allocate later). */
+ if (width < MAX_LONG_CHARS)
+ width = MAX_LONG_CHARS;
+
+ /* account for the size + '\0' to separate numbers
+ inside of the numberresults buffer */
+ numbersize += (width + 1);
+ }
+ }
+ else if ((unsigned char)*f > 127) {
+ PyErr_Format(PyExc_ValueError,
"PyUnicode_FromFormatV() expects an ASCII-encoded format "
"string, got a non-ASCII byte: 0x%02x",
(unsigned char)*f);
- return NULL;
- }
+ return NULL;
+ }
}
/* step 2: allocate memory for the results of
* PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */
if (callcount) {
- callresults = PyObject_Malloc(sizeof(PyObject *)*callcount);
+ callresults = PyObject_Malloc(sizeof(PyObject *) * callcount);
if (!callresults) {
PyErr_NoMemory();
return NULL;
}
callresult = callresults;
}
- /* step 3: figure out how large a buffer we need */
+ /* step 2.5: allocate memory for the results of formating numbers */
+ if (numbersize) {
+ numberresults = PyObject_Malloc(numbersize);
+ if (!numberresults) {
+ PyErr_NoMemory();
+ goto fail;
+ }
+ numberresult = numberresults;
+ }
+
+ /* step 3: format numbers and figure out how large a buffer we need */
for (f = format; *f; f++) {
if (*f == '%') {
-#ifdef HAVE_LONG_LONG
- int longlongflag;
-#endif
const char* p;
+ int longflag;
+ int longlongflag;
+ int size_tflag;
+ int numprinted;
p = f;
- f = parse_format_flags(f, &width, NULL,
- NULL, &longlongflag, NULL);
-
+ zeropad = (f[1] == '0');
+ f = parse_format_flags(f, &width, &precision,
+ &longflag, &longlongflag, &size_tflag);
switch (*f) {
case 'c':
{
-#ifndef Py_UNICODE_WIDE
- int ordinal = va_arg(count, int);
- if (ordinal > 0xffff)
- n += 2;
- else
- n++;
-#else
- (void)va_arg(count, int);
+ Py_UCS4 ordinal = va_arg(count, int);
+ maxchar = PY_MAX(maxchar, ordinal);
n++;
-#endif
break;
}
case '%':
n++;
break;
- case 'd': case 'u': case 'i': case 'x':
- (void) va_arg(count, int);
+ case 'i':
+ case 'd':
+ makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
+ width, precision, *f);
+ if (longflag)
+ numprinted = sprintf(numberresult, fmt,
+ va_arg(count, long));
#ifdef HAVE_LONG_LONG
- if (longlongflag) {
- if (width < MAX_LONG_LONG_CHARS)
- width = MAX_LONG_LONG_CHARS;
- }
+ else if (longlongflag)
+ numprinted = sprintf(numberresult, fmt,
+ va_arg(count, PY_LONG_LONG));
+#endif
+ else if (size_tflag)
+ numprinted = sprintf(numberresult, fmt,
+ va_arg(count, Py_ssize_t));
else
+ numprinted = sprintf(numberresult, fmt,
+ va_arg(count, int));
+ n += numprinted;
+ /* advance by +1 to skip over the '\0' */
+ numberresult += (numprinted + 1);
+ assert(*(numberresult - 1) == '\0');
+ assert(*(numberresult - 2) != '\0');
+ assert(numprinted >= 0);
+ assert(numberresult <= numberresults + numbersize);
+ break;
+ case 'u':
+ makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
+ width, precision, 'u');
+ if (longflag)
+ numprinted = sprintf(numberresult, fmt,
+ va_arg(count, unsigned long));
+#ifdef HAVE_LONG_LONG
+ else if (longlongflag)
+ numprinted = sprintf(numberresult, fmt,
+ va_arg(count, unsigned PY_LONG_LONG));
#endif
- /* MAX_LONG_CHARS is enough to hold a 64-bit integer,
- including sign. Decimal takes the most space. This
- isn't enough for octal. If a width is specified we
- need more (which we allocate later). */
- if (width < MAX_LONG_CHARS)
- width = MAX_LONG_CHARS;
- n += width;
- /* XXX should allow for large precision here too. */
- if (abuffersize < width)
- abuffersize = width;
+ else if (size_tflag)
+ numprinted = sprintf(numberresult, fmt,
+ va_arg(count, size_t));
+ else
+ numprinted = sprintf(numberresult, fmt,
+ va_arg(count, unsigned int));
+ n += numprinted;
+ numberresult += (numprinted + 1);
+ assert(*(numberresult - 1) == '\0');
+ assert(*(numberresult - 2) != '\0');
+ assert(numprinted >= 0);
+ assert(numberresult <= numberresults + numbersize);
+ break;
+ case 'x':
+ makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x');
+ numprinted = sprintf(numberresult, fmt, va_arg(count, int));
+ n += numprinted;
+ numberresult += (numprinted + 1);
+ assert(*(numberresult - 1) == '\0');
+ assert(*(numberresult - 2) != '\0');
+ assert(numprinted >= 0);
+ assert(numberresult <= numberresults + numbersize);
+ break;
+ case 'p':
+ numprinted = sprintf(numberresult, "%p", va_arg(count, void*));
+ /* %p is ill-defined: ensure leading 0x. */
+ if (numberresult[1] == 'X')
+ numberresult[1] = 'x';
+ else if (numberresult[1] != 'x') {
+ memmove(numberresult + 2, numberresult,
+ strlen(numberresult) + 1);
+ numberresult[0] = '0';
+ numberresult[1] = 'x';
+ numprinted += 2;
+ }
+ n += numprinted;
+ numberresult += (numprinted + 1);
+ assert(*(numberresult - 1) == '\0');
+ assert(*(numberresult - 2) != '\0');
+ assert(numprinted >= 0);
+ assert(numberresult <= numberresults + numbersize);
break;
case 's':
{
@@ -902,7 +1614,11 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace");
if (!str)
goto fail;
- n += PyUnicode_GET_SIZE(str);
+ /* since PyUnicode_DecodeUTF8 returns already flexible
+ unicode objects, there is no need to call ready on them */
+ argmaxchar = PyUnicode_MAX_CHAR_VALUE(str);
+ maxchar = PY_MAX(maxchar, argmaxchar);
+ n += PyUnicode_GET_LENGTH(str);
/* Remember the str and switch to the next slot */
*callresult++ = str;
break;
@@ -911,7 +1627,11 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
{
PyObject *obj = va_arg(count, PyObject *);
assert(obj && PyUnicode_Check(obj));
- n += PyUnicode_GET_SIZE(obj);
+ if (PyUnicode_READY(obj) == -1)
+ goto fail;
+ argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj);
+ maxchar = PY_MAX(maxchar, argmaxchar);
+ n += PyUnicode_GET_LENGTH(obj);
break;
}
case 'V':
@@ -922,14 +1642,20 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
assert(obj || str);
assert(!obj || PyUnicode_Check(obj));
if (obj) {
- n += PyUnicode_GET_SIZE(obj);
+ if (PyUnicode_READY(obj) == -1)
+ goto fail;
+ argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj);
+ maxchar = PY_MAX(maxchar, argmaxchar);
+ n += PyUnicode_GET_LENGTH(obj);
*callresult++ = NULL;
}
else {
str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace");
if (!str_obj)
goto fail;
- n += PyUnicode_GET_SIZE(str_obj);
+ argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj);
+ maxchar = PY_MAX(maxchar, argmaxchar);
+ n += PyUnicode_GET_LENGTH(str_obj);
*callresult++ = str_obj;
}
break;
@@ -940,9 +1666,11 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
PyObject *str;
assert(obj);
str = PyObject_Str(obj);
- if (!str)
+ if (!str || PyUnicode_READY(str) == -1)
goto fail;
- n += PyUnicode_GET_SIZE(str);
+ argmaxchar = PyUnicode_MAX_CHAR_VALUE(str);
+ maxchar = PY_MAX(maxchar, argmaxchar);
+ n += PyUnicode_GET_LENGTH(str);
/* Remember the str and switch to the next slot */
*callresult++ = str;
break;
@@ -953,9 +1681,11 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
PyObject *repr;
assert(obj);
repr = PyObject_Repr(obj);
- if (!repr)
+ if (!repr || PyUnicode_READY(repr) == -1)
goto fail;
- n += PyUnicode_GET_SIZE(repr);
+ argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr);
+ maxchar = PY_MAX(maxchar, argmaxchar);
+ n += PyUnicode_GET_LENGTH(repr);
/* Remember the repr and switch to the next slot */
*callresult++ = repr;
break;
@@ -966,22 +1696,15 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
PyObject *ascii;
assert(obj);
ascii = PyObject_ASCII(obj);
- if (!ascii)
+ if (!ascii || PyUnicode_READY(ascii) == -1)
goto fail;
- n += PyUnicode_GET_SIZE(ascii);
+ argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii);
+ maxchar = PY_MAX(maxchar, argmaxchar);
+ n += PyUnicode_GET_LENGTH(ascii);
/* Remember the repr and switch to the next slot */
*callresult++ = ascii;
break;
}
- case 'p':
- (void) va_arg(count, int);
- /* maximum 64-bit pointer representation:
- * 0xffffffffffffffff
- * so 19 characters is enough.
- * XXX I count 18 -- what's the extra for?
- */
- n += 19;
- break;
default:
/* if we stumble upon an unknown
formatting code, copy the rest of
@@ -996,98 +1719,65 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
n++;
}
expand:
- if (abuffersize > ITEM_BUFFER_LEN) {
- /* add 1 for sprintf's trailing null byte */
- abuffer = PyObject_Malloc(abuffersize + 1);
- if (!abuffer) {
- PyErr_NoMemory();
- goto fail;
- }
- realbuffer = abuffer;
- }
- else
- realbuffer = buffer;
/* step 4: fill the buffer */
- /* Since we've analyzed how much space we need for the worst case,
+ /* Since we've analyzed how much space we need,
we don't have to resize the string.
There can be no errors beyond this point. */
- string = PyUnicode_FromUnicode(NULL, n);
+ string = (PyUnicodeObject *)PyUnicode_New(n, maxchar);
if (!string)
goto fail;
-
- s = PyUnicode_AS_UNICODE(string);
+ kind = PyUnicode_KIND(string);
+ data = PyUnicode_DATA(string);
callresult = callresults;
+ numberresult = numberresults;
- for (f = format; *f; f++) {
+ for (i = 0, f = format; *f; f++) {
if (*f == '%') {
const char* p;
- int longflag;
- int longlongflag;
- int size_tflag;
p = f;
- zeropad = (f[1] == '0');
- f = parse_format_flags(f, &width, &precision,
- &longflag, &longlongflag, &size_tflag);
+ f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL);
+ /* checking for == because the last argument could be a empty
+ string, which causes i to point to end, the assert at the end of
+ the loop */
+ assert(i <= PyUnicode_GET_LENGTH(string));
switch (*f) {
case 'c':
{
- int ordinal = va_arg(vargs, int);
-#ifndef Py_UNICODE_WIDE
- if (ordinal > 0xffff) {
- ordinal -= 0x10000;
- *s++ = 0xD800 | (ordinal >> 10);
- *s++ = 0xDC00 | (ordinal & 0x3FF);
- } else
-#endif
- *s++ = ordinal;
+ const int ordinal = va_arg(vargs, int);
+ PyUnicode_WRITE(kind, data, i++, ordinal);
break;
}
case 'i':
case 'd':
- makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
- width, precision, *f);
- if (longflag)
- sprintf(realbuffer, fmt, va_arg(vargs, long));
-#ifdef HAVE_LONG_LONG
- else if (longlongflag)
- sprintf(realbuffer, fmt, va_arg(vargs, PY_LONG_LONG));
-#endif
- else if (size_tflag)
- sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t));
- else
- sprintf(realbuffer, fmt, va_arg(vargs, int));
- appendstring(realbuffer);
- break;
case 'u':
- makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
- width, precision, 'u');
- if (longflag)
- sprintf(realbuffer, fmt, va_arg(vargs, unsigned long));
-#ifdef HAVE_LONG_LONG
- else if (longlongflag)
- sprintf(realbuffer, fmt, va_arg(vargs,
- unsigned PY_LONG_LONG));
-#endif
- else if (size_tflag)
- sprintf(realbuffer, fmt, va_arg(vargs, size_t));
- else
- sprintf(realbuffer, fmt, va_arg(vargs, unsigned int));
- appendstring(realbuffer);
- break;
case 'x':
- makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x');
- sprintf(realbuffer, fmt, va_arg(vargs, int));
- appendstring(realbuffer);
+ case 'p':
+ /* unused, since we already have the result */
+ if (*f == 'p')
+ (void) va_arg(vargs, void *);
+ else
+ (void) va_arg(vargs, int);
+ /* extract the result from numberresults and append. */
+ for (; *numberresult; ++i, ++numberresult)
+ PyUnicode_WRITE(kind, data, i, *numberresult);
+ /* skip over the separating '\0' */
+ assert(*numberresult == '\0');
+ numberresult++;
+ assert(numberresult <= numberresults + numbersize);
break;
case 's':
{
/* unused, since we already have the result */
+ Py_ssize_t size;
(void) va_arg(vargs, char *);
- Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult),
- PyUnicode_GET_SIZE(*callresult));
- s += PyUnicode_GET_SIZE(*callresult);
+ size = PyUnicode_GET_LENGTH(*callresult);
+ assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string));
+ PyUnicode_CopyCharacters((PyObject*)string, i,
+ *callresult, 0,
+ size);
+ i += size;
/* We're done with the unicode()/repr() => forget it */
Py_DECREF(*callresult);
/* switch to next unicode()/repr() result */
@@ -1097,23 +1787,35 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
case 'U':
{
PyObject *obj = va_arg(vargs, PyObject *);
- Py_ssize_t size = PyUnicode_GET_SIZE(obj);
- Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size);
- s += size;
+ Py_ssize_t size;
+ assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string));
+ size = PyUnicode_GET_LENGTH(obj);
+ PyUnicode_CopyCharacters((PyObject*)string, i,
+ obj, 0,
+ size);
+ i += size;
break;
}
case 'V':
{
+ Py_ssize_t size;
PyObject *obj = va_arg(vargs, PyObject *);
va_arg(vargs, const char *);
if (obj) {
- Py_ssize_t size = PyUnicode_GET_SIZE(obj);
- Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size);
- s += size;
+ size = PyUnicode_GET_LENGTH(obj);
+ assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string));
+ PyUnicode_CopyCharacters((PyObject*)string, i,
+ obj, 0,
+ size);
+ i += size;
} else {
- Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult),
- PyUnicode_GET_SIZE(*callresult));
- s += PyUnicode_GET_SIZE(*callresult);
+ size = PyUnicode_GET_LENGTH(*callresult);
+ assert(PyUnicode_KIND(*callresult) <=
+ PyUnicode_KIND(string));
+ PyUnicode_CopyCharacters((PyObject*)string, i,
+ *callresult,
+ 0, size);
+ i += size;
Py_DECREF(*callresult);
}
++callresult;
@@ -1123,52 +1825,42 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
case 'R':
case 'A':
{
- Py_UNICODE *ucopy;
- Py_ssize_t usize;
- Py_ssize_t upos;
/* unused, since we already have the result */
(void) va_arg(vargs, PyObject *);
- ucopy = PyUnicode_AS_UNICODE(*callresult);
- usize = PyUnicode_GET_SIZE(*callresult);
- for (upos = 0; upos<usize;)
- *s++ = ucopy[upos++];
+ assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string));
+ PyUnicode_CopyCharacters((PyObject*)string, i,
+ *callresult, 0,
+ PyUnicode_GET_LENGTH(*callresult));
+ i += PyUnicode_GET_LENGTH(*callresult);
/* We're done with the unicode()/repr() => forget it */
Py_DECREF(*callresult);
/* switch to next unicode()/repr() result */
++callresult;
break;
}
- case 'p':
- sprintf(buffer, "%p", va_arg(vargs, void*));
- /* %p is ill-defined: ensure leading 0x. */
- if (buffer[1] == 'X')
- buffer[1] = 'x';
- else if (buffer[1] != 'x') {
- memmove(buffer+2, buffer, strlen(buffer)+1);
- buffer[0] = '0';
- buffer[1] = 'x';
- }
- appendstring(buffer);
- break;
case '%':
- *s++ = '%';
+ PyUnicode_WRITE(kind, data, i++, '%');
break;
default:
- appendstring(p);
+ for (; *p; ++p, ++i)
+ PyUnicode_WRITE(kind, data, i, *p);
+ assert(i == PyUnicode_GET_LENGTH(string));
goto end;
}
}
- else
- *s++ = *f;
+ else {
+ assert(i < PyUnicode_GET_LENGTH(string));
+ PyUnicode_WRITE(kind, data, i++, *f);
+ }
}
+ assert(i == PyUnicode_GET_LENGTH(string));
end:
if (callresults)
PyObject_Free(callresults);
- if (abuffer)
- PyObject_Free(abuffer);
- PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string));
- return string;
+ if (numberresults)
+ PyObject_Free(numberresults);
+ return (PyObject *)string;
fail:
if (callresults) {
PyObject **callresult2 = callresults;
@@ -1178,13 +1870,11 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
}
PyObject_Free(callresults);
}
- if (abuffer)
- PyObject_Free(abuffer);
+ if (numberresults)
+ PyObject_Free(numberresults);
return NULL;
}
-#undef appendstring
-
PyObject *
PyUnicode_FromFormat(const char *format, ...)
{
@@ -1201,6 +1891,8 @@ PyUnicode_FromFormat(const char *format, ...)
return ret;
}
+#ifdef HAVE_WCHAR_H
+
/* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString():
convert a Unicode object to a wide character string.
@@ -1215,99 +1907,23 @@ unicode_aswidechar(PyUnicodeObject *unicode,
wchar_t *w,
Py_ssize_t size)
{
-#if Py_UNICODE_SIZE == SIZEOF_WCHAR_T
Py_ssize_t res;
+ const wchar_t *wstr;
+
+ wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res);
+ if (wstr == NULL)
+ return -1;
+
if (w != NULL) {
- res = PyUnicode_GET_SIZE(unicode);
if (size > res)
size = res + 1;
else
res = size;
- memcpy(w, unicode->str, size * sizeof(wchar_t));
+ Py_MEMCPY(w, wstr, size * sizeof(wchar_t));
return res;
}
else
- return PyUnicode_GET_SIZE(unicode) + 1;
-#elif Py_UNICODE_SIZE == 2 && SIZEOF_WCHAR_T == 4
- register const Py_UNICODE *u;
- const Py_UNICODE *uend;
- const wchar_t *worig, *wend;
- Py_ssize_t nchar;
-
- u = PyUnicode_AS_UNICODE(unicode);
- uend = u + PyUnicode_GET_SIZE(unicode);
- if (w != NULL) {
- worig = w;
- wend = w + size;
- while (u != uend && w != wend) {
- if (0xD800 <= u[0] && u[0] <= 0xDBFF
- && 0xDC00 <= u[1] && u[1] <= 0xDFFF)
- {
- *w = (((u[0] & 0x3FF) << 10) | (u[1] & 0x3FF)) + 0x10000;
- u += 2;
- }
- else {
- *w = *u;
- u++;
- }
- w++;
- }
- if (w != wend)
- *w = L'\0';
- return w - worig;
- }
- else {
- nchar = 1; /* null character at the end */
- while (u != uend) {
- if (0xD800 <= u[0] && u[0] <= 0xDBFF
- && 0xDC00 <= u[1] && u[1] <= 0xDFFF)
- u += 2;
- else
- u++;
- nchar++;
- }
- }
- return nchar;
-#elif Py_UNICODE_SIZE == 4 && SIZEOF_WCHAR_T == 2
- register Py_UNICODE *u, *uend, ordinal;
- register Py_ssize_t i;
- wchar_t *worig, *wend;
- Py_ssize_t nchar;
-
- u = PyUnicode_AS_UNICODE(unicode);
- uend = u + PyUnicode_GET_SIZE(u);
- if (w != NULL) {
- worig = w;
- wend = w + size;
- while (u != uend && w != wend) {
- ordinal = *u;
- if (ordinal > 0xffff) {
- ordinal -= 0x10000;
- *w++ = 0xD800 | (ordinal >> 10);
- *w++ = 0xDC00 | (ordinal & 0x3FF);
- }
- else
- *w++ = ordinal;
- u++;
- }
- if (w != wend)
- *w = 0;
- return w - worig;
- }
- else {
- nchar = 1; /* null character */
- while (u != uend) {
- if (*u > 0xffff)
- nchar += 2;
- else
- nchar++;
- u++;
- }
- return nchar;
- }
-#else
-# error "unsupported wchar_t and Py_UNICODE sizes, see issue #8670"
-#endif
+ return res + 1;
}
Py_ssize_t
@@ -1335,6 +1951,8 @@ PyUnicode_AsWideCharString(PyObject *unicode,
}
buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0);
+ if (buflen == -1)
+ return NULL;
if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) {
PyErr_NoMemory();
return NULL;
@@ -1346,35 +1964,33 @@ PyUnicode_AsWideCharString(PyObject *unicode,
return NULL;
}
buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen);
+ if (buflen == -1)
+ return NULL;
if (size != NULL)
*size = buflen;
return buffer;
}
-#endif
+#endif /* HAVE_WCHAR_H */
PyObject *
PyUnicode_FromOrdinal(int ordinal)
{
- Py_UNICODE s[2];
-
+ PyObject *v;
if (ordinal < 0 || ordinal > 0x10ffff) {
PyErr_SetString(PyExc_ValueError,
"chr() arg not in range(0x110000)");
return NULL;
}
-#ifndef Py_UNICODE_WIDE
- if (ordinal > 0xffff) {
- ordinal -= 0x10000;
- s[0] = 0xD800 | (ordinal >> 10);
- s[1] = 0xDC00 | (ordinal & 0x3FF);
- return PyUnicode_FromUnicode(s, 2);
- }
-#endif
+ if (ordinal < 256)
+ return get_latin1_char(ordinal);
- s[0] = (Py_UNICODE)ordinal;
- return PyUnicode_FromUnicode(s, 1);
+ v = PyUnicode_New(1, ordinal);
+ if (v == NULL)
+ return NULL;
+ PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal);
+ return v;
}
PyObject *
@@ -1389,8 +2005,9 @@ PyUnicode_FromObject(register PyObject *obj)
if (PyUnicode_Check(obj)) {
/* For a Unicode subtype that's not a Unicode object,
return a true Unicode object with the same data. */
- return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
- PyUnicode_GET_SIZE(obj));
+ if (PyUnicode_READY(obj) == -1)
+ return NULL;
+ return substring((PyUnicodeObject *)obj, 0, PyUnicode_GET_LENGTH(obj));
}
PyErr_Format(PyExc_TypeError,
"Can't convert '%.100s' object to str implicitly",
@@ -1536,6 +2153,10 @@ PyUnicode_Decode(const char *s,
goto onError;
}
Py_DECREF(buffer);
+ if (PyUnicode_READY(unicode)) {
+ Py_DECREF(unicode);
+ return NULL;
+ }
return unicode;
onError:
@@ -1649,9 +2270,7 @@ PyUnicode_EncodeFSDefault(PyObject *unicode)
PyUnicode_GET_SIZE(unicode),
NULL);
#elif defined(__APPLE__)
- return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
- PyUnicode_GET_SIZE(unicode),
- "surrogateescape");
+ return _PyUnicode_AsUTF8String(unicode, "surrogateescape");
#else
PyInterpreterState *interp = PyThreadState_GET()->interp;
/* Bootstrap check: if the filesystem codec is implemented in Python, we
@@ -1721,11 +2340,9 @@ PyUnicode_AsEncodedString(PyObject *unicode,
if (encoding == NULL) {
if (errors == NULL || strcmp(errors, "strict") == 0)
- return PyUnicode_AsUTF8String(unicode);
+ return _PyUnicode_AsUTF8String(unicode, NULL);
else
- return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
- PyUnicode_GET_SIZE(unicode),
- errors);
+ return _PyUnicode_AsUTF8String(unicode, errors);
}
/* Shortcuts for common default encodings */
@@ -1734,18 +2351,14 @@ PyUnicode_AsEncodedString(PyObject *unicode,
(strcmp(lower, "utf8") == 0))
{
if (errors == NULL || strcmp(errors, "strict") == 0)
- return PyUnicode_AsUTF8String(unicode);
+ return _PyUnicode_AsUTF8String(unicode, NULL);
else
- return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
- PyUnicode_GET_SIZE(unicode),
- errors);
+ return _PyUnicode_AsUTF8String(unicode, errors);
}
else if ((strcmp(lower, "latin-1") == 0) ||
(strcmp(lower, "latin1") == 0) ||
(strcmp(lower, "iso-8859-1") == 0))
- return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode),
- PyUnicode_GET_SIZE(unicode),
- errors);
+ return _PyUnicode_AsLatin1String(unicode, errors);
#ifdef HAVE_MBCS
else if (strcmp(lower, "mbcs") == 0)
return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode),
@@ -1753,9 +2366,7 @@ PyUnicode_AsEncodedString(PyObject *unicode,
errors);
#endif
else if (strcmp(lower, "ascii") == 0)
- return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode),
- PyUnicode_GET_SIZE(unicode),
- errors);
+ return _PyUnicode_AsASCIIString(unicode, errors);
}
/* Encode via the codec registry */
@@ -1824,21 +2435,6 @@ PyUnicode_AsEncodedUnicode(PyObject *unicode,
return NULL;
}
-PyObject *
-_PyUnicode_AsDefaultEncodedString(PyObject *unicode)
-{
- PyObject *v = ((PyUnicodeObject *)unicode)->defenc;
- if (v)
- return v;
- v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
- PyUnicode_GET_SIZE(unicode),
- NULL);
- if (!v)
- return NULL;
- ((PyUnicodeObject *)unicode)->defenc = v;
- return v;
-}
-
PyObject*
PyUnicode_DecodeFSDefault(const char *s) {
Py_ssize_t size = (Py_ssize_t)strlen(s);
@@ -1935,13 +2531,13 @@ int
PyUnicode_FSDecoder(PyObject* arg, void* addr)
{
PyObject *output = NULL;
- Py_ssize_t size;
- void *data;
if (arg == NULL) {
Py_DECREF(*(PyObject**)addr);
return 1;
}
if (PyUnicode_Check(arg)) {
+ if (PyUnicode_READY(arg))
+ return 0;
output = arg;
Py_INCREF(output);
}
@@ -1960,9 +2556,8 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
return 0;
}
}
- size = PyUnicode_GET_SIZE(output);
- data = PyUnicode_AS_UNICODE(output);
- if (size != Py_UNICODE_strlen(data)) {
+ if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output),
+ PyUnicode_GET_LENGTH(output), 0, 1)) {
PyErr_SetString(PyExc_TypeError, "embedded NUL character");
Py_DECREF(output);
return 0;
@@ -1973,40 +2568,172 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
char*
-_PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize)
+PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
{
PyObject *bytes;
+ PyUnicodeObject *u = (PyUnicodeObject *)unicode;
+
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
return NULL;
}
- bytes = _PyUnicode_AsDefaultEncodedString(unicode);
- if (bytes == NULL)
+ if (PyUnicode_READY(u) == -1)
return NULL;
- if (psize != NULL)
- *psize = PyBytes_GET_SIZE(bytes);
- return PyBytes_AS_STRING(bytes);
+
+ if (_PyUnicode_UTF8(unicode) == NULL) {
+ bytes = _PyUnicode_AsUTF8String(unicode, "strict");
+ if (bytes == NULL)
+ return NULL;
+ u->_base.utf8 = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1);
+ if (u->_base.utf8 == NULL) {
+ Py_DECREF(bytes);
+ return NULL;
+ }
+ u->_base.utf8_length = PyBytes_GET_SIZE(bytes);
+ Py_MEMCPY(u->_base.utf8, PyBytes_AS_STRING(bytes), u->_base.utf8_length + 1);
+ Py_DECREF(bytes);
+ }
+
+ if (psize)
+ *psize = _PyUnicode_UTF8_LENGTH(unicode);
+ return _PyUnicode_UTF8(unicode);
}
char*
-_PyUnicode_AsString(PyObject *unicode)
+PyUnicode_AsUTF8(PyObject *unicode)
{
- return _PyUnicode_AsStringAndSize(unicode, NULL);
+ return PyUnicode_AsUTF8AndSize(unicode, NULL);
}
+#ifdef Py_DEBUG
+int unicode_as_unicode_calls = 0;
+#endif
+
+
Py_UNICODE *
-PyUnicode_AsUnicode(PyObject *unicode)
+PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size)
{
+ PyUnicodeObject *u;
+ const unsigned char *one_byte;
+#if SIZEOF_WCHAR_T == 4
+ const Py_UCS2 *two_bytes;
+#else
+ const Py_UCS4 *four_bytes;
+ const Py_UCS4 *ucs4_end;
+ Py_ssize_t num_surrogates;
+#endif
+ wchar_t *w;
+ wchar_t *wchar_end;
+
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
- goto onError;
+ return NULL;
}
- return PyUnicode_AS_UNICODE(unicode);
+ u = (PyUnicodeObject*)unicode;
+ if (_PyUnicode_WSTR(u) == NULL) {
+ /* Non-ASCII compact unicode object */
+ assert(_PyUnicode_KIND(u) != 0);
+ assert(PyUnicode_IS_READY(u));
- onError:
- return NULL;
+#ifdef Py_DEBUG
+ ++unicode_as_unicode_calls;
+#endif
+
+ if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) {
+#if SIZEOF_WCHAR_T == 2
+ four_bytes = PyUnicode_4BYTE_DATA(u);
+ ucs4_end = four_bytes + _PyUnicode_LENGTH(u);
+ num_surrogates = 0;
+
+ for (; four_bytes < ucs4_end; ++four_bytes) {
+ if (*four_bytes > 0xFFFF)
+ ++num_surrogates;
+ }
+
+ _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(
+ sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates));
+ if (!_PyUnicode_WSTR(u)) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates;
+
+ w = _PyUnicode_WSTR(u);
+ wchar_end = w + _PyUnicode_WSTR_LENGTH(u);
+ four_bytes = PyUnicode_4BYTE_DATA(u);
+ for (; four_bytes < ucs4_end; ++four_bytes, ++w) {
+ if (*four_bytes > 0xFFFF) {
+ /* encode surrogate pair in this case */
+ *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10);
+ *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF);
+ }
+ else
+ *w = *four_bytes;
+
+ if (w > wchar_end) {
+ assert(0 && "Miscalculated string end");
+ }
+ }
+ *w = 0;
+#else
+ /* sizeof(wchar_t) == 4 */
+ Py_FatalError("Impossible unicode object state, wstr and str "
+ "should share memory already.");
+ return NULL;
+#endif
+ }
+ else {
+ _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) *
+ (_PyUnicode_LENGTH(u) + 1));
+ if (!_PyUnicode_WSTR(u)) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ if (!PyUnicode_IS_COMPACT_ASCII(u))
+ _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u);
+ w = _PyUnicode_WSTR(u);
+ wchar_end = w + _PyUnicode_LENGTH(u);
+
+ if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) {
+ one_byte = PyUnicode_1BYTE_DATA(u);
+ for (; w < wchar_end; ++one_byte, ++w)
+ *w = *one_byte;
+ /* null-terminate the wstr */
+ *w = 0;
+ }
+ else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) {
+#if SIZEOF_WCHAR_T == 4
+ two_bytes = PyUnicode_2BYTE_DATA(u);
+ for (; w < wchar_end; ++two_bytes, ++w)
+ *w = *two_bytes;
+ /* null-terminate the wstr */
+ *w = 0;
+#else
+ /* sizeof(wchar_t) == 2 */
+ PyObject_FREE(_PyUnicode_WSTR(u));
+ _PyUnicode_WSTR(u) = NULL;
+ Py_FatalError("Impossible unicode object state, wstr "
+ "and str should share memory already.");
+ return NULL;
+#endif
+ }
+ else {
+ assert(0 && "This should never happen.");
+ }
+ }
+ }
+ if (size != NULL)
+ *size = PyUnicode_WSTR_LENGTH(u);
+ return _PyUnicode_WSTR(u);
+}
+
+Py_UNICODE *
+PyUnicode_AsUnicode(PyObject *unicode)
+{
+ return PyUnicode_AsUnicodeAndSize(unicode, NULL);
}
+
Py_ssize_t
PyUnicode_GetSize(PyObject *unicode)
{
@@ -2020,6 +2747,40 @@ PyUnicode_GetSize(PyObject *unicode)
return -1;
}
+Py_ssize_t
+PyUnicode_GetLength(PyObject *unicode)
+{
+ if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) != -1) {
+ PyErr_BadArgument();
+ return -1;
+ }
+
+ return PyUnicode_GET_LENGTH(unicode);
+}
+
+Py_UCS4
+PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)
+{
+ if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) != -1) {
+ return PyErr_BadArgument();
+ return (Py_UCS4)-1;
+ }
+ return PyUnicode_READ_CHAR(unicode, index);
+}
+
+int
+PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch)
+{
+ if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) {
+ return PyErr_BadArgument();
+ return -1;
+ }
+
+ PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode),
+ index, ch);
+ return 0;
+}
+
const char *
PyUnicode_GetDefaultEncoding(void)
{
@@ -2075,7 +2836,7 @@ unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler,
Py_ssize_t insize;
Py_ssize_t requiredsize;
Py_ssize_t newpos;
- Py_UNICODE *repptr;
+ const Py_UNICODE *repptr;
PyObject *inputobj = NULL;
Py_ssize_t repsize;
int res = -1;
@@ -2281,7 +3042,7 @@ PyUnicode_DecodeUTF7Stateful(const char *s,
return (PyObject *)unicode;
}
- p = unicode->str;
+ p = PyUnicode_AS_UNICODE(unicode);
shiftOutStart = p;
e = s + size;
@@ -2431,6 +3192,10 @@ utf7Error:
Py_XDECREF(errorHandler);
Py_XDECREF(exc);
+ if (PyUnicode_READY(unicode) == -1) {
+ Py_DECREF(unicode);
+ return NULL;
+ }
return (PyObject *)unicode;
onError:
@@ -2588,38 +3353,197 @@ PyUnicode_DecodeUTF8(const char *s,
# error C 'long' size should be either 4 or 8!
#endif
+/* Scans a UTF-8 string and returns the maximum character to be expected,
+ the size of the decoded unicode string and if any major errors were
+ encountered.
+
+ This function does check basic UTF-8 sanity, it does however NOT CHECK
+ if the string contains surrogates, and if all continuation bytes are
+ within the correct ranges, these checks are performed in
+ PyUnicode_DecodeUTF8Stateful.
+
+ If it sets has_errors to 1, it means the value of unicode_size and max_char
+ will be bogus and you should not rely on useful information in them.
+ */
+static Py_UCS4
+utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size,
+ Py_ssize_t *unicode_size, Py_ssize_t* consumed,
+ int *has_errors)
+{
+ Py_ssize_t n;
+ Py_ssize_t char_count = 0;
+ Py_UCS4 max_char = 127, new_max;
+ Py_UCS4 upper_bound;
+ const unsigned char *p = (const unsigned char *)s;
+ const unsigned char *end = p + string_size;
+ const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK);
+ int err = 0;
+
+ for (; p < end && !err; ++p, ++char_count) {
+ /* Only check value if it's not a ASCII char... */
+ if (*p < 0x80) {
+ /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for
+ an explanation. */
+ if (!((size_t) p & LONG_PTR_MASK)) {
+ /* Help register allocation */
+ register const unsigned char *_p = p;
+ while (_p < aligned_end) {
+ unsigned long value = *(unsigned long *) _p;
+ if (value & ASCII_CHAR_MASK)
+ break;
+ _p += SIZEOF_LONG;
+ char_count += SIZEOF_LONG;
+ }
+ p = _p;
+ if (p == end)
+ break;
+ }
+ }
+ if (*p >= 0x80) {
+ n = utf8_code_length[*p];
+ new_max = max_char;
+ switch (n) {
+ /* invalid start byte */
+ case 0:
+ err = 1;
+ break;
+ case 2:
+ /* Code points between 0x00FF and 0x07FF inclusive.
+ Approximate the upper bound of the code point,
+ if this flips over 255 we can be sure it will be more
+ than 255 and the string will need 2 bytes per code coint,
+ if it stays under or equal to 255, we can be sure 1 byte
+ is enough.
+ ((*p & 0b00011111) << 6) | 0b00111111 */
+ upper_bound = ((*p & 0x1F) << 6) | 0x3F;
+ if (max_char < upper_bound)
+ new_max = upper_bound;
+ /* Ensure we track at least that we left ASCII space. */
+ if (new_max < 128)
+ new_max = 128;
+ break;
+ case 3:
+ /* Between 0x0FFF and 0xFFFF inclusive, so values are
+ always > 255 and <= 65535 and will always need 2 bytes. */
+ if (max_char < 65535)
+ new_max = 65535;
+ break;
+ case 4:
+ /* Code point will be above 0xFFFF for sure in this case. */
+ new_max = 65537;
+ break;
+ /* Internal error, this should be caught by the first if */
+ case 1:
+ default:
+ assert(0 && "Impossible case in utf8_max_char_and_size");
+ err = 1;
+ }
+ /* Instead of number of overall bytes for this code point,
+ n containts the number of following bytes: */
+ --n;
+ /* Check if the follow up chars are all valid continuation bytes */
+ if (n >= 1) {
+ const unsigned char *cont;
+ if ((p + n) >= end) {
+ if (consumed == 0)
+ /* incomplete data, non-incremental decoding */
+ err = 1;
+ break;
+ }
+ for (cont = p + 1; cont < (p + n); ++cont) {
+ if ((*cont & 0xc0) != 0x80) {
+ err = 1;
+ break;
+ }
+ }
+ p += n;
+ }
+ else
+ err = 1;
+ max_char = new_max;
+ }
+ }
+
+ if (unicode_size)
+ *unicode_size = char_count;
+ if (has_errors)
+ *has_errors = err;
+ return max_char;
+}
+
+/* Similar to PyUnicode_WRITE but can also write into wstr field
+ of the legacy unicode representation */
+#define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \
+ do { \
+ const int k_ = (kind); \
+ if (k_ == PyUnicode_WCHAR_KIND) \
+ ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \
+ else if (k_ == PyUnicode_1BYTE_KIND) \
+ ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \
+ else if (k_ == PyUnicode_2BYTE_KIND) \
+ ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \
+ else \
+ ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \
+ } while (0)
+
PyObject *
PyUnicode_DecodeUTF8Stateful(const char *s,
- Py_ssize_t size,
- const char *errors,
- Py_ssize_t *consumed)
+ Py_ssize_t size,
+ const char *errors,
+ Py_ssize_t *consumed)
{
const char *starts = s;
int n;
int k;
Py_ssize_t startinpos;
Py_ssize_t endinpos;
- Py_ssize_t outpos;
const char *e, *aligned_end;
PyUnicodeObject *unicode;
- Py_UNICODE *p;
const char *errmsg = "";
PyObject *errorHandler = NULL;
PyObject *exc = NULL;
+ Py_UCS4 maxchar = 0;
+ Py_ssize_t unicode_size;
+ Py_ssize_t i;
+ int kind;
+ void *data;
+ int has_errors;
+ Py_UNICODE *error_outptr;
+#if SIZEOF_WCHAR_T == 2
+ Py_ssize_t wchar_offset = 0;
+#endif
- /* Note: size will always be longer than the resulting Unicode
- character count */
- unicode = _PyUnicode_New(size);
- if (!unicode)
- return NULL;
if (size == 0) {
if (consumed)
*consumed = 0;
- return (PyObject *)unicode;
+ return (PyObject *)PyUnicode_New(0, 0);
+ }
+ maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size,
+ consumed, &has_errors);
+ if (has_errors) {
+ unicode = _PyUnicode_New(size);
+ if (!unicode)
+ return NULL;
+ kind = PyUnicode_WCHAR_KIND;
+ data = PyUnicode_AS_UNICODE(unicode);
+ assert(data != NULL);
+ }
+ else {
+ unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar);
+ if (!unicode)
+ return NULL;
+ /* When the string is ASCII only, just use memcpy and return.
+ unicode_size may be != size if there is an incomplete UTF-8
+ sequence at the end of the ASCII block. */
+ if (maxchar < 128 && size == unicode_size) {
+ Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size);
+ return (PyObject *)unicode;
+ }
+ kind = PyUnicode_KIND(unicode);
+ data = PyUnicode_DATA(unicode);
}
-
/* Unpack UTF-8 encoded data */
- p = unicode->str;
+ i = 0;
e = s + size;
aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK);
@@ -2637,29 +3561,29 @@ PyUnicode_DecodeUTF8Stateful(const char *s,
if (!((size_t) s & LONG_PTR_MASK)) {
/* Help register allocation */
register const char *_s = s;
- register Py_UNICODE *_p = p;
+ register Py_ssize_t _i = i;
while (_s < aligned_end) {
/* Read a whole long at a time (either 4 or 8 bytes),
and do a fast unrolled copy if it only contains ASCII
characters. */
- unsigned long data = *(unsigned long *) _s;
- if (data & ASCII_CHAR_MASK)
+ unsigned long value = *(unsigned long *) _s;
+ if (value & ASCII_CHAR_MASK)
break;
- _p[0] = (unsigned char) _s[0];
- _p[1] = (unsigned char) _s[1];
- _p[2] = (unsigned char) _s[2];
- _p[3] = (unsigned char) _s[3];
+ WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]);
+ WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]);
+ WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]);
+ WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]);
#if (SIZEOF_LONG == 8)
- _p[4] = (unsigned char) _s[4];
- _p[5] = (unsigned char) _s[5];
- _p[6] = (unsigned char) _s[6];
- _p[7] = (unsigned char) _s[7];
+ WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]);
+ WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]);
+ WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]);
+ WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]);
#endif
_s += SIZEOF_LONG;
- _p += SIZEOF_LONG;
+ _i += SIZEOF_LONG;
}
s = _s;
- p = _p;
+ i = _i;
if (s == e)
break;
ch = (unsigned char)*s;
@@ -2667,7 +3591,7 @@ PyUnicode_DecodeUTF8Stateful(const char *s,
}
if (ch < 0x80) {
- *p++ = (Py_UNICODE)ch;
+ WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch);
s++;
continue;
}
@@ -2710,7 +3634,7 @@ PyUnicode_DecodeUTF8Stateful(const char *s,
}
ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f);
assert ((ch > 0x007F) && (ch <= 0x07FF));
- *p++ = (Py_UNICODE)ch;
+ WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch);
break;
case 3:
@@ -2739,7 +3663,7 @@ PyUnicode_DecodeUTF8Stateful(const char *s,
}
ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f);
assert ((ch > 0x07FF) && (ch <= 0xFFFF));
- *p++ = (Py_UNICODE)ch;
+ WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch);
break;
case 4:
@@ -2764,19 +3688,27 @@ PyUnicode_DecodeUTF8Stateful(const char *s,
((s[2] & 0x3f) << 6) + (s[3] & 0x3f);
assert ((ch > 0xFFFF) && (ch <= 0x10ffff));
-#ifdef Py_UNICODE_WIDE
- *p++ = (Py_UNICODE)ch;
-#else
- /* compute and append the two surrogates: */
+ /* If the string is flexible or we have native UCS-4, write
+ directly.. */
+ if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND)
+ WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch);
- /* translate from 10000..10FFFF to 0..FFFF */
- ch -= 0x10000;
+ else {
+ /* compute and append the two surrogates: */
- /* high surrogate = top 10 bits added to D800 */
- *p++ = (Py_UNICODE)(0xD800 + (ch >> 10));
+ /* translate from 10000..10FFFF to 0..FFFF */
+ ch -= 0x10000;
- /* low surrogate = bottom 10 bits added to DC00 */
- *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF));
+ /* high surrogate = top 10 bits added to D800 */
+ WRITE_FLEXIBLE_OR_WSTR(kind, data, i++,
+ (Py_UNICODE)(0xD800 + (ch >> 10)));
+
+ /* low surrogate = bottom 10 bits added to DC00 */
+ WRITE_FLEXIBLE_OR_WSTR(kind, data, i++,
+ (Py_UNICODE)(0xDC00 + (ch & 0x03FF)));
+ }
+#if SIZEOF_WCHAR_T == 2
+ wchar_offset++;
#endif
break;
}
@@ -2784,24 +3716,57 @@ PyUnicode_DecodeUTF8Stateful(const char *s,
continue;
utf8Error:
- outpos = p-PyUnicode_AS_UNICODE(unicode);
+ /* If this is not yet a resizable string, make it one.. */
+ if (kind != PyUnicode_WCHAR_KIND) {
+ const Py_UNICODE *u;
+ PyUnicodeObject *new_unicode = _PyUnicode_New(size);
+ if (!new_unicode)
+ goto onError;
+ u = PyUnicode_AsUnicode((PyObject *)unicode);
+ if (!u)
+ goto onError;
+#if SIZEOF_WCHAR_T == 2
+ i += wchar_offset;
+#endif
+ Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i);
+ Py_DECREF(unicode);
+ unicode = new_unicode;
+ kind = 0;
+ data = PyUnicode_AS_UNICODE(new_unicode);
+ assert(data != NULL);
+ }
+ error_outptr = PyUnicode_AS_UNICODE(unicode) + i;
if (unicode_decode_call_errorhandler(
errors, &errorHandler,
"utf8", errmsg,
&starts, &e, &startinpos, &endinpos, &exc, &s,
- &unicode, &outpos, &p))
+ &unicode, &i, &error_outptr))
goto onError;
+ /* Update data because unicode_decode_call_errorhandler might have
+ re-created or resized the unicode object. */
+ data = PyUnicode_AS_UNICODE(unicode);
aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK);
}
+ /* Ensure the unicode_size calculation above was correct: */
+ assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size);
+
if (consumed)
*consumed = s-starts;
- /* Adjust length */
- if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0)
- goto onError;
+ /* Adjust length and ready string when it contained errors and
+ is of the old resizable kind. */
+ if (kind == PyUnicode_WCHAR_KIND) {
+ if (_PyUnicode_Resize(&unicode, i) < 0 ||
+ PyUnicode_READY(unicode) == -1)
+ goto onError;
+ }
Py_XDECREF(errorHandler);
Py_XDECREF(exc);
+ if (PyUnicode_READY(unicode) == -1) {
+ Py_DECREF(unicode);
+ return NULL;
+ }
return (PyObject *)unicode;
onError:
@@ -2811,7 +3776,7 @@ PyUnicode_DecodeUTF8Stateful(const char *s,
return NULL;
}
-#undef ASCII_CHAR_MASK
+#undef WRITE_FLEXIBLE_OR_WSTR
#ifdef __APPLE__
@@ -2882,7 +3847,7 @@ _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size)
}
ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f);
assert ((ch > 0x07FF) && (ch <= 0xFFFF));
- *p++ = (Py_UNICODE)ch;
+ *p++ = (wchar_t)ch;
break;
case 4:
@@ -2928,15 +3893,15 @@ _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size)
#endif /* __APPLE__ */
-/* Allocation strategy: if the string is short, convert into a stack buffer
+/* Primary internal function which creates utf8 encoded bytes objects.
+
+ Allocation strategy: if the string is short, convert into a stack buffer
and allocate exactly as much space needed at the end. Else allocate the
maximum possible needed (4 result bytes per Unicode character), and return
the excess memory at the end.
*/
PyObject *
-PyUnicode_EncodeUTF8(const Py_UNICODE *s,
- Py_ssize_t size,
- const char *errors)
+_PyUnicode_AsUTF8String(PyObject *obj, const char *errors)
{
#define MAX_SHORT_UNICHARS 300 /* largest size we'll do on the stack */
@@ -2948,8 +3913,30 @@ PyUnicode_EncodeUTF8(const Py_UNICODE *s,
char stackbuf[MAX_SHORT_UNICHARS * 4];
PyObject *errorHandler = NULL;
PyObject *exc = NULL;
+ int kind;
+ void *data;
+ Py_ssize_t size;
+ PyUnicodeObject *unicode = (PyUnicodeObject *)obj;
+#if SIZEOF_WCHAR_T == 2
+ Py_ssize_t wchar_offset = 0;
+#endif
+
+ if (!PyUnicode_Check(unicode)) {
+ PyErr_BadArgument();
+ return NULL;
+ }
+
+ if (PyUnicode_READY(unicode) == -1)
+ return NULL;
+
+ if (_PyUnicode_UTF8(unicode))
+ return PyBytes_FromStringAndSize(_PyUnicode_UTF8(unicode),
+ _PyUnicode_UTF8_LENGTH(unicode));
+
+ kind = PyUnicode_KIND(unicode);
+ data = PyUnicode_DATA(unicode);
+ size = PyUnicode_GET_LENGTH(unicode);
- assert(s != NULL);
assert(size >= 0);
if (size <= MAX_SHORT_UNICHARS) {
@@ -2973,7 +3960,7 @@ PyUnicode_EncodeUTF8(const Py_UNICODE *s,
}
for (i = 0; i < size;) {
- Py_UCS4 ch = s[i++];
+ Py_UCS4 ch = PyUnicode_READ(kind, data, i++);
if (ch < 0x80)
/* Encode ASCII */
@@ -2984,83 +3971,72 @@ PyUnicode_EncodeUTF8(const Py_UNICODE *s,
*p++ = (char)(0xc0 | (ch >> 6));
*p++ = (char)(0x80 | (ch & 0x3f));
} else if (0xD800 <= ch && ch <= 0xDFFF) {
-#ifndef Py_UNICODE_WIDE
- /* Special case: check for high and low surrogate */
- if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) {
- Py_UCS4 ch2 = s[i];
- /* Combine the two surrogates to form a UCS4 value */
- ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000;
- i++;
-
- /* Encode UCS4 Unicode ordinals */
- *p++ = (char)(0xf0 | (ch >> 18));
- *p++ = (char)(0x80 | ((ch >> 12) & 0x3f));
- *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
- *p++ = (char)(0x80 | (ch & 0x3f));
- } else {
+ Py_ssize_t newpos;
+ PyObject *rep;
+ Py_ssize_t repsize, k, startpos;
+ startpos = i-1;
+#if SIZEOF_WCHAR_T == 2
+ startpos += wchar_offset;
#endif
- Py_ssize_t newpos;
- PyObject *rep;
- Py_ssize_t repsize, k;
- rep = unicode_encode_call_errorhandler
- (errors, &errorHandler, "utf-8", "surrogates not allowed",
- s, size, &exc, i-1, i, &newpos);
- if (!rep)
- goto error;
+ rep = unicode_encode_call_errorhandler(
+ errors, &errorHandler, "utf-8", "surrogates not allowed",
+ PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode),
+ &exc, startpos, startpos+1, &newpos);
+ if (!rep)
+ goto error;
+
+ if (PyBytes_Check(rep))
+ repsize = PyBytes_GET_SIZE(rep);
+ else
+ repsize = PyUnicode_GET_SIZE(rep);
- if (PyBytes_Check(rep))
- repsize = PyBytes_GET_SIZE(rep);
- else
- repsize = PyUnicode_GET_SIZE(rep);
+ if (repsize > 4) {
+ Py_ssize_t offset;
- if (repsize > 4) {
- Py_ssize_t offset;
+ if (result == NULL)
+ offset = p - stackbuf;
+ else
+ offset = p - PyBytes_AS_STRING(result);
+ if (nallocated > PY_SSIZE_T_MAX - repsize + 4) {
+ /* integer overflow */
+ PyErr_NoMemory();
+ goto error;
+ }
+ nallocated += repsize - 4;
+ if (result != NULL) {
+ if (_PyBytes_Resize(&result, nallocated) < 0)
+ goto error;
+ } else {
+ result = PyBytes_FromStringAndSize(NULL, nallocated);
if (result == NULL)
- offset = p - stackbuf;
- else
- offset = p - PyBytes_AS_STRING(result);
-
- if (nallocated > PY_SSIZE_T_MAX - repsize + 4) {
- /* integer overflow */
- PyErr_NoMemory();
goto error;
- }
- nallocated += repsize - 4;
- if (result != NULL) {
- if (_PyBytes_Resize(&result, nallocated) < 0)
- goto error;
- } else {
- result = PyBytes_FromStringAndSize(NULL, nallocated);
- if (result == NULL)
- goto error;
- Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset);
- }
- p = PyBytes_AS_STRING(result) + offset;
+ Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset);
}
+ p = PyBytes_AS_STRING(result) + offset;
+ }
- if (PyBytes_Check(rep)) {
- char *prep = PyBytes_AS_STRING(rep);
- for(k = repsize; k > 0; k--)
- *p++ = *prep++;
- } else /* rep is unicode */ {
- Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep);
- Py_UNICODE c;
-
- for(k=0; k<repsize; k++) {
- c = prep[k];
- if (0x80 <= c) {
- raise_encode_exception(&exc, "utf-8", s, size,
- i-1, i, "surrogates not allowed");
- goto error;
- }
- *p++ = (char)prep[k];
+ if (PyBytes_Check(rep)) {
+ char *prep = PyBytes_AS_STRING(rep);
+ for(k = repsize; k > 0; k--)
+ *p++ = *prep++;
+ } else /* rep is unicode */ {
+ const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep);
+ Py_UNICODE c;
+
+ for(k=0; k<repsize; k++) {
+ c = prep[k];
+ if (0x80 <= c) {
+ raise_encode_exception(&exc, "utf-8",
+ PyUnicode_AS_UNICODE(unicode),
+ size, i-1, i,
+ "surrogates not allowed");
+ goto error;
}
+ *p++ = (char)prep[k];
}
- Py_DECREF(rep);
-#ifndef Py_UNICODE_WIDE
}
-#endif
+ Py_DECREF(rep);
} else if (ch < 0x10000) {
*p++ = (char)(0xe0 | (ch >> 12));
*p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
@@ -3071,6 +4047,9 @@ PyUnicode_EncodeUTF8(const Py_UNICODE *s,
*p++ = (char)(0x80 | ((ch >> 12) & 0x3f));
*p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
*p++ = (char)(0x80 | (ch & 0x3f));
+#if SIZEOF_WCHAR_T == 2
+ wchar_offset++;
+#endif
}
}
@@ -3086,6 +4065,7 @@ PyUnicode_EncodeUTF8(const Py_UNICODE *s,
assert(nneeded <= nallocated);
_PyBytes_Resize(&result, nneeded);
}
+
Py_XDECREF(errorHandler);
Py_XDECREF(exc);
return result;
@@ -3099,18 +4079,24 @@ PyUnicode_EncodeUTF8(const Py_UNICODE *s,
}
PyObject *
-PyUnicode_AsUTF8String(PyObject *unicode)
+PyUnicode_EncodeUTF8(const Py_UNICODE *s,
+ Py_ssize_t size,
+ const char *errors)
{
- PyObject *utf8;
- if (!PyUnicode_Check(unicode)) {
- PyErr_BadArgument();
- return NULL;
- }
- utf8 = _PyUnicode_AsDefaultEncodedString(unicode);
- if (utf8 == NULL)
+ PyObject *v, *unicode;
+
+ unicode = PyUnicode_FromUnicode(s, size);
+ if (unicode == NULL)
return NULL;
- Py_INCREF(utf8);
- return utf8;
+ v = _PyUnicode_AsUTF8String(unicode, errors);
+ Py_DECREF(unicode);
+ return v;
+}
+
+PyObject *
+PyUnicode_AsUTF8String(PyObject *unicode)
+{
+ return _PyUnicode_AsUTF8String(unicode, NULL);
}
/* --- UTF-32 Codec ------------------------------------------------------- */
@@ -3222,7 +4208,7 @@ PyUnicode_DecodeUTF32Stateful(const char *s,
return (PyObject *)unicode;
/* Unpack UTF-32 encoded data */
- p = unicode->str;
+ p = PyUnicode_AS_UNICODE(unicode);
while (q < e) {
Py_UCS4 ch;
@@ -3275,11 +4261,15 @@ PyUnicode_DecodeUTF32Stateful(const char *s,
*consumed = (const char *)q-starts;
/* Adjust length */
- if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0)
+ if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0)
goto onError;
Py_XDECREF(errorHandler);
Py_XDECREF(exc);
+ if (PyUnicode_READY(unicode) == -1) {
+ Py_DECREF(unicode);
+ return NULL;
+ }
return (PyObject *)unicode;
onError:
@@ -3452,7 +4442,7 @@ PyUnicode_DecodeUTF16Stateful(const char *s,
return (PyObject *)unicode;
/* Unpack UTF-16 encoded data */
- p = unicode->str;
+ p = PyUnicode_AS_UNICODE(unicode);
q = (unsigned char *)s;
e = q + size - 1;
@@ -3669,11 +4659,15 @@ PyUnicode_DecodeUTF16Stateful(const char *s,
*consumed = (const char *)q-starts;
/* Adjust length */
- if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0)
+ if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0)
goto onError;
Py_XDECREF(errorHandler);
Py_XDECREF(exc);
+ if (PyUnicode_READY(unicode) == -1) {
+ Py_DECREF(unicode);
+ return NULL;
+ }
return (PyObject *)unicode;
onError:
@@ -3782,6 +4776,76 @@ PyUnicode_AsUTF16String(PyObject *unicode)
/* --- Unicode Escape Codec ----------------------------------------------- */
+/* Helper function for PyUnicode_DecodeUnicodeEscape, determines
+ if all the escapes in the string make it still a valid ASCII string.
+ Returns -1 if any escapes were found which cause the string to
+ pop out of ASCII range. Otherwise returns the length of the
+ required buffer to hold the string.
+ */
+Py_ssize_t
+length_of_escaped_ascii_string(const char *s, Py_ssize_t size)
+{
+ const unsigned char *p = (const unsigned char *)s;
+ const unsigned char *end = p + size;
+ Py_ssize_t length = 0;
+
+ if (size < 0)
+ return -1;
+
+ for (; p < end; ++p) {
+ if (*p > 127) {
+ /* Non-ASCII */
+ return -1;
+ }
+ else if (*p != '\\') {
+ /* Normal character */
+ ++length;
+ }
+ else {
+ /* Backslash-escape, check next char */
+ ++p;
+ /* Escape sequence reaches till end of string or
+ non-ASCII follow-up. */
+ if (p >= end || *p > 127)
+ return -1;
+ switch (*p) {
+ case '\n':
+ /* backslash + \n result in zero characters */
+ break;
+ case '\\': case '\'': case '\"':
+ case 'b': case 'f': case 't':
+ case 'n': case 'r': case 'v': case 'a':
+ ++length;
+ break;
+ case '0': case '1': case '2': case '3':
+ case '4': case '5': case '6': case '7':
+ case 'x': case 'u': case 'U': case 'N':
+ /* these do not guarantee ASCII characters */
+ return -1;
+ default:
+ /* count the backslash + the other character */
+ length += 2;
+ }
+ }
+ }
+ return length;
+}
+
+/* Similar to PyUnicode_WRITE but either write into wstr field
+ or treat string as ASCII. */
+#define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \
+ do { \
+ if ((kind) != PyUnicode_WCHAR_KIND) \
+ ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \
+ else \
+ ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \
+ } while (0)
+
+#define WRITE_WSTR(buf, index, value) \
+ assert(kind == PyUnicode_WCHAR_KIND), \
+ ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value)
+
+
static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL;
PyObject *
@@ -3792,8 +4856,7 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
const char *starts = s;
Py_ssize_t startinpos;
Py_ssize_t endinpos;
- Py_ssize_t outpos;
- int i;
+ int j;
PyUnicodeObject *v;
Py_UNICODE *p;
const char *end;
@@ -3801,19 +4864,42 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */
PyObject *errorHandler = NULL;
PyObject *exc = NULL;
+ Py_ssize_t ascii_length;
+ Py_ssize_t i;
+ int kind;
+ void *data;
+
+ ascii_length = length_of_escaped_ascii_string(s, size);
+
+ /* After length_of_escaped_ascii_string() there are two alternatives,
+ either the string is pure ASCII with named escapes like \n, etc.
+ and we determined it's exact size (common case)
+ or it contains \x, \u, ... escape sequences. then we create a
+ legacy wchar string and resize it at the end of this function. */
+ if (ascii_length >= 0) {
+ v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127);
+ if (!v)
+ goto onError;
+ assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
+ kind = PyUnicode_1BYTE_KIND;
+ data = PyUnicode_DATA(v);
+ }
+ else {
+ /* Escaped strings will always be longer than the resulting
+ Unicode string, so we start with size here and then reduce the
+ length after conversion to the true value.
+ (but if the error callback returns a long replacement string
+ we'll have to allocate more space) */
+ v = _PyUnicode_New(size);
+ if (!v)
+ goto onError;
+ kind = PyUnicode_WCHAR_KIND;
+ data = PyUnicode_AS_UNICODE(v);
+ }
- /* Escaped strings will always be longer than the resulting
- Unicode string, so we start with size here and then reduce the
- length after conversion to the true value.
- (but if the error callback returns a long replacement string
- we'll have to allocate more space) */
- v = _PyUnicode_New(size);
- if (v == NULL)
- goto onError;
if (size == 0)
return (PyObject *)v;
-
- p = PyUnicode_AS_UNICODE(v);
+ i = 0;
end = s + size;
while (s < end) {
@@ -3821,9 +4907,18 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
Py_UNICODE x;
int digits;
+ if (kind == PyUnicode_WCHAR_KIND) {
+ assert(i < _PyUnicode_WSTR_LENGTH(v));
+ }
+ else {
+ /* The only case in which i == ascii_length is a backslash
+ followed by a newline. */
+ assert(i <= ascii_length);
+ }
+
/* Non-escape characters are interpreted as Unicode ordinals */
if (*s != '\\') {
- *p++ = (unsigned char) *s++;
+ WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++);
continue;
}
@@ -3833,20 +4928,33 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
c = *s++;
if (s > end)
c = '\0'; /* Invalid after \ */
+
+ if (kind == PyUnicode_WCHAR_KIND) {
+ assert(i < _PyUnicode_WSTR_LENGTH(v));
+ }
+ else {
+ /* The only case in which i == ascii_length is a backslash
+ followed by a newline. */
+ assert(i < ascii_length || (i == ascii_length && c == '\n'));
+ }
+
switch (c) {
/* \x escapes */
case '\n': break;
- case '\\': *p++ = '\\'; break;
- case '\'': *p++ = '\''; break;
- case '\"': *p++ = '\"'; break;
- case 'b': *p++ = '\b'; break;
- case 'f': *p++ = '\014'; break; /* FF */
- case 't': *p++ = '\t'; break;
- case 'n': *p++ = '\n'; break;
- case 'r': *p++ = '\r'; break;
- case 'v': *p++ = '\013'; break; /* VT */
- case 'a': *p++ = '\007'; break; /* BEL, not classic C */
+ case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break;
+ case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break;
+ case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break;
+ case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break;
+ /* FF */
+ case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break;
+ case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break;
+ case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break;
+ case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break;
+ /* VT */
+ case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break;
+ /* BEL, not classic C */
+ case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break;
/* \OOO (octal) escapes */
case '0': case '1': case '2': case '3':
@@ -3857,7 +4965,7 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
if (s < end && '0' <= *s && *s <= '7')
x = (x<<3) + *s++ - '0';
}
- *p++ = x;
+ WRITE_WSTR(data, i++, x);
break;
/* hex escapes */
@@ -3879,27 +4987,30 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
message = "truncated \\UXXXXXXXX escape";
hexescape:
chr = 0;
- outpos = p-PyUnicode_AS_UNICODE(v);
+ p = PyUnicode_AS_UNICODE(v) + i;
if (s+digits>end) {
endinpos = size;
if (unicode_decode_call_errorhandler(
errors, &errorHandler,
"unicodeescape", "end of string in escape sequence",
&starts, &end, &startinpos, &endinpos, &exc, &s,
- &v, &outpos, &p))
+ &v, &i, &p))
goto onError;
+ data = PyUnicode_AS_UNICODE(v);
goto nextByte;
}
- for (i = 0; i < digits; ++i) {
- c = (unsigned char) s[i];
+ for (j = 0; j < digits; ++j) {
+ c = (unsigned char) s[j];
if (!Py_ISXDIGIT(c)) {
- endinpos = (s+i+1)-starts;
+ endinpos = (s+j+1)-starts;
+ p = PyUnicode_AS_UNICODE(v) + i;
if (unicode_decode_call_errorhandler(
errors, &errorHandler,
"unicodeescape", message,
&starts, &end, &startinpos, &endinpos, &exc, &s,
- &v, &outpos, &p))
+ &v, &i, &p))
goto onError;
+ data = PyUnicode_AS_UNICODE(v);
goto nextByte;
}
chr = (chr<<4) & ~0xF;
@@ -3910,7 +5021,7 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
else
chr += 10 + c - 'A';
}
- s += i;
+ s += j;
if (chr == 0xffffffff && PyErr_Occurred())
/* _decoding_error will have already written into the
target buffer. */
@@ -3919,26 +5030,27 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
/* when we get here, chr is a 32-bit unicode character */
if (chr <= 0xffff)
/* UCS-2 character */
- *p++ = (Py_UNICODE) chr;
+ WRITE_WSTR(data, i++, chr);
else if (chr <= 0x10ffff) {
/* UCS-4 character. Either store directly, or as
surrogate pair. */
#ifdef Py_UNICODE_WIDE
- *p++ = chr;
+ WRITE_WSTR(data, i++, chr);
#else
chr -= 0x10000L;
- *p++ = 0xD800 + (Py_UNICODE) (chr >> 10);
- *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF);
+ WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10));
+ WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF));
#endif
} else {
endinpos = s-starts;
- outpos = p-PyUnicode_AS_UNICODE(v);
+ p = PyUnicode_AS_UNICODE(v) + i;
if (unicode_decode_call_errorhandler(
errors, &errorHandler,
"unicodeescape", "illegal Unicode character",
&starts, &end, &startinpos, &endinpos, &exc, &s,
- &v, &outpos, &p))
+ &v, &i, &p))
goto onError;
+ data = PyUnicode_AS_UNICODE(v);
}
break;
@@ -3947,7 +5059,8 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
message = "malformed \\N character escape";
if (ucnhash_CAPI == NULL) {
/* load the unicode data module */
- ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1);
+ ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(
+ PyUnicodeData_CAPSULE_NAME, 1);
if (ucnhash_CAPI == NULL)
goto ucnhashError;
}
@@ -3960,43 +5073,51 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
/* found a name. look it up in the unicode database */
message = "unknown Unicode character name";
s++;
- if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr))
+ if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1),
+ &chr))
goto store;
}
}
endinpos = s-starts;
- outpos = p-PyUnicode_AS_UNICODE(v);
+ p = PyUnicode_AS_UNICODE(v) + i;
if (unicode_decode_call_errorhandler(
errors, &errorHandler,
"unicodeescape", message,
&starts, &end, &startinpos, &endinpos, &exc, &s,
- &v, &outpos, &p))
+ &v, &i, &p))
goto onError;
+ data = PyUnicode_AS_UNICODE(v);
break;
default:
if (s > end) {
+ assert(kind == PyUnicode_WCHAR_KIND);
message = "\\ at end of string";
s--;
endinpos = s-starts;
- outpos = p-PyUnicode_AS_UNICODE(v);
+ p = PyUnicode_AS_UNICODE(v) + i;
if (unicode_decode_call_errorhandler(
errors, &errorHandler,
"unicodeescape", message,
&starts, &end, &startinpos, &endinpos, &exc, &s,
- &v, &outpos, &p))
+ &v, &i, &p))
goto onError;
+ data = PyUnicode_AS_UNICODE(v);
}
else {
- *p++ = '\\';
- *p++ = (unsigned char)s[-1];
+ WRITE_ASCII_OR_WSTR(kind, data, i++, '\\');
+ WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]);
}
break;
}
nextByte:
;
}
- if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0)
+ /* Ensure the length prediction worked in case of ASCII strings */
+ assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length);
+
+ if (kind == PyUnicode_WCHAR_KIND && (_PyUnicode_Resize(&v, i) < 0 ||
+ PyUnicode_READY(v) == -1))
goto onError;
Py_XDECREF(errorHandler);
Py_XDECREF(exc);
@@ -4019,6 +5140,9 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
return NULL;
}
+#undef WRITE_ASCII_OR_WSTR
+#undef WRITE_WSTR
+
/* Return a Unicode-Escape string version of the Unicode object.
If quotes is true, the string is enclosed in u"" or u'' quotes as
@@ -4026,21 +5150,6 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
*/
-Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s,
- Py_ssize_t size,
- Py_UNICODE ch)
-{
- /* like wcschr, but doesn't stop at NULL characters */
-
- while (size-- > 0) {
- if (*s == ch)
- return s;
- s++;
- }
-
- return NULL;
-}
-
static const char *hexdigits = "0123456789abcdef";
PyObject *
@@ -4309,6 +5418,10 @@ PyUnicode_DecodeRawUnicodeEscape(const char *s,
goto onError;
Py_XDECREF(errorHandler);
Py_XDECREF(exc);
+ if (PyUnicode_READY(v) == -1) {
+ Py_DECREF(v);
+ return NULL;
+ }
return (PyObject *)v;
onError:
@@ -4447,7 +5560,9 @@ _PyUnicode_DecodeUnicodeInternal(const char *s,
v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE);
if (v == NULL)
goto onError;
- if (PyUnicode_GetSize((PyObject *)v) == 0)
+ /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH
+ as string was created with the old API. */
+ if (PyUnicode_GET_SIZE(v) == 0)
return (PyObject *)v;
p = PyUnicode_AS_UNICODE(v);
end = s + size;
@@ -4491,6 +5606,10 @@ _PyUnicode_DecodeUnicodeInternal(const char *s,
goto onError;
Py_XDECREF(errorHandler);
Py_XDECREF(exc);
+ if (PyUnicode_READY(v) == -1) {
+ Py_DECREF(v);
+ return NULL;
+ }
return (PyObject *)v;
onError:
@@ -4507,41 +5626,8 @@ PyUnicode_DecodeLatin1(const char *s,
Py_ssize_t size,
const char *errors)
{
- PyUnicodeObject *v;
- Py_UNICODE *p;
- const char *e, *unrolled_end;
-
/* Latin-1 is equivalent to the first 256 ordinals in Unicode. */
- if (size == 1) {
- Py_UNICODE r = *(unsigned char*)s;
- return PyUnicode_FromUnicode(&r, 1);
- }
-
- v = _PyUnicode_New(size);
- if (v == NULL)
- goto onError;
- if (size == 0)
- return (PyObject *)v;
- p = PyUnicode_AS_UNICODE(v);
- e = s + size;
- /* Unrolling the copy makes it much faster by reducing the looping
- overhead. This is similar to what many memcpy() implementations do. */
- unrolled_end = e - 4;
- while (s < unrolled_end) {
- p[0] = (unsigned char) s[0];
- p[1] = (unsigned char) s[1];
- p[2] = (unsigned char) s[2];
- p[3] = (unsigned char) s[3];
- s += 4;
- p += 4;
- }
- while (s < e)
- *p++ = (unsigned char) *s++;
- return (PyObject *)v;
-
- onError:
- Py_XDECREF(v);
- return NULL;
+ return PyUnicode_FromUCS1((unsigned char*)s, size);
}
/* create or adjust a UnicodeEncodeError */
@@ -4849,15 +5935,30 @@ PyUnicode_EncodeLatin1(const Py_UNICODE *p,
}
PyObject *
-PyUnicode_AsLatin1String(PyObject *unicode)
+_PyUnicode_AsLatin1String(PyObject *unicode, const char *errors)
{
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
return NULL;
}
+ if (PyUnicode_READY(unicode) == -1)
+ return NULL;
+ /* Fast path: if it is a one-byte string, construct
+ bytes object directly. */
+ if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND)
+ return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode),
+ PyUnicode_GET_LENGTH(unicode));
+ /* Non-Latin-1 characters present. Defer to above function to
+ raise the exception. */
return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode),
PyUnicode_GET_SIZE(unicode),
- NULL);
+ errors);
+}
+
+PyObject*
+PyUnicode_AsLatin1String(PyObject *unicode)
+{
+ return _PyUnicode_AsLatin1String(unicode, NULL);
}
/* --- 7-bit ASCII Codec -------------------------------------------------- */
@@ -4874,14 +5975,32 @@ PyUnicode_DecodeASCII(const char *s,
Py_ssize_t endinpos;
Py_ssize_t outpos;
const char *e;
+ unsigned char* d;
PyObject *errorHandler = NULL;
PyObject *exc = NULL;
+ Py_ssize_t i;
/* ASCII is equivalent to the first 128 ordinals in Unicode. */
- if (size == 1 && *(unsigned char*)s < 128) {
- Py_UNICODE r = *(unsigned char*)s;
- return PyUnicode_FromUnicode(&r, 1);
+ if (size == 1 && *(unsigned char*)s < 128)
+ return PyUnicode_FromOrdinal(*(unsigned char*)s);
+
+ /* Fast path. Assume the input actually *is* ASCII, and allocate
+ a single-block Unicode object with that assumption. If there is
+ an error, drop the object and start over. */
+ v = (PyUnicodeObject*)PyUnicode_New(size, 127);
+ if (v == NULL)
+ goto onError;
+ d = PyUnicode_1BYTE_DATA(v);
+ for (i = 0; i < size; i++) {
+ unsigned char ch = ((unsigned char*)s)[i];
+ if (ch < 128)
+ d[i] = ch;
+ else
+ break;
}
+ if (i == size)
+ return (PyObject*)v;
+ Py_DECREF(v); /* start over */
v = _PyUnicode_New(size);
if (v == NULL)
@@ -4913,6 +6032,10 @@ PyUnicode_DecodeASCII(const char *s,
goto onError;
Py_XDECREF(errorHandler);
Py_XDECREF(exc);
+ if (PyUnicode_READY(v) == -1) {
+ Py_DECREF(v);
+ return NULL;
+ }
return (PyObject *)v;
onError:
@@ -4931,15 +6054,28 @@ PyUnicode_EncodeASCII(const Py_UNICODE *p,
}
PyObject *
-PyUnicode_AsASCIIString(PyObject *unicode)
+_PyUnicode_AsASCIIString(PyObject *unicode, const char *errors)
{
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
return NULL;
}
+ if (PyUnicode_READY(unicode) == -1)
+ return NULL;
+ /* Fast path: if it is an ASCII-only string, construct bytes object
+ directly. Else defer to above function to raise the exception. */
+ if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128)
+ return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode),
+ PyUnicode_GET_LENGTH(unicode));
return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode),
PyUnicode_GET_SIZE(unicode),
- NULL);
+ errors);
+}
+
+PyObject *
+PyUnicode_AsASCIIString(PyObject *unicode)
+{
+ return _PyUnicode_AsASCIIString(unicode, NULL);
}
#ifdef HAVE_MBCS
@@ -5090,7 +6226,10 @@ PyUnicode_DecodeMBCSStateful(const char *s,
goto retry;
}
#endif
-
+ if (PyUnicode_READY(v) == -1) {
+ Py_DECREF(v);
+ return NULL;
+ }
return (PyObject *)v;
}
@@ -5386,6 +6525,10 @@ PyUnicode_DecodeCharmap(const char *s,
goto onError;
Py_XDECREF(errorHandler);
Py_XDECREF(exc);
+ if (PyUnicode_READY(v) == -1) {
+ Py_DECREF(v);
+ return NULL;
+ }
return (PyObject *)v;
onError:
@@ -5471,7 +6614,6 @@ static PyTypeObject EncodingMapType = {
PyObject*
PyUnicode_BuildEncodingMap(PyObject* string)
{
- Py_UNICODE *decode;
PyObject *result;
struct encoding_map *mresult;
int i;
@@ -5480,35 +6622,36 @@ PyUnicode_BuildEncodingMap(PyObject* string)
unsigned char level2[512];
unsigned char *mlevel1, *mlevel2, *mlevel3;
int count2 = 0, count3 = 0;
+ int kind;
+ void *data;
+ Py_UCS4 ch;
- if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) {
+ if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) {
PyErr_BadArgument();
return NULL;
}
- decode = PyUnicode_AS_UNICODE(string);
+ kind = PyUnicode_KIND(string);
+ data = PyUnicode_DATA(string);
memset(level1, 0xFF, sizeof level1);
memset(level2, 0xFF, sizeof level2);
/* If there isn't a one-to-one mapping of NULL to \0,
or if there are non-BMP characters, we need to use
a mapping dictionary. */
- if (decode[0] != 0)
+ if (PyUnicode_READ(kind, data, 0) != 0)
need_dict = 1;
for (i = 1; i < 256; i++) {
int l1, l2;
- if (decode[i] == 0
-#ifdef Py_UNICODE_WIDE
- || decode[i] > 0xFFFF
-#endif
- ) {
+ ch = PyUnicode_READ(kind, data, i);
+ if (ch == 0 || ch > 0xFFFF) {
need_dict = 1;
break;
}
- if (decode[i] == 0xFFFE)
+ if (ch == 0xFFFE)
/* unmapped character */
continue;
- l1 = decode[i] >> 11;
- l2 = decode[i] >> 7;
+ l1 = ch >> 11;
+ l2 = ch >> 7;
if (level1[l1] == 0xFF)
level1[l1] = count2++;
if (level2[l2] == 0xFF)
@@ -5524,7 +6667,7 @@ PyUnicode_BuildEncodingMap(PyObject* string)
if (!result)
return NULL;
for (i = 0; i < 256; i++) {
- key = PyLong_FromLong(decode[i]);
+ key = PyLong_FromLong(PyUnicode_READ(kind, data, i));
value = PyLong_FromLong(i);
if (!key || !value)
goto failed1;
@@ -5559,15 +6702,15 @@ PyUnicode_BuildEncodingMap(PyObject* string)
count3 = 0;
for (i = 1; i < 256; i++) {
int o1, o2, o3, i2, i3;
- if (decode[i] == 0xFFFE)
+ if (PyUnicode_READ(kind, data, i) == 0xFFFE)
/* unmapped character */
continue;
- o1 = decode[i]>>11;
- o2 = (decode[i]>>7) & 0xF;
+ o1 = PyUnicode_READ(kind, data, i)>>11;
+ o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF;
i2 = 16*mlevel1[o1] + o2;
if (mlevel2[i2] == 0xFF)
mlevel2[i2] = count3++;
- o3 = decode[i] & 0x7F;
+ o3 = PyUnicode_READ(kind, data, i) & 0x7F;
i3 = 128*mlevel2[i2] + o3;
mlevel3[i3] = i;
}
@@ -5951,13 +7094,13 @@ PyUnicode_AsCharmapString(PyObject *unicode,
/* create or adjust a UnicodeTranslateError */
static void
make_translate_exception(PyObject **exceptionObject,
- const Py_UNICODE *unicode, Py_ssize_t size,
+ PyObject *unicode,
Py_ssize_t startpos, Py_ssize_t endpos,
const char *reason)
{
if (*exceptionObject == NULL) {
- *exceptionObject = PyUnicodeTranslateError_Create(
- unicode, size, startpos, endpos, reason);
+ *exceptionObject = _PyUnicodeTranslateError_Create(
+ unicode, startpos, endpos, reason);
}
else {
if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos))
@@ -5976,12 +7119,12 @@ make_translate_exception(PyObject **exceptionObject,
/* raises a UnicodeTranslateError */
static void
raise_translate_exception(PyObject **exceptionObject,
- const Py_UNICODE *unicode, Py_ssize_t size,
+ PyObject *unicode,
Py_ssize_t startpos, Py_ssize_t endpos,
const char *reason)
{
make_translate_exception(exceptionObject,
- unicode, size, startpos, endpos, reason);
+ unicode, startpos, endpos, reason);
if (*exceptionObject != NULL)
PyCodec_StrictErrors(*exceptionObject);
}
@@ -5994,7 +7137,7 @@ static PyObject *
unicode_translate_call_errorhandler(const char *errors,
PyObject **errorHandler,
const char *reason,
- const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject,
+ PyObject *unicode, PyObject **exceptionObject,
Py_ssize_t startpos, Py_ssize_t endpos,
Py_ssize_t *newpos)
{
@@ -6011,7 +7154,7 @@ unicode_translate_call_errorhandler(const char *errors,
}
make_translate_exception(exceptionObject,
- unicode, size, startpos, endpos, reason);
+ unicode, startpos, endpos, reason);
if (*exceptionObject == NULL)
return NULL;
@@ -6030,10 +7173,10 @@ unicode_translate_call_errorhandler(const char *errors,
return NULL;
}
if (i_newpos<0)
- *newpos = size+i_newpos;
+ *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos;
else
*newpos = i_newpos;
- if (*newpos<0 || *newpos>size) {
+ if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) {
PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos);
Py_DECREF(restuple);
return NULL;
@@ -6047,7 +7190,7 @@ unicode_translate_call_errorhandler(const char *errors,
which must be decrefed by the caller.
Return 0 on success, -1 on error */
static int
-charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result)
+charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result)
{
PyObject *w = PyLong_FromLong((long)c);
PyObject *x;
@@ -6097,19 +7240,18 @@ charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result)
if not reallocate and adjust various state variables.
Return 0 on success, -1 on error */
static int
-charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp,
+charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize,
Py_ssize_t requiredsize)
{
- Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj);
+ Py_ssize_t oldsize = *psize;
if (requiredsize > oldsize) {
- /* remember old output position */
- Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj);
/* exponentially overallocate to minimize reallocations */
if (requiredsize < 2 * oldsize)
requiredsize = 2 * oldsize;
- if (PyUnicode_Resize(outobj, requiredsize) < 0)
+ *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4));
+ if (*outobj == 0)
return -1;
- *outp = PyUnicode_AS_UNICODE(*outobj) + outpos;
+ *psize = requiredsize;
}
return 0;
}
@@ -6120,37 +7262,43 @@ charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp,
The called must decref result.
Return 0 on success, -1 on error. */
static int
-charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp,
- Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp,
+charmaptranslate_output(PyObject *input, Py_ssize_t ipos,
+ PyObject *mapping, Py_UCS4 **output,
+ Py_ssize_t *osize, Py_ssize_t *opos,
PyObject **res)
{
- if (charmaptranslate_lookup(*curinp, mapping, res))
+ Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos);
+ if (charmaptranslate_lookup(curinp, mapping, res))
return -1;
if (*res==NULL) {
/* not found => default to 1:1 mapping */
- *(*outp)++ = *curinp;
+ (*output)[(*opos)++] = curinp;
}
else if (*res==Py_None)
;
else if (PyLong_Check(*res)) {
/* no overflow check, because we know that the space is enough */
- *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res);
+ (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res);
}
else if (PyUnicode_Check(*res)) {
- Py_ssize_t repsize = PyUnicode_GET_SIZE(*res);
+ Py_ssize_t repsize;
+ if (PyUnicode_READY(*res) == -1)
+ return -1;
+ repsize = PyUnicode_GET_LENGTH(*res);
if (repsize==1) {
/* no overflow check, because we know that the space is enough */
- *(*outp)++ = *PyUnicode_AS_UNICODE(*res);
+ (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0);
}
else if (repsize!=0) {
/* more than one character */
- Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) +
- (insize - (curinp-startinp)) +
+ Py_ssize_t requiredsize = *opos +
+ (PyUnicode_GET_LENGTH(input) - ipos) +
repsize - 1;
- if (charmaptranslate_makespace(outobj, outp, requiredsize))
+ Py_ssize_t i;
+ if (charmaptranslate_makespace(output, osize, requiredsize))
return -1;
- memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize);
- *outp += repsize;
+ for(i = 0; i < repsize; i++)
+ (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i);
}
}
else
@@ -6159,20 +7307,20 @@ charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp,
}
PyObject *
-PyUnicode_TranslateCharmap(const Py_UNICODE *p,
- Py_ssize_t size,
- PyObject *mapping,
- const char *errors)
-{
- /* output object */
- PyObject *res = NULL;
- /* pointers to the beginning and end+1 of input */
- const Py_UNICODE *startp = p;
- const Py_UNICODE *endp = p + size;
- /* pointer into the output */
- Py_UNICODE *str;
+_PyUnicode_TranslateCharmap(PyObject *input,
+ PyObject *mapping,
+ const char *errors)
+{
+ /* input object */
+ char *idata;
+ Py_ssize_t size, i;
+ int kind;
+ /* output buffer */
+ Py_UCS4 *output = NULL;
+ Py_ssize_t osize;
+ PyObject *res;
/* current output position */
- Py_ssize_t respos = 0;
+ Py_ssize_t opos;
char *reason = "character maps to <undefined>";
PyObject *errorHandler = NULL;
PyObject *exc = NULL;
@@ -6186,38 +7334,52 @@ PyUnicode_TranslateCharmap(const Py_UNICODE *p,
return NULL;
}
+ if (PyUnicode_READY(input) == -1)
+ return NULL;
+ idata = (char*)PyUnicode_DATA(input);
+ kind = PyUnicode_KIND(input);
+ size = PyUnicode_GET_LENGTH(input);
+ i = 0;
+
+ if (size == 0) {
+ Py_INCREF(input);
+ return input;
+ }
+
/* allocate enough for a simple 1:1 translation without
replacements, if we need more, we'll resize */
- res = PyUnicode_FromUnicode(NULL, size);
- if (res == NULL)
+ osize = size;
+ output = PyMem_Malloc(osize * sizeof(Py_UCS4));
+ opos = 0;
+ if (output == NULL) {
+ PyErr_NoMemory();
goto onError;
- if (size == 0)
- return res;
- str = PyUnicode_AS_UNICODE(res);
+ }
- while (p<endp) {
+ while (i<size) {
/* try to encode it */
PyObject *x = NULL;
- if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) {
+ if (charmaptranslate_output(input, i, mapping,
+ &output, &osize, &opos, &x)) {
Py_XDECREF(x);
goto onError;
}
Py_XDECREF(x);
if (x!=Py_None) /* it worked => adjust input pointer */
- ++p;
+ ++i;
else { /* untranslatable character */
PyObject *repunicode = NULL; /* initialize to prevent gcc warning */
Py_ssize_t repsize;
Py_ssize_t newpos;
- Py_UNICODE *uni2;
+ Py_ssize_t uni2;
/* startpos for collecting untranslatable chars */
- const Py_UNICODE *collstart = p;
- const Py_UNICODE *collend = p+1;
- const Py_UNICODE *coll;
+ Py_ssize_t collstart = i;
+ Py_ssize_t collend = i+1;
+ Py_ssize_t coll;
/* find all untranslatable characters */
- while (collend < endp) {
- if (charmaptranslate_lookup(*collend, mapping, &x))
+ while (collend < size) {
+ if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x))
goto onError;
Py_XDECREF(x);
if (x!=Py_None)
@@ -6240,67 +7402,79 @@ PyUnicode_TranslateCharmap(const Py_UNICODE *p,
}
switch (known_errorHandler) {
case 1: /* strict */
- raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason);
+ raise_translate_exception(&exc, input, collstart,
+ collend, reason);
goto onError;
case 2: /* replace */
/* No need to check for space, this is a 1:1 replacement */
- for (coll = collstart; coll<collend; ++coll)
- *str++ = '?';
+ for (coll = collstart; coll<collend; coll++)
+ output[opos++] = '?';
/* fall through */
case 3: /* ignore */
- p = collend;
+ i = collend;
break;
case 4: /* xmlcharrefreplace */
- /* generate replacement (temporarily (mis)uses p) */
- for (p = collstart; p < collend; ++p) {
+ /* generate replacement (temporarily (mis)uses i) */
+ for (i = collstart; i < collend; ++i) {
char buffer[2+29+1+1];
char *cp;
- sprintf(buffer, "&#%d;", (int)*p);
- if (charmaptranslate_makespace(&res, &str,
- (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend)))
+ sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i));
+ if (charmaptranslate_makespace(&output, &osize,
+ opos+strlen(buffer)+(size-collend)))
goto onError;
for (cp = buffer; *cp; ++cp)
- *str++ = *cp;
+ output[opos++] = *cp;
}
- p = collend;
+ i = collend;
break;
default:
repunicode = unicode_translate_call_errorhandler(errors, &errorHandler,
- reason, startp, size, &exc,
- collstart-startp, collend-startp, &newpos);
- if (repunicode == NULL)
+ reason, input, &exc,
+ collstart, collend, &newpos);
+ if (repunicode == NULL || PyUnicode_READY(repunicode) == -1)
goto onError;
/* generate replacement */
- repsize = PyUnicode_GET_SIZE(repunicode);
- if (charmaptranslate_makespace(&res, &str,
- (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) {
+ repsize = PyUnicode_GET_LENGTH(repunicode);
+ if (charmaptranslate_makespace(&output, &osize,
+ opos+repsize+(size-collend))) {
Py_DECREF(repunicode);
goto onError;
}
- for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2)
- *str++ = *uni2;
- p = startp + newpos;
+ for (uni2 = 0; repsize-->0; ++uni2)
+ output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2);
+ i = newpos;
Py_DECREF(repunicode);
}
}
}
- /* Resize if we allocated to much */
- respos = str-PyUnicode_AS_UNICODE(res);
- if (respos<PyUnicode_GET_SIZE(res)) {
- if (PyUnicode_Resize(&res, respos) < 0)
- goto onError;
- }
+ res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos);
+ if (!res)
+ goto onError;
+ PyMem_Free(output);
Py_XDECREF(exc);
Py_XDECREF(errorHandler);
return res;
onError:
- Py_XDECREF(res);
+ PyMem_Free(output);
Py_XDECREF(exc);
Py_XDECREF(errorHandler);
return NULL;
}
+/* Deprecated. Use PyUnicode_Translate instead. */
+PyObject *
+PyUnicode_TranslateCharmap(const Py_UNICODE *p,
+ Py_ssize_t size,
+ PyObject *mapping,
+ const char *errors)
+{
+ PyObject *unicode = PyUnicode_FromUnicode(p, size);
+ if (!unicode)
+ return NULL;
+ return _PyUnicode_TranslateCharmap(unicode, mapping, errors);
+}
+
PyObject *
PyUnicode_Translate(PyObject *str,
PyObject *mapping,
@@ -6311,10 +7485,7 @@ PyUnicode_Translate(PyObject *str,
str = PyUnicode_FromObject(str);
if (str == NULL)
goto onError;
- result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- mapping,
- errors);
+ result = _PyUnicode_TranslateCharmap(str, mapping, errors);
Py_DECREF(str);
return result;
@@ -6323,6 +7494,60 @@ PyUnicode_Translate(PyObject *str,
return NULL;
}
+static Py_UCS4
+fix_decimal_and_space_to_ascii(PyUnicodeObject *self)
+{
+ /* No need to call PyUnicode_READY(self) because this function is only
+ called as a callback from fixup() which does it already. */
+ const Py_ssize_t len = PyUnicode_GET_LENGTH(self);
+ const int kind = PyUnicode_KIND(self);
+ void *data = PyUnicode_DATA(self);
+ Py_UCS4 maxchar = 0, ch, fixed;
+ Py_ssize_t i;
+
+ for (i = 0; i < len; ++i) {
+ ch = PyUnicode_READ(kind, data, i);
+ fixed = 0;
+ if (ch > 127) {
+ if (Py_UNICODE_ISSPACE(ch))
+ fixed = ' ';
+ else {
+ const int decimal = Py_UNICODE_TODECIMAL(ch);
+ if (decimal >= 0)
+ fixed = '0' + decimal;
+ }
+ if (fixed != 0) {
+ if (fixed > maxchar)
+ maxchar = fixed;
+ PyUnicode_WRITE(kind, data, i, fixed);
+ }
+ else if (ch > maxchar)
+ maxchar = ch;
+ }
+ else if (ch > maxchar)
+ maxchar = ch;
+ }
+
+ return maxchar;
+}
+
+PyObject *
+_PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode)
+{
+ if (!PyUnicode_Check(unicode)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ if (PyUnicode_READY(unicode) == -1)
+ return NULL;
+ if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) {
+ /* If the string is already ASCII, just return the same string */
+ Py_INCREF(unicode);
+ return unicode;
+ }
+ return fixup((PyUnicodeObject *)unicode, fix_decimal_and_space_to_ascii);
+}
+
PyObject *
PyUnicode_TransformDecimalToASCII(Py_UNICODE *s,
Py_ssize_t length)
@@ -6345,6 +7570,10 @@ PyUnicode_TransformDecimalToASCII(Py_UNICODE *s,
p[i] = '0' + decimal;
}
}
+ if (PyUnicode_READY((PyUnicodeObject*)result) == -1) {
+ Py_DECREF(result);
+ return NULL;
+ }
return result;
}
/* --- Decimal Encoder ---------------------------------------------------- */
@@ -6487,17 +7716,123 @@ PyUnicode_EncodeDecimal(Py_UNICODE *s,
/* --- Helpers ------------------------------------------------------------ */
-#include "stringlib/unicodedefs.h"
+#include "stringlib/ucs1lib.h"
#include "stringlib/fastsearch.h"
-
+#include "stringlib/partition.h"
+#include "stringlib/split.h"
#include "stringlib/count.h"
#include "stringlib/find.h"
+#include "stringlib/localeutil.h"
+#include "stringlib/undef.h"
+
+#include "stringlib/ucs2lib.h"
+#include "stringlib/fastsearch.h"
#include "stringlib/partition.h"
#include "stringlib/split.h"
+#include "stringlib/count.h"
+#include "stringlib/find.h"
+#include "stringlib/localeutil.h"
+#include "stringlib/undef.h"
-#define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping
-#define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale
+#include "stringlib/ucs4lib.h"
+#include "stringlib/fastsearch.h"
+#include "stringlib/partition.h"
+#include "stringlib/split.h"
+#include "stringlib/count.h"
+#include "stringlib/find.h"
#include "stringlib/localeutil.h"
+#include "stringlib/undef.h"
+
+static Py_ssize_t
+any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ucs1)(const Py_UCS1*, Py_ssize_t,
+ const Py_UCS1*, Py_ssize_t,
+ Py_ssize_t, Py_ssize_t),
+ Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t,
+ const Py_UCS2*, Py_ssize_t,
+ Py_ssize_t, Py_ssize_t),
+ Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t,
+ const Py_UCS4*, Py_ssize_t,
+ Py_ssize_t, Py_ssize_t),
+ PyObject* s1, PyObject* s2,
+ Py_ssize_t start,
+ Py_ssize_t end)
+{
+ int kind1, kind2, kind;
+ void *buf1, *buf2;
+ Py_ssize_t len1, len2, result;
+
+ kind1 = PyUnicode_KIND(s1);
+ kind2 = PyUnicode_KIND(s2);
+ kind = kind1 > kind2 ? kind1 : kind2;
+ buf1 = PyUnicode_DATA(s1);
+ buf2 = PyUnicode_DATA(s2);
+ if (kind1 != kind)
+ buf1 = _PyUnicode_AsKind(s1, kind);
+ if (!buf1)
+ return -2;
+ if (kind2 != kind)
+ buf2 = _PyUnicode_AsKind(s2, kind);
+ if (!buf2) {
+ if (kind1 != kind) PyMem_Free(buf1);
+ return -2;
+ }
+ len1 = PyUnicode_GET_LENGTH(s1);
+ len2 = PyUnicode_GET_LENGTH(s2);
+
+ switch(kind) {
+ case PyUnicode_1BYTE_KIND:
+ result = ucs1(buf1, len1, buf2, len2, start, end);
+ break;
+ case PyUnicode_2BYTE_KIND:
+ result = ucs2(buf1, len1, buf2, len2, start, end);
+ break;
+ case PyUnicode_4BYTE_KIND:
+ result = ucs4(buf1, len1, buf2, len2, start, end);
+ break;
+ default:
+ assert(0); result = -2;
+ }
+
+ if (kind1 != kind)
+ PyMem_Free(buf1);
+ if (kind2 != kind)
+ PyMem_Free(buf2);
+
+ return result;
+}
+
+Py_ssize_t
+_PyUnicode_InsertThousandsGrouping(int kind, void *data,
+ Py_ssize_t n_buffer,
+ void *digits, Py_ssize_t n_digits,
+ Py_ssize_t min_width,
+ const char *grouping,
+ const char *thousands_sep)
+{
+ switch(kind) {
+ case PyUnicode_1BYTE_KIND:
+ return _PyUnicode_ucs1_InsertThousandsGrouping(
+ (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits,
+ min_width, grouping, thousands_sep);
+ case PyUnicode_2BYTE_KIND:
+ return _PyUnicode_ucs2_InsertThousandsGrouping(
+ (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits,
+ min_width, grouping, thousands_sep);
+ case PyUnicode_4BYTE_KIND:
+ return _PyUnicode_ucs4_InsertThousandsGrouping(
+ (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits,
+ min_width, grouping, thousands_sep);
+ }
+ assert(0);
+ return -1;
+}
+
+
+#include "stringlib/unicodedefs.h"
+#include "stringlib/fastsearch.h"
+
+#include "stringlib/count.h"
+#include "stringlib/find.h"
/* helper macro to fixup start/end slice values */
#define ADJUST_INDICES(start, end, len) \
@@ -6514,28 +7849,6 @@ PyUnicode_EncodeDecimal(Py_UNICODE *s,
start = 0; \
}
-/* _Py_UNICODE_NEXT is a private macro used to retrieve the character pointed
- * by 'ptr', possibly combining surrogate pairs on narrow builds.
- * 'ptr' and 'end' must be Py_UNICODE*, with 'ptr' pointing at the character
- * that should be returned and 'end' pointing to the end of the buffer.
- * ('end' is used on narrow builds to detect a lone surrogate at the
- * end of the buffer that should be returned unchanged.)
- * The ptr and end arguments should be side-effect free and ptr must an lvalue.
- * The type of the returned char is always Py_UCS4.
- *
- * Note: the macro advances ptr to next char, so it might have side-effects
- * (especially if used with other macros).
- */
-#ifdef Py_UNICODE_WIDE
-#define _Py_UNICODE_NEXT(ptr, end) *(ptr)++
-#else
-#define _Py_UNICODE_NEXT(ptr, end) \
- (((Py_UNICODE_IS_HIGH_SURROGATE(*(ptr)) && (ptr) < (end)) && \
- Py_UNICODE_IS_LOW_SURROGATE((ptr)[1])) ? \
- ((ptr) += 2,Py_UNICODE_JOIN_SURROGATES((ptr)[-2], (ptr)[-1])) : \
- (Py_UCS4)*(ptr)++)
-#endif
-
Py_ssize_t
PyUnicode_Count(PyObject *str,
PyObject *substr,
@@ -6545,26 +7858,76 @@ PyUnicode_Count(PyObject *str,
Py_ssize_t result;
PyUnicodeObject* str_obj;
PyUnicodeObject* sub_obj;
+ int kind1, kind2, kind;
+ void *buf1 = NULL, *buf2 = NULL;
+ Py_ssize_t len1, len2;
str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str);
- if (!str_obj)
+ if (!str_obj || PyUnicode_READY(str_obj) == -1)
return -1;
sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr);
- if (!sub_obj) {
+ if (!sub_obj || PyUnicode_READY(str_obj) == -1) {
Py_DECREF(str_obj);
return -1;
}
- ADJUST_INDICES(start, end, str_obj->length);
- result = stringlib_count(
- str_obj->str + start, end - start, sub_obj->str, sub_obj->length,
- PY_SSIZE_T_MAX
- );
+ kind1 = PyUnicode_KIND(str_obj);
+ kind2 = PyUnicode_KIND(sub_obj);
+ kind = kind1 > kind2 ? kind1 : kind2;
+ buf1 = PyUnicode_DATA(str_obj);
+ if (kind1 != kind)
+ buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind);
+ if (!buf1)
+ goto onError;
+ buf2 = PyUnicode_DATA(sub_obj);
+ if (kind2 != kind)
+ buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind);
+ if (!buf2)
+ goto onError;
+ len1 = PyUnicode_GET_LENGTH(str_obj);
+ len2 = PyUnicode_GET_LENGTH(sub_obj);
+
+ ADJUST_INDICES(start, end, len1);
+ switch(kind) {
+ case PyUnicode_1BYTE_KIND:
+ result = ucs1lib_count(
+ ((Py_UCS1*)buf1) + start, end - start,
+ buf2, len2, PY_SSIZE_T_MAX
+ );
+ break;
+ case PyUnicode_2BYTE_KIND:
+ result = ucs2lib_count(
+ ((Py_UCS2*)buf1) + start, end - start,
+ buf2, len2, PY_SSIZE_T_MAX
+ );
+ break;
+ case PyUnicode_4BYTE_KIND:
+ result = ucs4lib_count(
+ ((Py_UCS4*)buf1) + start, end - start,
+ buf2, len2, PY_SSIZE_T_MAX
+ );
+ break;
+ default:
+ assert(0); result = 0;
+ }
Py_DECREF(sub_obj);
Py_DECREF(str_obj);
+ if (kind1 != kind)
+ PyMem_Free(buf1);
+ if (kind2 != kind)
+ PyMem_Free(buf2);
+
return result;
+ onError:
+ Py_DECREF(sub_obj);
+ Py_DECREF(str_obj);
+ if (kind1 != kind && buf1)
+ PyMem_Free(buf1);
+ if (kind2 != kind && buf2)
+ PyMem_Free(buf2);
+ return -1;
}
Py_ssize_t
@@ -6577,25 +7940,23 @@ PyUnicode_Find(PyObject *str,
Py_ssize_t result;
str = PyUnicode_FromObject(str);
- if (!str)
+ if (!str || PyUnicode_READY(str) == -1)
return -2;
sub = PyUnicode_FromObject(sub);
- if (!sub) {
+ if (!sub || PyUnicode_READY(sub) == -1) {
Py_DECREF(str);
return -2;
}
if (direction > 0)
- result = stringlib_find_slice(
- PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str),
- PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub),
- start, end
+ result = any_find_slice(
+ ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice,
+ str, sub, start, end
);
else
- result = stringlib_rfind_slice(
- PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str),
- PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub),
- start, end
+ result = any_find_slice(
+ ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice,
+ str, sub, start, end
);
Py_DECREF(str);
@@ -6604,6 +7965,27 @@ PyUnicode_Find(PyObject *str,
return result;
}
+Py_ssize_t
+PyUnicode_FindChar(PyObject *str, Py_UCS4 ch,
+ Py_ssize_t start, Py_ssize_t end,
+ int direction)
+{
+ char *result;
+ int kind;
+ if (PyUnicode_READY(str) == -1)
+ return -2;
+ if (end > PyUnicode_GET_LENGTH(str))
+ end = PyUnicode_GET_LENGTH(str);
+ kind = PyUnicode_KIND(str);
+ result = findchar(PyUnicode_1BYTE_DATA(str)
+ + PyUnicode_KIND_SIZE(kind, start),
+ kind,
+ end-start, ch, direction);
+ if (!result)
+ return -1;
+ return (result-(char*)PyUnicode_DATA(str)) >> (kind-1);
+}
+
static int
tailmatch(PyUnicodeObject *self,
PyUnicodeObject *substring,
@@ -6611,20 +7993,62 @@ tailmatch(PyUnicodeObject *self,
Py_ssize_t end,
int direction)
{
- if (substring->length == 0)
+ int kind_self;
+ int kind_sub;
+ void *data_self;
+ void *data_sub;
+ Py_ssize_t offset;
+ Py_ssize_t i;
+ Py_ssize_t end_sub;
+
+ if (PyUnicode_READY(self) == -1 ||
+ PyUnicode_READY(substring) == -1)
+ return 0;
+
+ if (PyUnicode_GET_LENGTH(substring) == 0)
return 1;
- ADJUST_INDICES(start, end, self->length);
- end -= substring->length;
+ ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self));
+ end -= PyUnicode_GET_LENGTH(substring);
if (end < start)
return 0;
- if (direction > 0) {
- if (Py_UNICODE_MATCH(self, end, substring))
- return 1;
- } else {
- if (Py_UNICODE_MATCH(self, start, substring))
+ kind_self = PyUnicode_KIND(self);
+ data_self = PyUnicode_DATA(self);
+ kind_sub = PyUnicode_KIND(substring);
+ data_sub = PyUnicode_DATA(substring);
+ end_sub = PyUnicode_GET_LENGTH(substring) - 1;
+
+ if (direction > 0)
+ offset = end;
+ else
+ offset = start;
+
+ if (PyUnicode_READ(kind_self, data_self, offset) ==
+ PyUnicode_READ(kind_sub, data_sub, 0) &&
+ PyUnicode_READ(kind_self, data_self, offset + end_sub) ==
+ PyUnicode_READ(kind_sub, data_sub, end_sub)) {
+ /* If both are of the same kind, memcmp is sufficient */
+ if (kind_self == kind_sub) {
+ return ! memcmp((char *)data_self +
+ (offset * PyUnicode_CHARACTER_SIZE(substring)),
+ data_sub,
+ PyUnicode_GET_LENGTH(substring) *
+ PyUnicode_CHARACTER_SIZE(substring));
+ }
+ /* otherwise we have to compare each character by first accesing it */
+ else {
+ /* We do not need to compare 0 and len(substring)-1 because
+ the if statement above ensured already that they are equal
+ when we end up here. */
+ // TODO: honor direction and do a forward or backwards search
+ for (i = 1; i < end_sub; ++i) {
+ if (PyUnicode_READ(kind_self, data_self, offset + i) !=
+ PyUnicode_READ(kind_sub, data_sub, i))
+ return 0;
+ }
return 1;
+ }
}
return 0;
@@ -6661,18 +8085,39 @@ PyUnicode_Tailmatch(PyObject *str,
static PyObject *
fixup(PyUnicodeObject *self,
- int (*fixfct)(PyUnicodeObject *s))
+ Py_UCS4 (*fixfct)(PyUnicodeObject *s))
{
+ PyObject *u;
+ Py_UCS4 maxchar_old, maxchar_new = 0;
- PyUnicodeObject *u;
-
- u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length);
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ maxchar_old = PyUnicode_MAX_CHAR_VALUE(self);
+ u = PyUnicode_New(PyUnicode_GET_LENGTH(self),
+ maxchar_old);
if (u == NULL)
return NULL;
- Py_UNICODE_COPY(u->str, self->str, self->length);
+ Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self),
+ PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u));
+
+ /* fix functions return the new maximum character in a string,
+ if the kind of the resulting unicode object does not change,
+ everything is fine. Otherwise we need to change the string kind
+ and re-run the fix function. */
+ maxchar_new = fixfct((PyUnicodeObject*)u);
+ if (maxchar_new == 0)
+ /* do nothing, keep maxchar_new at 0 which means no changes. */;
+ else if (maxchar_new <= 127)
+ maxchar_new = 127;
+ else if (maxchar_new <= 255)
+ maxchar_new = 255;
+ else if (maxchar_new <= 65535)
+ maxchar_new = 65535;
+ else
+ maxchar_new = 1114111; /* 0x10ffff */
- if (!fixfct(u) && PyUnicode_CheckExact(self)) {
+ if (!maxchar_new && PyUnicode_CheckExact(self)) {
/* fixfct should return TRUE if it modified the buffer. If
FALSE, return a reference to the original buffer instead
(to save space, not time) */
@@ -6680,123 +8125,207 @@ fixup(PyUnicodeObject *self,
Py_DECREF(u);
return (PyObject*) self;
}
- return (PyObject*) u;
+ else if (maxchar_new == maxchar_old) {
+ return u;
+ }
+ else {
+ /* In case the maximum character changed, we need to
+ convert the string to the new category. */
+ PyObject *v = PyUnicode_New(
+ PyUnicode_GET_LENGTH(self), maxchar_new);
+ if (v == NULL) {
+ Py_DECREF(u);
+ return NULL;
+ }
+ if (maxchar_new > maxchar_old) {
+ /* If the maxchar increased so that the kind changed, not all
+ characters are representable anymore and we need to fix the
+ string again. This only happens in very few cases. */
+ PyUnicode_CopyCharacters(v, 0, (PyObject*)self, 0, PyUnicode_GET_LENGTH(self));
+ maxchar_old = fixfct((PyUnicodeObject*)v);
+ assert(maxchar_old > 0 && maxchar_old <= maxchar_new);
+ }
+ else
+ PyUnicode_CopyCharacters(v, 0, u, 0, PyUnicode_GET_LENGTH(self));
+
+ Py_DECREF(u);
+ return v;
+ }
}
-static int
+static Py_UCS4
fixupper(PyUnicodeObject *self)
{
- Py_ssize_t len = self->length;
- Py_UNICODE *s = self->str;
- int status = 0;
-
- while (len-- > 0) {
- register Py_UNICODE ch;
+ /* No need to call PyUnicode_READY(self) because this function is only
+ called as a callback from fixup() which does it already. */
+ const Py_ssize_t len = PyUnicode_GET_LENGTH(self);
+ const int kind = PyUnicode_KIND(self);
+ void *data = PyUnicode_DATA(self);
+ int touched = 0;
+ Py_UCS4 maxchar = 0;
+ Py_ssize_t i;
- ch = Py_UNICODE_TOUPPER(*s);
- if (ch != *s) {
- status = 1;
- *s = ch;
+ for (i = 0; i < len; ++i) {
+ const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
+ const Py_UCS4 up = Py_UNICODE_TOUPPER(ch);
+ if (up != ch) {
+ if (up > maxchar)
+ maxchar = up;
+ PyUnicode_WRITE(kind, data, i, up);
+ touched = 1;
}
- s++;
+ else if (ch > maxchar)
+ maxchar = ch;
}
- return status;
+ if (touched)
+ return maxchar;
+ else
+ return 0;
}
-static int
+static Py_UCS4
fixlower(PyUnicodeObject *self)
{
- Py_ssize_t len = self->length;
- Py_UNICODE *s = self->str;
- int status = 0;
-
- while (len-- > 0) {
- register Py_UNICODE ch;
+ /* No need to call PyUnicode_READY(self) because fixup() which does it. */
+ const Py_ssize_t len = PyUnicode_GET_LENGTH(self);
+ const int kind = PyUnicode_KIND(self);
+ void *data = PyUnicode_DATA(self);
+ int touched = 0;
+ Py_UCS4 maxchar = 0;
+ Py_ssize_t i;
- ch = Py_UNICODE_TOLOWER(*s);
- if (ch != *s) {
- status = 1;
- *s = ch;
+ for(i = 0; i < len; ++i) {
+ const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
+ const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch);
+ if (lo != ch) {
+ if (lo > maxchar)
+ maxchar = lo;
+ PyUnicode_WRITE(kind, data, i, lo);
+ touched = 1;
}
- s++;
+ else if (ch > maxchar)
+ maxchar = ch;
}
- return status;
+ if (touched)
+ return maxchar;
+ else
+ return 0;
}
-static int
+static Py_UCS4
fixswapcase(PyUnicodeObject *self)
{
- Py_ssize_t len = self->length;
- Py_UNICODE *s = self->str;
- int status = 0;
+ /* No need to call PyUnicode_READY(self) because fixup() which does it. */
+ const Py_ssize_t len = PyUnicode_GET_LENGTH(self);
+ const int kind = PyUnicode_KIND(self);
+ void *data = PyUnicode_DATA(self);
+ int touched = 0;
+ Py_UCS4 maxchar = 0;
+ Py_ssize_t i;
- while (len-- > 0) {
- if (Py_UNICODE_ISUPPER(*s)) {
- *s = Py_UNICODE_TOLOWER(*s);
- status = 1;
- } else if (Py_UNICODE_ISLOWER(*s)) {
- *s = Py_UNICODE_TOUPPER(*s);
- status = 1;
+ for(i = 0; i < len; ++i) {
+ const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
+ Py_UCS4 nu = 0;
+
+ if (Py_UNICODE_ISUPPER(ch))
+ nu = Py_UNICODE_TOLOWER(ch);
+ else if (Py_UNICODE_ISLOWER(ch))
+ nu = Py_UNICODE_TOUPPER(ch);
+
+ if (nu != 0) {
+ if (nu > maxchar)
+ maxchar = nu;
+ PyUnicode_WRITE(kind, data, i, nu);
+ touched = 1;
}
- s++;
+ else if (ch > maxchar)
+ maxchar = ch;
}
- return status;
+ if (touched)
+ return maxchar;
+ else
+ return 0;
}
-static int
+static Py_UCS4
fixcapitalize(PyUnicodeObject *self)
{
- Py_ssize_t len = self->length;
- Py_UNICODE *s = self->str;
- int status = 0;
+ /* No need to call PyUnicode_READY(self) because fixup() which does it. */
+ const Py_ssize_t len = PyUnicode_GET_LENGTH(self);
+ const int kind = PyUnicode_KIND(self);
+ void *data = PyUnicode_DATA(self);
+ int touched = 0;
+ Py_UCS4 maxchar = 0;
+ Py_ssize_t i = 0;
+ Py_UCS4 ch;
if (len == 0)
return 0;
- if (!Py_UNICODE_ISUPPER(*s)) {
- *s = Py_UNICODE_TOUPPER(*s);
- status = 1;
+
+ ch = PyUnicode_READ(kind, data, i);
+ if (!Py_UNICODE_ISUPPER(ch)) {
+ maxchar = Py_UNICODE_TOUPPER(ch);
+ PyUnicode_WRITE(kind, data, i, maxchar);
+ touched = 1;
}
- s++;
- while (--len > 0) {
- if (!Py_UNICODE_ISLOWER(*s)) {
- *s = Py_UNICODE_TOLOWER(*s);
- status = 1;
+ ++i;
+ for(; i < len; ++i) {
+ ch = PyUnicode_READ(kind, data, i);
+ if (!Py_UNICODE_ISLOWER(ch)) {
+ const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch);
+ if (lo > maxchar)
+ maxchar = lo;
+ PyUnicode_WRITE(kind, data, i, lo);
+ touched = 1;
}
- s++;
+ else if (ch > maxchar)
+ maxchar = ch;
}
- return status;
+
+ if (touched)
+ return maxchar;
+ else
+ return 0;
}
-static int
+static Py_UCS4
fixtitle(PyUnicodeObject *self)
{
- register Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
- register Py_UNICODE *e;
+ /* No need to call PyUnicode_READY(self) because fixup() which does it. */
+ const Py_ssize_t len = PyUnicode_GET_LENGTH(self);
+ const int kind = PyUnicode_KIND(self);
+ void *data = PyUnicode_DATA(self);
+ Py_UCS4 maxchar = 0;
+ Py_ssize_t i = 0;
int previous_is_cased;
/* Shortcut for single character strings */
- if (PyUnicode_GET_SIZE(self) == 1) {
- Py_UNICODE ch = Py_UNICODE_TOTITLE(*p);
- if (*p != ch) {
- *p = ch;
- return 1;
+ if (len == 1) {
+ const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
+ const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch);
+ if (ti != ch) {
+ PyUnicode_WRITE(kind, data, i, ti);
+ return ti;
}
else
return 0;
}
-
- e = p + PyUnicode_GET_SIZE(self);
previous_is_cased = 0;
- for (; p < e; p++) {
- register const Py_UNICODE ch = *p;
+ for(; i < len; ++i) {
+ const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
+ Py_UCS4 nu;
if (previous_is_cased)
- *p = Py_UNICODE_TOLOWER(ch);
+ nu = Py_UNICODE_TOLOWER(ch);
else
- *p = Py_UNICODE_TOTITLE(ch);
+ nu = Py_UNICODE_TOTITLE(ch);
+
+ if (nu > maxchar)
+ maxchar = nu;
+ PyUnicode_WRITE(kind, data, i, nu);
if (Py_UNICODE_ISLOWER(ch) ||
Py_UNICODE_ISUPPER(ch) ||
@@ -6805,22 +8334,22 @@ fixtitle(PyUnicodeObject *self)
else
previous_is_cased = 0;
}
- return 1;
+ return maxchar;
}
PyObject *
PyUnicode_Join(PyObject *separator, PyObject *seq)
{
- const Py_UNICODE blank = ' ';
- const Py_UNICODE *sep = &blank;
+ PyObject *sep = NULL;
Py_ssize_t seplen = 1;
- PyUnicodeObject *res = NULL; /* the result */
- Py_UNICODE *res_p; /* pointer to free byte in res's string area */
+ PyObject *res = NULL; /* the result */
PyObject *fseq; /* PySequence_Fast(seq) */
Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */
PyObject **items;
PyObject *item;
- Py_ssize_t sz, i;
+ Py_ssize_t sz, i, res_offset;
+ Py_UCS4 maxchar = 0;
+ Py_UCS4 item_maxchar;
fseq = PySequence_Fast(seq, "");
if (fseq == NULL) {
@@ -6834,7 +8363,7 @@ PyUnicode_Join(PyObject *separator, PyObject *seq)
seqlen = PySequence_Fast_GET_SIZE(fseq);
/* If empty sequence, return u"". */
if (seqlen == 0) {
- res = _PyUnicode_New(0); /* empty sequence; return u"" */
+ res = PyUnicode_New(0, 0);
goto Done;
}
items = PySequence_Fast_ITEMS(fseq);
@@ -6843,15 +8372,17 @@ PyUnicode_Join(PyObject *separator, PyObject *seq)
item = items[0];
if (PyUnicode_CheckExact(item)) {
Py_INCREF(item);
- res = (PyUnicodeObject *)item;
+ res = item;
goto Done;
}
}
else {
/* Set up sep and seplen */
if (separator == NULL) {
- sep = &blank;
- seplen = 1;
+ /* fall back to a blank space separator */
+ sep = PyUnicode_FromOrdinal(' ');
+ if (!sep || PyUnicode_READY(sep) == -1)
+ goto onError;
}
else {
if (!PyUnicode_Check(separator)) {
@@ -6861,8 +8392,14 @@ PyUnicode_Join(PyObject *separator, PyObject *seq)
Py_TYPE(separator)->tp_name);
goto onError;
}
- sep = PyUnicode_AS_UNICODE(separator);
- seplen = PyUnicode_GET_SIZE(separator);
+ if (PyUnicode_READY(separator) == -1)
+ goto onError;
+ sep = separator;
+ seplen = PyUnicode_GET_LENGTH(separator);
+ maxchar = PyUnicode_MAX_CHAR_VALUE(separator);
+ /* inc refcount to keep this code path symetric with the
+ above case of a blank separator */
+ Py_INCREF(sep);
}
}
@@ -6882,7 +8419,12 @@ PyUnicode_Join(PyObject *separator, PyObject *seq)
i, Py_TYPE(item)->tp_name);
goto onError;
}
- sz += PyUnicode_GET_SIZE(item);
+ if (PyUnicode_READY(item) == -1)
+ goto onError;
+ sz += PyUnicode_GET_LENGTH(item);
+ item_maxchar = PyUnicode_MAX_CHAR_VALUE(item);
+ if (item_maxchar > maxchar)
+ maxchar = item_maxchar;
if (i != 0)
sz += seplen;
if (sz < old_sz || sz > PY_SSIZE_T_MAX) {
@@ -6892,42 +8434,70 @@ PyUnicode_Join(PyObject *separator, PyObject *seq)
}
}
- res = _PyUnicode_New(sz);
+ res = PyUnicode_New(sz, maxchar);
if (res == NULL)
goto onError;
/* Catenate everything. */
- res_p = PyUnicode_AS_UNICODE(res);
- for (i = 0; i < seqlen; ++i) {
+ for (i = 0, res_offset = 0; i < seqlen; ++i) {
Py_ssize_t itemlen;
item = items[i];
- itemlen = PyUnicode_GET_SIZE(item);
+ itemlen = PyUnicode_GET_LENGTH(item);
/* Copy item, and maybe the separator. */
if (i) {
- Py_UNICODE_COPY(res_p, sep, seplen);
- res_p += seplen;
+ PyUnicode_CopyCharacters(res, res_offset,
+ sep, 0, seplen);
+ res_offset += seplen;
}
- Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen);
- res_p += itemlen;
+ PyUnicode_CopyCharacters(res, res_offset,
+ item, 0, itemlen);
+ res_offset += itemlen;
}
+ assert(res_offset == PyUnicode_GET_LENGTH(res));
Done:
Py_DECREF(fseq);
- return (PyObject *)res;
+ Py_XDECREF(sep);
+ return res;
onError:
Py_DECREF(fseq);
+ Py_XDECREF(sep);
Py_XDECREF(res);
return NULL;
}
+#define FILL(kind, data, value, start, length) \
+ do { \
+ Py_ssize_t i_ = 0; \
+ assert(kind != PyUnicode_WCHAR_KIND); \
+ switch ((kind)) { \
+ case PyUnicode_1BYTE_KIND: { \
+ unsigned char * to_ = (unsigned char *)((data)) + (start); \
+ memset(to_, (unsigned char)value, length); \
+ break; \
+ } \
+ case PyUnicode_2BYTE_KIND: { \
+ Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \
+ for (; i_ < (length); ++i_, ++to_) *to_ = (value); \
+ break; \
+ } \
+ default: { \
+ Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \
+ for (; i_ < (length); ++i_, ++to_) *to_ = (value); \
+ break; \
+ } \
+ } \
+ } while (0)
+
static PyUnicodeObject *
pad(PyUnicodeObject *self,
Py_ssize_t left,
Py_ssize_t right,
- Py_UNICODE fill)
+ Py_UCS4 fill)
{
- PyUnicodeObject *u;
+ PyObject *u;
+ Py_UCS4 maxchar;
if (left < 0)
left = 0;
@@ -6939,22 +8509,28 @@ pad(PyUnicodeObject *self,
return self;
}
- if (left > PY_SSIZE_T_MAX - self->length ||
- right > PY_SSIZE_T_MAX - (left + self->length)) {
+ if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) ||
+ right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) {
PyErr_SetString(PyExc_OverflowError, "padded string is too long");
return NULL;
}
- u = _PyUnicode_New(left + self->length + right);
+ maxchar = PyUnicode_MAX_CHAR_VALUE(self);
+ if (fill > maxchar)
+ maxchar = fill;
+ u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar);
if (u) {
+ int kind = PyUnicode_KIND(u);
+ void *data = PyUnicode_DATA(u);
if (left)
- Py_UNICODE_FILL(u->str, fill, left);
- Py_UNICODE_COPY(u->str + left, self->str, self->length);
+ FILL(kind, data, fill, 0, left);
if (right)
- Py_UNICODE_FILL(u->str + left + self->length, fill, right);
+ FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right);
+ PyUnicode_CopyCharacters(u, left, (PyObject*)self, 0, _PyUnicode_LENGTH(self));
}
- return u;
+ return (PyUnicodeObject*)u;
}
+#undef FILL
PyObject *
PyUnicode_Splitlines(PyObject *string, int keepends)
@@ -6962,13 +8538,29 @@ PyUnicode_Splitlines(PyObject *string, int keepends)
PyObject *list;
string = PyUnicode_FromObject(string);
- if (string == NULL)
+ if (string == NULL || PyUnicode_READY(string) == -1)
return NULL;
- list = stringlib_splitlines(
- (PyObject*) string, PyUnicode_AS_UNICODE(string),
- PyUnicode_GET_SIZE(string), keepends);
-
+ switch(PyUnicode_KIND(string)) {
+ case PyUnicode_1BYTE_KIND:
+ list = ucs1lib_splitlines(
+ (PyObject*) string, PyUnicode_1BYTE_DATA(string),
+ PyUnicode_GET_LENGTH(string), keepends);
+ break;
+ case PyUnicode_2BYTE_KIND:
+ list = ucs2lib_splitlines(
+ (PyObject*) string, PyUnicode_2BYTE_DATA(string),
+ PyUnicode_GET_LENGTH(string), keepends);
+ break;
+ case PyUnicode_4BYTE_KIND:
+ list = ucs4lib_splitlines(
+ (PyObject*) string, PyUnicode_4BYTE_DATA(string),
+ PyUnicode_GET_LENGTH(string), keepends);
+ break;
+ default:
+ assert(0);
+ list = 0;
+ }
Py_DECREF(string);
return list;
}
@@ -6978,19 +8570,81 @@ split(PyUnicodeObject *self,
PyUnicodeObject *substring,
Py_ssize_t maxcount)
{
+ int kind1, kind2, kind;
+ void *buf1, *buf2;
+ Py_ssize_t len1, len2;
+ PyObject* out;
+
if (maxcount < 0)
maxcount = PY_SSIZE_T_MAX;
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+
if (substring == NULL)
- return stringlib_split_whitespace(
- (PyObject*) self, self->str, self->length, maxcount
- );
+ switch(PyUnicode_KIND(self)) {
+ case PyUnicode_1BYTE_KIND:
+ return ucs1lib_split_whitespace(
+ (PyObject*) self, PyUnicode_1BYTE_DATA(self),
+ PyUnicode_GET_LENGTH(self), maxcount
+ );
+ case PyUnicode_2BYTE_KIND:
+ return ucs2lib_split_whitespace(
+ (PyObject*) self, PyUnicode_2BYTE_DATA(self),
+ PyUnicode_GET_LENGTH(self), maxcount
+ );
+ case PyUnicode_4BYTE_KIND:
+ return ucs4lib_split_whitespace(
+ (PyObject*) self, PyUnicode_4BYTE_DATA(self),
+ PyUnicode_GET_LENGTH(self), maxcount
+ );
+ default:
+ assert(0);
+ return NULL;
+ }
- return stringlib_split(
- (PyObject*) self, self->str, self->length,
- substring->str, substring->length,
- maxcount
- );
+ if (PyUnicode_READY(substring) == -1)
+ return NULL;
+
+ kind1 = PyUnicode_KIND(self);
+ kind2 = PyUnicode_KIND(substring);
+ kind = kind1 > kind2 ? kind1 : kind2;
+ buf1 = PyUnicode_DATA(self);
+ buf2 = PyUnicode_DATA(substring);
+ if (kind1 != kind)
+ buf1 = _PyUnicode_AsKind((PyObject*)self, kind);
+ if (!buf1)
+ return NULL;
+ if (kind2 != kind)
+ buf2 = _PyUnicode_AsKind((PyObject*)substring, kind);
+ if (!buf2) {
+ if (kind1 != kind) PyMem_Free(buf1);
+ return NULL;
+ }
+ len1 = PyUnicode_GET_LENGTH(self);
+ len2 = PyUnicode_GET_LENGTH(substring);
+
+ switch(kind) {
+ case PyUnicode_1BYTE_KIND:
+ out = ucs1lib_split(
+ (PyObject*) self, buf1, len1, buf2, len2, maxcount);
+ break;
+ case PyUnicode_2BYTE_KIND:
+ out = ucs2lib_split(
+ (PyObject*) self, buf1, len1, buf2, len2, maxcount);
+ break;
+ case PyUnicode_4BYTE_KIND:
+ out = ucs4lib_split(
+ (PyObject*) self, buf1, len1, buf2, len2, maxcount);
+ break;
+ default:
+ out = NULL;
+ }
+ if (kind1 != kind)
+ PyMem_Free(buf1);
+ if (kind2 != kind)
+ PyMem_Free(buf2);
+ return out;
}
static PyObject *
@@ -6998,159 +8652,366 @@ rsplit(PyUnicodeObject *self,
PyUnicodeObject *substring,
Py_ssize_t maxcount)
{
+ int kind1, kind2, kind;
+ void *buf1, *buf2;
+ Py_ssize_t len1, len2;
+ PyObject* out;
+
if (maxcount < 0)
maxcount = PY_SSIZE_T_MAX;
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+
if (substring == NULL)
- return stringlib_rsplit_whitespace(
- (PyObject*) self, self->str, self->length, maxcount
- );
+ switch(PyUnicode_KIND(self)) {
+ case PyUnicode_1BYTE_KIND:
+ return ucs1lib_rsplit_whitespace(
+ (PyObject*) self, PyUnicode_1BYTE_DATA(self),
+ PyUnicode_GET_LENGTH(self), maxcount
+ );
+ case PyUnicode_2BYTE_KIND:
+ return ucs2lib_rsplit_whitespace(
+ (PyObject*) self, PyUnicode_2BYTE_DATA(self),
+ PyUnicode_GET_LENGTH(self), maxcount
+ );
+ case PyUnicode_4BYTE_KIND:
+ return ucs4lib_rsplit_whitespace(
+ (PyObject*) self, PyUnicode_4BYTE_DATA(self),
+ PyUnicode_GET_LENGTH(self), maxcount
+ );
+ default:
+ assert(0);
+ return NULL;
+ }
- return stringlib_rsplit(
- (PyObject*) self, self->str, self->length,
- substring->str, substring->length,
- maxcount
- );
+ if (PyUnicode_READY(substring) == -1)
+ return NULL;
+
+ kind1 = PyUnicode_KIND(self);
+ kind2 = PyUnicode_KIND(substring);
+ kind = kind1 > kind2 ? kind1 : kind2;
+ buf1 = PyUnicode_DATA(self);
+ buf2 = PyUnicode_DATA(substring);
+ if (kind1 != kind)
+ buf1 = _PyUnicode_AsKind((PyObject*)self, kind);
+ if (!buf1)
+ return NULL;
+ if (kind2 != kind)
+ buf2 = _PyUnicode_AsKind((PyObject*)substring, kind);
+ if (!buf2) {
+ if (kind1 != kind) PyMem_Free(buf1);
+ return NULL;
+ }
+ len1 = PyUnicode_GET_LENGTH(self);
+ len2 = PyUnicode_GET_LENGTH(substring);
+
+ switch(kind) {
+ case PyUnicode_1BYTE_KIND:
+ out = ucs1lib_rsplit(
+ (PyObject*) self, buf1, len1, buf2, len2, maxcount);
+ break;
+ case PyUnicode_2BYTE_KIND:
+ out = ucs2lib_rsplit(
+ (PyObject*) self, buf1, len1, buf2, len2, maxcount);
+ break;
+ case PyUnicode_4BYTE_KIND:
+ out = ucs4lib_rsplit(
+ (PyObject*) self, buf1, len1, buf2, len2, maxcount);
+ break;
+ default:
+ out = NULL;
+ }
+ if (kind1 != kind)
+ PyMem_Free(buf1);
+ if (kind2 != kind)
+ PyMem_Free(buf2);
+ return out;
}
-static PyObject *
-replace(PyUnicodeObject *self,
- PyUnicodeObject *str1,
- PyUnicodeObject *str2,
- Py_ssize_t maxcount)
+static Py_ssize_t
+anylib_find(int kind, void *buf1, Py_ssize_t len1,
+ void *buf2, Py_ssize_t len2, Py_ssize_t offset)
+{
+ switch(kind) {
+ case PyUnicode_1BYTE_KIND:
+ return ucs1lib_find(buf1, len1, buf2, len2, offset);
+ case PyUnicode_2BYTE_KIND:
+ return ucs2lib_find(buf1, len1, buf2, len2, offset);
+ case PyUnicode_4BYTE_KIND:
+ return ucs4lib_find(buf1, len1, buf2, len2, offset);
+ }
+ assert(0);
+ return -1;
+}
+
+static Py_ssize_t
+anylib_count(int kind, void* sbuf, Py_ssize_t slen,
+ void *buf1, Py_ssize_t len1, Py_ssize_t maxcount)
{
- PyUnicodeObject *u;
+ switch(kind) {
+ case PyUnicode_1BYTE_KIND:
+ return ucs1lib_count(sbuf, slen, buf1, len1, maxcount);
+ case PyUnicode_2BYTE_KIND:
+ return ucs2lib_count(sbuf, slen, buf1, len1, maxcount);
+ case PyUnicode_4BYTE_KIND:
+ return ucs4lib_count(sbuf, slen, buf1, len1, maxcount);
+ }
+ assert(0);
+ return 0;
+}
+
+static PyObject *
+replace(PyObject *self, PyObject *str1,
+ PyObject *str2, Py_ssize_t maxcount)
+{
+ PyObject *u;
+ char *sbuf = PyUnicode_DATA(self);
+ char *buf1 = PyUnicode_DATA(str1);
+ char *buf2 = PyUnicode_DATA(str2);
+ int srelease = 0, release1 = 0, release2 = 0;
+ int skind = PyUnicode_KIND(self);
+ int kind1 = PyUnicode_KIND(str1);
+ int kind2 = PyUnicode_KIND(str2);
+ Py_ssize_t slen = PyUnicode_GET_LENGTH(self);
+ Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1);
+ Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2);
if (maxcount < 0)
maxcount = PY_SSIZE_T_MAX;
- else if (maxcount == 0 || self->length == 0)
+ else if (maxcount == 0 || slen == 0)
+ goto nothing;
+
+ if (skind < kind1)
+ /* substring too wide to be present */
goto nothing;
- if (str1->length == str2->length) {
+ if (len1 == len2) {
Py_ssize_t i;
/* same length */
- if (str1->length == 0)
+ if (len1 == 0)
goto nothing;
- if (str1->length == 1) {
+ if (len1 == 1) {
/* replace characters */
- Py_UNICODE u1, u2;
- if (!findchar(self->str, self->length, str1->str[0]))
+ Py_UCS4 u1, u2, maxchar;
+ int mayshrink, rkind;
+ u1 = PyUnicode_READ_CHAR(str1, 0);
+ if (!findchar(sbuf, PyUnicode_KIND(self),
+ slen, u1, 1))
goto nothing;
- u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length);
+ u2 = PyUnicode_READ_CHAR(str2, 0);
+ maxchar = PyUnicode_MAX_CHAR_VALUE(self);
+ /* Replacing u1 with u2 may cause a maxchar reduction in the
+ result string. */
+ mayshrink = maxchar > 127;
+ if (u2 > maxchar) {
+ maxchar = u2;
+ mayshrink = 0;
+ }
+ u = PyUnicode_New(slen, maxchar);
if (!u)
- return NULL;
- Py_UNICODE_COPY(u->str, self->str, self->length);
- u1 = str1->str[0];
- u2 = str2->str[0];
- for (i = 0; i < u->length; i++)
- if (u->str[i] == u1) {
+ goto error;
+ PyUnicode_CopyCharacters(u, 0,
+ (PyObject*)self, 0, slen);
+ rkind = PyUnicode_KIND(u);
+ for (i = 0; i < PyUnicode_GET_LENGTH(u); i++)
+ if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) {
if (--maxcount < 0)
break;
- u->str[i] = u2;
+ PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2);
}
+ if (mayshrink) {
+ PyObject *tmp = u;
+ u = PyUnicode_FromKindAndData(rkind, PyUnicode_DATA(tmp),
+ PyUnicode_GET_LENGTH(tmp));
+ Py_DECREF(tmp);
+ }
} else {
- i = stringlib_find(
- self->str, self->length, str1->str, str1->length, 0
- );
+ int rkind = skind;
+ char *res;
+ if (kind1 < rkind) {
+ /* widen substring */
+ buf1 = _PyUnicode_AsKind(str1, rkind);
+ if (!buf1) goto error;
+ release1 = 1;
+ }
+ i = anylib_find(rkind, sbuf, slen, buf1, len1, 0);
if (i < 0)
goto nothing;
- u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length);
- if (!u)
- return NULL;
- Py_UNICODE_COPY(u->str, self->str, self->length);
-
+ if (rkind > kind2) {
+ /* widen replacement */
+ buf2 = _PyUnicode_AsKind(str2, rkind);
+ if (!buf2) goto error;
+ release2 = 1;
+ }
+ else if (rkind < kind2) {
+ /* widen self and buf1 */
+ rkind = kind2;
+ if (release1) PyMem_Free(buf1);
+ sbuf = _PyUnicode_AsKind(self, rkind);
+ if (!sbuf) goto error;
+ srelease = 1;
+ buf1 = _PyUnicode_AsKind(str1, rkind);
+ if (!buf1) goto error;
+ release1 = 1;
+ }
+ res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, slen));
+ if (!res) {
+ PyErr_NoMemory();
+ goto error;
+ }
+ memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen));
/* change everything in-place, starting with this one */
- Py_UNICODE_COPY(u->str+i, str2->str, str2->length);
- i += str1->length;
+ memcpy(res + PyUnicode_KIND_SIZE(rkind, i),
+ buf2,
+ PyUnicode_KIND_SIZE(rkind, len2));
+ i += len1;
while ( --maxcount > 0) {
- i = stringlib_find(self->str+i, self->length-i,
- str1->str, str1->length,
- i);
+ i = anylib_find(rkind, sbuf+PyUnicode_KIND_SIZE(rkind, i),
+ slen-i,
+ buf1, len1, i);
if (i == -1)
break;
- Py_UNICODE_COPY(u->str+i, str2->str, str2->length);
- i += str1->length;
+ memcpy(res + PyUnicode_KIND_SIZE(rkind, i),
+ buf2,
+ PyUnicode_KIND_SIZE(rkind, len2));
+ i += len1;
}
+
+ u = PyUnicode_FromKindAndData(rkind, res, slen);
+ PyMem_Free(res);
+ if (!u) goto error;
}
} else {
- Py_ssize_t n, i, j;
- Py_ssize_t product, new_size, delta;
- Py_UNICODE *p;
+ Py_ssize_t n, i, j, ires;
+ Py_ssize_t product, new_size;
+ int rkind = skind;
+ char *res;
- /* replace strings */
- n = stringlib_count(self->str, self->length, str1->str, str1->length,
- maxcount);
+ if (kind1 < rkind) {
+ buf1 = _PyUnicode_AsKind(str1, rkind);
+ if (!buf1) goto error;
+ release1 = 1;
+ }
+ n = anylib_count(rkind, sbuf, slen, buf1, len1, maxcount);
if (n == 0)
goto nothing;
- /* new_size = self->length + n * (str2->length - str1->length)); */
- delta = (str2->length - str1->length);
- if (delta == 0) {
- new_size = self->length;
- } else {
- product = n * (str2->length - str1->length);
- if ((product / (str2->length - str1->length)) != n) {
+ if (kind2 < rkind) {
+ buf2 = _PyUnicode_AsKind(str2, rkind);
+ if (!buf2) goto error;
+ release2 = 1;
+ }
+ else if (kind2 > rkind) {
+ rkind = kind2;
+ sbuf = _PyUnicode_AsKind(self, rkind);
+ if (!sbuf) goto error;
+ srelease = 1;
+ if (release1) PyMem_Free(buf1);
+ buf1 = _PyUnicode_AsKind(str1, rkind);
+ if (!buf1) goto error;
+ release1 = 1;
+ }
+ /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) -
+ PyUnicode_GET_LENGTH(str1))); */
+ product = n * (len2-len1);
+ if ((product / (len2-len1)) != n) {
PyErr_SetString(PyExc_OverflowError,
"replace string is too long");
- return NULL;
- }
- new_size = self->length + product;
- if (new_size < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "replace string is too long");
- return NULL;
- }
+ goto error;
}
- u = _PyUnicode_New(new_size);
- if (!u)
- return NULL;
- i = 0;
- p = u->str;
- if (str1->length > 0) {
+ new_size = slen + product;
+ if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) {
+ PyErr_SetString(PyExc_OverflowError,
+ "replace string is too long");
+ goto error;
+ }
+ res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, new_size));
+ if (!res)
+ goto error;
+ ires = i = 0;
+ if (len1 > 0) {
while (n-- > 0) {
/* look for next match */
- j = stringlib_find(self->str+i, self->length-i,
- str1->str, str1->length,
- i);
+ j = anylib_find(rkind,
+ sbuf + PyUnicode_KIND_SIZE(rkind, i),
+ slen-i, buf1, len1, i);
if (j == -1)
break;
else if (j > i) {
/* copy unchanged part [i:j] */
- Py_UNICODE_COPY(p, self->str+i, j-i);
- p += j - i;
+ memcpy(res + PyUnicode_KIND_SIZE(rkind, ires),
+ sbuf + PyUnicode_KIND_SIZE(rkind, i),
+ PyUnicode_KIND_SIZE(rkind, j-i));
+ ires += j - i;
}
/* copy substitution string */
- if (str2->length > 0) {
- Py_UNICODE_COPY(p, str2->str, str2->length);
- p += str2->length;
+ if (len2 > 0) {
+ memcpy(res + PyUnicode_KIND_SIZE(rkind, ires),
+ buf2,
+ PyUnicode_KIND_SIZE(rkind, len2));
+ ires += len2;
}
- i = j + str1->length;
+ i = j + len1;
}
- if (i < self->length)
+ if (i < slen)
/* copy tail [i:] */
- Py_UNICODE_COPY(p, self->str+i, self->length-i);
+ memcpy(res + PyUnicode_KIND_SIZE(rkind, ires),
+ sbuf + PyUnicode_KIND_SIZE(rkind, i),
+ PyUnicode_KIND_SIZE(rkind, slen-i));
} else {
/* interleave */
while (n > 0) {
- Py_UNICODE_COPY(p, str2->str, str2->length);
- p += str2->length;
+ memcpy(res + PyUnicode_KIND_SIZE(rkind, ires),
+ buf2,
+ PyUnicode_KIND_SIZE(rkind, len2));
+ ires += len2;
if (--n <= 0)
break;
- *p++ = self->str[i++];
+ memcpy(res + PyUnicode_KIND_SIZE(rkind, ires),
+ sbuf + PyUnicode_KIND_SIZE(rkind, i),
+ PyUnicode_KIND_SIZE(rkind, 1));
+ ires++;
+ i++;
}
- Py_UNICODE_COPY(p, self->str+i, self->length-i);
- }
- }
- return (PyObject *) u;
+ memcpy(res + PyUnicode_KIND_SIZE(rkind, ires),
+ sbuf + PyUnicode_KIND_SIZE(rkind, i),
+ PyUnicode_KIND_SIZE(rkind, slen-i));
+ }
+ u = PyUnicode_FromKindAndData(rkind, res, new_size);
+ }
+ if (srelease)
+ PyMem_FREE(sbuf);
+ if (release1)
+ PyMem_FREE(buf1);
+ if (release2)
+ PyMem_FREE(buf2);
+ return u;
nothing:
/* nothing to replace; return original string (when possible) */
+ if (srelease)
+ PyMem_FREE(sbuf);
+ if (release1)
+ PyMem_FREE(buf1);
+ if (release2)
+ PyMem_FREE(buf2);
if (PyUnicode_CheckExact(self)) {
Py_INCREF(self);
return (PyObject *) self;
}
- return PyUnicode_FromUnicode(self->str, self->length);
+ return PyUnicode_FromKindAndData(PyUnicode_KIND(self),
+ PyUnicode_DATA(self),
+ PyUnicode_GET_LENGTH(self));
+ error:
+ if (srelease && sbuf)
+ PyMem_FREE(sbuf);
+ if (release1 && buf1)
+ PyMem_FREE(buf1);
+ if (release2 && buf2)
+ PyMem_FREE(buf2);
+ return NULL;
}
/* --- Unicode Object Methods --------------------------------------------- */
@@ -7222,9 +9083,8 @@ unicode_capwords(PyUnicodeObject *self)
static int
convert_uc(PyObject *obj, void *addr)
{
- Py_UNICODE *fillcharloc = (Py_UNICODE *)addr;
+ Py_UCS4 *fillcharloc = (Py_UCS4 *)addr;
PyObject *uniobj;
- Py_UNICODE *unistr;
uniobj = PyUnicode_FromObject(obj);
if (uniobj == NULL) {
@@ -7232,14 +9092,17 @@ convert_uc(PyObject *obj, void *addr)
"The fill character cannot be converted to Unicode");
return 0;
}
- if (PyUnicode_GET_SIZE(uniobj) != 1) {
+ if (PyUnicode_GET_LENGTH(uniobj) != 1) {
PyErr_SetString(PyExc_TypeError,
"The fill character must be exactly one character long");
Py_DECREF(uniobj);
return 0;
}
- unistr = PyUnicode_AS_UNICODE(uniobj);
- *fillcharloc = unistr[0];
+ if (PyUnicode_READY(uniobj)) {
+ Py_DECREF(uniobj);
+ return 0;
+ }
+ *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0);
Py_DECREF(uniobj);
return 1;
}
@@ -7255,17 +9118,20 @@ unicode_center(PyUnicodeObject *self, PyObject *args)
{
Py_ssize_t marg, left;
Py_ssize_t width;
- Py_UNICODE fillchar = ' ';
+ Py_UCS4 fillchar = ' ';
+
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar))
return NULL;
- if (self->length >= width && PyUnicode_CheckExact(self)) {
+ if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) {
Py_INCREF(self);
return (PyObject*) self;
}
- marg = width - self->length;
+ marg = width - _PyUnicode_LENGTH(self);
left = marg / 2 + (marg & width & 1);
return (PyObject*) pad(self, left, marg - left, fillchar);
@@ -7297,8 +9163,8 @@ unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2)
Py_UNICODE *s1 = str1->str;
Py_UNICODE *s2 = str2->str;
- len1 = str1->length;
- len2 = str2->length;
+ len1 = str1->_base._base.length;
+ len2 = str2->_base._base.length;
while (len1 > 0 && len2 > 0) {
Py_UNICODE c1, c2;
@@ -7323,27 +9189,29 @@ unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2)
#else
+/* This function assumes that str1 and str2 are readied by the caller. */
+
static int
unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2)
{
- register Py_ssize_t len1, len2;
+ int kind1, kind2;
+ void *data1, *data2;
+ Py_ssize_t len1, len2, i;
- Py_UNICODE *s1 = str1->str;
- Py_UNICODE *s2 = str2->str;
-
- len1 = str1->length;
- len2 = str2->length;
-
- while (len1 > 0 && len2 > 0) {
- Py_UNICODE c1, c2;
+ kind1 = PyUnicode_KIND(str1);
+ kind2 = PyUnicode_KIND(str2);
+ data1 = PyUnicode_DATA(str1);
+ data2 = PyUnicode_DATA(str2);
+ len1 = PyUnicode_GET_LENGTH(str1);
+ len2 = PyUnicode_GET_LENGTH(str2);
- c1 = *s1++;
- c2 = *s2++;
+ for (i = 0; i < len1 && i < len2; ++i) {
+ Py_UCS4 c1, c2;
+ c1 = PyUnicode_READ(kind1, data1, i);
+ c2 = PyUnicode_READ(kind2, data2, i);
if (c1 != c2)
return (c1 < c2) ? -1 : 1;
-
- len1--; len2--;
}
return (len1 < len2) ? -1 : (len1 != len2);
@@ -7354,9 +9222,13 @@ unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2)
int
PyUnicode_Compare(PyObject *left, PyObject *right)
{
- if (PyUnicode_Check(left) && PyUnicode_Check(right))
+ if (PyUnicode_Check(left) && PyUnicode_Check(right)) {
+ if (PyUnicode_READY(left) == -1 ||
+ PyUnicode_READY(right) == -1)
+ return -1;
return unicode_compare((PyUnicodeObject *)left,
(PyUnicodeObject *)right);
+ }
PyErr_Format(PyExc_TypeError,
"Can't compare %.100s and %.100s",
left->ob_type->tp_name,
@@ -7367,17 +9239,23 @@ PyUnicode_Compare(PyObject *left, PyObject *right)
int
PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str)
{
- int i;
- Py_UNICODE *id;
+ Py_ssize_t i;
+ int kind;
+ void *data;
+ Py_UCS4 chr;
+
assert(PyUnicode_Check(uni));
- id = PyUnicode_AS_UNICODE(uni);
+ if (PyUnicode_READY(uni) == -1)
+ return -1;
+ kind = PyUnicode_KIND(uni);
+ data = PyUnicode_DATA(uni);
/* Compare Unicode string and source character set string */
- for (i = 0; id[i] && str[i]; i++)
- if (id[i] != str[i])
- return ((int)id[i] < (int)str[i]) ? -1 : 1;
+ for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++)
+ if (chr != str[i])
+ return (chr < (unsigned char)(str[i])) ? -1 : 1;
/* This check keeps Python strings that end in '\0' from comparing equal
to C strings identical up to that point. */
- if (PyUnicode_GET_SIZE(uni) != i || id[i])
+ if (PyUnicode_GET_LENGTH(uni) != i || chr)
return 1; /* uni is longer */
if (str[i])
return -1; /* str is longer */
@@ -7395,7 +9273,11 @@ PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
if (PyUnicode_Check(left) && PyUnicode_Check(right)) {
PyObject *v;
- if (PyUnicode_GET_SIZE(left) != PyUnicode_GET_SIZE(right)) {
+ if (PyUnicode_READY(left) == -1 ||
+ PyUnicode_READY(right) == -1)
+ return NULL;
+ if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) ||
+ PyUnicode_KIND(left) != PyUnicode_KIND(right)) {
if (op == Py_EQ) {
Py_INCREF(Py_False);
return Py_False;
@@ -7446,6 +9328,9 @@ int
PyUnicode_Contains(PyObject *container, PyObject *element)
{
PyObject *str, *sub;
+ int kind1, kind2, kind;
+ void *buf1, *buf2;
+ Py_ssize_t len1, len2;
int result;
/* Coerce the two arguments */
@@ -7456,18 +9341,59 @@ PyUnicode_Contains(PyObject *container, PyObject *element)
element->ob_type->tp_name);
return -1;
}
+ if (PyUnicode_READY(sub) == -1)
+ return -1;
str = PyUnicode_FromObject(container);
- if (!str) {
+ if (!str || PyUnicode_READY(container) == -1) {
Py_DECREF(sub);
return -1;
}
- result = stringlib_contains_obj(str, sub);
+ kind1 = PyUnicode_KIND(str);
+ kind2 = PyUnicode_KIND(sub);
+ kind = kind1 > kind2 ? kind1 : kind2;
+ buf1 = PyUnicode_DATA(str);
+ buf2 = PyUnicode_DATA(sub);
+ if (kind1 != kind)
+ buf1 = _PyUnicode_AsKind((PyObject*)str, kind);
+ if (!buf1) {
+ Py_DECREF(sub);
+ return -1;
+ }
+ if (kind2 != kind)
+ buf2 = _PyUnicode_AsKind((PyObject*)sub, kind);
+ if (!buf2) {
+ Py_DECREF(sub);
+ if (kind1 != kind) PyMem_Free(buf1);
+ return -1;
+ }
+ len1 = PyUnicode_GET_LENGTH(str);
+ len2 = PyUnicode_GET_LENGTH(sub);
+
+ switch(kind) {
+ case PyUnicode_1BYTE_KIND:
+ result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1;
+ break;
+ case PyUnicode_2BYTE_KIND:
+ result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1;
+ break;
+ case PyUnicode_4BYTE_KIND:
+ result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1;
+ break;
+ default:
+ result = -1;
+ assert(0);
+ }
Py_DECREF(str);
Py_DECREF(sub);
+ if (kind1 != kind)
+ PyMem_Free(buf1);
+ if (kind2 != kind)
+ PyMem_Free(buf2);
+
return result;
}
@@ -7476,36 +9402,46 @@ PyUnicode_Contains(PyObject *container, PyObject *element)
PyObject *
PyUnicode_Concat(PyObject *left, PyObject *right)
{
- PyUnicodeObject *u = NULL, *v = NULL, *w;
+ PyObject *u = NULL, *v = NULL, *w;
+ Py_UCS4 maxchar;
/* Coerce the two arguments */
- u = (PyUnicodeObject *)PyUnicode_FromObject(left);
+ u = PyUnicode_FromObject(left);
if (u == NULL)
goto onError;
- v = (PyUnicodeObject *)PyUnicode_FromObject(right);
+ v = PyUnicode_FromObject(right);
if (v == NULL)
goto onError;
/* Shortcuts */
- if (v == unicode_empty) {
+ if (v == (PyObject*)unicode_empty) {
Py_DECREF(v);
- return (PyObject *)u;
+ return u;
}
- if (u == unicode_empty) {
+ if (u == (PyObject*)unicode_empty) {
Py_DECREF(u);
- return (PyObject *)v;
+ return v;
}
+ if (PyUnicode_READY(u) == -1 || PyUnicode_READY(v) == -1)
+ goto onError;
+
+ maxchar = PyUnicode_MAX_CHAR_VALUE(u);
+ if (PyUnicode_MAX_CHAR_VALUE(v) > maxchar)
+ maxchar = PyUnicode_MAX_CHAR_VALUE(v);
+
/* Concat the two Unicode strings */
- w = _PyUnicode_New(u->length + v->length);
+ w = PyUnicode_New(
+ PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v),
+ maxchar);
if (w == NULL)
goto onError;
- Py_UNICODE_COPY(w->str, u->str, u->length);
- Py_UNICODE_COPY(w->str + u->length, v->str, v->length);
-
+ PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u));
+ PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), v, 0,
+ PyUnicode_GET_LENGTH(v));
Py_DECREF(u);
Py_DECREF(v);
- return (PyObject *)w;
+ return w;
onError:
Py_XDECREF(u);
@@ -7550,17 +9486,65 @@ unicode_count(PyUnicodeObject *self, PyObject *args)
Py_ssize_t start = 0;
Py_ssize_t end = PY_SSIZE_T_MAX;
PyObject *result;
+ int kind1, kind2, kind;
+ void *buf1, *buf2;
+ Py_ssize_t len1, len2, iresult;
if (!stringlib_parse_args_finds_unicode("count", args, &substring,
&start, &end))
return NULL;
- ADJUST_INDICES(start, end, self->length);
- result = PyLong_FromSsize_t(
- stringlib_count(self->str + start, end - start,
- substring->str, substring->length,
- PY_SSIZE_T_MAX)
- );
+ kind1 = PyUnicode_KIND(self);
+ kind2 = PyUnicode_KIND(substring);
+ kind = kind1 > kind2 ? kind1 : kind2;
+ buf1 = PyUnicode_DATA(self);
+ buf2 = PyUnicode_DATA(substring);
+ if (kind1 != kind)
+ buf1 = _PyUnicode_AsKind((PyObject*)self, kind);
+ if (!buf1) {
+ Py_DECREF(substring);
+ return NULL;
+ }
+ if (kind2 != kind)
+ buf2 = _PyUnicode_AsKind((PyObject*)substring, kind);
+ if (!buf2) {
+ Py_DECREF(substring);
+ if (kind1 != kind) PyMem_Free(buf1);
+ return NULL;
+ }
+ len1 = PyUnicode_GET_LENGTH(self);
+ len2 = PyUnicode_GET_LENGTH(substring);
+
+ ADJUST_INDICES(start, end, len1);
+ switch(kind) {
+ case PyUnicode_1BYTE_KIND:
+ iresult = ucs1lib_count(
+ ((Py_UCS1*)buf1) + start, end - start,
+ buf2, len2, PY_SSIZE_T_MAX
+ );
+ break;
+ case PyUnicode_2BYTE_KIND:
+ iresult = ucs2lib_count(
+ ((Py_UCS2*)buf1) + start, end - start,
+ buf2, len2, PY_SSIZE_T_MAX
+ );
+ break;
+ case PyUnicode_4BYTE_KIND:
+ iresult = ucs4lib_count(
+ ((Py_UCS4*)buf1) + start, end - start,
+ buf2, len2, PY_SSIZE_T_MAX
+ );
+ break;
+ default:
+ assert(0); iresult = 0;
+ }
+
+ result = PyLong_FromSsize_t(iresult);
+
+ if (kind1 != kind)
+ PyMem_Free(buf1);
+ if (kind2 != kind)
+ PyMem_Free(buf2);
Py_DECREF(substring);
@@ -7603,18 +9587,21 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args)
Py_UNICODE *p;
Py_UNICODE *q;
Py_UNICODE *qe;
- Py_ssize_t i, j, incr;
+ Py_ssize_t i, j, incr, wstr_length;
PyUnicodeObject *u;
int tabsize = 8;
if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))
return NULL;
+ if (PyUnicode_AsUnicodeAndSize((PyObject *)self, &wstr_length) == NULL)
+ return NULL;
+
/* First pass: determine size of output string */
i = 0; /* chars up to and including most recent \n or \r */
j = 0; /* chars since most recent \n or \r (use in tab calculations) */
- e = self->str + self->length; /* end of input */
- for (p = self->str; p < e; p++)
+ e = _PyUnicode_WSTR(self) + wstr_length; /* end of input */
+ for (p = _PyUnicode_WSTR(self); p < e; p++)
if (*p == '\t') {
if (tabsize > 0) {
incr = tabsize - (j % tabsize); /* cannot overflow */
@@ -7644,10 +9631,10 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args)
return NULL;
j = 0; /* same as in first pass */
- q = u->str; /* next output char */
- qe = u->str + u->length; /* end of output */
+ q = _PyUnicode_WSTR(u); /* next output char */
+ qe = _PyUnicode_WSTR(u) + PyUnicode_GET_SIZE(u); /* end of output */
- for (p = self->str; p < e; p++)
+ for (p = _PyUnicode_WSTR(self); p < e; p++)
if (*p == '\t') {
if (tabsize > 0) {
i = tabsize - (j % tabsize);
@@ -7668,6 +9655,10 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args)
j = 0;
}
+ if (PyUnicode_READY(u) == -1) {
+ Py_DECREF(u);
+ return NULL;
+ }
return (PyObject*) u;
overflow2:
@@ -7687,7 +9678,7 @@ arguments start and end are interpreted as in slice notation.\n\
Return -1 on failure.");
static PyObject *
-unicode_find(PyUnicodeObject *self, PyObject *args)
+unicode_find(PyObject *self, PyObject *args)
{
PyUnicodeObject *substring;
Py_ssize_t start;
@@ -7698,26 +9689,38 @@ unicode_find(PyUnicodeObject *self, PyObject *args)
&start, &end))
return NULL;
- result = stringlib_find_slice(
- PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
- PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),
- start, end
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ if (PyUnicode_READY(substring) == -1)
+ return NULL;
+
+ result = any_find_slice(
+ ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice,
+ self, (PyObject*)substring, start, end
);
Py_DECREF(substring);
+ if (result == -2)
+ return NULL;
+
return PyLong_FromSsize_t(result);
}
static PyObject *
unicode_getitem(PyUnicodeObject *self, Py_ssize_t index)
{
- if (index < 0 || index >= self->length) {
+ Py_UCS4 ch;
+
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ if (index < 0 || index >= _PyUnicode_LENGTH(self)) {
PyErr_SetString(PyExc_IndexError, "string index out of range");
return NULL;
}
- return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1);
+ ch = PyUnicode_READ(PyUnicode_KIND(self), PyUnicode_DATA(self), index);
+ return PyUnicode_FromOrdinal(ch);
}
/* Believe it or not, this produces the same value for ASCII strings
@@ -7726,22 +9729,48 @@ static Py_hash_t
unicode_hash(PyUnicodeObject *self)
{
Py_ssize_t len;
- Py_UNICODE *p;
Py_uhash_t x;
- if (self->hash != -1)
- return self->hash;
- len = Py_SIZE(self);
- p = self->str;
- x = (Py_uhash_t)*p << 7;
- while (--len >= 0)
- x = (1000003U*x) ^ (Py_uhash_t)*p++;
- x ^= (Py_uhash_t)Py_SIZE(self);
+ if (_PyUnicode_HASH(self) != -1)
+ return _PyUnicode_HASH(self);
+ if (PyUnicode_READY(self) == -1)
+ return -1;
+ len = PyUnicode_GET_LENGTH(self);
+
+ /* The hash function as a macro, gets expanded three times below. */
+#define HASH(P) \
+ x = (Py_uhash_t)*P << 7; \
+ while (--len >= 0) \
+ x = (1000003*x) ^ (Py_uhash_t)*P++;
+
+ switch (PyUnicode_KIND(self)) {
+ case PyUnicode_1BYTE_KIND: {
+ const unsigned char *c = PyUnicode_1BYTE_DATA(self);
+ HASH(c);
+ break;
+ }
+ case PyUnicode_2BYTE_KIND: {
+ const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self);
+ HASH(s);
+ break;
+ }
+ default: {
+ Py_UCS4 *l;
+ assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND &&
+ "Impossible switch case in unicode_hash");
+ l = PyUnicode_4BYTE_DATA(self);
+ HASH(l);
+ break;
+ }
+ }
+ x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self);
+
if (x == -1)
x = -2;
- self->hash = x;
+ _PyUnicode_HASH(self) = x;
return x;
}
+#undef HASH
PyDoc_STRVAR(index__doc__,
"S.index(sub[, start[, end]]) -> int\n\
@@ -7749,7 +9778,7 @@ PyDoc_STRVAR(index__doc__,
Like S.find() but raise ValueError when the substring is not found.");
static PyObject *
-unicode_index(PyUnicodeObject *self, PyObject *args)
+unicode_index(PyObject *self, PyObject *args)
{
Py_ssize_t result;
PyUnicodeObject *substring;
@@ -7760,14 +9789,21 @@ unicode_index(PyUnicodeObject *self, PyObject *args)
&start, &end))
return NULL;
- result = stringlib_find_slice(
- PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
- PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),
- start, end
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ if (PyUnicode_READY(substring) == -1)
+ return NULL;
+
+ result = any_find_slice(
+ ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice,
+ self, (PyObject*)substring, start, end
);
Py_DECREF(substring);
+ if (result == -2)
+ return NULL;
+
if (result < 0) {
PyErr_SetString(PyExc_ValueError, "substring not found");
return NULL;
@@ -7785,22 +9821,29 @@ at least one cased character in S, False otherwise.");
static PyObject*
unicode_islower(PyUnicodeObject *self)
{
- register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
- register const Py_UNICODE *e;
+ Py_ssize_t i, length;
+ int kind;
+ void *data;
int cased;
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ length = PyUnicode_GET_LENGTH(self);
+ kind = PyUnicode_KIND(self);
+ data = PyUnicode_DATA(self);
+
/* Shortcut for single character strings */
- if (PyUnicode_GET_SIZE(self) == 1)
- return PyBool_FromLong(Py_UNICODE_ISLOWER(*p));
+ if (length == 1)
+ return PyBool_FromLong(
+ Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0)));
/* Special case for empty strings */
- if (PyUnicode_GET_SIZE(self) == 0)
+ if (length == 0)
return PyBool_FromLong(0);
- e = p + PyUnicode_GET_SIZE(self);
cased = 0;
- while (p < e) {
- const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e);
+ for (i = 0; i < length; i++) {
+ const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch))
return PyBool_FromLong(0);
@@ -7819,22 +9862,29 @@ at least one cased character in S, False otherwise.");
static PyObject*
unicode_isupper(PyUnicodeObject *self)
{
- register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
- register const Py_UNICODE *e;
+ Py_ssize_t i, length;
+ int kind;
+ void *data;
int cased;
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ length = PyUnicode_GET_LENGTH(self);
+ kind = PyUnicode_KIND(self);
+ data = PyUnicode_DATA(self);
+
/* Shortcut for single character strings */
- if (PyUnicode_GET_SIZE(self) == 1)
- return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0);
+ if (length == 1)
+ return PyBool_FromLong(
+ Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0);
/* Special case for empty strings */
- if (PyUnicode_GET_SIZE(self) == 0)
+ if (length == 0)
return PyBool_FromLong(0);
- e = p + PyUnicode_GET_SIZE(self);
cased = 0;
- while (p < e) {
- const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e);
+ for (i = 0; i < length; i++) {
+ const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch))
return PyBool_FromLong(0);
@@ -7855,24 +9905,32 @@ Return False otherwise.");
static PyObject*
unicode_istitle(PyUnicodeObject *self)
{
- register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
- register const Py_UNICODE *e;
+ Py_ssize_t i, length;
+ int kind;
+ void *data;
int cased, previous_is_cased;
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ length = PyUnicode_GET_LENGTH(self);
+ kind = PyUnicode_KIND(self);
+ data = PyUnicode_DATA(self);
+
/* Shortcut for single character strings */
- if (PyUnicode_GET_SIZE(self) == 1)
- return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) ||
- (Py_UNICODE_ISUPPER(*p) != 0));
+ if (length == 1) {
+ Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
+ return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) ||
+ (Py_UNICODE_ISUPPER(ch) != 0));
+ }
/* Special case for empty strings */
- if (PyUnicode_GET_SIZE(self) == 0)
+ if (length == 0)
return PyBool_FromLong(0);
- e = p + PyUnicode_GET_SIZE(self);
cased = 0;
previous_is_cased = 0;
- while (p < e) {
- const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e);
+ for (i = 0; i < length; i++) {
+ const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) {
if (previous_is_cased)
@@ -7901,21 +9959,27 @@ and there is at least one character in S, False otherwise.");
static PyObject*
unicode_isspace(PyUnicodeObject *self)
{
- register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
- register const Py_UNICODE *e;
+ Py_ssize_t i, length;
+ int kind;
+ void *data;
+
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ length = PyUnicode_GET_LENGTH(self);
+ kind = PyUnicode_KIND(self);
+ data = PyUnicode_DATA(self);
/* Shortcut for single character strings */
- if (PyUnicode_GET_SIZE(self) == 1 &&
- Py_UNICODE_ISSPACE(*p))
- return PyBool_FromLong(1);
+ if (length == 1)
+ return PyBool_FromLong(
+ Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0)));
/* Special case for empty strings */
- if (PyUnicode_GET_SIZE(self) == 0)
+ if (length == 0)
return PyBool_FromLong(0);
- e = p + PyUnicode_GET_SIZE(self);
- while (p < e) {
- const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e);
+ for (i = 0; i < length; i++) {
+ const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
if (!Py_UNICODE_ISSPACE(ch))
return PyBool_FromLong(0);
}
@@ -7931,21 +9995,27 @@ and there is at least one character in S, False otherwise.");
static PyObject*
unicode_isalpha(PyUnicodeObject *self)
{
- register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
- register const Py_UNICODE *e;
+ Py_ssize_t i, length;
+ int kind;
+ void *data;
+
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ length = PyUnicode_GET_LENGTH(self);
+ kind = PyUnicode_KIND(self);
+ data = PyUnicode_DATA(self);
/* Shortcut for single character strings */
- if (PyUnicode_GET_SIZE(self) == 1 &&
- Py_UNICODE_ISALPHA(*p))
- return PyBool_FromLong(1);
+ if (length == 1)
+ return PyBool_FromLong(
+ Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0)));
/* Special case for empty strings */
- if (PyUnicode_GET_SIZE(self) == 0)
+ if (length == 0)
return PyBool_FromLong(0);
- e = p + PyUnicode_GET_SIZE(self);
- while (p < e) {
- if (!Py_UNICODE_ISALPHA(_Py_UNICODE_NEXT(p, e)))
+ for (i = 0; i < length; i++) {
+ if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i)))
return PyBool_FromLong(0);
}
return PyBool_FromLong(1);
@@ -7960,21 +10030,29 @@ and there is at least one character in S, False otherwise.");
static PyObject*
unicode_isalnum(PyUnicodeObject *self)
{
- register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
- register const Py_UNICODE *e;
+ int kind;
+ void *data;
+ Py_ssize_t len, i;
+
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+
+ kind = PyUnicode_KIND(self);
+ data = PyUnicode_DATA(self);
+ len = PyUnicode_GET_LENGTH(self);
/* Shortcut for single character strings */
- if (PyUnicode_GET_SIZE(self) == 1 &&
- Py_UNICODE_ISALNUM(*p))
- return PyBool_FromLong(1);
+ if (len == 1) {
+ const Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
+ return PyBool_FromLong(Py_UNICODE_ISALNUM(ch));
+ }
/* Special case for empty strings */
- if (PyUnicode_GET_SIZE(self) == 0)
+ if (len == 0)
return PyBool_FromLong(0);
- e = p + PyUnicode_GET_SIZE(self);
- while (p < e) {
- const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e);
+ for (i = 0; i < len; i++) {
+ const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
if (!Py_UNICODE_ISALNUM(ch))
return PyBool_FromLong(0);
}
@@ -7990,21 +10068,27 @@ False otherwise.");
static PyObject*
unicode_isdecimal(PyUnicodeObject *self)
{
- register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
- register const Py_UNICODE *e;
+ Py_ssize_t i, length;
+ int kind;
+ void *data;
+
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ length = PyUnicode_GET_LENGTH(self);
+ kind = PyUnicode_KIND(self);
+ data = PyUnicode_DATA(self);
/* Shortcut for single character strings */
- if (PyUnicode_GET_SIZE(self) == 1 &&
- Py_UNICODE_ISDECIMAL(*p))
- return PyBool_FromLong(1);
+ if (length == 1)
+ return PyBool_FromLong(
+ Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0)));
/* Special case for empty strings */
- if (PyUnicode_GET_SIZE(self) == 0)
+ if (length == 0)
return PyBool_FromLong(0);
- e = p + PyUnicode_GET_SIZE(self);
- while (p < e) {
- if (!Py_UNICODE_ISDECIMAL(_Py_UNICODE_NEXT(p, e)))
+ for (i = 0; i < length; i++) {
+ if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i)))
return PyBool_FromLong(0);
}
return PyBool_FromLong(1);
@@ -8019,21 +10103,28 @@ and there is at least one character in S, False otherwise.");
static PyObject*
unicode_isdigit(PyUnicodeObject *self)
{
- register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
- register const Py_UNICODE *e;
+ Py_ssize_t i, length;
+ int kind;
+ void *data;
+
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ length = PyUnicode_GET_LENGTH(self);
+ kind = PyUnicode_KIND(self);
+ data = PyUnicode_DATA(self);
/* Shortcut for single character strings */
- if (PyUnicode_GET_SIZE(self) == 1 &&
- Py_UNICODE_ISDIGIT(*p))
- return PyBool_FromLong(1);
+ if (length == 1) {
+ const Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
+ return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch));
+ }
/* Special case for empty strings */
- if (PyUnicode_GET_SIZE(self) == 0)
+ if (length == 0)
return PyBool_FromLong(0);
- e = p + PyUnicode_GET_SIZE(self);
- while (p < e) {
- if (!Py_UNICODE_ISDIGIT(_Py_UNICODE_NEXT(p, e)))
+ for (i = 0; i < length; i++) {
+ if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i)))
return PyBool_FromLong(0);
}
return PyBool_FromLong(1);
@@ -8048,21 +10139,27 @@ False otherwise.");
static PyObject*
unicode_isnumeric(PyUnicodeObject *self)
{
- register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
- register const Py_UNICODE *e;
+ Py_ssize_t i, length;
+ int kind;
+ void *data;
+
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ length = PyUnicode_GET_LENGTH(self);
+ kind = PyUnicode_KIND(self);
+ data = PyUnicode_DATA(self);
/* Shortcut for single character strings */
- if (PyUnicode_GET_SIZE(self) == 1 &&
- Py_UNICODE_ISNUMERIC(*p))
- return PyBool_FromLong(1);
+ if (length == 1)
+ return PyBool_FromLong(
+ Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0)));
/* Special case for empty strings */
- if (PyUnicode_GET_SIZE(self) == 0)
+ if (length == 0)
return PyBool_FromLong(0);
- e = p + PyUnicode_GET_SIZE(self);
- while (p < e) {
- if (!Py_UNICODE_ISNUMERIC(_Py_UNICODE_NEXT(p, e)))
+ for (i = 0; i < length; i++) {
+ if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i)))
return PyBool_FromLong(0);
}
return PyBool_FromLong(1);
@@ -8071,13 +10168,21 @@ unicode_isnumeric(PyUnicodeObject *self)
int
PyUnicode_IsIdentifier(PyObject *self)
{
- const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self);
- const Py_UNICODE *e;
+ int kind;
+ void *data;
+ Py_ssize_t i;
Py_UCS4 first;
+ if (PyUnicode_READY(self) == -1) {
+ Py_FatalError("identifier not ready");
+ return 0;
+ }
+
/* Special case for empty strings */
- if (PyUnicode_GET_SIZE(self) == 0)
+ if (PyUnicode_GET_LENGTH(self) == 0)
return 0;
+ kind = PyUnicode_KIND(self);
+ data = PyUnicode_DATA(self);
/* PEP 3131 says that the first character must be in
XID_Start and subsequent characters in XID_Continue,
@@ -8087,13 +10192,12 @@ PyUnicode_IsIdentifier(PyObject *self)
definition of XID_Start and XID_Continue, it is sufficient
to check just for these, except that _ must be allowed
as starting an identifier. */
- e = p + PyUnicode_GET_SIZE(self);
- first = _Py_UNICODE_NEXT(p, e);
+ first = PyUnicode_READ(kind, data, 0);
if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */)
return 0;
- while (p < e)
- if (!_PyUnicode_IsXidContinue(_Py_UNICODE_NEXT(p, e)))
+ for (i = 0; i < PyUnicode_GET_LENGTH(self); i++)
+ if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i)))
return 0;
return 1;
}
@@ -8119,17 +10223,23 @@ printable in repr() or S is empty, False otherwise.");
static PyObject*
unicode_isprintable(PyObject *self)
{
- register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
- register const Py_UNICODE *e;
+ Py_ssize_t i, length;
+ int kind;
+ void *data;
+
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ length = PyUnicode_GET_LENGTH(self);
+ kind = PyUnicode_KIND(self);
+ data = PyUnicode_DATA(self);
/* Shortcut for single character strings */
- if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) {
- Py_RETURN_TRUE;
- }
+ if (length == 1)
+ return PyBool_FromLong(
+ Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0)));
- e = p + PyUnicode_GET_SIZE(self);
- while (p < e) {
- if (!Py_UNICODE_ISPRINTABLE(_Py_UNICODE_NEXT(p, e))) {
+ for (i = 0; i < length; i++) {
+ if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) {
Py_RETURN_FALSE;
}
}
@@ -8151,7 +10261,9 @@ unicode_join(PyObject *self, PyObject *data)
static Py_ssize_t
unicode_length(PyUnicodeObject *self)
{
- return self->length;
+ if (PyUnicode_READY(self) == -1)
+ return -1;
+ return PyUnicode_GET_LENGTH(self);
}
PyDoc_STRVAR(ljust__doc__,
@@ -8164,17 +10276,20 @@ static PyObject *
unicode_ljust(PyUnicodeObject *self, PyObject *args)
{
Py_ssize_t width;
- Py_UNICODE fillchar = ' ';
+ Py_UCS4 fillchar = ' ';
+
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar))
return NULL;
- if (self->length >= width && PyUnicode_CheckExact(self)) {
+ if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) {
Py_INCREF(self);
return (PyObject*) self;
}
- return (PyObject*) pad(self, 0, width - self->length, fillchar);
+ return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar);
}
PyDoc_STRVAR(lower__doc__,
@@ -8201,17 +10316,25 @@ static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
PyObject *
_PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj)
{
- Py_UNICODE *s = PyUnicode_AS_UNICODE(self);
- Py_ssize_t len = PyUnicode_GET_SIZE(self);
- Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj);
- Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj);
- Py_ssize_t i, j;
+ void *data;
+ int kind;
+ Py_ssize_t i, j, len;
+ BLOOM_MASK sepmask;
+
+ if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1)
+ return NULL;
- BLOOM_MASK sepmask = make_bloom_mask(sep, seplen);
+ kind = PyUnicode_KIND(self);
+ data = PyUnicode_DATA(self);
+ len = PyUnicode_GET_LENGTH(self);
+ sepmask = make_bloom_mask(PyUnicode_KIND(sepobj),
+ PyUnicode_DATA(sepobj),
+ PyUnicode_GET_LENGTH(sepobj));
i = 0;
if (striptype != RIGHTSTRIP) {
- while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) {
+ while (i < len &&
+ BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) {
i++;
}
}
@@ -8220,7 +10343,8 @@ _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj)
if (striptype != LEFTSTRIP) {
do {
j--;
- } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen));
+ } while (j >= i &&
+ BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj));
j++;
}
@@ -8229,19 +10353,85 @@ _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj)
return (PyObject*)self;
}
else
- return PyUnicode_FromUnicode(s+i, j-i);
+ return PyUnicode_Substring((PyObject*)self, i, j);
+}
+
+/* Assumes an already ready self string. */
+
+static PyObject *
+substring(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t len)
+{
+ const int kind = PyUnicode_KIND(self);
+ void *data = PyUnicode_DATA(self);
+ Py_UCS4 maxchar = 0;
+ Py_ssize_t i;
+ PyObject *unicode;
+
+ if (start < 0 || len < 0 || (start + len) > PyUnicode_GET_LENGTH(self)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+
+ if (len == PyUnicode_GET_LENGTH(self) && PyUnicode_CheckExact(self)) {
+ Py_INCREF(self);
+ return (PyObject*)self;
+ }
+
+ for (i = 0; i < len; ++i) {
+ const Py_UCS4 ch = PyUnicode_READ(kind, data, start + i);
+ if (ch > maxchar)
+ maxchar = ch;
+ }
+
+ unicode = PyUnicode_New(len, maxchar);
+ if (unicode == NULL)
+ return NULL;
+ PyUnicode_CopyCharacters(unicode, 0,
+ (PyObject*)self, start, len);
+ return unicode;
}
+PyObject*
+PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end)
+{
+ unsigned char *data;
+ int kind;
+
+ if (start == 0 && end == PyUnicode_GET_LENGTH(self)
+ && PyUnicode_CheckExact(self))
+ {
+ Py_INCREF(self);
+ return (PyObject *)self;
+ }
+
+ if ((end - start) == 1)
+ return unicode_getitem((PyUnicodeObject*)self, start);
+
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ kind = PyUnicode_KIND(self);
+ data = PyUnicode_1BYTE_DATA(self);
+ return PyUnicode_FromKindAndData(kind, data + PyUnicode_KIND_SIZE(kind, start),
+ end-start);
+}
static PyObject *
do_strip(PyUnicodeObject *self, int striptype)
{
- Py_UNICODE *s = PyUnicode_AS_UNICODE(self);
- Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j;
+ int kind;
+ void *data;
+ Py_ssize_t len, i, j;
+
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+
+ kind = PyUnicode_KIND(self);
+ data = PyUnicode_DATA(self);
+ len = PyUnicode_GET_LENGTH(self);
i = 0;
if (striptype != RIGHTSTRIP) {
- while (i < len && Py_UNICODE_ISSPACE(s[i])) {
+ while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) {
i++;
}
}
@@ -8250,7 +10440,7 @@ do_strip(PyUnicodeObject *self, int striptype)
if (striptype != LEFTSTRIP) {
do {
j--;
- } while (j >= i && Py_UNICODE_ISSPACE(s[j]));
+ } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j)));
j++;
}
@@ -8259,7 +10449,7 @@ do_strip(PyUnicodeObject *self, int striptype)
return (PyObject*)self;
}
else
- return PyUnicode_FromUnicode(s+i, j-i);
+ return substring(self, i, j-i);
}
@@ -8339,9 +10529,8 @@ static PyObject*
unicode_repeat(PyUnicodeObject *str, Py_ssize_t len)
{
PyUnicodeObject *u;
- Py_UNICODE *p;
- Py_ssize_t nchars;
- size_t nbytes;
+ Py_ssize_t nchars, n;
+ size_t nbytes, char_size;
if (len < 1) {
Py_INCREF(unicode_empty);
@@ -8354,35 +10543,46 @@ unicode_repeat(PyUnicodeObject *str, Py_ssize_t len)
return (PyObject*) str;
}
+ if (PyUnicode_READY(str) == -1)
+ return NULL;
+
/* ensure # of chars needed doesn't overflow int and # of bytes
* needed doesn't overflow size_t
*/
- nchars = len * str->length;
- if (nchars / len != str->length) {
+ nchars = len * PyUnicode_GET_LENGTH(str);
+ if (nchars / len != PyUnicode_GET_LENGTH(str)) {
PyErr_SetString(PyExc_OverflowError,
"repeated string is too long");
return NULL;
}
- nbytes = (nchars + 1) * sizeof(Py_UNICODE);
- if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) {
+ char_size = PyUnicode_CHARACTER_SIZE(str);
+ nbytes = (nchars + 1) * char_size;
+ if (nbytes / char_size != (size_t)(nchars + 1)) {
PyErr_SetString(PyExc_OverflowError,
"repeated string is too long");
return NULL;
}
- u = _PyUnicode_New(nchars);
+ u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str));
if (!u)
return NULL;
- p = u->str;
-
- if (str->length == 1) {
- Py_UNICODE_FILL(p, str->str[0], len);
- } else {
- Py_ssize_t done = str->length; /* number of characters copied this far */
- Py_UNICODE_COPY(p, str->str, str->length);
+ if (PyUnicode_GET_LENGTH(str) == 1) {
+ const int kind = PyUnicode_KIND(str);
+ const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0);
+ void *to = PyUnicode_DATA(u);
+ for (n = 0; n < len; ++n)
+ PyUnicode_WRITE(kind, to, n, fill_char);
+ }
+ else {
+ /* number of characters copied this far */
+ Py_ssize_t done = PyUnicode_GET_LENGTH(str);
+ const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str);
+ char *to = (char *) PyUnicode_DATA(u);
+ Py_MEMCPY(to, PyUnicode_DATA(str),
+ PyUnicode_GET_LENGTH(str) * char_size);
while (done < nchars) {
- Py_ssize_t n = (done <= nchars-done) ? done : nchars-done;
- Py_UNICODE_COPY(p+done, p, n);
+ n = (done <= nchars-done) ? done : nchars-done;
+ Py_MEMCPY(to + (done * char_size), to, n * char_size);
done += n;
}
}
@@ -8402,23 +10602,20 @@ PyUnicode_Replace(PyObject *obj,
PyObject *result;
self = PyUnicode_FromObject(obj);
- if (self == NULL)
+ if (self == NULL || PyUnicode_READY(obj) == -1)
return NULL;
str1 = PyUnicode_FromObject(subobj);
- if (str1 == NULL) {
+ if (str1 == NULL || PyUnicode_READY(obj) == -1) {
Py_DECREF(self);
return NULL;
}
str2 = PyUnicode_FromObject(replobj);
- if (str2 == NULL) {
+ if (str2 == NULL || PyUnicode_READY(obj)) {
Py_DECREF(self);
Py_DECREF(str1);
return NULL;
}
- result = replace((PyUnicodeObject *)self,
- (PyUnicodeObject *)str1,
- (PyUnicodeObject *)str2,
- maxcount);
+ result = replace(self, str1, str2, maxcount);
Py_DECREF(self);
Py_DECREF(str1);
Py_DECREF(str2);
@@ -8433,20 +10630,22 @@ old replaced by new. If the optional argument count is\n\
given, only the first count occurrences are replaced.");
static PyObject*
-unicode_replace(PyUnicodeObject *self, PyObject *args)
+unicode_replace(PyObject *self, PyObject *args)
{
- PyUnicodeObject *str1;
- PyUnicodeObject *str2;
+ PyObject *str1;
+ PyObject *str2;
Py_ssize_t maxcount = -1;
PyObject *result;
if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount))
return NULL;
- str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1);
- if (str1 == NULL)
+ if (!PyUnicode_READY(self) == -1)
+ return NULL;
+ str1 = PyUnicode_FromObject(str1);
+ if (str1 == NULL || PyUnicode_READY(str1) == -1)
return NULL;
- str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2);
- if (str2 == NULL) {
+ str2 = PyUnicode_FromObject(str2);
+ if (str2 == NULL || PyUnicode_READY(str1) == -1) {
Py_DECREF(str1);
return NULL;
}
@@ -8462,148 +10661,149 @@ static PyObject *
unicode_repr(PyObject *unicode)
{
PyObject *repr;
- Py_UNICODE *p;
- Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode);
- Py_ssize_t size = PyUnicode_GET_SIZE(unicode);
+ Py_ssize_t isize;
+ Py_ssize_t osize, squote, dquote, i, o;
+ Py_UCS4 max, quote;
+ int ikind, okind;
+ void *idata, *odata;
- /* XXX(nnorwitz): rather than over-allocating, it would be
- better to choose a different scheme. Perhaps scan the
- first N-chars of the string and allocate based on that size.
- */
- /* Initial allocation is based on the longest-possible unichr
- escape.
+ if (PyUnicode_READY(unicode) == -1)
+ return NULL;
- In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source
- unichr, so in this case it's the longest unichr escape. In
- narrow (UTF-16) builds this is five chars per source unichr
- since there are two unichrs in the surrogate pair, so in narrow
- (UTF-16) builds it's not the longest unichr escape.
+ isize = PyUnicode_GET_LENGTH(unicode);
+ idata = PyUnicode_DATA(unicode);
+
+ /* Compute length of output, quote characters, and
+ maximum character */
+ osize = 2; /* quotes */
+ max = 127;
+ squote = dquote = 0;
+ ikind = PyUnicode_KIND(unicode);
+ for (i = 0; i < isize; i++) {
+ Py_UCS4 ch = PyUnicode_READ(ikind, idata, i);
+ switch (ch) {
+ case '\'': squote++; osize++; break;
+ case '"': dquote++; osize++; break;
+ case '\\': case '\t': case '\r': case '\n':
+ osize += 2; break;
+ default:
+ /* Fast-path ASCII */
+ if (ch < ' ' || ch == 0x7f)
+ osize += 4; /* \xHH */
+ else if (ch < 0x7f)
+ osize++;
+ else if (Py_UNICODE_ISPRINTABLE(ch)) {
+ osize++;
+ max = ch > max ? ch : max;
+ }
+ else if (ch < 0x100)
+ osize += 4; /* \xHH */
+ else if (ch < 0x10000)
+ osize += 6; /* \uHHHH */
+ else
+ osize += 10; /* \uHHHHHHHH */
+ }
+ }
- In wide or narrow builds '\uxxxx' is 6 chars per source unichr,
- so in the narrow (UTF-16) build case it's the longest unichr
- escape.
- */
+ quote = '\'';
+ if (squote) {
+ if (dquote)
+ /* Both squote and dquote present. Use squote,
+ and escape them */
+ osize += squote;
+ else
+ quote = '"';
+ }
- repr = PyUnicode_FromUnicode(NULL,
- 2 /* quotes */
-#ifdef Py_UNICODE_WIDE
- + 10*size
-#else
- + 6*size
-#endif
- + 1);
+ repr = PyUnicode_New(osize, max);
if (repr == NULL)
return NULL;
+ okind = PyUnicode_KIND(repr);
+ odata = PyUnicode_DATA(repr);
- p = PyUnicode_AS_UNICODE(repr);
+ PyUnicode_WRITE(okind, odata, 0, quote);
+ PyUnicode_WRITE(okind, odata, osize-1, quote);
- /* Add quote */
- *p++ = (findchar(s, size, '\'') &&
- !findchar(s, size, '"')) ? '"' : '\'';
- while (size-- > 0) {
- Py_UNICODE ch = *s++;
+ for (i = 0, o = 1; i < isize; i++) {
+ Py_UCS4 ch = PyUnicode_READ(ikind, idata, i);
/* Escape quotes and backslashes */
- if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) {
- *p++ = '\\';
- *p++ = ch;
+ if ((ch == quote) || (ch == '\\')) {
+ PyUnicode_WRITE(okind, odata, o++, '\\');
+ PyUnicode_WRITE(okind, odata, o++, ch);
continue;
}
/* Map special whitespace to '\t', \n', '\r' */
if (ch == '\t') {
- *p++ = '\\';
- *p++ = 't';
+ PyUnicode_WRITE(okind, odata, o++, '\\');
+ PyUnicode_WRITE(okind, odata, o++, 't');
}
else if (ch == '\n') {
- *p++ = '\\';
- *p++ = 'n';
+ PyUnicode_WRITE(okind, odata, o++, '\\');
+ PyUnicode_WRITE(okind, odata, o++, 'n');
}
else if (ch == '\r') {
- *p++ = '\\';
- *p++ = 'r';
+ PyUnicode_WRITE(okind, odata, o++, '\\');
+ PyUnicode_WRITE(okind, odata, o++, 'r');
}
/* Map non-printable US ASCII to '\xhh' */
else if (ch < ' ' || ch == 0x7F) {
- *p++ = '\\';
- *p++ = 'x';
- *p++ = hexdigits[(ch >> 4) & 0x000F];
- *p++ = hexdigits[ch & 0x000F];
+ PyUnicode_WRITE(okind, odata, o++, '\\');
+ PyUnicode_WRITE(okind, odata, o++, 'x');
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]);
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]);
}
/* Copy ASCII characters as-is */
else if (ch < 0x7F) {
- *p++ = ch;
+ PyUnicode_WRITE(okind, odata, o++, ch);
}
/* Non-ASCII characters */
else {
- Py_UCS4 ucs = ch;
-
-#ifndef Py_UNICODE_WIDE
- Py_UNICODE ch2 = 0;
- /* Get code point from surrogate pair */
- if (size > 0) {
- ch2 = *s;
- if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00
- && ch2 <= 0xDFFF) {
- ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF))
- + 0x00010000;
- s++;
- size--;
- }
- }
-#endif
/* Map Unicode whitespace and control characters
(categories Z* and C* except ASCII space)
*/
- if (!Py_UNICODE_ISPRINTABLE(ucs)) {
+ if (!Py_UNICODE_ISPRINTABLE(ch)) {
/* Map 8-bit characters to '\xhh' */
- if (ucs <= 0xff) {
- *p++ = '\\';
- *p++ = 'x';
- *p++ = hexdigits[(ch >> 4) & 0x000F];
- *p++ = hexdigits[ch & 0x000F];
+ if (ch <= 0xff) {
+ PyUnicode_WRITE(okind, odata, o++, '\\');
+ PyUnicode_WRITE(okind, odata, o++, 'x');
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]);
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]);
}
/* Map 21-bit characters to '\U00xxxxxx' */
- else if (ucs >= 0x10000) {
- *p++ = '\\';
- *p++ = 'U';
- *p++ = hexdigits[(ucs >> 28) & 0x0000000F];
- *p++ = hexdigits[(ucs >> 24) & 0x0000000F];
- *p++ = hexdigits[(ucs >> 20) & 0x0000000F];
- *p++ = hexdigits[(ucs >> 16) & 0x0000000F];
- *p++ = hexdigits[(ucs >> 12) & 0x0000000F];
- *p++ = hexdigits[(ucs >> 8) & 0x0000000F];
- *p++ = hexdigits[(ucs >> 4) & 0x0000000F];
- *p++ = hexdigits[ucs & 0x0000000F];
+ else if (ch >= 0x10000) {
+ PyUnicode_WRITE(okind, odata, o++, '\\');
+ PyUnicode_WRITE(okind, odata, o++, 'U');
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]);
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]);
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]);
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]);
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]);
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]);
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]);
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]);
}
/* Map 16-bit characters to '\uxxxx' */
else {
- *p++ = '\\';
- *p++ = 'u';
- *p++ = hexdigits[(ucs >> 12) & 0x000F];
- *p++ = hexdigits[(ucs >> 8) & 0x000F];
- *p++ = hexdigits[(ucs >> 4) & 0x000F];
- *p++ = hexdigits[ucs & 0x000F];
+ PyUnicode_WRITE(okind, odata, o++, '\\');
+ PyUnicode_WRITE(okind, odata, o++, 'u');
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]);
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]);
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]);
+ PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]);
}
}
/* Copy characters as-is */
else {
- *p++ = ch;
-#ifndef Py_UNICODE_WIDE
- if (ucs >= 0x10000)
- *p++ = ch2;
-#endif
+ PyUnicode_WRITE(okind, odata, o++, ch);
}
}
}
- /* Add quote */
- *p++ = PyUnicode_AS_UNICODE(repr)[0];
-
- *p = '\0';
- PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr));
+ /* Closing quote already added at the beginning */
return repr;
}
@@ -8617,7 +10817,7 @@ arguments start and end are interpreted as in slice notation.\n\
Return -1 on failure.");
static PyObject *
-unicode_rfind(PyUnicodeObject *self, PyObject *args)
+unicode_rfind(PyObject *self, PyObject *args)
{
PyUnicodeObject *substring;
Py_ssize_t start;
@@ -8628,14 +10828,21 @@ unicode_rfind(PyUnicodeObject *self, PyObject *args)
&start, &end))
return NULL;
- result = stringlib_rfind_slice(
- PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
- PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),
- start, end
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ if (PyUnicode_READY(substring) == -1)
+ return NULL;
+
+ result = any_find_slice(
+ ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice,
+ self, (PyObject*)substring, start, end
);
Py_DECREF(substring);
+ if (result == -2)
+ return NULL;
+
return PyLong_FromSsize_t(result);
}
@@ -8645,7 +10852,7 @@ PyDoc_STRVAR(rindex__doc__,
Like S.rfind() but raise ValueError when the substring is not found.");
static PyObject *
-unicode_rindex(PyUnicodeObject *self, PyObject *args)
+unicode_rindex(PyObject *self, PyObject *args)
{
PyUnicodeObject *substring;
Py_ssize_t start;
@@ -8656,18 +10863,26 @@ unicode_rindex(PyUnicodeObject *self, PyObject *args)
&start, &end))
return NULL;
- result = stringlib_rfind_slice(
- PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
- PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),
- start, end
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+ if (PyUnicode_READY(substring) == -1)
+ return NULL;
+
+ result = any_find_slice(
+ ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice,
+ self, (PyObject*)substring, start, end
);
Py_DECREF(substring);
+ if (result == -2)
+ return NULL;
+
if (result < 0) {
PyErr_SetString(PyExc_ValueError, "substring not found");
return NULL;
}
+
return PyLong_FromSsize_t(result);
}
@@ -8681,17 +10896,20 @@ static PyObject *
unicode_rjust(PyUnicodeObject *self, PyObject *args)
{
Py_ssize_t width;
- Py_UNICODE fillchar = ' ';
+ Py_UCS4 fillchar = ' ';
+
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar))
return NULL;
- if (self->length >= width && PyUnicode_CheckExact(self)) {
+ if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) {
Py_INCREF(self);
return (PyObject*) self;
}
- return (PyObject*) pad(self, width - self->length, 0, fillchar);
+ return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar);
}
PyObject *
@@ -8749,25 +10967,66 @@ PyUnicode_Partition(PyObject *str_in, PyObject *sep_in)
PyObject* str_obj;
PyObject* sep_obj;
PyObject* out;
+ int kind1, kind2, kind;
+ void *buf1 = NULL, *buf2 = NULL;
+ Py_ssize_t len1, len2;
str_obj = PyUnicode_FromObject(str_in);
- if (!str_obj)
+ if (!str_obj || PyUnicode_READY(str_in) == -1)
return NULL;
sep_obj = PyUnicode_FromObject(sep_in);
- if (!sep_obj) {
+ if (!sep_obj || PyUnicode_READY(sep_obj) == -1) {
Py_DECREF(str_obj);
return NULL;
}
- out = stringlib_partition(
- str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj),
- sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj)
- );
+ kind1 = PyUnicode_KIND(str_in);
+ kind2 = PyUnicode_KIND(sep_obj);
+ kind = kind1 > kind2 ? kind1 : kind2;
+ buf1 = PyUnicode_DATA(str_in);
+ if (kind1 != kind)
+ buf1 = _PyUnicode_AsKind(str_in, kind);
+ if (!buf1)
+ goto onError;
+ buf2 = PyUnicode_DATA(sep_obj);
+ if (kind2 != kind)
+ buf2 = _PyUnicode_AsKind(sep_obj, kind);
+ if (!buf2)
+ goto onError;
+ len1 = PyUnicode_GET_LENGTH(str_obj);
+ len2 = PyUnicode_GET_LENGTH(sep_obj);
+
+ switch(PyUnicode_KIND(str_in)) {
+ case PyUnicode_1BYTE_KIND:
+ out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2);
+ break;
+ case PyUnicode_2BYTE_KIND:
+ out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2);
+ break;
+ case PyUnicode_4BYTE_KIND:
+ out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2);
+ break;
+ default:
+ assert(0);
+ out = 0;
+ }
Py_DECREF(sep_obj);
Py_DECREF(str_obj);
+ if (kind1 != kind)
+ PyMem_Free(buf1);
+ if (kind2 != kind)
+ PyMem_Free(buf2);
return out;
+ onError:
+ Py_DECREF(sep_obj);
+ Py_DECREF(str_obj);
+ if (kind1 != kind && buf1)
+ PyMem_Free(buf1);
+ if (kind2 != kind && buf2)
+ PyMem_Free(buf2);
+ return NULL;
}
@@ -8777,6 +11036,9 @@ PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in)
PyObject* str_obj;
PyObject* sep_obj;
PyObject* out;
+ int kind1, kind2, kind;
+ void *buf1 = NULL, *buf2 = NULL;
+ Py_ssize_t len1, len2;
str_obj = PyUnicode_FromObject(str_in);
if (!str_obj)
@@ -8787,15 +11049,53 @@ PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in)
return NULL;
}
- out = stringlib_rpartition(
- str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj),
- sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj)
- );
+ kind1 = PyUnicode_KIND(str_in);
+ kind2 = PyUnicode_KIND(sep_obj);
+ kind = PY_MAX(kind1, kind2);
+ buf1 = PyUnicode_DATA(str_in);
+ if (kind1 != kind)
+ buf1 = _PyUnicode_AsKind(str_in, kind);
+ if (!buf1)
+ goto onError;
+ buf2 = PyUnicode_DATA(sep_obj);
+ if (kind2 != kind)
+ buf2 = _PyUnicode_AsKind(sep_obj, kind);
+ if (!buf2)
+ goto onError;
+ len1 = PyUnicode_GET_LENGTH(str_obj);
+ len2 = PyUnicode_GET_LENGTH(sep_obj);
+
+ switch(PyUnicode_KIND(str_in)) {
+ case PyUnicode_1BYTE_KIND:
+ out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2);
+ break;
+ case PyUnicode_2BYTE_KIND:
+ out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2);
+ break;
+ case PyUnicode_4BYTE_KIND:
+ out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2);
+ break;
+ default:
+ assert(0);
+ out = 0;
+ }
Py_DECREF(sep_obj);
Py_DECREF(str_obj);
+ if (kind1 != kind)
+ PyMem_Free(buf1);
+ if (kind2 != kind)
+ PyMem_Free(buf2);
return out;
+ onError:
+ Py_DECREF(sep_obj);
+ Py_DECREF(str_obj);
+ if (kind1 != kind && buf1)
+ PyMem_Free(buf1);
+ if (kind2 != kind && buf2)
+ PyMem_Free(buf2);
+ return NULL;
}
PyDoc_STRVAR(partition__doc__,
@@ -8943,22 +11243,28 @@ unicode_maketrans(PyUnicodeObject *null, PyObject *args)
if (!new)
return NULL;
if (y != NULL) {
+ int x_kind, y_kind, z_kind;
+ void *x_data, *y_data, *z_data;
+
/* x must be a string too, of equal length */
- Py_ssize_t ylen = PyUnicode_GET_SIZE(y);
if (!PyUnicode_Check(x)) {
PyErr_SetString(PyExc_TypeError, "first maketrans argument must "
"be a string if there is a second argument");
goto err;
}
- if (PyUnicode_GET_SIZE(x) != ylen) {
+ if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) {
PyErr_SetString(PyExc_ValueError, "the first two maketrans "
"arguments must have equal length");
goto err;
}
/* create entries for translating chars in x to those in y */
- for (i = 0; i < PyUnicode_GET_SIZE(x); i++) {
- key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]);
- value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]);
+ x_kind = PyUnicode_KIND(x);
+ y_kind = PyUnicode_KIND(y);
+ x_data = PyUnicode_DATA(x);
+ y_data = PyUnicode_DATA(y);
+ for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) {
+ key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i));
+ value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i));
if (!key || !value)
goto err;
res = PyDict_SetItem(new, key, value);
@@ -8969,8 +11275,10 @@ unicode_maketrans(PyUnicodeObject *null, PyObject *args)
}
/* create entries for deleting chars in z */
if (z != NULL) {
+ z_kind = PyUnicode_KIND(z);
+ z_data = PyUnicode_DATA(z);
for (i = 0; i < PyUnicode_GET_SIZE(z); i++) {
- key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]);
+ key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i));
if (!key)
goto err;
res = PyDict_SetItem(new, key, Py_None);
@@ -8980,6 +11288,9 @@ unicode_maketrans(PyUnicodeObject *null, PyObject *args)
}
}
} else {
+ int kind;
+ void *data;
+
/* x must be a dict */
if (!PyDict_CheckExact(x)) {
PyErr_SetString(PyExc_TypeError, "if you give only one argument "
@@ -8996,7 +11307,9 @@ unicode_maketrans(PyUnicodeObject *null, PyObject *args)
"table must be of length 1");
goto err;
}
- newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]);
+ kind = PyUnicode_KIND(key);
+ data = PyUnicode_DATA(key);
+ newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0));
if (!newkey)
goto err;
res = PyDict_SetItem(new, newkey, value);
@@ -9030,9 +11343,9 @@ Unmapped characters are left untouched. Characters mapped to None\n\
are deleted.");
static PyObject*
-unicode_translate(PyUnicodeObject *self, PyObject *table)
+unicode_translate(PyObject *self, PyObject *table)
{
- return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore");
+ return _PyUnicode_TranslateCharmap(self, table, "ignore");
}
PyDoc_STRVAR(upper__doc__,
@@ -9057,12 +11370,18 @@ unicode_zfill(PyUnicodeObject *self, PyObject *args)
{
Py_ssize_t fill;
PyUnicodeObject *u;
-
Py_ssize_t width;
+ int kind;
+ void *data;
+ Py_UCS4 chr;
+
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+
if (!PyArg_ParseTuple(args, "n:zfill", &width))
return NULL;
- if (self->length >= width) {
+ if (PyUnicode_GET_LENGTH(self) >= width) {
if (PyUnicode_CheckExact(self)) {
Py_INCREF(self);
return (PyObject*) self;
@@ -9074,34 +11393,31 @@ unicode_zfill(PyUnicodeObject *self, PyObject *args)
);
}
- fill = width - self->length;
+ fill = width - _PyUnicode_LENGTH(self);
u = pad(self, fill, 0, '0');
if (u == NULL)
return NULL;
- if (u->str[fill] == '+' || u->str[fill] == '-') {
+ kind = PyUnicode_KIND(u);
+ data = PyUnicode_DATA(u);
+ chr = PyUnicode_READ(kind, data, fill);
+
+ if (chr == '+' || chr == '-') {
/* move sign to beginning of string */
- u->str[0] = u->str[fill];
- u->str[fill] = '0';
+ PyUnicode_WRITE(kind, data, 0, chr);
+ PyUnicode_WRITE(kind, data, fill, '0');
}
return (PyObject*) u;
}
#if 0
-static PyObject*
-unicode_freelistsize(PyUnicodeObject *self)
-{
- return PyLong_FromLong(numfree);
-}
-
static PyObject *
unicode__decimal2ascii(PyObject *self)
{
- return PyUnicode_TransformDecimalToASCII(PyUnicode_AS_UNICODE(self),
- PyUnicode_GET_SIZE(self));
+ return PyUnicode_TransformDecimalAndSpaceToASCII(self);
}
#endif
@@ -9201,7 +11517,7 @@ unicode_endswith(PyUnicodeObject *self,
return PyBool_FromLong(result);
}
-#include "stringlib/string_format.h"
+#include "stringlib/unicode_format.h"
PyDoc_STRVAR(format__doc__,
"S.format(*args, **kwargs) -> str\n\
@@ -9223,9 +11539,8 @@ unicode__format__(PyObject* self, PyObject* args)
if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
return NULL;
- return _PyUnicode_FormatAdvanced(self,
- PyUnicode_AS_UNICODE(format_spec),
- PyUnicode_GET_SIZE(format_spec));
+ return _PyUnicode_FormatAdvanced(self, format_spec, 0,
+ PyUnicode_GET_LENGTH(format_spec));
}
PyDoc_STRVAR(p_format__doc__,
@@ -9236,8 +11551,35 @@ Return a formatted version of S as described by format_spec.");
static PyObject *
unicode__sizeof__(PyUnicodeObject *v)
{
- return PyLong_FromSsize_t(sizeof(PyUnicodeObject) +
- sizeof(Py_UNICODE) * (v->length + 1));
+ Py_ssize_t size;
+
+ /* If it's a compact object, account for base structure +
+ character data. */
+ if (PyUnicode_IS_COMPACT_ASCII(v))
+ size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1;
+ else if (PyUnicode_IS_COMPACT(v))
+ size = sizeof(PyCompactUnicodeObject) +
+ (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v);
+ else {
+ /* If it is a two-block object, account for base object, and
+ for character block if present. */
+ size = sizeof(PyUnicodeObject);
+ if (v->data.any)
+ size += (PyUnicode_GET_LENGTH(v) + 1) *
+ PyUnicode_CHARACTER_SIZE(v);
+ }
+ /* If the wstr pointer is present, account for it unless it is shared
+ with the data pointer. Since PyUnicode_DATA will crash if the object
+ is not ready, check whether it's either not ready (in which case the
+ data is entirely in wstr) or if the data is not shared. */
+ if (_PyUnicode_WSTR(v) &&
+ (!PyUnicode_IS_READY(v) ||
+ (PyUnicode_DATA(v) != _PyUnicode_WSTR(v))))
+ size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t);
+ if (_PyUnicode_UTF8(v) && _PyUnicode_UTF8(v) != PyUnicode_DATA(v))
+ size += _PyUnicode_UTF8_LENGTH(v) + 1;
+
+ return PyLong_FromSsize_t(size);
}
PyDoc_STRVAR(sizeof__doc__,
@@ -9246,7 +11588,17 @@ PyDoc_STRVAR(sizeof__doc__,
static PyObject *
unicode_getnewargs(PyUnicodeObject *v)
{
- return Py_BuildValue("(u#)", v->str, v->length);
+ PyObject *copy;
+ unsigned char *data;
+ int kind;
+ if (PyUnicode_READY(v) == -1)
+ return NULL;
+ kind = PyUnicode_KIND(v);
+ data = PyUnicode_1BYTE_DATA(v);
+ copy = PyUnicode_FromKindAndData(kind, data, PyUnicode_GET_LENGTH(v));
+ if (!copy)
+ return NULL;
+ return Py_BuildValue("(N)", copy);
}
static PyMethodDef unicode_methods[] = {
@@ -9306,7 +11658,6 @@ static PyMethodDef unicode_methods[] = {
#if 0
/* These methods are just used for debugging the implementation. */
- {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS},
{"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS},
#endif
@@ -9343,32 +11694,36 @@ static PySequenceMethods unicode_as_sequence = {
static PyObject*
unicode_subscript(PyUnicodeObject* self, PyObject* item)
{
+ if (PyUnicode_READY(self) == -1)
+ return NULL;
+
if (PyIndex_Check(item)) {
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return NULL;
if (i < 0)
- i += PyUnicode_GET_SIZE(self);
+ i += PyUnicode_GET_LENGTH(self);
return unicode_getitem(self, i);
} else if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelength, cur, i;
- Py_UNICODE* source_buf;
+ const Py_UNICODE* source_buf;
Py_UNICODE* result_buf;
PyObject* result;
- if (PySlice_GetIndicesEx(item, PyUnicode_GET_SIZE(self),
+ if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self),
&start, &stop, &step, &slicelength) < 0) {
return NULL;
}
if (slicelength <= 0) {
- return PyUnicode_FromUnicode(NULL, 0);
- } else if (start == 0 && step == 1 && slicelength == self->length &&
+ return PyUnicode_New(0, 0);
+ } else if (start == 0 && step == 1 &&
+ slicelength == PyUnicode_GET_LENGTH(self) &&
PyUnicode_CheckExact(self)) {
Py_INCREF(self);
return (PyObject *)self;
} else if (step == 1) {
- return PyUnicode_FromUnicode(self->str + start, slicelength);
+ return substring(self, start, slicelength);
} else {
source_buf = PyUnicode_AS_UNICODE((PyObject*)self);
result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength*
@@ -9436,7 +11791,7 @@ formatfloat(PyObject *v, int flags, int prec, int type)
(flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);
if (p == NULL)
return NULL;
- result = PyUnicode_FromStringAndSize(p, strlen(p));
+ result = PyUnicode_DecodeASCII(p, strlen(p), NULL);
PyMem_Free(p);
return result;
}
@@ -9452,37 +11807,23 @@ formatlong(PyObject *val, int flags, int prec, int type)
str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len);
if (!str)
return NULL;
- result = PyUnicode_FromStringAndSize(buf, len);
+ result = PyUnicode_DecodeASCII(buf, len, NULL);
Py_DECREF(str);
return result;
}
static int
-formatchar(Py_UNICODE *buf,
+formatchar(Py_UCS4 *buf,
size_t buflen,
PyObject *v)
{
/* presume that the buffer is at least 3 characters long */
if (PyUnicode_Check(v)) {
- if (PyUnicode_GET_SIZE(v) == 1) {
- buf[0] = PyUnicode_AS_UNICODE(v)[0];
+ if (PyUnicode_GET_LENGTH(v) == 1) {
+ buf[0] = PyUnicode_READ_CHAR(v, 0);
buf[1] = '\0';
return 1;
}
-#ifndef Py_UNICODE_WIDE
- if (PyUnicode_GET_SIZE(v) == 2) {
- /* Decode a valid surrogate pair */
- int c0 = PyUnicode_AS_UNICODE(v)[0];
- int c1 = PyUnicode_AS_UNICODE(v)[1];
- if (0xD800 <= c0 && c0 <= 0xDBFF &&
- 0xDC00 <= c1 && c1 <= 0xDFFF) {
- buf[0] = c0;
- buf[1] = c1;
- buf[2] = '\0';
- return 2;
- }
- }
-#endif
goto onError;
}
else {
@@ -9498,15 +11839,7 @@ formatchar(Py_UNICODE *buf,
return -1;
}
-#ifndef Py_UNICODE_WIDE
- if (x > 0xffff) {
- x -= 0x10000;
- buf[0] = (Py_UNICODE)(0xD800 | (x >> 10));
- buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF));
- return 2;
- }
-#endif
- buf[0] = (Py_UNICODE) x;
+ buf[0] = (Py_UCS4) x;
buf[1] = '\0';
return 1;
}
@@ -9525,28 +11858,35 @@ formatchar(Py_UNICODE *buf,
PyObject *
PyUnicode_Format(PyObject *format, PyObject *args)
{
- Py_UNICODE *fmt, *res;
- Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx;
+ void *fmt;
+ int fmtkind;
+ PyObject *result;
+ Py_UCS4 *res, *res0;
+ Py_UCS4 max;
+ int kind;
+ Py_ssize_t fmtcnt, fmtpos, rescnt, reslen, arglen, argidx;
int args_owned = 0;
- PyUnicodeObject *result = NULL;
PyObject *dict = NULL;
- PyObject *uformat;
+ PyUnicodeObject *uformat;
if (format == NULL || args == NULL) {
PyErr_BadInternalCall();
return NULL;
}
- uformat = PyUnicode_FromObject(format);
- if (uformat == NULL)
+ uformat = (PyUnicodeObject*)PyUnicode_FromObject(format);
+ if (uformat == NULL || PyUnicode_READY(uformat) == -1)
return NULL;
- fmt = PyUnicode_AS_UNICODE(uformat);
- fmtcnt = PyUnicode_GET_SIZE(uformat);
+ fmt = PyUnicode_DATA(uformat);
+ fmtkind = PyUnicode_KIND(uformat);
+ fmtcnt = PyUnicode_GET_LENGTH(uformat);
+ fmtpos = 0;
reslen = rescnt = fmtcnt + 100;
- result = _PyUnicode_New(reslen);
- if (result == NULL)
+ res = res0 = PyMem_Malloc(reslen * sizeof(Py_UCS4));
+ if (res0 == NULL) {
+ PyErr_NoMemory();
goto onError;
- res = PyUnicode_AS_UNICODE(result);
+ }
if (PyTuple_Check(args)) {
arglen = PyTuple_Size(args);
@@ -9561,35 +11901,39 @@ PyUnicode_Format(PyObject *format, PyObject *args)
dict = args;
while (--fmtcnt >= 0) {
- if (*fmt != '%') {
+ if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') {
if (--rescnt < 0) {
rescnt = fmtcnt + 100;
reslen += rescnt;
- if (_PyUnicode_Resize(&result, reslen) < 0)
+ res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4));
+ if (res0 == NULL){
+ PyErr_NoMemory();
goto onError;
- res = PyUnicode_AS_UNICODE(result) + reslen - rescnt;
+ }
+ res = res0 + reslen - rescnt;
--rescnt;
}
- *res++ = *fmt++;
+ *res++ = PyUnicode_READ(fmtkind, fmt, fmtpos++);
}
else {
/* Got a format specifier */
int flags = 0;
Py_ssize_t width = -1;
int prec = -1;
- Py_UNICODE c = '\0';
- Py_UNICODE fill;
+ Py_UCS4 c = '\0';
+ Py_UCS4 fill;
int isnumok;
PyObject *v = NULL;
PyObject *temp = NULL;
- Py_UNICODE *pbuf;
+ void *pbuf;
+ Py_ssize_t pindex;
Py_UNICODE sign;
- Py_ssize_t len;
- Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */
+ Py_ssize_t len, len1;
+ Py_UCS4 formatbuf[FORMATBUFLEN]; /* For formatchar() */
- fmt++;
- if (*fmt == '(') {
- Py_UNICODE *keystart;
+ fmtpos++;
+ if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') {
+ Py_ssize_t keystart;
Py_ssize_t keylen;
PyObject *key;
int pcount = 1;
@@ -9599,34 +11943,24 @@ PyUnicode_Format(PyObject *format, PyObject *args)
"format requires a mapping");
goto onError;
}
- ++fmt;
+ ++fmtpos;
--fmtcnt;
- keystart = fmt;
+ keystart = fmtpos;
/* Skip over balanced parentheses */
while (pcount > 0 && --fmtcnt >= 0) {
- if (*fmt == ')')
+ if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')')
--pcount;
- else if (*fmt == '(')
+ else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(')
++pcount;
- fmt++;
+ fmtpos++;
}
- keylen = fmt - keystart - 1;
+ keylen = fmtpos - keystart - 1;
if (fmtcnt < 0 || pcount > 0) {
PyErr_SetString(PyExc_ValueError,
"incomplete format key");
goto onError;
}
-#if 0
- /* keys are converted to strings using UTF-8 and
- then looked up since Python uses strings to hold
- variables names etc. in its namespaces and we
- wouldn't want to break common idioms. */
- key = PyUnicode_EncodeUTF8(keystart,
- keylen,
- NULL);
-#else
- key = PyUnicode_FromUnicode(keystart, keylen);
-#endif
+ key = substring(uformat, keystart, keylen);
if (key == NULL)
goto onError;
if (args_owned) {
@@ -9643,7 +11977,7 @@ PyUnicode_Format(PyObject *format, PyObject *args)
argidx = -2;
}
while (--fmtcnt >= 0) {
- switch (c = *fmt++) {
+ switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) {
case '-': flags |= F_LJUST; continue;
case '+': flags |= F_SIGN; continue;
case ' ': flags |= F_BLANK; continue;
@@ -9669,12 +12003,12 @@ PyUnicode_Format(PyObject *format, PyObject *args)
width = -width;
}
if (--fmtcnt >= 0)
- c = *fmt++;
+ c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
}
else if (c >= '0' && c <= '9') {
width = c - '0';
while (--fmtcnt >= 0) {
- c = *fmt++;
+ c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
if (c < '0' || c > '9')
break;
if ((width*10) / 10 != width) {
@@ -9688,7 +12022,7 @@ PyUnicode_Format(PyObject *format, PyObject *args)
if (c == '.') {
prec = 0;
if (--fmtcnt >= 0)
- c = *fmt++;
+ c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
if (c == '*') {
v = getnextarg(args, arglen, &argidx);
if (v == NULL)
@@ -9704,12 +12038,12 @@ PyUnicode_Format(PyObject *format, PyObject *args)
if (prec < 0)
prec = 0;
if (--fmtcnt >= 0)
- c = *fmt++;
+ c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
}
else if (c >= '0' && c <= '9') {
prec = c - '0';
while (--fmtcnt >= 0) {
- c = *fmt++;
+ c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
if (c < '0' || c > '9')
break;
if ((prec*10) / 10 != prec) {
@@ -9724,7 +12058,7 @@ PyUnicode_Format(PyObject *format, PyObject *args)
if (fmtcnt >= 0) {
if (c == 'h' || c == 'l' || c == 'L') {
if (--fmtcnt >= 0)
- c = *fmt++;
+ c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
}
}
if (fmtcnt < 0) {
@@ -9743,8 +12077,9 @@ PyUnicode_Format(PyObject *format, PyObject *args)
case '%':
pbuf = formatbuf;
+ kind = PyUnicode_4BYTE_KIND;
/* presume that buffer length is at least 1 */
- pbuf[0] = '%';
+ PyUnicode_WRITE(kind, pbuf, 0, '%');
len = 1;
break;
@@ -9773,8 +12108,13 @@ PyUnicode_Format(PyObject *format, PyObject *args)
goto onError;
}
}
- pbuf = PyUnicode_AS_UNICODE(temp);
- len = PyUnicode_GET_SIZE(temp);
+ if (PyUnicode_READY(temp) == -1) {
+ Py_CLEAR(temp);
+ goto onError;
+ }
+ pbuf = PyUnicode_DATA(temp);
+ kind = PyUnicode_KIND(temp);
+ len = PyUnicode_GET_LENGTH(temp);
if (prec >= 0 && len > prec)
len = prec;
break;
@@ -9803,8 +12143,13 @@ PyUnicode_Format(PyObject *format, PyObject *args)
Py_DECREF(iobj);
if (!temp)
goto onError;
- pbuf = PyUnicode_AS_UNICODE(temp);
- len = PyUnicode_GET_SIZE(temp);
+ if (PyUnicode_READY(temp) == -1) {
+ Py_CLEAR(temp);
+ goto onError;
+ }
+ pbuf = PyUnicode_DATA(temp);
+ kind = PyUnicode_KIND(temp);
+ len = PyUnicode_GET_LENGTH(temp);
sign = 1;
}
else {
@@ -9831,8 +12176,13 @@ PyUnicode_Format(PyObject *format, PyObject *args)
temp = formatfloat(v, flags, prec, c);
if (!temp)
goto onError;
- pbuf = PyUnicode_AS_UNICODE(temp);
- len = PyUnicode_GET_SIZE(temp);
+ if (PyUnicode_READY(temp) == -1) {
+ Py_CLEAR(temp);
+ goto onError;
+ }
+ pbuf = PyUnicode_DATA(temp);
+ kind = PyUnicode_KIND(temp);
+ len = PyUnicode_GET_LENGTH(temp);
sign = 1;
if (flags & F_ZERO)
fill = '0';
@@ -9840,6 +12190,7 @@ PyUnicode_Format(PyObject *format, PyObject *args)
case 'c':
pbuf = formatbuf;
+ kind = PyUnicode_4BYTE_KIND;
len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v);
if (len < 0)
goto onError;
@@ -9851,13 +12202,15 @@ PyUnicode_Format(PyObject *format, PyObject *args)
"at index %zd",
(31<=c && c<=126) ? (char)c : '?',
(int)c,
- (Py_ssize_t)(fmt - 1 -
- PyUnicode_AS_UNICODE(uformat)));
+ fmtpos - 1);
goto onError;
}
+ /* pbuf is initialized here. */
+ pindex = 0;
if (sign) {
- if (*pbuf == '-' || *pbuf == '+') {
- sign = *pbuf++;
+ if (PyUnicode_READ(kind, pbuf, pindex) == '-' ||
+ PyUnicode_READ(kind, pbuf, pindex) == '+') {
+ sign = PyUnicode_READ(kind, pbuf, pindex++);
len--;
}
else if (flags & F_SIGN)
@@ -9878,12 +12231,13 @@ PyUnicode_Format(PyObject *format, PyObject *args)
PyErr_NoMemory();
goto onError;
}
- if (_PyUnicode_Resize(&result, reslen) < 0) {
+ res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4));
+ if (res0 == 0) {
+ PyErr_NoMemory();
Py_XDECREF(temp);
goto onError;
}
- res = PyUnicode_AS_UNICODE(result)
- + reslen - rescnt;
+ res = res0 + reslen - rescnt;
}
if (sign) {
if (fill != ' ')
@@ -9893,11 +12247,11 @@ PyUnicode_Format(PyObject *format, PyObject *args)
width--;
}
if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) {
- assert(pbuf[0] == '0');
- assert(pbuf[1] == c);
+ assert(PyUnicode_READ(kind, pbuf, pindex) == '0');
+ assert(PyUnicode_READ(kind, pbuf, pindex+1) == c);
if (fill != ' ') {
- *res++ = *pbuf++;
- *res++ = *pbuf++;
+ *res++ = PyUnicode_READ(kind, pbuf, pindex++);
+ *res++ = PyUnicode_READ(kind, pbuf, pindex++);
}
rescnt -= 2;
width -= 2;
@@ -9915,15 +12269,18 @@ PyUnicode_Format(PyObject *format, PyObject *args)
if (sign)
*res++ = sign;
if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) {
- assert(pbuf[0] == '0');
- assert(pbuf[1] == c);
- *res++ = *pbuf++;
- *res++ = *pbuf++;
+ assert(PyUnicode_READ(kind, pbuf, pindex) == '0');
+ assert(PyUnicode_READ(kind, pbuf, pindex+1) == c);
+ *res++ = PyUnicode_READ(kind, pbuf, pindex++);
+ *res++ = PyUnicode_READ(kind, pbuf, pindex++);
}
}
- Py_UNICODE_COPY(res, pbuf, len);
- res += len;
- rescnt -= len;
+ /* Copy all characters, preserving len */
+ len1 = len;
+ while (len1--) {
+ *res++ = PyUnicode_READ(kind, pbuf, pindex++);
+ rescnt--;
+ }
while (--width >= len) {
--rescnt;
*res++ = ' ';
@@ -9943,8 +12300,17 @@ PyUnicode_Format(PyObject *format, PyObject *args)
goto onError;
}
- if (_PyUnicode_Resize(&result, reslen - rescnt) < 0)
+
+ for (max=0, res = res0; res < res0+reslen-rescnt; res++)
+ if (*res > max)
+ max = *res;
+ result = PyUnicode_New(reslen - rescnt, max);
+ if (!result)
goto onError;
+ kind = PyUnicode_KIND(result);
+ for (res = res0; res < res0+reslen-rescnt; res++)
+ PyUnicode_WRITE(kind, PyUnicode_DATA(result), res-res0, *res);
+ PyMem_Free(res0);
if (args_owned) {
Py_DECREF(args);
}
@@ -9952,7 +12318,7 @@ PyUnicode_Format(PyObject *format, PyObject *args)
return (PyObject *)result;
onError:
- Py_XDECREF(result);
+ PyMem_Free(res0);
Py_DECREF(uformat);
if (args_owned) {
Py_DECREF(args);
@@ -9977,7 +12343,7 @@ unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
kwlist, &x, &encoding, &errors))
return NULL;
if (x == NULL)
- return (PyObject *)_PyUnicode_New(0);
+ return (PyObject *)PyUnicode_New(0, 0);
if (encoding == NULL && errors == NULL)
return PyObject_Str(x);
else
@@ -9989,29 +12355,54 @@ unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyUnicodeObject *tmp, *pnew;
Py_ssize_t n;
+ PyObject *err = NULL;
assert(PyType_IsSubtype(type, &PyUnicode_Type));
tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds);
if (tmp == NULL)
return NULL;
assert(PyUnicode_Check(tmp));
- pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length);
+ // TODO: Verify the PyUnicode_GET_SIZE does the right thing.
+ // it seems kind of strange that tp_alloc gets passed the size
+ // of the unicode string because there will follow another
+ // malloc.
+ pnew = (PyUnicodeObject *) type->tp_alloc(type,
+ n = PyUnicode_GET_SIZE(tmp));
if (pnew == NULL) {
Py_DECREF(tmp);
return NULL;
}
- pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1));
- if (pnew->str == NULL) {
- _Py_ForgetReference((PyObject *)pnew);
- PyObject_Del(pnew);
- Py_DECREF(tmp);
- return PyErr_NoMemory();
+ _PyUnicode_WSTR(pnew) = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1));
+ if (_PyUnicode_WSTR(pnew) == NULL) {
+ err = PyErr_NoMemory();
+ goto onError;
+ }
+ Py_UNICODE_COPY(_PyUnicode_WSTR(pnew), PyUnicode_AS_UNICODE(tmp), n+1);
+ _PyUnicode_WSTR_LENGTH(pnew) = n;
+ _PyUnicode_HASH(pnew) = _PyUnicode_HASH(tmp);
+ _PyUnicode_STATE(pnew).interned = 0;
+ _PyUnicode_STATE(pnew).kind = 0;
+ _PyUnicode_STATE(pnew).compact = 0;
+ _PyUnicode_STATE(pnew).ready = 0;
+ _PyUnicode_STATE(pnew).ascii = 0;
+ pnew->data.any = NULL;
+ _PyUnicode_LENGTH(pnew) = 0;
+ pnew->_base.utf8 = NULL;
+ pnew->_base.utf8_length = 0;
+
+ if (PyUnicode_READY(pnew) == -1) {
+ PyObject_FREE(_PyUnicode_WSTR(pnew));
+ goto onError;
}
- Py_UNICODE_COPY(pnew->str, tmp->str, n+1);
- pnew->length = n;
- pnew->hash = tmp->hash;
+
Py_DECREF(tmp);
return (PyObject *)pnew;
+
+ onError:
+ _Py_ForgetReference((PyObject *)pnew);
+ PyObject_Del(pnew);
+ Py_DECREF(tmp);
+ return err;
}
PyDoc_STRVAR(unicode_doc,
@@ -10074,7 +12465,7 @@ void _PyUnicode_Init(void)
int i;
/* XXX - move this array to unicodectype.c ? */
- Py_UNICODE linebreak[] = {
+ Py_UCS2 linebreak[] = {
0x000A, /* LINE FEED */
0x000D, /* CARRIAGE RETURN */
0x001C, /* FILE SEPARATOR */
@@ -10086,11 +12477,9 @@ void _PyUnicode_Init(void)
};
/* Init the implementation */
- free_list = NULL;
- numfree = 0;
- unicode_empty = _PyUnicode_New(0);
+ unicode_empty = (PyUnicodeObject *) PyUnicode_New(0, 0);
if (!unicode_empty)
- return;
+ Py_FatalError("Can't create empty string");
for (i = 0; i < 256; i++)
unicode_latin1[i] = NULL;
@@ -10099,8 +12488,8 @@ void _PyUnicode_Init(void)
/* initialize the linebreak bloom filter */
bloom_linebreak = make_bloom_mask(
- linebreak, sizeof(linebreak) / sizeof(linebreak[0])
- );
+ PyUnicode_2BYTE_KIND, linebreak,
+ sizeof(linebreak) / sizeof(linebreak[0]));
PyType_Ready(&EncodingMapType);
}
@@ -10110,21 +12499,7 @@ void _PyUnicode_Init(void)
int
PyUnicode_ClearFreeList(void)
{
- int freelist_size = numfree;
- PyUnicodeObject *u;
-
- for (u = free_list; u != NULL;) {
- PyUnicodeObject *v = u;
- u = *(PyUnicodeObject **)u;
- if (v->str)
- PyObject_DEL(v->str);
- Py_XDECREF(v->defenc);
- PyObject_Del(v);
- numfree--;
- }
- free_list = NULL;
- assert(numfree == 0);
- return freelist_size;
+ return 0;
}
void
@@ -10158,6 +12533,10 @@ PyUnicode_InternInPlace(PyObject **p)
return;
if (PyUnicode_CHECK_INTERNED(s))
return;
+ if (PyUnicode_READY(s) == -1) {
+ assert(0 && "ready fail in intern...");
+ return;
+ }
if (interned == NULL) {
interned = PyDict_New();
if (interned == NULL) {
@@ -10189,15 +12568,17 @@ PyUnicode_InternInPlace(PyObject **p)
/* The two references in interned are not counted by refcnt.
The deallocator will take care of this */
Py_REFCNT(s) -= 2;
- PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL;
+ _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL;
}
void
PyUnicode_InternImmortal(PyObject **p)
{
+ PyUnicodeObject *u = (PyUnicodeObject *)*p;
+
PyUnicode_InternInPlace(p);
if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) {
- PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL;
+ _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL;
Py_INCREF(*p);
}
}
@@ -10238,22 +12619,24 @@ _Py_ReleaseInternedUnicodeStrings(void)
n);
for (i = 0; i < n; i++) {
s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i);
- switch (s->state) {
+ if (PyUnicode_READY(s) == -1)
+ fprintf(stderr, "could not ready string\n");
+ switch (PyUnicode_CHECK_INTERNED(s)) {
case SSTATE_NOT_INTERNED:
/* XXX Shouldn't happen */
break;
case SSTATE_INTERNED_IMMORTAL:
Py_REFCNT(s) += 1;
- immortal_size += s->length;
+ immortal_size += PyUnicode_GET_LENGTH(s);
break;
case SSTATE_INTERNED_MORTAL:
Py_REFCNT(s) += 2;
- mortal_size += s->length;
+ mortal_size += PyUnicode_GET_LENGTH(s);
break;
default:
Py_FatalError("Inconsistent interned string state.");
}
- s->state = SSTATE_NOT_INTERNED;
+ _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED;
}
fprintf(stderr, "total size of all interned strings: "
"%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d "
@@ -10300,9 +12683,11 @@ unicodeiter_next(unicodeiterobject *it)
return NULL;
assert(PyUnicode_Check(seq));
- if (it->it_index < PyUnicode_GET_SIZE(seq)) {
- item = PyUnicode_FromUnicode(
- PyUnicode_AS_UNICODE(seq)+it->it_index, 1);
+ if (it->it_index < PyUnicode_GET_LENGTH(seq)) {
+ int kind = PyUnicode_KIND(seq);
+ void *data = PyUnicode_DATA(seq);
+ Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index);
+ item = PyUnicode_FromOrdinal(chr);
if (item != NULL)
++it->it_index;
return item;
@@ -10372,6 +12757,8 @@ unicode_iter(PyObject *seq)
PyErr_BadInternalCall();
return NULL;
}
+ if (PyUnicode_READY(seq) == -1)
+ return NULL;
it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type);
if (it == NULL)
return NULL;
@@ -10382,95 +12769,16 @@ unicode_iter(PyObject *seq)
return (PyObject *)it;
}
-size_t
-Py_UNICODE_strlen(const Py_UNICODE *u)
-{
- int res = 0;
- while(*u++)
- res++;
- return res;
-}
-
-Py_UNICODE*
-Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2)
-{
- Py_UNICODE *u = s1;
- while ((*u++ = *s2++));
- return s1;
-}
-
-Py_UNICODE*
-Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n)
-{
- Py_UNICODE *u = s1;
- while ((*u++ = *s2++))
- if (n-- == 0)
- break;
- return s1;
-}
-
-Py_UNICODE*
-Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2)
-{
- Py_UNICODE *u1 = s1;
- u1 += Py_UNICODE_strlen(u1);
- Py_UNICODE_strcpy(u1, s2);
- return s1;
-}
-
-int
-Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2)
-{
- while (*s1 && *s2 && *s1 == *s2)
- s1++, s2++;
- if (*s1 && *s2)
- return (*s1 < *s2) ? -1 : +1;
- if (*s1)
- return 1;
- if (*s2)
- return -1;
- return 0;
-}
-
-int
-Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n)
-{
- register Py_UNICODE u1, u2;
- for (; n != 0; n--) {
- u1 = *s1;
- u2 = *s2;
- if (u1 != u2)
- return (u1 < u2) ? -1 : +1;
- if (u1 == '\0')
- return 0;
- s1++;
- s2++;
- }
- return 0;
-}
-
-Py_UNICODE*
-Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c)
-{
- const Py_UNICODE *p;
- for (p = s; *p; p++)
- if (*p == c)
- return (Py_UNICODE*)p;
- return NULL;
-}
-
-Py_UNICODE*
-Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c)
-{
- const Py_UNICODE *p;
- p = s + Py_UNICODE_strlen(s);
- while (p != s) {
- p--;
- if (*p == c)
- return (Py_UNICODE*)p;
- }
- return NULL;
-}
+#define UNIOP(x) Py_UNICODE_##x
+#define UNIOP_t Py_UNICODE
+#include "uniops.h"
+#undef UNIOP
+#undef UNIOP_t
+#define UNIOP(x) Py_UCS4_##x
+#define UNIOP_t Py_UCS4
+#include "uniops.h"
+#undef UNIOP
+#undef UNIOP_t
Py_UNICODE*
PyUnicode_AsUnicodeCopy(PyObject *object)
@@ -10479,6 +12787,10 @@ PyUnicode_AsUnicodeCopy(PyObject *object)
Py_UNICODE *copy;
Py_ssize_t size;
+ if (!PyUnicode_Check(unicode)) {
+ PyErr_BadArgument();
+ return NULL;
+ }
/* Ensure we won't overflow the size. */
if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) {
PyErr_NoMemory();
diff --git a/Objects/uniops.h b/Objects/uniops.h
new file mode 100644
index 0000000..06a0b4e
--- /dev/null
+++ b/Objects/uniops.h
@@ -0,0 +1,91 @@
+
+size_t
+UNIOP(strlen)(const UNIOP_t *u)
+{
+ int res = 0;
+ while(*u++)
+ res++;
+ return res;
+}
+
+UNIOP_t*
+UNIOP(strcpy)(UNIOP_t *s1, const UNIOP_t *s2)
+{
+ UNIOP_t *u = s1;
+ while ((*u++ = *s2++));
+ return s1;
+}
+
+UNIOP_t*
+UNIOP(strncpy)(UNIOP_t *s1, const UNIOP_t *s2, size_t n)
+{
+ UNIOP_t *u = s1;
+ while ((*u++ = *s2++))
+ if (n-- == 0)
+ break;
+ return s1;
+}
+
+UNIOP_t*
+UNIOP(strcat)(UNIOP_t *s1, const UNIOP_t *s2)
+{
+ UNIOP_t *u1 = s1;
+ u1 += UNIOP(strlen(u1));
+ UNIOP(strcpy(u1, s2));
+ return s1;
+}
+
+int
+UNIOP(strcmp)(const UNIOP_t *s1, const UNIOP_t *s2)
+{
+ while (*s1 && *s2 && *s1 == *s2)
+ s1++, s2++;
+ if (*s1 && *s2)
+ return (*s1 < *s2) ? -1 : +1;
+ if (*s1)
+ return 1;
+ if (*s2)
+ return -1;
+ return 0;
+}
+
+int
+UNIOP(strncmp)(const UNIOP_t *s1, const UNIOP_t *s2, size_t n)
+{
+ register UNIOP_t u1, u2;
+ for (; n != 0; n--) {
+ u1 = *s1;
+ u2 = *s2;
+ if (u1 != u2)
+ return (u1 < u2) ? -1 : +1;
+ if (u1 == '\0')
+ return 0;
+ s1++;
+ s2++;
+ }
+ return 0;
+}
+
+UNIOP_t*
+UNIOP(strchr)(const UNIOP_t *s, UNIOP_t c)
+{
+ const UNIOP_t *p;
+ for (p = s; *p; p++)
+ if (*p == c)
+ return (UNIOP_t*)p;
+ return NULL;
+}
+
+UNIOP_t*
+UNIOP(strrchr)(const UNIOP_t *s, UNIOP_t c)
+{
+ const UNIOP_t *p;
+ p = s + UNIOP(strlen)(s);
+ while (p != s) {
+ p--;
+ if (*p == c)
+ return (UNIOP_t*)p;
+ }
+ return NULL;
+}
+