Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <cassert>
#include <cstring>

#include <react/debug/react_native_assert.h>
#include <react/renderer/core/RawPropsPrimitives.h>

namespace facebook::react {
Expand All @@ -20,34 +19,29 @@ void RawPropsKey::render(char* buffer, RawPropsPropNameLength* length)
const noexcept {
*length = 0;

// Prefix
constexpr size_t maxLength = kPropNameLengthHardCap - 1;

auto appendSegment = [&](const char* segment) {
auto copyLen = std::min(std::strlen(segment), maxLength - *length);
std::memcpy(buffer + *length, segment, copyLen);
*length += static_cast<RawPropsPropNameLength>(copyLen);
};

if (prefix != nullptr) {
auto prefixLength =
static_cast<RawPropsPropNameLength>(std::strlen(prefix));
std::memcpy(buffer, prefix, prefixLength);
*length = prefixLength;
appendSegment(prefix);
}

// Name
auto nameLength = static_cast<RawPropsPropNameLength>(std::strlen(name));
std::memcpy(buffer + *length, name, nameLength);
*length += nameLength;
appendSegment(name);

// Suffix
if (suffix != nullptr) {
auto suffixLength =
static_cast<RawPropsPropNameLength>(std::strlen(suffix));
std::memcpy(buffer + *length, suffix, suffixLength);
*length += suffixLength;
appendSegment(suffix);
}
react_native_assert(*length < kPropNameLengthHardCap);
}

RawPropsKey::operator std::string() const noexcept {
auto buffer = std::array<char, kPropNameLengthHardCap>();
RawPropsPropNameLength length = 0;
render(buffer.data(), &length);
react_native_assert(length < kPropNameLengthHardCap);
return std::string{buffer.data(), length};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void RawPropsKeyMap::reindex() noexcept {

buckets_.resize(kPropNameLengthHardCap);

auto length = RawPropsPropNameLength{0};
RawPropsPropNameLength length = 0;
for (size_t i = 0; i < items_.size(); i++) {
auto& item = items_[i];
if (item.length != length) {
Expand All @@ -96,6 +96,9 @@ RawPropsValueIndex RawPropsKeyMap::at(
RawPropsPropNameLength length) noexcept {
react_native_assert(length > 0);
react_native_assert(length < kPropNameLengthHardCap);
if (length == 0 || length >= kPropNameLengthHardCap) [[unlikely]] {
return kRawPropsValueIndexEmpty;
}
// 1. Find the bucket.
auto lower = int{buckets_[length - 1]};
auto upper = int{buckets_[length]} - 1;
Expand Down
Loading