Skip to content

Commit 2f1e137

Browse files
Remove double-conversion dependency from DoubleConversions.cpp
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] Differential Revision: D96082175
1 parent 3a7ed3d commit 2f1e137

1 file changed

Lines changed: 48 additions & 17 deletions

File tree

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

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,63 @@
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
26-
std::array<char, 256> buffer{};
27-
double_conversion::StringBuilder builder(buffer.data(), buffer.size());
28-
if (!conv.ToShortest(doubleValue, &builder)) {
17+
std::array<char, 256> buffer;
18+
int len;
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 matches JavaScript's Number.toString()
30+
// behavior.
31+
if (absValue != 0.0 && (absValue < 1e-6 || absValue >= 1e21)) {
32+
// %G is like %g but uses uppercase E
33+
len = std::snprintf(buffer.data(), buffer.size(), "%G", doubleValue);
34+
} else if (
35+
(absValue >= 1e-6 && absValue < 1e-4) ||
36+
(absValue >= 1e6 && absValue < 1e21)) {
37+
// %g switches to scientific notation for exponents < -4 or >= precision
38+
// (default 6), so we use %f for [1e-6, 1e-4) and [1e6, 1e21).
39+
len = std::snprintf(buffer.data(), buffer.size(), "%.20f", doubleValue);
40+
stripZeros = true;
41+
} else {
42+
len = std::snprintf(buffer.data(), buffer.size(), "%g", doubleValue);
43+
}
44+
45+
if (len <= 0 || static_cast<size_t>(len) >= buffer.size()) {
46+
buffer[0] = '0';
47+
len = 1;
48+
} else if (stripZeros) {
49+
// Strip trailing zeros and unnecessary decimal point
50+
auto end = static_cast<size_t>(len);
51+
while (end > 0 && buffer[end - 1] == '0') {
52+
--end;
53+
}
54+
if (end > 0 && buffer[end - 1] == '.') {
55+
--end;
56+
}
57+
len = static_cast<int>(end);
58+
}
3159
}
60+
61+
auto resultLen = static_cast<size_t>(len);
3262
if (suffix != '\0') {
33-
builder.AddCharacter(suffix);
63+
buffer[resultLen] = suffix;
64+
++resultLen;
3465
}
35-
return builder.Finalize();
66+
return std::string(buffer.data(), resultLen);
3667
}
3768

3869
} // namespace facebook::react

0 commit comments

Comments
 (0)