Files
react-native/ReactCommon/react/renderer/core/RawPropsKeyMap.cpp
T
Andrew Coates 4d87d8c6b2 Fix various c++ warnings (#31399)
Summary:
react-native-windows runs with a more strict set of warnings as errors.  This fixes a bunch of warnings being hit while compiling core react-native code as part of react-native-windows.  In particular warnings about mismatched signed/unsigned comparisons, lossy conversions, and variable names that conflict with names in outer scopes (yoga has a global for `leading` and `trailing` that conflicts with some local variable names)

## Changelog

[Internal] [Fixed] - Fix various C++ warnings

Pull Request resolved: https://github.com/facebook/react-native/pull/31399

Test Plan: I've run these changes in react-native-windows. -- Shouldn't have any functionality difference.

Reviewed By: sammy-SC

Differential Revision: D28290188

Pulled By: rozele

fbshipit-source-id: 2f7cf87f58d73a3f43510ac888dbcb9ab177d134
2021-05-12 12:35:33 -07:00

109 lines
3.1 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "RawPropsKeyMap.h"
#include <react/debug/react_native_assert.h>
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <cstring>
namespace facebook {
namespace react {
bool RawPropsKeyMap::hasSameName(Item const &lhs, Item const &rhs) noexcept {
return lhs.length == rhs.length &&
(std::memcmp(lhs.name, rhs.name, lhs.length) == 0);
}
bool RawPropsKeyMap::shouldFirstOneBeBeforeSecondOne(
Item const &lhs,
Item const &rhs) noexcept {
if (lhs.length != rhs.length) {
return lhs.length < rhs.length;
}
return std::memcmp(lhs.name, rhs.name, rhs.length) < 0;
}
void RawPropsKeyMap::insert(
RawPropsKey const &key,
RawPropsValueIndex value) noexcept {
auto item = Item{};
item.value = value;
key.render(item.name, &item.length);
items_.push_back(item);
}
void RawPropsKeyMap::reindex() noexcept {
// Sorting `items_` by property names length and then lexicographically.
// Note, sort algorithm must be stable.
std::stable_sort(
items_.begin(),
items_.end(),
&RawPropsKeyMap::shouldFirstOneBeBeforeSecondOne);
// Filtering out duplicating keys.
// If some `*Props` object requests a prop more than once, only the first
// request will be fulfilled. E.g. `TextInputProps` class has a sub-property
// `backgroundColor` twice, the first time as part of `ViewProps` base-class
// and the second as part of `BaseTextProps` base-class. In this
// configuration, the only one which comes first (from `ViewProps`, which
// appear first) will be assigned.
items_.erase(
std::unique(items_.begin(), items_.end(), &RawPropsKeyMap::hasSameName),
items_.end());
buckets_.resize(kPropNameLengthHardCap);
auto length = RawPropsPropNameLength{0};
for (size_t i = 0; i < items_.size(); i++) {
auto &item = items_[i];
if (item.length != length) {
for (auto j = length; j < item.length; j++) {
buckets_[j] = static_cast<RawPropsPropNameLength>(i);
}
length = item.length;
}
}
for (auto j = length; j < buckets_.size(); j++) {
buckets_[j] = static_cast<RawPropsPropNameLength>(items_.size());
}
}
RawPropsValueIndex RawPropsKeyMap::at(
char const *name,
RawPropsPropNameLength length) noexcept {
react_native_assert(length > 0);
react_native_assert(length < kPropNameLengthHardCap);
// 1. Find the bucket.
auto lower = int{buckets_[length - 1]};
auto upper = int{buckets_[length]} - 1;
react_native_assert(lower - 1 <= upper);
// 2. Binary search in the bucket.
while (lower <= upper) {
auto median = (lower + upper) / 2;
auto condition = std::memcmp(items_[median].name, name, length);
if (condition < 0) {
lower = median + 1;
} else if (condition == 0) {
return items_[median].value;
} else /* if (condition > 0) */ {
upper = median - 1;
}
}
return kRawPropsValueIndexEmpty;
}
} // namespace react
} // namespace facebook