Skip to content

Commit d64f818

Browse files
christophpurrermeta-codesync[bot]
authored andcommitted
Remove double-conversion dependency from DoubleConversions.cpp (#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] Differential Revision: D96082175
1 parent f8e2d9d commit d64f818

1 file changed

Lines changed: 48 additions & 16 deletions

File tree

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

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,64 @@
77

88
#include "DoubleConversions.h"
99

10-
#include <double-conversion/double-conversion.h>
1110
#include <array>
11+
#include <cmath>
12+
#include <cstdio>
1213

1314
namespace facebook::react {
1415

1516
std::string toString(double doubleValue, char suffix) {
16-
// Format taken from folly's toString
17-
static double_conversion::DoubleToStringConverter conv(
18-
0,
19-
nullptr,
20-
nullptr,
21-
'E',
22-
-6, // detail::kConvMaxDecimalInShortestLow,
23-
21, // detail::kConvMaxDecimalInShortestHigh,
24-
6, // max leading padding zeros
25-
1); // max trailing padding zeros
2617
std::array<char, 256> buffer{};
27-
double_conversion::StringBuilder builder(buffer.data(), buffer.size());
28-
if (!conv.ToShortest(doubleValue, &builder)) {
18+
int len = 0;
19+
bool stripZeros = false;
20+
21+
if (!std::isfinite(doubleValue)) {
2922
// Serialize infinite and NaN as 0
30-
builder.AddCharacter('0');
23+
buffer[0] = '0';
24+
len = 1;
25+
} else {
26+
double absValue = std::abs(doubleValue);
27+
28+
// Use fixed notation for values in [1e-6, 1e21), scientific notation
29+
// with uppercase E otherwise. This approximates JavaScript's
30+
// Number.toString() behavior, though %g's default precision of 6
31+
// significant digits may lose precision for values with more digits.
32+
if (absValue != 0.0 && (absValue < 1e-6 || absValue >= 1e21)) {
33+
// %G is like %g but uses uppercase E
34+
len = std::snprintf(buffer.data(), buffer.size(), "%G", doubleValue);
35+
} else if (
36+
(absValue >= 1e-6 && absValue < 1e-4) ||
37+
(absValue >= 1e6 && absValue < 1e21)) {
38+
// %g switches to scientific notation for exponents < -4 or >= precision
39+
// (default 6), so we use %f for [1e-6, 1e-4) and [1e6, 1e21).
40+
len = std::snprintf(buffer.data(), buffer.size(), "%.20f", doubleValue);
41+
stripZeros = true;
42+
} else {
43+
len = std::snprintf(buffer.data(), buffer.size(), "%g", doubleValue);
44+
}
45+
46+
if (len <= 0 || static_cast<size_t>(len) >= buffer.size()) {
47+
buffer[0] = '0';
48+
len = 1;
49+
} else if (stripZeros) {
50+
// Strip trailing zeros and unnecessary decimal point
51+
auto end = static_cast<size_t>(len);
52+
while (end > 0 && buffer[end - 1] == '0') {
53+
--end;
54+
}
55+
if (end > 0 && buffer[end - 1] == '.') {
56+
--end;
57+
}
58+
len = static_cast<int>(end);
59+
}
3160
}
61+
62+
auto resultLen = static_cast<size_t>(len);
3263
if (suffix != '\0') {
33-
builder.AddCharacter(suffix);
64+
buffer[resultLen] = suffix;
65+
++resultLen;
3466
}
35-
return builder.Finalize();
67+
return std::string(buffer.data(), resultLen);
3668
}
3769

3870
} // namespace facebook::react

0 commit comments

Comments
 (0)