diff --git a/include/opendht/utils.h b/include/opendht/utils.h index 7de8ec848e6689c533272854400df70cd1e4f6b6..691e936e5462ea79c759761f7f8ea0b36479dabe 100644 --- a/include/opendht/utils.h +++ b/include/opendht/utils.h @@ -178,8 +178,8 @@ msgpack::object* findMapValue(const msgpack::object& map, const char* key, size_ inline msgpack::object* findMapValue(const msgpack::object& map, const char* key) { return findMapValue(map, key, strlen(key)); } -inline msgpack::object* findMapValue(const msgpack::object& map, const std::string& key) { - return findMapValue(map, key.c_str(), key.size()); +inline msgpack::object* findMapValue(const msgpack::object& map, std::string_view key) { + return findMapValue(map, key.data(), key.size()); } } // namespace dht diff --git a/include/opendht/value.h b/include/opendht/value.h index 5539ee8756fef15773ffea07b985e82d2a75a0cc..43f2f75320050d9c3000dc2b6b8133476630b2b0 100644 --- a/include/opendht/value.h +++ b/include/opendht/value.h @@ -270,8 +270,8 @@ struct OPENDHT_PUBLIC Value }; } - static Filter UserTypeFilter(const std::string& ut) { - return [ut](const Value& v) { + static Filter UserTypeFilter(std::string ut) { + return [ut = std::move(ut)](const Value& v) { return v.user_type == ut; }; } @@ -660,7 +660,7 @@ struct OPENDHT_PUBLIC FieldValue FieldValue() {} FieldValue(Value::Field f, uint64_t int_value) : field(f), intValue(int_value) {} FieldValue(Value::Field f, InfoHash hash_value) : field(f), hashValue(hash_value) {} - FieldValue(Value::Field f, Blob blob_value) : field(f), blobValue(blob_value) {} + FieldValue(Value::Field f, Blob blob_value) : field(f), blobValue(std::move(blob_value)) {} bool operator==(const FieldValue& fd) const; @@ -673,9 +673,9 @@ struct OPENDHT_PUBLIC FieldValue template <typename Packer> void msgpack_pack(Packer& p) const { p.pack_map(2); - p.pack(std::string("f")); p.pack(static_cast<uint8_t>(field)); + p.pack("f"sv); p.pack(static_cast<uint8_t>(field)); - p.pack(std::string("v")); + p.pack("v"sv); switch (field) { case Value::Field::Id: case Value::Field::ValueType: @@ -697,12 +697,12 @@ struct OPENDHT_PUBLIC FieldValue hashValue = {}; blobValue.clear(); - if (auto f = findMapValue(msg, "f")) + if (auto f = findMapValue(msg, "f"sv)) field = (Value::Field)f->as<unsigned>(); else throw msgpack::type_error(); - auto v = findMapValue(msg, "v"); + auto v = findMapValue(msg, "v"sv); if (not v) throw msgpack::type_error(); else @@ -864,7 +864,7 @@ struct OPENDHT_PUBLIC Where * * @return the resulting Where instance. */ - Where& userType(std::string user_type) { + Where& userType(std::string_view user_type) { FieldValue fv {Value::Field::UserType, Blob {user_type.begin(), user_type.end()}}; if (std::find(filters_.begin(), filters_.end(), fv) == filters_.end()) filters_.emplace_back(std::move(fv)); diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp index 26940438699be834053275b19fd4103dab5539e9..92c5148566b37e5c5c26b125b1396f628c70d55a 100644 --- a/src/dht_proxy_server.cpp +++ b/src/dht_proxy_server.cpp @@ -38,6 +38,7 @@ using namespace std::placeholders; using namespace std::chrono_literals; +using namespace std::literals; namespace dht { constexpr char RESP_MSG_JSON_INCORRECT[] = "{\"err:\":\"Incorrect JSON\"}"; @@ -151,25 +152,25 @@ struct DhtProxyServer::RestRouterTraits : public restinio::default_traits_t void DhtProxyServer::PermanentPut::msgpack_unpack(const msgpack::object& o) { - if (auto cid = findMapValue(o, "cid")) { + if (auto cid = findMapValue(o, "cid"sv)) { clientId = cid->as<std::string>(); } - if (auto exp = findMapValue(o, "exp")) { + if (auto exp = findMapValue(o, "exp"sv)) { expiration = from_time_t(exp->as<time_t>()); } - if (auto token = findMapValue(o, "token")) { + if (auto token = findMapValue(o, "token"sv)) { pushToken = token->as<std::string>(); } - if (auto sid = findMapValue(o, "sid")) { + if (auto sid = findMapValue(o, "sid"sv)) { if (not sessionCtx) sessionCtx = std::make_shared<PushSessionContext>(sid->as<std::string>()); else sessionCtx->sessionId = sid->as<std::string>(); } - if (auto t = findMapValue(o, "t")) { + if (auto t = findMapValue(o, "t"sv)) { type = t->as<PushType>(); } - if (auto val = findMapValue(o, "value")) { + if (auto val = findMapValue(o, "value"sv)) { value = std::make_shared<dht::Value>(*val); } } @@ -178,19 +179,19 @@ DhtProxyServer::PermanentPut::msgpack_unpack(const msgpack::object& o) void DhtProxyServer::Listener::msgpack_unpack(const msgpack::object& o) { - if (auto cid = findMapValue(o, "cid")) { + if (auto cid = findMapValue(o, "cid"sv)) { clientId = cid->as<std::string>(); } - if (auto exp = findMapValue(o, "exp")) { + if (auto exp = findMapValue(o, "exp"sv)) { expiration = from_time_t(exp->as<time_t>()); } - if (auto sid = findMapValue(o, "sid")) { + if (auto sid = findMapValue(o, "sid"sv)) { if (not sessionCtx) sessionCtx = std::make_shared<PushSessionContext>(sid->as<std::string>()); else sessionCtx->sessionId = sid->as<std::string>(); } - if (auto t = findMapValue(o, "t")) { + if (auto t = findMapValue(o, "t"sv)) { type = t->as<PushType>(); } } @@ -345,7 +346,7 @@ DhtProxyServer::loadState(Is& is, size_t size) { while (pac.next(oh)) { if (oh.get().type != msgpack::type::MAP) continue; - if (auto puts = findMapValue(oh.get(), "puts")) { + if (auto puts = findMapValue(oh.get(), "puts"sv)) { std::lock_guard<std::mutex> lock(lockSearchPuts_); puts_ = puts->as<decltype(puts_)>(); if (logger_) @@ -382,7 +383,7 @@ DhtProxyServer::loadState(Is& is, size_t size) { logger_->d("No persistent puts in state"); } #ifdef OPENDHT_PUSH_NOTIFICATIONS - if (auto pushListeners = findMapValue(oh.get(), "pushListeners")) { + if (auto pushListeners = findMapValue(oh.get(), "pushListeners"sv)) { std::lock_guard<std::mutex> lock(lockListener_); pushListeners_ = pushListeners->as<decltype(pushListeners_)>(); if (logger_) diff --git a/src/parsed_message.h b/src/parsed_message.h index a4538e7f368b20448d35f791cc818ec3a893f3ac..fea98d4dccbbd0b4378d5c7916d817bb0889e983 100644 --- a/src/parsed_message.h +++ b/src/parsed_message.h @@ -237,8 +237,8 @@ ParsedMessage::msgpack_unpack(const msgpack::object& msg) throw msgpack::type_error(); for (size_t i = 0; i < parsed.v->via.map.size; ++i) { auto& vdat = parsed.v->via.map.ptr[i]; - auto o = findMapValue(vdat.val, "o"); - auto d = findMapValue(vdat.val, "d"); + auto o = findMapValue(vdat.val, "o"sv); + auto d = findMapValue(vdat.val, "d"sv); if (not o or not d) continue; value_parts.emplace(vdat.key.as<unsigned>(), std::pair<size_t, Blob>(o->as<size_t>(), unpackBlob(*d))); @@ -341,9 +341,9 @@ ParsedMessage::msgpack_unpack(const msgpack::object& msg) } } } else if (parsedReq.fields) { - if (auto rfields = findMapValue(*parsedReq.fields, "f")) { + if (auto rfields = findMapValue(*parsedReq.fields, "f"sv)) { auto vfields = rfields->as<std::set<Value::Field>>(); - if (auto rvalues = findMapValue(*parsedReq.fields, "v")) { + if (auto rvalues = findMapValue(*parsedReq.fields, "v"sv)) { if (rvalues->type != msgpack::type::ARRAY) throw msgpack::type_error(); size_t val_num = rvalues->via.array.size / vfields.size(); diff --git a/src/value.cpp b/src/value.cpp index 6279921923d6a609ed4463711467b274f748f292..8252b56fe58a5b5c82026b1bb6c9c9a7e0d649f5 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -482,13 +482,13 @@ Query::msgpack_unpack(const msgpack::object& o) if (o.type != msgpack::type::MAP) throw msgpack::type_error(); - auto rfilters = findMapValue(o, "w"); /* unpacking filters */ + auto rfilters = findMapValue(o, "w"sv); /* unpacking filters */ if (rfilters) where.msgpack_unpack(*rfilters); else throw msgpack::type_error(); - auto rfield_selector = findMapValue(o, "s"); /* unpacking field selectors */ + auto rfield_selector = findMapValue(o, "s"sv); /* unpacking field selectors */ if (rfield_selector) select.msgpack_unpack(*rfield_selector); else