From 5b9cefcf90ecab0eec30aaa782bc4bc95dabf837 Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Tue, 21 Apr 2026 13:29:46 -0700 Subject: [PATCH] Remove double-conversion dependency and also DoubleConversions.cpp and replace it with DebugStringConvertible (#56110) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/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 fbshipit-source-id: abfb09d1175eba33e1a1694191f11534b4e7a00e --- .../ReactAndroid/src/main/jni/CMakeLists.txt | 2 +- .../renderer/debug/DebugStringConvertible.cpp | 12 +++ .../renderer/debug/DebugStringConvertible.h | 6 +- .../tests/DebugStringConvertibleTest.cpp | 37 ++++++++- .../react/renderer/graphics/CMakeLists.txt | 1 + .../renderer/graphics/DoubleConversions.cpp | 38 --------- .../renderer/graphics/DoubleConversions.h | 16 ---- .../renderer/graphics/React-graphics.podspec | 1 + .../react/renderer/graphics/ValueUnit.cpp | 2 +- .../graphics/tests/DoubleConversionsTest.cpp | 78 ------------------- .../api-snapshots/ReactAndroidDebugCxx.api | 1 + .../api-snapshots/ReactAndroidReleaseCxx.api | 1 + .../api-snapshots/ReactAppleDebugCxx.api | 1 + .../api-snapshots/ReactAppleReleaseCxx.api | 1 + .../api-snapshots/ReactCommonDebugCxx.api | 1 + .../api-snapshots/ReactCommonReleaseCxx.api | 1 + 16 files changed, 60 insertions(+), 139 deletions(-) delete mode 100644 packages/react-native/ReactCommon/react/renderer/graphics/DoubleConversions.cpp delete mode 100644 packages/react-native/ReactCommon/react/renderer/graphics/DoubleConversions.h delete mode 100644 packages/react-native/ReactCommon/react/renderer/graphics/tests/DoubleConversionsTest.cpp diff --git a/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt b/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt index 53715561365..01742cce7f0 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt +++ b/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt @@ -99,8 +99,8 @@ add_react_common_subdir(react/renderer/css) add_react_common_subdir(react/renderer/uimanager/consistency) add_react_common_subdir(react/renderer/dom) add_react_common_subdir(react/renderer/element) -add_react_common_subdir(react/renderer/graphics) add_react_common_subdir(react/renderer/debug) +add_react_common_subdir(react/renderer/graphics) add_react_common_subdir(react/renderer/imagemanager) add_react_common_subdir(react/renderer/components/view) add_react_common_subdir(react/renderer/components/switch) diff --git a/packages/react-native/ReactCommon/react/renderer/debug/DebugStringConvertible.cpp b/packages/react-native/ReactCommon/react/renderer/debug/DebugStringConvertible.cpp index 969f3b6434c..584caec8438 100644 --- a/packages/react-native/ReactCommon/react/renderer/debug/DebugStringConvertible.cpp +++ b/packages/react-native/ReactCommon/react/renderer/debug/DebugStringConvertible.cpp @@ -124,6 +124,8 @@ SharedDebugStringConvertibleList DebugStringConvertible::getDebugProps() const { return {}; } +#endif + /* * `toString`-family implementation. */ @@ -152,6 +154,16 @@ std::string toString(const double& value) { return result; } +std::string toString(const double doubleValue, char suffix) { + auto result = toString(doubleValue); + if (suffix != '\0') { + result += suffix; + } + return result; +} + +#if RN_DEBUG_STRING_CONVERTIBLE + std::string toString(const void* value) { if (value == nullptr) { return "null"; diff --git a/packages/react-native/ReactCommon/react/renderer/debug/DebugStringConvertible.h b/packages/react-native/ReactCommon/react/renderer/debug/DebugStringConvertible.h index 166d9a51110..726531a2837 100644 --- a/packages/react-native/ReactCommon/react/renderer/debug/DebugStringConvertible.h +++ b/packages/react-native/ReactCommon/react/renderer/debug/DebugStringConvertible.h @@ -81,13 +81,15 @@ class DebugStringConvertible {}; #endif -#if RN_DEBUG_STRING_CONVERTIBLE - /* * Set of particular-format-opinionated functions that convert base types to * `std::string` */ std::string toString(const double &value); +std::string toString(double doubleValue, char suffix); + +#if RN_DEBUG_STRING_CONVERTIBLE + std::string toString(const void *value); inline std::string toString(const std::string &value) diff --git a/packages/react-native/ReactCommon/react/renderer/debug/tests/DebugStringConvertibleTest.cpp b/packages/react-native/ReactCommon/react/renderer/debug/tests/DebugStringConvertibleTest.cpp index b8952b12f1c..38b293b3ef3 100644 --- a/packages/react-native/ReactCommon/react/renderer/debug/tests/DebugStringConvertibleTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/debug/tests/DebugStringConvertibleTest.cpp @@ -5,14 +5,44 @@ * LICENSE file in the root directory of this source tree. */ +#include + +#include +#include + +using namespace facebook::react; + +TEST(DebugStringConvertibleTest, toStringDoubleWithSuffix) { + EXPECT_EQ(toString(3.14, '%'), "3.14%"); + EXPECT_EQ(toString(100.0, 'x'), "100x"); +} + +TEST(DebugStringConvertibleTest, toStringDoubleWithNullSuffix) { + auto result = toString(42.0, '\0'); + EXPECT_EQ(result, "42"); +} + +TEST(DebugStringConvertibleTest, toStringDoubleWithSuffixZeroValue) { + EXPECT_EQ(toString(0.0, '%'), "0%"); +} + +TEST(DebugStringConvertibleTest, toStringDoubleWithSuffixNegativeValue) { + EXPECT_EQ(toString(-2.75, '%'), "-2.75%"); +} + +TEST(DebugStringConvertibleTest, toStringDoubleWithSuffixSpecialValues) { + EXPECT_EQ( + toString(std::numeric_limits::infinity(), '%'), "Infinity%"); + EXPECT_EQ( + toString(-std::numeric_limits::infinity(), '%'), "-Infinity%"); + EXPECT_EQ(toString(std::numeric_limits::quiet_NaN(), 'x'), "NaNx"); +} + #if RN_DEBUG_STRING_CONVERTIBLE #include -#include #include -using namespace facebook::react; - TEST(DebugStringConvertibleTest, handleSimpleNode) { SharedDebugStringConvertibleList empty; auto item = std::make_shared( @@ -83,4 +113,5 @@ TEST(DebugStringConvertibleTest, handleNodeWithComplexProps) { item->getDebugDescription().c_str(), ""); } + #endif diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/CMakeLists.txt b/packages/react-native/ReactCommon/react/renderer/graphics/CMakeLists.txt index 56007edeec9..7113ce1bcdc 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/CMakeLists.txt +++ b/packages/react-native/ReactCommon/react/renderer/graphics/CMakeLists.txt @@ -30,6 +30,7 @@ target_link_libraries(react_renderer_graphics ${fbjni} folly_runtime react_debug + react_renderer_debug react_utils ) target_compile_reactnative_options(react_renderer_graphics PRIVATE) diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/DoubleConversions.cpp b/packages/react-native/ReactCommon/react/renderer/graphics/DoubleConversions.cpp deleted file mode 100644 index ce9d3435412..00000000000 --- a/packages/react-native/ReactCommon/react/renderer/graphics/DoubleConversions.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include "DoubleConversions.h" - -#include -#include - -namespace facebook::react { - -std::string toString(double doubleValue, char suffix) { - // Format taken from folly's toString - static double_conversion::DoubleToStringConverter conv( - 0, - nullptr, - nullptr, - 'E', - -6, // detail::kConvMaxDecimalInShortestLow, - 21, // detail::kConvMaxDecimalInShortestHigh, - 6, // max leading padding zeros - 1); // max trailing padding zeros - std::array buffer{}; - double_conversion::StringBuilder builder(buffer.data(), buffer.size()); - if (!conv.ToShortest(doubleValue, &builder)) { - // Serialize infinite and NaN as 0 - builder.AddCharacter('0'); - } - if (suffix != '\0') { - builder.AddCharacter(suffix); - } - return builder.Finalize(); -} - -} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/DoubleConversions.h b/packages/react-native/ReactCommon/react/renderer/graphics/DoubleConversions.h deleted file mode 100644 index 504b3c8804a..00000000000 --- a/packages/react-native/ReactCommon/react/renderer/graphics/DoubleConversions.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include - -namespace facebook::react { - -std::string toString(double doubleValue, char suffix); - -} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/React-graphics.podspec b/packages/react-native/ReactCommon/react/renderer/graphics/React-graphics.podspec index bd57bdb1c0e..f6265ab54e6 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/React-graphics.podspec +++ b/packages/react-native/ReactCommon/react/renderer/graphics/React-graphics.podspec @@ -49,6 +49,7 @@ Pod::Spec.new do |s| s.dependency "React-jsiexecutor" s.dependency "React-featureflags" s.dependency "React-utils" + s.dependency "React-rendererdebug" depend_on_js_engine(s) add_rn_third_party_dependencies(s) diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/ValueUnit.cpp b/packages/react-native/ReactCommon/react/renderer/graphics/ValueUnit.cpp index a44cadc6e95..c4479e9503f 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/ValueUnit.cpp +++ b/packages/react-native/ReactCommon/react/renderer/graphics/ValueUnit.cpp @@ -7,7 +7,7 @@ #include "ValueUnit.h" -#include "DoubleConversions.h" +#include namespace facebook::react { diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/tests/DoubleConversionsTest.cpp b/packages/react-native/ReactCommon/react/renderer/graphics/tests/DoubleConversionsTest.cpp deleted file mode 100644 index c355e33bdb3..00000000000 --- a/packages/react-native/ReactCommon/react/renderer/graphics/tests/DoubleConversionsTest.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include - -#include -#include -#include - -using namespace facebook::react; - -TEST(DoubleConversionsTest, zeroValue) { - EXPECT_EQ(toString(0.0, '\0'), "0"); -} - -TEST(DoubleConversionsTest, negativeZero) { - EXPECT_EQ(toString(-0.0, '\0'), "-0"); -} - -TEST(DoubleConversionsTest, positiveIntegers) { - EXPECT_EQ(toString(1.0, '\0'), "1"); - EXPECT_EQ(toString(42.0, '\0'), "42"); - EXPECT_EQ(toString(1000.0, '\0'), "1000"); -} - -TEST(DoubleConversionsTest, negativeIntegers) { - EXPECT_EQ(toString(-1.0, '\0'), "-1"); - EXPECT_EQ(toString(-42.0, '\0'), "-42"); -} - -TEST(DoubleConversionsTest, fractionalValues) { - EXPECT_EQ(toString(3.14, '\0'), "3.14"); - EXPECT_EQ(toString(0.5, '\0'), "0.5"); - EXPECT_EQ(toString(-2.75, '\0'), "-2.75"); -} - -TEST(DoubleConversionsTest, verySmallValues) { - EXPECT_EQ(toString(0.000001, '\0'), "0.000001"); -} - -TEST(DoubleConversionsTest, veryLargeValues) { - EXPECT_EQ(toString(1e20, '\0'), "100000000000000000000"); -} - -TEST(DoubleConversionsTest, scientificNotation) { - // Values below 1e-6 should use scientific notation - auto result = toString(1e-7, '\0'); - EXPECT_NE(result.find('E'), std::string::npos); -} - -TEST(DoubleConversionsTest, infinitySerializedAsZero) { - EXPECT_EQ(toString(std::numeric_limits::infinity(), '\0'), "0"); - EXPECT_EQ(toString(-std::numeric_limits::infinity(), '\0'), "0"); -} - -TEST(DoubleConversionsTest, nanSerializedAsZero) { - EXPECT_EQ(toString(std::numeric_limits::quiet_NaN(), '\0'), "0"); -} - -TEST(DoubleConversionsTest, suffixAppended) { - EXPECT_EQ(toString(3.14, '%'), "3.14%"); - EXPECT_EQ(toString(100.0, 'x'), "100x"); -} - -TEST(DoubleConversionsTest, nullSuffixNotAppended) { - auto result = toString(42.0, '\0'); - EXPECT_EQ(result, "42"); - EXPECT_EQ(result.back(), '2'); -} - -TEST(DoubleConversionsTest, suffixWithSpecialValues) { - EXPECT_EQ(toString(std::numeric_limits::infinity(), '%'), "0%"); - EXPECT_EQ(toString(std::numeric_limits::quiet_NaN(), 'x'), "0x"); -} diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api index f7cec440d7c..d6da4543b31 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api @@ -927,6 +927,7 @@ std::pair facebook::react::calcu std::string facebook::react::base64Encode(const std::string_view s); std::string facebook::react::componentNameByReactViewName(std::string viewName); std::string facebook::react::generateRandomUuidString(); +std::string facebook::react::toString(const double& value); std::string facebook::react::toString(const facebook::react::AccessibilityRole& accessibilityRole); std::string facebook::react::toString(const facebook::react::AttributedString::Range& range); std::string facebook::react::toString(const facebook::react::BlendMode& blendMode); diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api index 8e6baa5be02..761824e2d82 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api @@ -927,6 +927,7 @@ std::pair facebook::react::calcu std::string facebook::react::base64Encode(const std::string_view s); std::string facebook::react::componentNameByReactViewName(std::string viewName); std::string facebook::react::generateRandomUuidString(); +std::string facebook::react::toString(const double& value); std::string facebook::react::toString(const facebook::react::AccessibilityRole& accessibilityRole); std::string facebook::react::toString(const facebook::react::AttributedString::Range& range); std::string facebook::react::toString(const facebook::react::BlendMode& blendMode); diff --git a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api index 927aebd9ee7..20544511a83 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api @@ -4120,6 +4120,7 @@ std::pair facebook::react::calcu std::string facebook::react::base64Encode(const std::string_view s); std::string facebook::react::componentNameByReactViewName(std::string viewName); std::string facebook::react::generateRandomUuidString(); +std::string facebook::react::toString(const double& value); std::string facebook::react::toString(const facebook::react::AccessibilityRole& accessibilityRole); std::string facebook::react::toString(const facebook::react::AttributedString::Range& range); std::string facebook::react::toString(const facebook::react::BlendMode& blendMode); diff --git a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api index 6a62c5bf38d..b16776b44a6 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api @@ -4120,6 +4120,7 @@ std::pair facebook::react::calcu std::string facebook::react::base64Encode(const std::string_view s); std::string facebook::react::componentNameByReactViewName(std::string viewName); std::string facebook::react::generateRandomUuidString(); +std::string facebook::react::toString(const double& value); std::string facebook::react::toString(const facebook::react::AccessibilityRole& accessibilityRole); std::string facebook::react::toString(const facebook::react::AttributedString::Range& range); std::string facebook::react::toString(const facebook::react::BlendMode& blendMode); diff --git a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api index 64f67e68aa4..2223d18a695 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api @@ -503,6 +503,7 @@ std::pair facebook::react::calculateAnimationProgress(uint64_t now std::string facebook::react::base64Encode(const std::string_view s); std::string facebook::react::componentNameByReactViewName(std::string viewName); std::string facebook::react::generateRandomUuidString(); +std::string facebook::react::toString(const double& value); std::string facebook::react::toString(const facebook::react::AccessibilityRole& accessibilityRole); std::string facebook::react::toString(const facebook::react::AttributedString::Range& range); std::string facebook::react::toString(const facebook::react::BlendMode& blendMode); diff --git a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api index ab31e2b613e..244e66ee698 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api @@ -503,6 +503,7 @@ std::pair facebook::react::calculateAnimationProgress(uint64_t now std::string facebook::react::base64Encode(const std::string_view s); std::string facebook::react::componentNameByReactViewName(std::string viewName); std::string facebook::react::generateRandomUuidString(); +std::string facebook::react::toString(const double& value); std::string facebook::react::toString(const facebook::react::AccessibilityRole& accessibilityRole); std::string facebook::react::toString(const facebook::react::AttributedString::Range& range); std::string facebook::react::toString(const facebook::react::BlendMode& blendMode);