From: Xiaoxi Chen Date: Mon, 11 May 2015 06:46:53 +0000 (+0800) Subject: common/str_map: trim start/tailing writesapce in key and value. X-Git-Tag: v9.0.2~157^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=fcea61045d8c61ebf62e2ba3b57fb20d0cb08fe2;p=ceph.git common/str_map: trim start/tailing writesapce in key and value. Signed-off-by: Xiaoxi Chen --- diff --git a/src/common/str_map.cc b/src/common/str_map.cc index b731af334d40..bd68612aa135 100644 --- a/src/common/str_map.cc +++ b/src/common/str_map.cc @@ -58,6 +58,20 @@ int get_json_str_map( } return 0; } +string trim(const string& 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(); +} int get_str_map( const string &str, @@ -71,9 +85,9 @@ int get_str_map( if (equal == string::npos) (*str_map)[*i] = string(); else { - const string key = i->substr(0, equal); + const string key = trim(i->substr(0, equal)); equal++; - const string value = i->substr(equal); + const string value = trim(i->substr(equal)); (*str_map)[key] = value; } } diff --git a/src/test/common/test_str_map.cc b/src/test/common/test_str_map.cc index b1a27c82e9a9..5a324ba9b31e 100644 --- a/src/test/common/test_str_map.cc +++ b/src/test/common/test_str_map.cc @@ -56,6 +56,14 @@ TEST(str_map, plaintext) { ASSERT_EQ(0, get_str_map("", &str_map)); ASSERT_EQ(0u, str_map.size()); } + { + map str_map; + ASSERT_EQ(0, get_str_map(" key1=val1; key2=\tval2; key3\t = \t val3; \n ", "\n;", &str_map)); + ASSERT_EQ(4u, str_map.size()); + ASSERT_EQ("val1", str_map["key1"]); + ASSERT_EQ("val2", str_map["key2"]); + ASSERT_EQ("val3", str_map["key3"]); + } } /*