diff options
author | Kefu Chai <tchaikov@gmail.com> | 2021-12-19 05:25:50 +0100 |
---|---|---|
committer | Kefu Chai <tchaikov@gmail.com> | 2021-12-19 08:02:45 +0100 |
commit | b5d638dc0f3045f1e9c9a796b6271a84ac5ed393 (patch) | |
tree | fa18d220d680cdca38c2d90ca2c3bf40452a5094 /src/include/str_map.h | |
parent | Merge PR #44208 into master (diff) | |
download | ceph-b5d638dc0f3045f1e9c9a796b6271a84ac5ed393.tar.xz ceph-b5d638dc0f3045f1e9c9a796b6271a84ac5ed393.zip |
common/str_map: reimplement get_str_list() using for_each_pair
to avoid creating a temporary list<string> and then dropping it
on the floor after iterating through it for collecting the kv
pairs in it.
Signed-off-by: Kefu Chai <tchaikov@gmail.com>
Diffstat (limited to '')
-rw-r--r-- | src/include/str_map.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/include/str_map.h b/src/include/str_map.h index f8bef057902..7f354fd4696 100644 --- a/src/include/str_map.h +++ b/src/include/str_map.h @@ -23,6 +23,23 @@ #include <string> #include <sstream> +template <typename Func> +void for_each_pair(std::string_view s, const char* delims, Func&& f) +{ + auto pos = s.find_first_not_of(delims); + while (pos != s.npos) { + s.remove_prefix(pos); // trim delims from the front + auto end = s.find_first_of(delims); + auto kv = s.substr(0, end); + if (auto equal = kv.find('='); equal != kv.npos) { + f(kv.substr(0, equal), kv.substr(equal + 1)); + } else { + f(kv.substr(0, equal), std::string_view()); + } + pos = s.find_first_not_of(delims, end); + } +} + using str_map_t = std::map<std::string,std::string>; /** |