Skip to content
Draft
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 @@ -83,6 +83,30 @@ const RawValue* RawPropsParser::at(
rawProps.keyIndexCursor_ = keyIndex;
}

return valueAtCursor(rawProps);
}

const RawValue* RawPropsParser::at(const RawProps& rawProps, const char* key)
const noexcept {
// In-order reads pass the same string literal that was stored for the next
// key when the parser was prepared. Recognize it by its address before
// measuring it: the terminator check confirms the length in O(1).
if (ready_ && !keys_.empty()) [[likely]] {
auto next = static_cast<size_t>(rawProps.keyIndexCursor_) + 1;
if (next >= keys_.size()) [[unlikely]] {
next = 0;
}
const auto& expected = keys_[next];
if (expected.data() == key && key[expected.size()] == '\0') {
rawProps.keyIndexCursor_ = static_cast<int>(next);
return valueAtCursor(rawProps);
}
}
return at(rawProps, std::string_view{key});
}

const RawValue* RawPropsParser::valueAtCursor(
const RawProps& rawProps) const noexcept {
auto valueIndex = rawProps.keyIndexToValueIndex_[rawProps.keyIndexCursor_];
return valueIndex == kRawPropsValueIndexEmpty ? nullptr
: &rawProps.values_[valueIndex];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class RawPropsParser final {
* To be used by `RawProps` only.
*/
const RawValue *at(const RawProps &rawProps, std::string_view key) const noexcept;
const RawValue *at(const RawProps &rawProps, const char *key) const noexcept;

const RawValue *valueAtCursor(const RawProps &rawProps) const noexcept;

mutable std::vector<std::string_view> keys_{};
mutable RawPropsKeyMap nameToIndex_{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,42 @@ TEST(RawPropsTest, handleRawPropsPrimitiveTypesGetOutOfOrder) {
EXPECT_EQ((bool)*raw.at("boolValue"), true);
}

TEST(RawPropsTest, handleRawPropsNamesFromOtherStorage) {
ContextContainer contextContainer{};
PropsParserContext parserContext{-1, contextContainer};

auto raw = RawProps(
folly::dynamic::object("intValue", (int)42)("doubleValue", (double)17.42)(
"floatValue", (float)66.67)("stringValue", "helloworld")(
"boolValue", true));

auto parser = RawPropsParser();
parser.prepare<PropsPrimitiveTypes>();
raw.parse(parser);

// Same characters at other addresses than the literals the parser was
// prepared with: they must be compared by value, in order and out of order.
const std::string intValue = "intValue";
const std::string doubleValue = "doubleValue";
const std::string floatValue = "floatValue";
const std::string stringValue = "stringValue";
const std::string boolValue = "boolValue";
// A longer name that starts with a prepared one must not match it.
const std::string intValueSuffixed = "intValueX";

EXPECT_EQ((int)*raw.at(intValue.c_str()), 42);
EXPECT_NEAR((double)*raw.at(doubleValue.c_str()), 17.42, 0.0001);
EXPECT_NEAR((float)*raw.at(floatValue.c_str()), 66.67, 0.00001);
EXPECT_STREQ(
((std::string)*raw.at(stringValue.c_str())).c_str(), "helloworld");
EXPECT_EQ((bool)*raw.at(boolValue.c_str()), true);

EXPECT_EQ((bool)*raw.at(boolValue.c_str()), true);
EXPECT_EQ((int)*raw.at(intValue.c_str()), 42);
EXPECT_EQ(raw.at(intValueSuffixed.c_str()), nullptr);
EXPECT_NEAR((double)*raw.at(doubleValue.c_str()), 17.42, 0.0001);
}

TEST(RawPropsTest, handleRawPropsPrimitiveTypesIncomplete) {
ContextContainer contextContainer{};
PropsParserContext parserContext{-1, contextContainer};
Expand Down
Loading