Skip to content

Commit bb6bbfc

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
remove folly::tryTo (facebook#48557)
Summary: Pull Request resolved: facebook#48557 changelog: [internal] delete use of folly::tryTo from react native. Reviewed By: christophpurrer Differential Revision: D67942789 fbshipit-source-id: 976caa12b6ff6063041be3259aa8ebd642ca3ca0
1 parent 1051bd8 commit bb6bbfc

File tree

1 file changed

+33
-13
lines changed
  • packages/react-native/ReactCommon/react/renderer/components/view

1 file changed

+33
-13
lines changed

packages/react-native/ReactCommon/react/renderer/components/view/conversions.h

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <algorithm>
3232
#include <cmath>
3333
#include <optional>
34+
#include <string>
3435
#include <unordered_map>
3536

3637
namespace facebook::react {
@@ -64,6 +65,25 @@ inline float yogaFloatFromFloat(Float value) {
6465
return (float)value;
6566
}
6667

68+
/*
69+
* Converts string to float only if the entire string is valid float.
70+
*/
71+
inline std::optional<float> stringToFloat(const std::string& string) {
72+
try {
73+
size_t pos = 0;
74+
auto result = std::stof(string, &pos);
75+
// Check if entire string was valid
76+
if (pos == string.length()) {
77+
return result;
78+
}
79+
} catch (...) {
80+
// Ignore, caller falls back to default value.
81+
return std::nullopt;
82+
}
83+
84+
return std::nullopt;
85+
}
86+
6787
/*
6888
* `yoga::FloatOptional` <-> React Native's `Float`
6989
*
@@ -467,15 +487,15 @@ inline void fromRawValue(
467487
return;
468488
} else {
469489
if (stringValue.back() == '%') {
470-
auto tryValue = folly::tryTo<float>(
471-
std::string_view(stringValue).substr(0, stringValue.length() - 1));
472-
if (tryValue.hasValue()) {
490+
auto tryValue =
491+
stringToFloat(stringValue.substr(0, stringValue.length() - 1));
492+
if (tryValue.has_value()) {
473493
result = yoga::StyleSizeLength::percent(tryValue.value());
474494
return;
475495
}
476496
} else {
477-
auto tryValue = folly::tryTo<float>(stringValue);
478-
if (tryValue.hasValue()) {
497+
auto tryValue = stringToFloat(stringValue);
498+
if (tryValue.has_value()) {
479499
result = yoga::StyleSizeLength::points(tryValue.value());
480500
return;
481501
}
@@ -499,15 +519,15 @@ inline void fromRawValue(
499519
return;
500520
} else {
501521
if (stringValue.back() == '%') {
502-
auto tryValue = folly::tryTo<float>(
503-
std::string_view(stringValue).substr(0, stringValue.length() - 1));
504-
if (tryValue.hasValue()) {
522+
auto tryValue =
523+
stringToFloat(stringValue.substr(0, stringValue.length() - 1));
524+
if (tryValue.has_value()) {
505525
result = yoga::StyleLength::percent(tryValue.value());
506526
return;
507527
}
508528
} else {
509-
auto tryValue = folly::tryTo<float>(stringValue);
510-
if (tryValue.hasValue()) {
529+
auto tryValue = stringToFloat(stringValue);
530+
if (tryValue.has_value()) {
511531
result = yoga::StyleLength::points(tryValue.value());
512532
return;
513533
}
@@ -573,9 +593,9 @@ inline void fromRawValue(
573593
const auto stringValue = (std::string)value;
574594

575595
if (stringValue.back() == '%') {
576-
auto tryValue = folly::tryTo<float>(
577-
std::string_view(stringValue).substr(0, stringValue.length() - 1));
578-
if (tryValue.hasValue()) {
596+
auto tryValue =
597+
stringToFloat(stringValue.substr(0, stringValue.length() - 1));
598+
if (tryValue.has_value()) {
579599
valueUnit = ValueUnit(tryValue.value(), UnitType::Percent);
580600
}
581601
}

0 commit comments

Comments
 (0)