Remove ByteString::FormatFloat() FloatToString(), and DoubleToString()

- Remove the last test-only caller to ByteString::FormatFloat() and
  replace its use with hard-coded values.
- Remove FloatToString(), which in turn has no callers except for its
  own unit tests.
- Remove DoubleToString(), which has no callers except its own unit
  tests.
- Remove the implementation shared between FloatToString() and
  DoubleToString().

Change-Id: Ic150a96444c25c5f1ae9db828c8c33f08b7abbda
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/124350
Reviewed-by: Tom Sepez <tsepez@google.com>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_psengine_unittest.cpp b/core/fpdfapi/page/cpdf_psengine_unittest.cpp
index cad8dc8..79b33b3 100644
--- a/core/fpdfapi/page/cpdf_psengine_unittest.cpp
+++ b/core/fpdfapi/page/cpdf_psengine_unittest.cpp
@@ -7,6 +7,7 @@
 #include <iterator>
 #include <limits>
 
+#include "core/fxcrt/notreached.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
 namespace {
@@ -80,10 +81,17 @@
     EXPECT_EQ(item.op, new_op);
     if (new_op == PSOP_CONST) {
       float fv = new_psop->GetFloatValue();
-      if (word == "invalid")
-        EXPECT_FLOAT_EQ(0, fv);
-      else
-        EXPECT_EQ(word, ByteString::FormatFloat(fv));
+      if (word == "55") {
+        EXPECT_FLOAT_EQ(55.0f, fv);
+      } else if (word == "123.4") {
+        EXPECT_FLOAT_EQ(123.4f, fv);
+      } else if (word == "-5") {
+        EXPECT_FLOAT_EQ(-5.0f, fv);
+      } else if (word == "invalid") {
+        EXPECT_FLOAT_EQ(0.0f, fv);
+      } else {
+        NOTREACHED_NORETURN();
+      }
     }
   }
 }
diff --git a/core/fxcrt/bytestring.cpp b/core/fxcrt/bytestring.cpp
index 7274d76..49fc738 100644
--- a/core/fxcrt/bytestring.cpp
+++ b/core/fxcrt/bytestring.cpp
@@ -49,12 +49,6 @@
 }
 
 // static
-ByteString ByteString::FormatFloat(float f) {
-  char buf[32];
-  return UNSAFE_TODO(ByteString(buf, FloatToString(f, buf)));
-}
-
-// static
 ByteString ByteString::FormatV(const char* pFormat, va_list argList) {
   va_list argListCopy;
   va_copy(argListCopy, argList);
diff --git a/core/fxcrt/bytestring.h b/core/fxcrt/bytestring.h
index 633f311..2ba59fc 100644
--- a/core/fxcrt/bytestring.h
+++ b/core/fxcrt/bytestring.h
@@ -25,7 +25,6 @@
 class ByteString : public StringTemplate<char> {
  public:
   [[nodiscard]] static ByteString FormatInteger(int i);
-  [[nodiscard]] static ByteString FormatFloat(float f);
   [[nodiscard]] static ByteString Format(const char* pFormat, ...);
   [[nodiscard]] static ByteString FormatV(const char* pFormat, va_list argList);
 
diff --git a/core/fxcrt/fx_string.cpp b/core/fxcrt/fx_string.cpp
index 0215441..948c255 100644
--- a/core/fxcrt/fx_string.cpp
+++ b/core/fxcrt/fx_string.cpp
@@ -9,16 +9,14 @@
 #include <stdint.h>
 
 #include <array>
-#include <iterator>
+#include <string>
+#include <vector>
 
 #include "build/build_config.h"
 #include "core/fxcrt/bytestring.h"
 #include "core/fxcrt/code_point_view.h"
-#include "core/fxcrt/compiler_specific.h"
 #include "core/fxcrt/fx_extension.h"
 #include "core/fxcrt/span.h"
-#include "core/fxcrt/stl_util.h"
-#include "core/fxcrt/string_view_template.h"
 #include "core/fxcrt/utf16.h"
 #include "core/fxcrt/widestring.h"
 
@@ -150,54 +148,6 @@
   return bNegative ? -value : value;
 }
 
-template <class T>
-size_t ToString(T value, int (*round_func)(T), pdfium::span<char> buf) {
-  buf[0] = '0';
-  buf[1] = '\0';
-  if (value == 0) {
-    return 1;
-  }
-  bool bNegative = false;
-  if (value < 0) {
-    bNegative = true;
-    value = -value;
-  }
-  int scale = 1;
-  int scaled = round_func(value);
-  while (scaled < 100000) {
-    if (scale == 1000000) {
-      break;
-    }
-    scale *= 10;
-    scaled = round_func(value * scale);
-  }
-  if (scaled == 0) {
-    return 1;
-  }
-  char buf2[32];
-  size_t buf_size = 0;
-  if (bNegative) {
-    buf[buf_size++] = '-';
-  }
-  int i = scaled / scale;
-  FXSYS_itoa(i, buf2, 10);
-  size_t len = strlen(buf2);
-  fxcrt::Copy(pdfium::make_span(buf2).first(len), buf.subspan(buf_size));
-  buf_size += len;
-  int fraction = scaled % scale;
-  if (fraction == 0) {
-    return buf_size;
-  }
-  buf[buf_size++] = '.';
-  scale /= 10;
-  while (fraction) {
-    buf[buf_size++] = '0' + fraction / scale;
-    fraction %= scale;
-    scale /= 10;
-  }
-  return buf_size;
-}
-
 }  // namespace
 
 float StringToFloat(ByteStringView strc) {
@@ -208,10 +158,6 @@
   return StringToFloat(FX_UTF8Encode(wsStr).AsStringView());
 }
 
-size_t FloatToString(float f, pdfium::span<char> buf) {
-  return ToString<float>(f, FXSYS_roundf, buf);
-}
-
 double StringToDouble(ByteStringView strc) {
   return StringTo<double>(strc, kFractionScalesDouble);
 }
@@ -220,10 +166,6 @@
   return StringToDouble(FX_UTF8Encode(wsStr).AsStringView());
 }
 
-size_t DoubleToString(double d, pdfium::span<char> buf) {
-  return ToString<double>(d, FXSYS_round, buf);
-}
-
 namespace fxcrt {
 
 template std::vector<ByteString> Split<ByteString>(const ByteString& that,
diff --git a/core/fxcrt/fx_string.h b/core/fxcrt/fx_string.h
index 3485a80..cd79418 100644
--- a/core/fxcrt/fx_string.h
+++ b/core/fxcrt/fx_string.h
@@ -26,11 +26,9 @@
 
 float StringToFloat(ByteStringView str);
 float StringToFloat(WideStringView wsStr);
-size_t FloatToString(float f, pdfium::span<char> buf);
 
 double StringToDouble(ByteStringView str);
 double StringToDouble(WideStringView wsStr);
-size_t DoubleToString(double d, pdfium::span<char> buf);
 
 namespace fxcrt {
 
diff --git a/core/fxcrt/fx_string_unittest.cpp b/core/fxcrt/fx_string_unittest.cpp
index 830182f..3d39f87 100644
--- a/core/fxcrt/fx_string_unittest.cpp
+++ b/core/fxcrt/fx_string_unittest.cpp
@@ -10,18 +10,6 @@
 #include "core/fxcrt/utf16.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
-char* TerminatedFloatToString(float value, pdfium::span<char> buf) {
-  size_t buflen = FloatToString(value, buf);
-  buf[buflen] = '\0';
-  return buf.data();
-}
-
-char* TerminatedDoubleToString(double value, pdfium::span<char> buf) {
-  size_t buflen = DoubleToString(value, buf);
-  buf[buflen] = '\0';
-  return buf.data();
-}
-
 TEST(fxstring, FXUTF8Encode) {
   EXPECT_EQ("", FX_UTF8Encode(WideStringView()));
   EXPECT_EQ(
@@ -110,44 +98,6 @@
   EXPECT_FLOAT_EQ(1.999999881f, StringToFloat(L"1.999999881"));
 }
 
-TEST(fxstring, FloatToString) {
-  char buf[32];
-
-  EXPECT_STREQ("0", TerminatedFloatToString(0.0f, buf));
-  EXPECT_STREQ("0", TerminatedFloatToString(-0.0f, buf));
-  EXPECT_STREQ("0",
-               TerminatedFloatToString(std::numeric_limits<float>::min(), buf));
-  EXPECT_STREQ(
-      "0", TerminatedFloatToString(-std::numeric_limits<float>::min(), buf));
-
-  EXPECT_STREQ("0.25", TerminatedFloatToString(0.25f, buf));
-  EXPECT_STREQ("-0.25", TerminatedFloatToString(-0.25f, buf));
-
-  EXPECT_STREQ("100", TerminatedFloatToString(100.0f, buf));
-  EXPECT_STREQ("-100", TerminatedFloatToString(-100.0f, buf));
-
-  // FloatToString won't convert beyond the maximum integer, and values
-  // larger than that get converted to a string representing that.
-  EXPECT_STREQ("2147483647", TerminatedFloatToString(2147483647.0f, buf));
-  EXPECT_STREQ("2147483647", TerminatedFloatToString(2147483647.5f, buf));
-  EXPECT_STREQ("2147483647",
-               TerminatedFloatToString(std::numeric_limits<float>::max(), buf));
-
-  // FloatToString won't convert beyond the minimum integer, and values
-  // smaller than that get converted to a string representing that.
-  EXPECT_STREQ("-2147483647", TerminatedFloatToString(-2147483647.0f, buf));
-  EXPECT_STREQ("-2147483647", TerminatedFloatToString(-2147483647.5f, buf));
-  EXPECT_STREQ("-2147483647", TerminatedFloatToString(
-                                  -std::numeric_limits<float>::max(), buf));
-
-  // Conversion only acknowledges precision to 5 digit past decimal, and
-  // rounds beyond that.
-  EXPECT_STREQ("1", TerminatedFloatToString(1.000001119f, buf));
-  EXPECT_STREQ("1.00001", TerminatedFloatToString(1.000011119f, buf));
-  EXPECT_STREQ("1.99999", TerminatedFloatToString(1.999988881f, buf));
-  EXPECT_STREQ("2", TerminatedFloatToString(1.999999881f, buf));
-}
-
 TEST(fxstring, ByteStringToDouble) {
   EXPECT_FLOAT_EQ(0.0, StringToDouble(""));
   EXPECT_FLOAT_EQ(0.0, StringToDouble("0"));
@@ -194,44 +144,6 @@
   EXPECT_FLOAT_EQ(1.999999881, StringToDouble(L"1.999999881"));
 }
 
-TEST(fxstring, DoubleToString) {
-  char buf[32];
-
-  EXPECT_STREQ("0", TerminatedDoubleToString(0.0f, buf));
-  EXPECT_STREQ("0", TerminatedDoubleToString(-0.0f, buf));
-  EXPECT_STREQ(
-      "0", TerminatedDoubleToString(std::numeric_limits<double>::min(), buf));
-  EXPECT_STREQ(
-      "0", TerminatedDoubleToString(-std::numeric_limits<double>::min(), buf));
-
-  EXPECT_STREQ("0.25", TerminatedDoubleToString(0.25f, buf));
-  EXPECT_STREQ("-0.25", TerminatedDoubleToString(-0.25f, buf));
-
-  EXPECT_STREQ("100", TerminatedDoubleToString(100.0f, buf));
-  EXPECT_STREQ("-100", TerminatedDoubleToString(-100.0f, buf));
-
-  // DoubleToString won't convert beyond the maximum integer, and values
-  // larger than that get converted to a string representing that.
-  EXPECT_STREQ("2147483647", TerminatedDoubleToString(2147483647.0f, buf));
-  EXPECT_STREQ("2147483647", TerminatedDoubleToString(2147483647.5f, buf));
-  EXPECT_STREQ("2147483647", TerminatedDoubleToString(
-                                 std::numeric_limits<double>::max(), buf));
-
-  // DoubleToString won't convert beyond the minimum integer, and values
-  // smaller than that get converted to a string representing that.
-  EXPECT_STREQ("-2147483647", TerminatedDoubleToString(-2147483647.0f, buf));
-  EXPECT_STREQ("-2147483647", TerminatedDoubleToString(-2147483647.5f, buf));
-  EXPECT_STREQ("-2147483647", TerminatedDoubleToString(
-                                  -std::numeric_limits<double>::max(), buf));
-
-  // Conversion only acknowledges precision to 5 digit past decimal, and
-  // rounds beyond that.
-  EXPECT_STREQ("1", TerminatedDoubleToString(1.000001119f, buf));
-  EXPECT_STREQ("1.00001", TerminatedDoubleToString(1.000011119f, buf));
-  EXPECT_STREQ("1.99999", TerminatedDoubleToString(1.999988881f, buf));
-  EXPECT_STREQ("2", TerminatedDoubleToString(1.999999881f, buf));
-}
-
 TEST(fxstring, SplitByteString) {
   std::vector<ByteString> result;
   result = fxcrt::Split(ByteString(""), ',');