Extract ValueUnit percent formatting to separate file (#54170)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Nick Lefever
2025-10-16 17:16:31 -07:00
committed by meta-codesync[bot]
parent 7c543db181
commit ae5728bbb4
3 changed files with 53 additions and 25 deletions
@@ -0,0 +1,36 @@
/*
* 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 <double-conversion/double-conversion.h>
#include <array>
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<char, 256> buffer{};
double_conversion::StringBuilder builder(buffer.data(), buffer.size());
if (!conv.ToShortest(doubleValue, &builder)) {
// Serialize infinite and NaN as 0
builder.AddCharacter('0');
}
builder.AddCharacter(suffix);
return builder.Finalize();
}
} // namespace facebook::react
@@ -0,0 +1,16 @@
/*
* 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 <string>
namespace facebook::react {
std::string toString(double doubleValue, char suffix);
} // namespace facebook::react
@@ -8,37 +8,13 @@
#include "ValueUnit.h"
#ifdef RN_SERIALIZABLE_STATE
#include <double-conversion/double-conversion.h>
#include <array>
#include <string>
#include "DoubleConversions.h"
#endif
namespace facebook::react {
#ifdef RN_SERIALIZABLE_STATE
std::string toString(double doubleValue, char suffix) {
// Format taken from folly's toString
static double_conversion::DoubleToStringConverter conv(
0,
NULL,
NULL,
'E',
-6, // detail::kConvMaxDecimalInShortestLow,
21, // detail::kConvMaxDecimalInShortestHigh,
6, // max leading padding zeros
1); // max trailing padding zeros
std::array<char, 256> buffer{};
double_conversion::StringBuilder builder(buffer.data(), buffer.size());
if (!conv.ToShortest(doubleValue, &builder)) {
// Serialize infinite and NaN as 0%
builder.AddCharacter('0');
builder.AddCharacter('%');
}
builder.AddCharacter(suffix);
return builder.Finalize();
}
folly::dynamic ValueUnit::toDynamic() const {
switch (unit) {
case UnitType::Undefined: