From 270fccb621c2b9fc50cb84dbc4ad406311b54f6b Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sat, 10 Apr 2021 10:42:56 +0200 Subject: [PATCH] Add `DoubleToStringConverter::ToShortestString` overload set 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 https://github.com/google/double-conversion/pull/158 commit eee1a4544fda1fd70f93412ad7a210d40693ef1c "Add `StringToDoubleConverter::StringTo` member function templates" --- double-conversion/double-to-string.h | 10 ++++++ test/cctest/test-conversions.cc | 52 ++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/double-conversion/double-to-string.h b/double-conversion/double-to-string.h index 04a4ac38..cb8a1d0d 100644 --- a/double-conversion/double-to-string.h +++ b/double-conversion/double-to-string.h @@ -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. diff --git a/test/cctest/test-conversions.cc b/test/cctest/test-conversions.cc index 4343f0c2..accffcba 100644 --- a/test/cctest/test-conversions.cc +++ b/test/cctest/test-conversions.cc @@ -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(i), &builder1)); + CHECK(converter.ToShortestString(static_cast(i), &builder2)); + + const char expected[2] = { static_cast('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];