From e4c0eb1720dacb37c7d3aeee389771565e30808c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 28 Aug 2010 13:59:37 +0200 Subject: [PATCH] Create Py_UNICODE_strdup() function --- Include/unicodeobject.h | 8 ++++++++ Objects/unicodeobject.c | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 0 deletions(-) diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h index 5d9307b..8476a55 100644 --- a/Include/unicodeobject.h +++ b/Include/unicodeobject.h @@ -1634,6 +1634,14 @@ PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strrchr( const Py_UNICODE *s, Py_UNICODE c ); +/* Create a copy of a unicode string ending with a nul character. + Return NULL on memory error, otherwise return a new allocated buffer: + use PyMem_Free() to free the buffer. */ + +PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strdup( + PyObject *unicode + ); + #ifdef __cplusplus } #endif diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 13b2608..c4ac30c 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -10018,6 +10018,22 @@ Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) return NULL; } +Py_UNICODE* +Py_UNICODE_strdup(PyObject *object) +{ + PyUnicodeObject *unicode = (PyUnicodeObject *)object; + Py_UNICODE *copy; + Py_ssize_t size; + size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ + size *= sizeof(Py_UNICODE); + copy = PyMem_Malloc(size); + if (copy == NULL) { + PyErr_NoMemory(); + return NULL; + } + memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); + return copy; +} #ifdef __cplusplus } -- 1.6.2.5