]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/str_map: better trim() impl using boost 26698/head
authorSage Weil <sage@redhat.com>
Fri, 1 Mar 2019 05:41:59 +0000 (23:41 -0600)
committerSage Weil <sage@redhat.com>
Fri, 1 Mar 2019 05:41:59 +0000 (23:41 -0600)
Thanks Kefu!

Signed-off-by: Sage Weil <sage@redhat.com>
src/common/str_map.cc

index b1f2b78ee90be376e8900abe3dd314b1ec156340..947ad21a4b2de192c3fbbdc00b25ff124b57dccb 100644 (file)
@@ -17,6 +17,8 @@
 #include "include/str_map.h"
 #include "include/str_list.h"
 
+#include <boost/algorithm/string.hpp>
+
 #include "json_spirit/json_spirit.h"
 
 using namespace std;
@@ -56,22 +58,13 @@ int get_json_str_map(
   }
   return 0;
 }
+
 string trim(const string& str) {
-  if (str.empty()) {
-    return str;
-  }
-  size_t start = 0;
-  size_t end = str.size() - 1;
-  while (start <= end && isspace(str[start]) != 0) {
-    ++start;
-  }
-  while (start <= end && isspace(str[end]) != 0) {
-    --end;
-  }
-  if (start <= end) {
-    return str.substr(start, end - start + 1);
-  }
-  return string();
+  return boost::algorithm::trim_copy_if(
+    str,
+    [](unsigned char c) {
+      return std::isspace(c);
+    });
 }
 
 int get_str_map(