string_util: simplify TrimWhitespaceASCII

No callers of the function passed anything other than TRIM_ALL, looked
at the return value, or needed to pass a pointer for the output, so
remove those features and improve the tests.

BUG=b:245989146
TEST=cros_sdk env FEATURES="test" emerge-${BOARD} chromeos-base/gestures

Change-Id: Iec25ef20fdafc16bd5e23dc1e6744fdbc439b865
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/gestures/+/5318732
Reviewed-by: Henry Barnor <hbarnor@chromium.org>
Reviewed-by: Kenneth Albanowski <kenalba@google.com>
Reviewed-by: Torsha Banerjee <torsha@google.com>
Tested-by: Harry Cutts <hcutts@chromium.org>
Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
Commit-Queue: Harry Cutts <hcutts@chromium.org>
diff --git a/include/string_util.h b/include/string_util.h
index decaef9..67fda6c 100644
--- a/include/string_util.h
+++ b/include/string_util.h
@@ -21,22 +21,9 @@
 void StringAppendV(std::string* dst, const char* format, va_list ap)
     PRINTF_FORMAT(2, 0);
 
-// Trims any whitespace from either end of the input string.  Returns where
-// whitespace was found.
-// The non-wide version has two functions:
-// * TrimWhitespaceASCII()
-//   This function is for ASCII strings and only looks for ASCII whitespace;
-// Please choose the best one according to your usage.
-// NOTE: Safe to use the same variable for both input and output.
-enum TrimPositions {
-  TRIM_NONE     = 0,
-  TRIM_LEADING  = 1 << 0,
-  TRIM_TRAILING = 1 << 1,
-  TRIM_ALL      = TRIM_LEADING | TRIM_TRAILING,
-};
-TrimPositions TrimWhitespaceASCII(const std::string& input,
-                                              TrimPositions positions,
-                                              std::string* output);
+// Trims whitespace from the start and end of the input string.  This function
+// is for ASCII strings and only looks for ASCII whitespace.
+std::string TrimWhitespaceASCII(const std::string& input);
 
 }  // namespace gestures
 
diff --git a/src/activity_log.cc b/src/activity_log.cc
index 9e6bfbf..d668f9c 100644
--- a/src/activity_log.cc
+++ b/src/activity_log.cc
@@ -579,7 +579,7 @@
   string gestures_version = VCSID;
 
   // Strip tailing whitespace.
-  TrimWhitespaceASCII(gestures_version, TRIM_ALL, &gestures_version);
+  gestures_version = TrimWhitespaceASCII(gestures_version);
   (*root)["gesturesVersion"] = Json::Value(gestures_version);
   (*root)[kKeyProperties] = EncodePropRegistry();
 }
