// mutations void StringData::setChar(int offset, CStrRef substring) { ASSERT(!isStatic()); if (offset >= 0) { StringSlice s = slice(); if (s.len == 0) { // PHP will treat data as an array and we don't want to follow that. throw OffsetOutOfRangeException(); } if (uint32_t(offset) < s.len) { char* buf = (char*)s.ptr; buf[offset] = substring.empty() ? 0 : substring.data()[0]; } else if (offset <= RuntimeOption::StringOffsetLimit) { // We are mutating, so we don't need to repropagate our own taint int newlen = offset + 1; char *buf = (char *)Util::safe_malloc(newlen + 1); memcpy(buf, s.ptr, s.len); memset(buf + s.len, ' ', newlen - s.len); buf[newlen] = 0; buf[offset] = substring.empty() ? 0 : substring.data()[0]; attach(buf, newlen); } else { throw OffsetOutOfRangeException(); } m_hash = 0; // since we modified the string. } }
void StringData::setChar(int offset, CStrRef substring) { ASSERT(!isStatic()); if (offset >= 0) { int len = size(); if (len == 0) { // PHP will treat data as an array and we don't want to follow that. throw OffsetOutOfRangeException(); } if (offset < len) { if (!substring.empty()) { setChar(offset, substring.data()[0]); } else { removeChar(offset); } } else if (offset > RuntimeOption::StringOffsetLimit) { throw OffsetOutOfRangeException(); } else { int newlen = offset + 1; char *buf = (char *)Util::safe_malloc(newlen + 1); memset(buf, ' ', newlen); buf[newlen] = 0; memcpy(buf, data(), len); if (!substring.empty()) buf[offset] = substring.data()[0]; assign(buf, newlen, AttachString); } } }
// mutations void StringData::setChar(int offset, CStrRef substring) { assert(!isStatic()); if (offset >= 0) { StringSlice s = slice(); if (s.len == 0) { // PHP will treat data as an array and we don't want to follow that. throw OffsetOutOfRangeException(); } char c = substring.empty() ? 0 : substring.data()[0]; if (uint32_t(offset) < s.len) { ((char*)s.ptr)[offset] = c; } else if (offset <= RuntimeOption::StringOffsetLimit) { uint32_t newlen = offset + 1; MutableSlice buf = isImmutable() ? escalate(newlen) : reserve(newlen); memset(buf.ptr + s.len, ' ', newlen - s.len); buf.ptr[offset] = c; setSize(newlen); } else { throw OffsetOutOfRangeException(); } m_hash = 0; // since we modified the string. } }