Skip to content

Commit

Permalink
Use UTF-8 string caching introduced with Py3.2.
Browse files Browse the repository at this point in the history
According to the Python documentation, calling `PyUnicode_AsUTF8AndSize`
will cache a UTF-8 version of the python string for future use. So let's
do that instead of reencoding UTF-16 or UTF-32 to UTF-8 each call to
`PyUnicode_AsSTString`.
  • Loading branch information
Hoikas committed May 16, 2020
1 parent bddc231 commit cd03839
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,10 @@ You can contact Cyan Worlds, Inc. by email [email protected]
ST::string PyUnicode_AsSTString(PyObject* obj)
{
if (PyUnicode_Check(obj)) {
#if (Py_UNICODE_SIZE == 2)
return ST::string::from_utf16(reinterpret_cast<const char16_t *>(PyUnicode_AsUnicode(obj)));
#elif (Py_UNICODE_SIZE == 4)
return ST::string::from_utf32(reinterpret_cast<const char32_t *>(PyUnicode_AsUnicode(obj)));
#else
# error "Py_UNICODE is an unexpected size"
#endif
Py_ssize_t size;
const char* str = PyUnicode_AsUTF8AndSize(obj, &size);
if (str)
return ST::string::from_utf8(str, size, ST::assume_valid);
}

return ST::null;
Expand Down

0 comments on commit cd03839

Please sign in to comment.