From 5ec1c196ee2560db574222e142e743afec662500 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Thu, 28 Feb 2019 23:41:59 -0600 Subject: [PATCH] common/str_map: better trim() impl using boost Thanks Kefu! Signed-off-by: Sage Weil (cherry picked from commit 6154874aa9ae6dd66a2a0decb289f2a718f65048) Conflicts: src/common/str_map.cc : Resolved in trim --- src/common/str_map.cc | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/common/str_map.cc b/src/common/str_map.cc index 6278473de296b..947ad21a4b2de 100644 --- a/src/common/str_map.cc +++ b/src/common/str_map.cc @@ -17,6 +17,8 @@ #include "include/str_map.h" #include "include/str_list.h" +#include + #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 (isspace(str[start]) != 0 && start <= end) { - ++start; - } - while (isspace(str[end]) != 0 && start <= end) { - --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( -- 2.39.5