Skip to content

Commit

Permalink
remove folly::tryTo (#48557)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #48557

changelog: [internal]

delete use of folly::tryTo from react native.

Reviewed By: christophpurrer

Differential Revision: D67942789

fbshipit-source-id: 976caa12b6ff6063041be3259aa8ebd642ca3ca0
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Jan 10, 2025
1 parent 1051bd8 commit bb6bbfc
Showing 1 changed file with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <algorithm>
#include <cmath>
#include <optional>
#include <string>
#include <unordered_map>

namespace facebook::react {
Expand Down Expand Up @@ -64,6 +65,25 @@ inline float yogaFloatFromFloat(Float value) {
return (float)value;
}

/*
* Converts string to float only if the entire string is valid float.
*/
inline std::optional<float> stringToFloat(const std::string& string) {
try {
size_t pos = 0;
auto result = std::stof(string, &pos);
// Check if entire string was valid
if (pos == string.length()) {
return result;
}
} catch (...) {
// Ignore, caller falls back to default value.
return std::nullopt;
}

return std::nullopt;
}

/*
* `yoga::FloatOptional` <-> React Native's `Float`
*
Expand Down Expand Up @@ -467,15 +487,15 @@ inline void fromRawValue(
return;
} else {
if (stringValue.back() == '%') {
auto tryValue = folly::tryTo<float>(
std::string_view(stringValue).substr(0, stringValue.length() - 1));
if (tryValue.hasValue()) {
auto tryValue =
stringToFloat(stringValue.substr(0, stringValue.length() - 1));
if (tryValue.has_value()) {
result = yoga::StyleSizeLength::percent(tryValue.value());
return;
}
} else {
auto tryValue = folly::tryTo<float>(stringValue);
if (tryValue.hasValue()) {
auto tryValue = stringToFloat(stringValue);
if (tryValue.has_value()) {
result = yoga::StyleSizeLength::points(tryValue.value());
return;
}
Expand All @@ -499,15 +519,15 @@ inline void fromRawValue(
return;
} else {
if (stringValue.back() == '%') {
auto tryValue = folly::tryTo<float>(
std::string_view(stringValue).substr(0, stringValue.length() - 1));
if (tryValue.hasValue()) {
auto tryValue =
stringToFloat(stringValue.substr(0, stringValue.length() - 1));
if (tryValue.has_value()) {
result = yoga::StyleLength::percent(tryValue.value());
return;
}
} else {
auto tryValue = folly::tryTo<float>(stringValue);
if (tryValue.hasValue()) {
auto tryValue = stringToFloat(stringValue);
if (tryValue.has_value()) {
result = yoga::StyleLength::points(tryValue.value());
return;
}
Expand Down Expand Up @@ -573,9 +593,9 @@ inline void fromRawValue(
const auto stringValue = (std::string)value;

if (stringValue.back() == '%') {
auto tryValue = folly::tryTo<float>(
std::string_view(stringValue).substr(0, stringValue.length() - 1));
if (tryValue.hasValue()) {
auto tryValue =
stringToFloat(stringValue.substr(0, stringValue.length() - 1));
if (tryValue.has_value()) {
valueUnit = ValueUnit(tryValue.value(), UnitType::Percent);
}
}
Expand Down

0 comments on commit bb6bbfc

Please sign in to comment.