]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/str_map: trim start/tailing writesapce in key and value.
authorXiaoxi Chen <xiaoxi.chen@intel.com>
Mon, 11 May 2015 06:46:53 +0000 (14:46 +0800)
committerXiaoxi Chen <xiaoxi.chen@intel.com>
Wed, 13 May 2015 00:22:45 +0000 (08:22 +0800)
Signed-off-by: Xiaoxi Chen <xiaoxi.chen@intel.com>
src/common/str_map.cc
src/test/common/test_str_map.cc

index b731af334d40ac71ae436d275b240c36283f6a4e..bd68612aa135e42883a6c460adf3ebc0200641f4 100644 (file)
@@ -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;
     }
   }
index b1a27c82e9a9a87696134a9ad81fc8ae3360b3ea..5a324ba9b31e513b5eeb6ce7312340f66ef661c4 100644 (file)
@@ -56,6 +56,14 @@ TEST(str_map, plaintext) {
     ASSERT_EQ(0, get_str_map("", &str_map));
     ASSERT_EQ(0u, str_map.size());
   }
+  {
+    map<string,string> 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"]);
+  }
 }
 
 /*