Skip to content

Commit e814f92

Browse files
Remove double-conversion dependency and also DoubleConversions.cpp and replace it with DebugStringConvertible (#56110)
Summary: **Summary** Removes the `double-conversion` dependency from `DoubleConversions.cpp` in the React Native repository. - Removed Dependencies: `double-conversion/double-conversion.h` Updated Code * Modified `DoubleConversions.cpp` to use standard C++ library headers (`cmath` and `cstdio`) instead of `double-conversion`. This change aims to reduce dependencies and simplify the codebase. The removed dependency was previously used for string conversion, which has been replaced with standard C++ library functions. Changelog: [Internal] Reviewed By: NickGerleman Differential Revision: D96082175
1 parent c533369 commit e814f92

16 files changed

Lines changed: 81 additions & 167 deletions

packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ add_react_common_subdir(react/renderer/css)
9999
add_react_common_subdir(react/renderer/uimanager/consistency)
100100
add_react_common_subdir(react/renderer/dom)
101101
add_react_common_subdir(react/renderer/element)
102-
add_react_common_subdir(react/renderer/graphics)
103102
add_react_common_subdir(react/renderer/debug)
103+
add_react_common_subdir(react/renderer/graphics)
104104
add_react_common_subdir(react/renderer/imagemanager)
105105
add_react_common_subdir(react/renderer/components/view)
106106
add_react_common_subdir(react/renderer/components/switch)

packages/react-native/ReactCommon/react/renderer/debug/DebugStringConvertible.cpp

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,46 @@
1414

1515
namespace facebook::react {
1616

17+
#if RN_DEBUG_STRING_CONVERTIBLE || defined(RN_SERIALIZABLE_STATE)
18+
19+
/*
20+
* `toString`-family implementation.
21+
*/
22+
std::string toString(const double& value) {
23+
// Handle special floating-point values with prettier output
24+
if (std::isnan(value)) {
25+
return "NaN";
26+
}
27+
if (std::isinf(value)) {
28+
return value < 0 ? "-Infinity" : "Infinity";
29+
}
30+
31+
std::ostringstream stream;
32+
stream << std::fixed << std::setprecision(4) << value;
33+
std::string result = stream.str();
34+
35+
// Strip trailing zeros and unnecessary decimal point
36+
if (auto dotPos = result.find('.'); dotPos != std::string::npos) {
37+
auto lastNonZero = result.find_last_not_of('0');
38+
if (lastNonZero == dotPos) {
39+
result.erase(dotPos);
40+
} else {
41+
result.erase(lastNonZero + 1);
42+
}
43+
}
44+
return result;
45+
}
46+
47+
std::string toString(const double doubleValue, char suffix) {
48+
auto result = toString(doubleValue);
49+
if (suffix != '\0') {
50+
result += suffix;
51+
}
52+
return result;
53+
}
54+
55+
#endif
56+
1757
#if RN_DEBUG_STRING_CONVERTIBLE
1858

1959
std::string DebugStringConvertible::getDebugChildrenDescription(
@@ -124,34 +164,6 @@ SharedDebugStringConvertibleList DebugStringConvertible::getDebugProps() const {
124164
return {};
125165
}
126166

127-
/*
128-
* `toString`-family implementation.
129-
*/
130-
std::string toString(const double& value) {
131-
// Handle special floating-point values with prettier output
132-
if (std::isnan(value)) {
133-
return "NaN";
134-
}
135-
if (std::isinf(value)) {
136-
return value < 0 ? "-Infinity" : "Infinity";
137-
}
138-
139-
std::ostringstream stream;
140-
stream << std::fixed << std::setprecision(4) << value;
141-
std::string result = stream.str();
142-
143-
// Strip trailing zeros and unnecessary decimal point
144-
if (auto dotPos = result.find('.'); dotPos != std::string::npos) {
145-
auto lastNonZero = result.find_last_not_of('0');
146-
if (lastNonZero == dotPos) {
147-
result.erase(dotPos);
148-
} else {
149-
result.erase(lastNonZero + 1);
150-
}
151-
}
152-
return result;
153-
}
154-
155167
std::string toString(const void* value) {
156168
if (value == nullptr) {
157169
return "null";

packages/react-native/ReactCommon/react/renderer/debug/DebugStringConvertible.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,19 @@ class DebugStringConvertible {};
8181

8282
#endif
8383

84-
#if RN_DEBUG_STRING_CONVERTIBLE
84+
#if RN_DEBUG_STRING_CONVERTIBLE || defined(RN_SERIALIZABLE_STATE)
8585

8686
/*
8787
* Set of particular-format-opinionated functions that convert base types to
8888
* `std::string`
8989
*/
9090
std::string toString(const double &value);
91+
std::string toString(const double doubleValue, char suffix);
92+
93+
#endif
94+
95+
#if RN_DEBUG_STRING_CONVERTIBLE
96+
9197
std::string toString(const void *value);
9298

9399
inline std::string toString(const std::string &value)

packages/react-native/ReactCommon/react/renderer/debug/tests/DebugStringConvertibleTest.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
*/
77

88
#if RN_DEBUG_STRING_CONVERTIBLE
9+
#include <limits>
910
#include <memory>
1011

1112
#include <gtest/gtest.h>
13+
#include <react/renderer/debug/DebugStringConvertible.h>
1214
#include <react/renderer/debug/DebugStringConvertibleItem.h>
1315

1416
using namespace facebook::react;
@@ -83,4 +85,30 @@ TEST(DebugStringConvertibleTest, handleNodeWithComplexProps) {
8385
item->getDebugDescription().c_str(),
8486
"<View=hello x=1(height=100 width=200)/>");
8587
}
88+
89+
TEST(DebugStringConvertibleTest, toStringDoubleWithSuffix) {
90+
EXPECT_EQ(toString(3.14, '%'), "3.14%");
91+
EXPECT_EQ(toString(100.0, 'x'), "100x");
92+
}
93+
94+
TEST(DebugStringConvertibleTest, toStringDoubleWithNullSuffix) {
95+
auto result = toString(42.0, '\0');
96+
EXPECT_EQ(result, "42");
97+
}
98+
99+
TEST(DebugStringConvertibleTest, toStringDoubleWithSuffixZeroValue) {
100+
EXPECT_EQ(toString(0.0, '%'), "0%");
101+
}
102+
103+
TEST(DebugStringConvertibleTest, toStringDoubleWithSuffixNegativeValue) {
104+
EXPECT_EQ(toString(-2.75, '%'), "-2.75%");
105+
}
106+
107+
TEST(DebugStringConvertibleTest, toStringDoubleWithSuffixSpecialValues) {
108+
EXPECT_EQ(
109+
toString(std::numeric_limits<double>::infinity(), '%'), "Infinity%");
110+
EXPECT_EQ(
111+
toString(-std::numeric_limits<double>::infinity(), '%'), "-Infinity%");
112+
EXPECT_EQ(toString(std::numeric_limits<double>::quiet_NaN(), 'x'), "NaNx");
113+
}
86114
#endif

packages/react-native/ReactCommon/react/renderer/graphics/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ target_link_libraries(react_renderer_graphics
3030
${fbjni}
3131
folly_runtime
3232
react_debug
33+
react_renderer_debug
3334
react_utils
3435
)
3536
target_compile_reactnative_options(react_renderer_graphics PRIVATE)

packages/react-native/ReactCommon/react/renderer/graphics/DoubleConversions.cpp

Lines changed: 0 additions & 38 deletions
This file was deleted.

packages/react-native/ReactCommon/react/renderer/graphics/DoubleConversions.h

Lines changed: 0 additions & 16 deletions
This file was deleted.

packages/react-native/ReactCommon/react/renderer/graphics/React-graphics.podspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Pod::Spec.new do |s|
4949
s.dependency "React-jsiexecutor"
5050
s.dependency "React-featureflags"
5151
s.dependency "React-utils"
52+
s.dependency "React-rendererdebug"
5253

5354
depend_on_js_engine(s)
5455
add_rn_third_party_dependencies(s)

packages/react-native/ReactCommon/react/renderer/graphics/ValueUnit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "ValueUnit.h"
99

10-
#include "DoubleConversions.h"
10+
#include <react/renderer/debug/DebugStringConvertible.h>
1111

1212
namespace facebook::react {
1313

packages/react-native/ReactCommon/react/renderer/graphics/tests/DoubleConversionsTest.cpp

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)