diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-09-29 14:03:49 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-10-01 13:41:41 +0200 |
commit | 0331cadf31cbb7d32ab8e094c00c745a644b76fe (patch) | |
tree | b6888e87dc97c8e3f211de0b3ecdf6e981b35e99 /sources | |
parent | 614787bfc8f0210e30beecc0f03c9fe772f9c139 (diff) |
libshiboken: Split Shiboken::String::toCString() into 2 overloads
Split out the rarely used version taking a Py_ssize_t *len
for clarity.
Change-Id: Ia0f39a6993558e8aae1523a9b54c67c94beec4ab
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources')
-rw-r--r-- | sources/shiboken6/libshiboken/sbkstring.cpp | 26 | ||||
-rw-r--r-- | sources/shiboken6/libshiboken/sbkstring.h | 3 |
2 files changed, 20 insertions, 9 deletions
diff --git a/sources/shiboken6/libshiboken/sbkstring.cpp b/sources/shiboken6/libshiboken/sbkstring.cpp index 43654d589..0a2cd5c60 100644 --- a/sources/shiboken6/libshiboken/sbkstring.cpp +++ b/sources/shiboken6/libshiboken/sbkstring.cpp @@ -107,23 +107,33 @@ PyObject *fromCString(const char *value, int len) return PyUnicode_FromStringAndSize(value, len); } -const char *toCString(PyObject *str, Py_ssize_t *len) +const char *toCString(PyObject *str) { if (str == Py_None) return nullptr; + if (PyUnicode_Check(str)) + return _PepUnicode_AsString(str); + if (PyBytes_Check(str)) + return PyBytes_AS_STRING(str); + return nullptr; +} + +const char *toCString(PyObject *str, Py_ssize_t *len) +{ + if (str == Py_None) { + *len = 0; + return nullptr; + } if (PyUnicode_Check(str)) { - if (len) { - // We need to encode the unicode string into utf8 to know the size of returned char *. - Shiboken::AutoDecRef uniStr(PyUnicode_AsUTF8String(str)); - *len = PyBytes_GET_SIZE(uniStr.object()); - } + // We need to encode the unicode string into utf8 to know the size of returned char *. + Shiboken::AutoDecRef uniStr(PyUnicode_AsUTF8String(str)); + *len = PyBytes_GET_SIZE(uniStr.object()); // Return unicode from str instead of uniStr, because the lifetime of the returned pointer // depends on the lifetime of str. return _PepUnicode_AsString(str); } if (PyBytes_Check(str)) { - if (len) - *len = PyBytes_GET_SIZE(str); + *len = PyBytes_GET_SIZE(str); return PyBytes_AS_STRING(str); } return nullptr; diff --git a/sources/shiboken6/libshiboken/sbkstring.h b/sources/shiboken6/libshiboken/sbkstring.h index 4b416e16c..500149bed 100644 --- a/sources/shiboken6/libshiboken/sbkstring.h +++ b/sources/shiboken6/libshiboken/sbkstring.h @@ -55,7 +55,8 @@ namespace String LIBSHIBOKEN_API bool isConvertible(PyObject *obj); LIBSHIBOKEN_API PyObject *fromCString(const char *value); LIBSHIBOKEN_API PyObject *fromCString(const char *value, int len); - LIBSHIBOKEN_API const char *toCString(PyObject *str, Py_ssize_t *len = nullptr); + LIBSHIBOKEN_API const char *toCString(PyObject *str); + LIBSHIBOKEN_API const char *toCString(PyObject *str, Py_ssize_t *len); LIBSHIBOKEN_API bool concat(PyObject **val1, PyObject *val2); LIBSHIBOKEN_API PyObject *fromFormat(const char *format, ...); LIBSHIBOKEN_API PyObject *fromStringAndSize(const char *str, Py_ssize_t size); |