Skip to content

Commit

Permalink
Add DoubleToStringConverter::ToShortestString overload set
Browse files Browse the repository at this point in the history
Allowed users to write generic code more easily, in cases where the input value is either a double, or a single-precision float.

Included a unit test, `TEST(DoubleToShortestString)`.

Complementary to pull request #158 commit eee1a45 "Add `StringToDoubleConverter::StringTo<T>` member function templates"
  • Loading branch information
N-Dekker committed Aug 16, 2022
1 parent af09fd6 commit 270fccb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
10 changes: 10 additions & 0 deletions double-conversion/double-to-string.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ class DoubleToStringConverter {
return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE);
}

// Same as ToShortest, but overloaded for single-precision floats.
bool ToShortestString(double value, StringBuilder* result_builder) const {
return ToShortestIeeeNumber(value, result_builder, SHORTEST);
}

// Same as ToShortestSingle. Overload, to ease writing generic code that
// supports both double and single-precision float input values.
bool ToShortestString(float value, StringBuilder* result_builder) const {
return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE);
}

// Computes a decimal representation with a fixed number of digits after the
// decimal point. The last emitted digit is rounded.
Expand Down
52 changes: 52 additions & 0 deletions test/cctest/test-conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,58 @@ TEST(DoubleToShortestSingle) {
}


TEST(DoubleToShortestString) {
const DoubleToStringConverter& converter = DoubleToStringConverter::EcmaScriptConverter();
const int kBufferSize = DoubleToStringConverter::kMaxCharsEcmaScriptShortest + 1;

// First check conversion from 0 and 1 for both ToShortestString overloads:
for (int i = 0; i <= 1; ++i)
{
char buffer1[kBufferSize] = "";
char buffer2[kBufferSize] = "";
StringBuilder builder1(buffer1, kBufferSize);
StringBuilder builder2(buffer2, kBufferSize);

CHECK(converter.ToShortestString(static_cast<double>(i), &builder1));
CHECK(converter.ToShortestString(static_cast<float>(i), &builder2));

const char expected[2] = { static_cast<char>('0' + i), '\0' };
CHECK_EQ(expected, builder1.Finalize());
CHECK_EQ(expected, builder2.Finalize());
}
{
// Check that ToShortestString behaves like ToShortest, for a double
// input value. Use an input value that can be represented by a double,
// but not by a single-precision float.
const double value = 1e+100;

char buffer1[kBufferSize] = "";
char buffer2[kBufferSize] = "";
StringBuilder builder1(buffer1, kBufferSize);
StringBuilder builder2(buffer2, kBufferSize);

CHECK_EQ(converter.ToShortest(value, &builder1),
converter.ToShortestString(value, &builder2));
CHECK_EQ(builder1.Finalize(), builder2.Finalize());
}
{
// Check that ToShortestString behaves like ToShortestSingle, for a
// single-precision float input value. Use an input value for which
// ToShortestSingle yields a different string than ToShortest.
const float value = 0.1f;

char buffer1[kBufferSize] = "";
char buffer2[kBufferSize] = "";
StringBuilder builder1(buffer1, kBufferSize);
StringBuilder builder2(buffer2, kBufferSize);

CHECK_EQ(converter.ToShortestSingle(value, &builder1),
converter.ToShortestString(value, &builder2));
CHECK_EQ(builder1.Finalize(), builder2.Finalize());
}
}


TEST(DoubleToFixed) {
const int kBufferSize = 168;
char buffer[kBufferSize];
Expand Down

0 comments on commit 270fccb

Please sign in to comment.