Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DoubleToStringConverter::ToShortestString overload set #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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