31
31
#include < algorithm>
32
32
#include < cmath>
33
33
#include < optional>
34
+ #include < string>
34
35
#include < unordered_map>
35
36
36
37
namespace facebook ::react {
@@ -64,6 +65,25 @@ inline float yogaFloatFromFloat(Float value) {
64
65
return (float )value;
65
66
}
66
67
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
+
67
87
/*
68
88
* `yoga::FloatOptional` <-> React Native's `Float`
69
89
*
@@ -467,15 +487,15 @@ inline void fromRawValue(
467
487
return ;
468
488
} else {
469
489
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 ()) {
473
493
result = yoga::StyleSizeLength::percent (tryValue.value ());
474
494
return ;
475
495
}
476
496
} else {
477
- auto tryValue = folly::tryTo< float > (stringValue);
478
- if (tryValue.hasValue ()) {
497
+ auto tryValue = stringToFloat (stringValue);
498
+ if (tryValue.has_value ()) {
479
499
result = yoga::StyleSizeLength::points (tryValue.value ());
480
500
return ;
481
501
}
@@ -499,15 +519,15 @@ inline void fromRawValue(
499
519
return ;
500
520
} else {
501
521
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 ()) {
505
525
result = yoga::StyleLength::percent (tryValue.value ());
506
526
return ;
507
527
}
508
528
} else {
509
- auto tryValue = folly::tryTo< float > (stringValue);
510
- if (tryValue.hasValue ()) {
529
+ auto tryValue = stringToFloat (stringValue);
530
+ if (tryValue.has_value ()) {
511
531
result = yoga::StyleLength::points (tryValue.value ());
512
532
return ;
513
533
}
@@ -573,9 +593,9 @@ inline void fromRawValue(
573
593
const auto stringValue = (std::string)value;
574
594
575
595
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 ()) {
579
599
valueUnit = ValueUnit (tryValue.value (), UnitType::Percent);
580
600
}
581
601
}
0 commit comments