Skip to content

Commit ae5728b

Browse files
Nick Lefevermeta-codesync[bot]
authored andcommitted
Extract ValueUnit percent formatting to separate file (facebook#54170)
Summary: Pull Request resolved: facebook#54170 This diff extracts the `toString(double, char)` function used to format percent values from `ValueUnit` into a separate `DoubleConversions` file. This was added to graphics directly instead of moving it to core to avoid introducting cyclic dependencies since core depends on graphics. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D84714535 fbshipit-source-id: e41be90d211c28dba12e0920293698b3e1d3a1c7
1 parent 7c543db commit ae5728b

File tree

3 files changed

+53
-25
lines changed

3 files changed

+53
-25
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "DoubleConversions.h"
9+
10+
#include <double-conversion/double-conversion.h>
11+
#include <array>
12+
13+
namespace facebook::react {
14+
15+
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)) {
29+
// Serialize infinite and NaN as 0
30+
builder.AddCharacter('0');
31+
}
32+
builder.AddCharacter(suffix);
33+
return builder.Finalize();
34+
}
35+
36+
} // namespace facebook::react
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <string>
11+
12+
namespace facebook::react {
13+
14+
std::string toString(double doubleValue, char suffix);
15+
16+
} // namespace facebook::react

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,13 @@
88
#include "ValueUnit.h"
99

1010
#ifdef RN_SERIALIZABLE_STATE
11-
#include <double-conversion/double-conversion.h>
12-
#include <array>
13-
#include <string>
11+
#include "DoubleConversions.h"
1412
#endif
1513

1614
namespace facebook::react {
1715

1816
#ifdef RN_SERIALIZABLE_STATE
1917

20-
std::string toString(double doubleValue, char suffix) {
21-
// Format taken from folly's toString
22-
static double_conversion::DoubleToStringConverter conv(
23-
0,
24-
NULL,
25-
NULL,
26-
'E',
27-
-6, // detail::kConvMaxDecimalInShortestLow,
28-
21, // detail::kConvMaxDecimalInShortestHigh,
29-
6, // max leading padding zeros
30-
1); // max trailing padding zeros
31-
std::array<char, 256> buffer{};
32-
double_conversion::StringBuilder builder(buffer.data(), buffer.size());
33-
if (!conv.ToShortest(doubleValue, &builder)) {
34-
// Serialize infinite and NaN as 0%
35-
builder.AddCharacter('0');
36-
builder.AddCharacter('%');
37-
}
38-
builder.AddCharacter(suffix);
39-
return builder.Finalize();
40-
}
41-
4218
folly::dynamic ValueUnit::toDynamic() const {
4319
switch (unit) {
4420
case UnitType::Undefined:

0 commit comments

Comments
 (0)