mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-11 08:03:55 +00:00
Avoid extra allocations in operator str/bytes
This commit is contained in:
parent
35c51c477b
commit
fd6cede7e9
@ -364,8 +364,7 @@ public:
|
|||||||
}
|
}
|
||||||
char *buffer;
|
char *buffer;
|
||||||
ssize_t length;
|
ssize_t length;
|
||||||
int err = PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), &buffer, &length);
|
if (PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), &buffer, &length))
|
||||||
if (err == -1)
|
|
||||||
pybind11_fail("Unable to extract string contents! (invalid type)");
|
pybind11_fail("Unable to extract string contents! (invalid type)");
|
||||||
return std::string(buffer, (size_t) length);
|
return std::string(buffer, (size_t) length);
|
||||||
}
|
}
|
||||||
@ -394,8 +393,7 @@ public:
|
|||||||
operator std::string() const {
|
operator std::string() const {
|
||||||
char *buffer;
|
char *buffer;
|
||||||
ssize_t length;
|
ssize_t length;
|
||||||
int err = PYBIND11_BYTES_AS_STRING_AND_SIZE(m_ptr, &buffer, &length);
|
if (PYBIND11_BYTES_AS_STRING_AND_SIZE(m_ptr, &buffer, &length))
|
||||||
if (err == -1)
|
|
||||||
pybind11_fail("Unable to extract bytes contents!");
|
pybind11_fail("Unable to extract bytes contents!");
|
||||||
return std::string(buffer, (size_t) length);
|
return std::string(buffer, (size_t) length);
|
||||||
}
|
}
|
||||||
@ -404,11 +402,31 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
inline str::operator bytes() const {
|
inline str::operator bytes() const {
|
||||||
return bytes((std::string) *this);
|
object temp = *this;
|
||||||
|
if (PyUnicode_Check(m_ptr)) {
|
||||||
|
temp = object(PyUnicode_AsUTF8String(m_ptr), false);
|
||||||
|
if (!temp)
|
||||||
|
pybind11_fail("Unable to extract string contents! (encoding issue)");
|
||||||
|
}
|
||||||
|
char *buffer;
|
||||||
|
ssize_t length;
|
||||||
|
if (PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), &buffer, &length))
|
||||||
|
pybind11_fail("Unable to extract string contents! (invalid type)");
|
||||||
|
auto obj = object(PYBIND11_BYTES_FROM_STRING_AND_SIZE(buffer, length), false);
|
||||||
|
if (!obj)
|
||||||
|
pybind11_fail("Could not allocate bytes object!");
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bytes::operator pybind11::str() const {
|
inline bytes::operator pybind11::str() const {
|
||||||
return pybind11::str((std::string) *this);
|
char *buffer;
|
||||||
|
ssize_t length;
|
||||||
|
if (PYBIND11_BYTES_AS_STRING_AND_SIZE(m_ptr, &buffer, &length))
|
||||||
|
pybind11_fail("Unable to extract bytes contents!");
|
||||||
|
auto obj = object(PyUnicode_FromStringAndSize(buffer, (ssize_t) length), false);
|
||||||
|
if (!obj)
|
||||||
|
pybind11_fail("Could not allocate string object!");
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
class none : public object {
|
class none : public object {
|
||||||
|
Loading…
Reference in New Issue
Block a user