Update nul termination handling to avoid copies on iOS

Reviewed By: javache

Differential Revision: D3495998

fbshipit-source-id: 8d69ecc4777402a5d6b0e814af9f1862b47ecaa1
This commit is contained in:
Marc Horowitz 2016-06-30 16:41:27 -07:00 committed by Facebook Github Bot 6
parent e19225aa64
commit ccd608a4b6
2 changed files with 15 additions and 14 deletions

View File

@ -102,8 +102,12 @@ private:
class JSBigBufferString : public facebook::react::JSBigString {
public:
JSBigBufferString(size_t size)
: m_data(new char[size])
, m_size(size) {}
: m_data(new char[size + 1])
, m_size(size) {
// Guarantee nul-termination. The caller is responsible for
// filling in the rest of m_data.
m_data[m_size] = '\0';
}
~JSBigBufferString() {
delete[] m_data;

View File

@ -96,20 +96,17 @@ public:
return JSStringIsEqualToUTF8CString(m_string, utf8);
}
static String createExpectingAscii(const char* utf8, size_t len) {
#if WITH_FBJSCEXTENSIONS
return String(JSStringCreateWithUTF8CStringExpectAscii(utf8, len), true);
#else
return createExpectingAscii(std::string(utf8, len));
#endif
// This assumes ascii is nul-terminated.
static String createExpectingAscii(const char* ascii, size_t len) {
#if WITH_FBJSCEXTENSIONS
return String(JSStringCreateWithUTF8CStringExpectAscii(ascii, len), true);
#else
return String(JSStringCreateWithUTF8CString(ascii), true);
#endif
}
static String createExpectingAscii(std::string const &utf8) {
#if WITH_FBJSCEXTENSIONS
return createExpectingAscii(utf8.c_str(), utf8.size());
#else
return String(JSStringCreateWithUTF8CString(utf8.c_str()), true);
#endif
static String createExpectingAscii(std::string const &ascii) {
return createExpectingAscii(ascii.c_str(), ascii.size());
}
static String ref(JSStringRef string) {