Py::String should be constructible from ptr and length
At present, class Py::String can be constructed from a const char* or a const std::string&, but not from const char* and a size_t. We should have a version available that takes advantage of PyString_FromStringAndSize() by taking a length argument. This would allow construction from a non-null-terminated character array (especially a subset of such an array). Also, the Py::String constructor taking a const std::string& as an argument should not use the c_str() function to call PyString_FromString(). Rather, it should call PyString_FromStringAndSize( str.data(), str.length() ) // or str.size() since we've already gone through the trouble of figuring out the length of the contained string. -- Steven E. Harris Primus Knowledge Solutions, Inc. http://www.primus.com
These c'tor's have been in CXX for a while but did not make it into the first cut of checkins to SourceForge. THe c'tor's have been on SourceForge for a couple of weeks now. However as you suggest I should use std::string::size() and std::string::data() rather then c_str() and the implied strlen() call. These improvements have been committed to SourceForge. Barry
-----Original Message----- From: c++-sig-admin@python.org [mailto:c++-sig-admin@python.org]On Behalf Of Steve Harris Sent: 18 May 2000 18:49 To: c++-sig@python.org Subject: [C++-SIG] Py::String should be constructible from ptr and length
At present, class Py::String can be constructed from a const char* or a const std::string&, but not from const char* and a size_t. We should have a version available that takes advantage of PyString_FromStringAndSize() by taking a length argument. This would allow construction from a non-null-terminated character array (especially a subset of such an array).
Also, the Py::String constructor taking a const std::string& as an argument should not use the c_str() function to call PyString_FromString(). Rather, it should call
PyString_FromStringAndSize( str.data(), str.length() ) // or str.size()
since we've already gone through the trouble of figuring out the length of the contained string.
-- Steven E. Harris Primus Knowledge Solutions, Inc. http://www.primus.com
_______________________________________________ C++-SIG maillist - C++-SIG@python.org http://www.python.org/mailman/listinfo/c++-sig
participants (2)
-
Barry Scott -
Steve Harris