Skip to content

Commit b008b7e

Browse files
christophpurrermeta-codesync[bot]
authored andcommitted
Remove double-conversion dependency and also DoubleConversions.cpp and replace it with DebugStringConvertible (#56110)
Summary: Pull Request resolved: #56110 **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 2902df0 commit b008b7e

16 files changed

Lines changed: 90 additions & 173 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: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,42 @@
1414

1515
namespace facebook::react {
1616

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

1955
std::string DebugStringConvertible::getDebugChildrenDescription(
@@ -124,34 +160,6 @@ SharedDebugStringConvertibleList DebugStringConvertible::getDebugProps() const {
124160
return {};
125161
}
126162

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-
155163
std::string toString(const void* value) {
156164
if (value == nullptr) {
157165
return "null";

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,15 @@ class DebugStringConvertible {};
8181

8282
#endif
8383

84-
#if RN_DEBUG_STRING_CONVERTIBLE
85-
8684
/*
8785
* Set of particular-format-opinionated functions that convert base types to
8886
* `std::string`
8987
*/
9088
std::string toString(const double &value);
89+
std::string toString(const double doubleValue, char suffix);
90+
91+
#if RN_DEBUG_STRING_CONVERTIBLE
92+
9193
std::string toString(const void *value);
9294

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

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,44 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
#if RN_DEBUG_STRING_CONVERTIBLE
9-
#include <memory>
8+
#include <limits>
109

1110
#include <gtest/gtest.h>
12-
#include <react/renderer/debug/DebugStringConvertibleItem.h>
11+
#include <react/renderer/debug/DebugStringConvertible.h>
1312

1413
using namespace facebook::react;
1514

15+
TEST(DebugStringConvertibleTest, toStringDoubleWithSuffix) {
16+
EXPECT_EQ(toString(3.14, '%'), "3.14%");
17+
EXPECT_EQ(toString(100.0, 'x'), "100x");
18+
}
19+
20+
TEST(DebugStringConvertibleTest, toStringDoubleWithNullSuffix) {
21+
auto result = toString(42.0, '\0');
22+
EXPECT_EQ(result, "42");
23+
}
24+
25+
TEST(DebugStringConvertibleTest, toStringDoubleWithSuffixZeroValue) {
26+
EXPECT_EQ(toString(0.0, '%'), "0%");
27+
}
28+
29+
TEST(DebugStringConvertibleTest, toStringDoubleWithSuffixNegativeValue) {
30+
EXPECT_EQ(toString(-2.75, '%'), "-2.75%");
31+
}
32+
33+
TEST(DebugStringConvertibleTest, toStringDoubleWithSuffixSpecialValues) {
34+
EXPECT_EQ(
35+
toString(std::numeric_limits<double>::infinity(), '%'), "Infinity%");
36+
EXPECT_EQ(
37+
toString(-std::numeric_limits<double>::infinity(), '%'), "-Infinity%");
38+
EXPECT_EQ(toString(std::numeric_limits<double>::quiet_NaN(), 'x'), "NaNx");
39+
}
40+
41+
#if RN_DEBUG_STRING_CONVERTIBLE
42+
#include <memory>
43+
44+
#include <react/renderer/debug/DebugStringConvertibleItem.h>
45+
1646
TEST(DebugStringConvertibleTest, handleSimpleNode) {
1747
SharedDebugStringConvertibleList empty;
1848
auto item = std::make_shared<DebugStringConvertibleItem>(
@@ -83,4 +113,5 @@ TEST(DebugStringConvertibleTest, handleNodeWithComplexProps) {
83113
item->getDebugDescription().c_str(),
84114
"<View=hello x=1(height=100 width=200)/>");
85115
}
116+
86117
#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)