diff --git a/src/activity_replay_unittest.cc b/src/activity_replay_unittest.cc
index 19ff6df..0dd2879 100644
--- a/src/activity_replay_unittest.cc
+++ b/src/activity_replay_unittest.cc
@@ -34,7 +34,7 @@
     if (i == c || str[i] == s) {
       STR tmp(str, last, i - last);
       if (trim_whitespace)
-        TrimWhitespaceASCII(tmp, TRIM_ALL, &tmp);
+        tmp = TrimWhitespaceASCII(tmp);
       // Avoid converting an empty or all-whitespace source string into a vector
       // of one empty string.
       if (i != c || !r->empty() || !tmp.empty())
diff --git a/src/command_line.cc b/src/command_line.cc
index 7f309a2..2779b13 100644
--- a/src/command_line.cc
+++ b/src/command_line.cc
@@ -59,7 +59,7 @@
   bool parse_switches = true;
   for (size_t i = 1; i < argv.size(); ++i) {
     std::string arg = argv[i];
-    TrimWhitespaceASCII(arg, TRIM_ALL, &arg);
+    arg = TrimWhitespaceASCII(arg);
 
     std::string switch_string;
     std::string switch_value;
@@ -131,7 +131,7 @@
 }
 
 void CommandLine::SetProgram(const std::string& program) {
-  TrimWhitespaceASCII(program, TRIM_ALL, &argv_[0]);
+  argv_[0] = TrimWhitespaceASCII(program);
 }
 
 bool CommandLine::HasSwitch(const std::string& switch_string) const {
diff --git a/src/string_util.cc b/src/string_util.cc
index be4fe21..863399c 100644
--- a/src/string_util.cc
+++ b/src/string_util.cc
@@ -86,35 +86,23 @@
 }
 
 template<typename STR>
-TrimPositions TrimStringT(const STR& input,
-                          const typename STR::value_type trim_chars[],
-                          TrimPositions positions,
-                          STR* output) {
-  // Find the edges of leading/trailing whitespace as desired.
-  const typename STR::size_type last_char = input.length() - 1;
-  const typename STR::size_type first_good_char = (positions & TRIM_LEADING) ?
-      input.find_first_not_of(trim_chars) : 0;
-  const typename STR::size_type last_good_char = (positions & TRIM_TRAILING) ?
-      input.find_last_not_of(trim_chars) : last_char;
+STR TrimStringT(const STR& input, const typename STR::value_type trim_chars[]) {
+  if (input.empty()) {
+    return "";
+  }
 
-  // When the string was all whitespace, report that we stripped off whitespace
-  // from whichever position the caller was interested in.  For empty input, we
-  // stripped no whitespace, but we still need to clear |output|.
-  if (input.empty() ||
-      (first_good_char == STR::npos) || (last_good_char == STR::npos)) {
-    bool input_was_empty = input.empty();  // in case output == &input
-    output->clear();
-    return input_was_empty ? TRIM_NONE : positions;
+  // Find the edges of leading/trailing whitespace.
+  const typename STR::size_type first_good_char =
+      input.find_first_not_of(trim_chars);
+  const typename STR::size_type last_good_char =
+      input.find_last_not_of(trim_chars);
+
+  if (first_good_char == STR::npos || last_good_char == STR::npos) {
+    return "";
   }
 
   // Trim the whitespace.
-  *output =
-      input.substr(first_good_char, last_good_char - first_good_char + 1);
-
-  // Return where we trimmed from.
-  return static_cast<TrimPositions>(
-      ((first_good_char == 0) ? TRIM_NONE : TRIM_LEADING) |
-      ((last_good_char == last_char) ? TRIM_NONE : TRIM_TRAILING));
+  return input.substr(first_good_char, last_good_char - first_good_char + 1);
 }
 
 }  // namespace
@@ -132,10 +120,8 @@
   return result;
 }
 
-TrimPositions TrimWhitespaceASCII(const std::string& input,
-                                  TrimPositions positions,
-                                  std::string* output) {
-  return TrimStringT(input, kWhitespaceASCII, positions, output);
+std::string TrimWhitespaceASCII(const std::string& input) {
+  return TrimStringT(input, kWhitespaceASCII);
 }
 
 }  // namespace gestures
diff --git a/src/string_util_unittest.cc b/src/string_util_unittest.cc
index 3529ab9..b1edda6 100644
--- a/src/string_util_unittest.cc
+++ b/src/string_util_unittest.cc
@@ -13,9 +13,7 @@
 
 class StringUtilTest : public ::testing::Test {};
 
-// This test adds code coverage to string_util.
-
-TEST(StringUtilTest, SimpleTest) {
+TEST(StringUtilTest, StringPrintfTest) {
   const char *pstr =
     "0123456789012345678901234567890123456789012345678901234567890123456789";
   std::string str = StringPrintf(
@@ -26,10 +24,16 @@
   );
   int expected_length = (70*15)+15+1;
   EXPECT_EQ(str.size(), expected_length);
+}
 
-  TrimPositions trimmed_from = TrimWhitespaceASCII(str, TRIM_ALL, &str);
-  EXPECT_EQ(trimmed_from, TRIM_ALL);
-  EXPECT_EQ(str.size(), expected_length-2);
+TEST(StringUtilTest, TrimWhitespaceASCIITest) {
+  EXPECT_EQ(TrimWhitespaceASCII(""), "");
+  EXPECT_EQ(TrimWhitespaceASCII(" x    "), "x");
+  EXPECT_EQ(TrimWhitespaceASCII("badger"), "badger");
+  EXPECT_EQ(TrimWhitespaceASCII("badger  "), "badger");
+  EXPECT_EQ(TrimWhitespaceASCII("  badger"), "badger");
+  EXPECT_EQ(TrimWhitespaceASCII("  \t \n\r "), "");
+  EXPECT_EQ(TrimWhitespaceASCII("   Bees and ponies     "), "Bees and ponies");
 }
 
 }  // namespace gestures