MCPcopy Create free account
hub / github.com/AimRT/AimRT / SplitToMap

Function SplitToMap

src/common/util/string_util.h:56–91  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

54 requires(std::is_same_v<StringType, std::string_view> ||
55 std::is_same_v<StringType, std::string>)
56inline std::map<StringType, StringType> SplitToMap(std::string_view source,
57 std::string_view vsep = "&",
58 std::string_view msep = "=",
59 bool trim = true,
60 bool clear = true) {
61 std::map<StringType, StringType> result;
62
63 if (source.empty() || vsep.empty() || msep.empty() || vsep == msep)
64 return result;
65 size_t v_pos_end = 0, v_pos_start = 0;
66 do {
67 v_pos_end = source.find(vsep, v_pos_start);
68 if (v_pos_end == std::string_view::npos) v_pos_end = source.length();
69 if (v_pos_end > v_pos_start) {
70 auto kv_str = source.substr(v_pos_start, v_pos_end - v_pos_start);
71 size_t msep_pos = kv_str.find(msep);
72 if (msep_pos != std::string_view::npos) {
73 auto first = kv_str.substr(0, msep_pos);
74 auto second = kv_str.substr(msep_pos + msep.size());
75 if (trim) {
76 Trim(first);
77 if (!(clear && first.empty())) {
78 result[StringType(first)] = StringType(Trim(second));
79 }
80 } else {
81 if (!(clear && first.empty())) {
82 result[StringType(first)] = StringType(second);
83 }
84 }
85 }
86 }
87 v_pos_start = v_pos_end + vsep.size();
88 } while (v_pos_end < source.length());
89
90 return result;
91}
92
93/**
94 * @brief The string of the splicing map is a=1&b=2&c=3

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected