From 6e7facf58c0edea766e7db4d30c22f1dadcd4dbd Mon Sep 17 00:00:00 2001
From: Gunhan Gulsoy <gunan@google.com>
Date: Sun, 11 Aug 2019 00:50:56 -0700
Subject: [PATCH] Remove SplitAndParseAsFloats from tensorflow::strings API.

The function is very specific, and has limited use.

PiperOrigin-RevId: 262782380
---
 .../core/kernels/spectrogram_test_utils.cc    |  8 +++++-
 tensorflow/core/lib/strings/str_util.cc       | 10 --------
 tensorflow/core/lib/strings/str_util.h        |  2 --
 tensorflow/core/lib/strings/str_util_test.cc  | 25 -------------------
 tensorflow/examples/multibox_detector/main.cc | 10 +++++---
 tensorflow/tools/benchmark/benchmark_model.cc | 16 +++++++++---
 6 files changed, 26 insertions(+), 45 deletions(-)

diff --git a/tensorflow/core/kernels/spectrogram_test_utils.cc b/tensorflow/core/kernels/spectrogram_test_utils.cc
index bb9d18e915a..684cbc19e77 100644
--- a/tensorflow/core/kernels/spectrogram_test_utils.cc
+++ b/tensorflow/core/kernels/spectrogram_test_utils.cc
@@ -20,6 +20,7 @@ limitations under the License.
 
 #include "tensorflow/core/lib/core/status_test_util.h"
 #include "tensorflow/core/lib/io/path.h"
+#include "tensorflow/core/lib/strings/numbers.h"
 #include "tensorflow/core/lib/strings/str_util.h"
 #include "tensorflow/core/lib/wav/wav_io.h"
 #include "tensorflow/core/platform/env.h"
@@ -162,7 +163,12 @@ void ReadCSVFileToArrayOrDie(const string& filename,
   std::vector<float> values;
   for (int l = 0; l < lines.size(); ++l) {
     values.clear();
-    CHECK(str_util::SplitAndParseAsFloats(lines[l], ',', &values));
+    std::vector<string> split_line = str_util::Split(lines[l], ",");
+    for (const string& token : split_line) {
+      float tmp;
+      CHECK(strings::safe_strtof(token, &tmp));
+      values.push_back(tmp);
+    }
     array->push_back(values);
   }
 }
diff --git a/tensorflow/core/lib/strings/str_util.cc b/tensorflow/core/lib/strings/str_util.cc
index b2feadea9bc..f2e7094d025 100644
--- a/tensorflow/core/lib/strings/str_util.cc
+++ b/tensorflow/core/lib/strings/str_util.cc
@@ -228,16 +228,6 @@ bool SplitAndParseAsInts(StringPiece text, char delim,
   return SplitAndParseAsInts<int64>(text, delim, strings::safe_strto64, result);
 }
 
-bool SplitAndParseAsFloats(StringPiece text, char delim,
-                           std::vector<float>* result) {
-  return SplitAndParseAsInts<float>(
-      text, delim,
-      [](StringPiece str, float* value) {
-        return strings::safe_strtof(str, value);
-      },
-      result);
-}
-
 size_t Strnlen(const char* str, const size_t string_max_len) {
   size_t len = 0;
   while (len < string_max_len && str[len] != '\0') {
diff --git a/tensorflow/core/lib/strings/str_util.h b/tensorflow/core/lib/strings/str_util.h
index 122044b4c91..597a534793b 100644
--- a/tensorflow/core/lib/strings/str_util.h
+++ b/tensorflow/core/lib/strings/str_util.h
@@ -154,8 +154,6 @@ bool SplitAndParseAsInts(StringPiece text, char delim,
                          std::vector<int32>* result);
 bool SplitAndParseAsInts(StringPiece text, char delim,
                          std::vector<int64>* result);
-bool SplitAndParseAsFloats(StringPiece text, char delim,
-                           std::vector<float>* result);
 
 // StartsWith()
 //
diff --git a/tensorflow/core/lib/strings/str_util_test.cc b/tensorflow/core/lib/strings/str_util_test.cc
index 3bf3e99825f..82c97c89696 100644
--- a/tensorflow/core/lib/strings/str_util_test.cc
+++ b/tensorflow/core/lib/strings/str_util_test.cc
@@ -314,31 +314,6 @@ TEST(SplitAndParseAsInts, Int64) {
   EXPECT_FALSE(str_util::SplitAndParseAsInts("13,abc,5", ',', &nums));
 }
 
-TEST(SplitAndParseAsFloats, Float) {
-  std::vector<float> nums;
-  EXPECT_TRUE(str_util::SplitAndParseAsFloats("", ',', &nums));
-  EXPECT_EQ(nums.size(), 0);
-
-  EXPECT_TRUE(str_util::SplitAndParseAsFloats("134.2323", ',', &nums));
-  ASSERT_EQ(nums.size(), 1);
-  EXPECT_NEAR(nums[0], 134.2323f, 1e-5f);
-
-  EXPECT_TRUE(str_util::SplitAndParseAsFloats("134.9,2.123,13.0000,-5.999,1e6",
-                                              ',', &nums));
-  ASSERT_EQ(nums.size(), 5);
-  EXPECT_NEAR(nums[0], 134.9f, 1e-5f);
-  EXPECT_NEAR(nums[1], 2.123f, 1e-5f);
-  EXPECT_NEAR(nums[2], 13.0f, 1e-5f);
-  EXPECT_NEAR(nums[3], -5.999f, 1e-5f);
-  EXPECT_NEAR(nums[4], 1e6f, 1e1f);
-
-  EXPECT_FALSE(str_util::SplitAndParseAsFloats("abc", ',', &nums));
-
-  EXPECT_FALSE(str_util::SplitAndParseAsFloats("-13.0,abc", ',', &nums));
-
-  EXPECT_FALSE(str_util::SplitAndParseAsFloats("13.0,abc,-5.999", ',', &nums));
-}
-
 TEST(Lowercase, Basic) {
   EXPECT_EQ("", str_util::Lowercase(""));
   EXPECT_EQ("hello", str_util::Lowercase("hello"));
diff --git a/tensorflow/examples/multibox_detector/main.cc b/tensorflow/examples/multibox_detector/main.cc
index 82552a71740..823a2c718e4 100644
--- a/tensorflow/examples/multibox_detector/main.cc
+++ b/tensorflow/examples/multibox_detector/main.cc
@@ -16,6 +16,7 @@ limitations under the License.
 #include <setjmp.h>
 #include <stdio.h>
 #include <string.h>
+
 #include <cmath>
 #include <fstream>
 #include <vector>
@@ -31,6 +32,7 @@ limitations under the License.
 #include "tensorflow/core/lib/core/stringpiece.h"
 #include "tensorflow/core/lib/core/threadpool.h"
 #include "tensorflow/core/lib/io/path.h"
+#include "tensorflow/core/lib/strings/numbers.h"
 #include "tensorflow/core/lib/strings/str_util.h"
 #include "tensorflow/core/lib/strings/stringprintf.h"
 #include "tensorflow/core/platform/init_main.h"
@@ -59,9 +61,11 @@ Status ReadLocationsFile(const string& file_name, std::vector<float>* result,
   result->clear();
   string line;
   while (std::getline(file, line)) {
-    std::vector<float> tokens;
-    CHECK(tensorflow::str_util::SplitAndParseAsFloats(line, ',', &tokens));
-    for (auto number : tokens) {
+    std::vector<string> string_tokens = tensorflow::str_util::Split(line, ',');
+    result->reserve(string_tokens.size());
+    for (const string& string_token : string_tokens) {
+      float number;
+      CHECK(tensorflow::strings::safe_strtof(string_token, &number));
       result->push_back(number);
     }
   }
diff --git a/tensorflow/tools/benchmark/benchmark_model.cc b/tensorflow/tools/benchmark/benchmark_model.cc
index 7ebba437e4c..0850a346eb2 100644
--- a/tensorflow/tools/benchmark/benchmark_model.cc
+++ b/tensorflow/tools/benchmark/benchmark_model.cc
@@ -33,6 +33,7 @@ limitations under the License.
 #include "tensorflow/core/graph/algorithm.h"
 #include "tensorflow/core/graph/graph.h"
 #include "tensorflow/core/graph/graph_constructor.h"
+#include "tensorflow/core/lib/strings/numbers.h"
 #include "tensorflow/core/lib/strings/str_util.h"
 #include "tensorflow/core/lib/strings/strcat.h"
 #include "tensorflow/core/platform/env.h"
@@ -546,10 +547,17 @@ int Main(int argc, char** argv) {
     }
     input.name = input_layers[n];
     if (n < input_layer_values.size()) {
-      CHECK(str_util::SplitAndParseAsFloats(input_layer_values[n], ',',
-                                            &input.initialization_values))
-          << "Incorrect initialization values string specified: "
-          << input_layer_values[n];
+      std::vector<string> string_tokens =
+          str_util::Split(input_layer_values[n], ',');
+      input.initialization_values.clear();
+      input.initialization_values.reserve(string_tokens.size());
+      for (const string& str_val : string_tokens) {
+        float val;
+        CHECK(strings::safe_strtof(str_val, &val))
+            << "Incorrect initialization values string specified: "
+            << input_layer_values[n];
+        input.initialization_values.push_back(val);
+      }
     }
     inputs.push_back(input);
   }