diff options
author | Walter Dörwald <walter@livinglogic.de> | 2007-05-05 12:00:46 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2007-05-05 12:00:46 (GMT) |
commit | acaa5a16d6cd9a94e7e111761264eef14a033d2c (patch) | |
tree | c2c2156bc64012da26a254caebdaf1a35b96f706 /Objects | |
parent | 1255ed62bfc2f9f8f5c50935c21cbe0a34e12cc7 (diff) | |
download | cpython-acaa5a16d6cd9a94e7e111761264eef14a033d2c.zip cpython-acaa5a16d6cd9a94e7e111761264eef14a033d2c.tar.gz cpython-acaa5a16d6cd9a94e7e111761264eef14a033d2c.tar.bz2 |
Add PyUnicode_FromString(), which create a unicode object from a
const char * (i.e. 0-terminated latin-1 encoded bytes).
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/unicodeobject.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 45c52cc..c9a922d 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -393,6 +393,51 @@ PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, return (PyObject *)unicode; } +PyObject *PyUnicode_FromString(const char *u) +{ + PyUnicodeObject *unicode; + Py_ssize_t size = strlen(u); + + /* 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; + } + } + + unicode = _PyUnicode_New(size); + if (!unicode) + return NULL; + + /* Copy the Unicode data into the new object */ + if (u != NULL) { + char *p = unicode->str; + while (*p++ = *u++) + ; + } + + return (PyObject *)unicode; +} + #ifdef HAVE_WCHAR_H PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |