From 263a103be2f54bcc7b1aaeab49921a3fe17aad1f Mon Sep 17 00:00:00 2001 From: Alexander Reichert Date: Fri, 1 Dec 2023 18:51:07 +0100 Subject: [PATCH] util: remove obsolete function starts_with --- daemon/api/test/test_api.cpp | 1 - .../common/app/manifest/volume/src/volume.cpp | 3 +- .../deployment/src/deployment_docker.cpp | 4 +- daemon/modules/apps/src/impl/apps_impl.cpp | 1 + .../instances/src/impl/instances_impl.cpp | 3 +- daemon/modules/system/src/system.cpp | 13 ++--- flunder/src/impl/flunder_client_impl.cpp | 18 +++---- util/archive/src/archive.cpp | 1 - util/cxx20/string.h | 52 ------------------- util/cxx20/test/test_cxx20.cpp | 12 ----- util/fs/fs.h | 4 +- util/network/src/netif_type.cpp | 12 ++--- 12 files changed, 24 insertions(+), 100 deletions(-) diff --git a/daemon/api/test/test_api.cpp b/daemon/api/test/test_api.cpp index a5e1edfaf..2204f63ed 100644 --- a/daemon/api/test/test_api.cpp +++ b/daemon/api/test/test_api.cpp @@ -18,7 +18,6 @@ #include "daemon/api/api.h" #include "gtest/gtest.h" -#include "util/cxx20/string.h" #include "util/json/json.h" class test_flecs_api_t diff --git a/daemon/common/app/manifest/volume/src/volume.cpp b/daemon/common/app/manifest/volume/src/volume.cpp index 540607a07..44b860900 100644 --- a/daemon/common/app/manifest/volume/src/volume.cpp +++ b/daemon/common/app/manifest/volume/src/volume.cpp @@ -16,7 +16,6 @@ #include -#include "util/cxx20/string.h" #include "util/fs/fs.h" #include "util/string/string_utils.h" @@ -37,7 +36,7 @@ volume_t::volume_t(std::string_view volume_str) noexcept return; } - if (cxx20::starts_with(parts[0], '/')) { + if (parts[0].starts_with('/')) { // bind mount try { const auto path = fs::path{parts[0]}; diff --git a/daemon/common/deployment/src/deployment_docker.cpp b/daemon/common/deployment/src/deployment_docker.cpp index 5cc00c4b5..646174545 100644 --- a/daemon/common/deployment/src/deployment_docker.cpp +++ b/daemon/common/deployment/src/deployment_docker.cpp @@ -120,7 +120,7 @@ auto deployment_docker_t::create_container(std::shared_ptr instance) if (!network.mac_address.empty()) { docker_process.arg("--mac-address"); - if (cxx20::starts_with(network.mac_address, "clone:")) { + if (network.mac_address.starts_with("clone:")) { const auto parts = split(network.mac_address, ':'); if (parts.size() != 2) { return {-1, "Cloned MAC address is invalid"}; @@ -199,7 +199,7 @@ auto deployment_docker_t::create_container(std::shared_ptr instance) trim(cmd); cmd.erase(cmd.find_first_of('['), 1); cmd.erase(cmd.find_last_of(']'), 1); - if (cxx20::starts_with(cmd, "/bin/sh -c ")) { + if (cmd.starts_with("/bin/sh -c ")) { cmd.erase(0, 11); } diff --git a/daemon/modules/apps/src/impl/apps_impl.cpp b/daemon/modules/apps/src/impl/apps_impl.cpp index e47c0ec55..38dd1cf9d 100644 --- a/daemon/modules/apps/src/impl/apps_impl.cpp +++ b/daemon/modules/apps/src/impl/apps_impl.cpp @@ -27,6 +27,7 @@ #include "modules/jobs/jobs.h" #include "modules/manifests/manifests.h" #include "modules/marketplace/marketplace.h" +#include "util/cxx20/string.h" #include "util/fs/fs.h" #include "util/json/json.h" #include "util/process/process.h" diff --git a/daemon/modules/instances/src/impl/instances_impl.cpp b/daemon/modules/instances/src/impl/instances_impl.cpp index 0bf641ecf..1a6ebb91f 100644 --- a/daemon/modules/instances/src/impl/instances_impl.cpp +++ b/daemon/modules/instances/src/impl/instances_impl.cpp @@ -22,7 +22,6 @@ #include "modules/factory/factory.h" #include "modules/jobs/jobs.h" #include "modules/system/system.h" -#include "util/cxx20/string.h" #include "util/datetime/datetime.h" #include "util/network/network.h" #include "util/process/process.h" @@ -63,7 +62,7 @@ auto build_network_adapters_json(std::shared_ptr instance) } } for (decltype(auto) network : instance->networks()) { - if (cxx20::starts_with(network.network_name, "flecs-macvlan-")) { + if (network.network_name.starts_with("flecs-macvlan-")) { const auto adapter = network.network_name.substr(14); if (!adapters.count(adapter)) { auto adapter_json = json_t{}; diff --git a/daemon/modules/system/src/system.cpp b/daemon/modules/system/src/system.cpp index c60be12e1..8f805ca8c 100644 --- a/daemon/modules/system/src/system.cpp +++ b/daemon/modules/system/src/system.cpp @@ -25,7 +25,6 @@ #include #include "factory/factory.h" -#include "util/cxx20/string.h" #include "util/network/ip_addr.h" #include "util/network/network.h" #include "util/string/string_utils.h" @@ -171,17 +170,15 @@ auto module_system_t::get_network_adapters() const // } for (decltype(auto) adapter : adapters) { - if ((cxx20::starts_with(adapter.first, "en") || - (cxx20::starts_with(adapter.first, "eth")))) { + if (adapter.first.starts_with("en") || adapter.first.starts_with("eth")) { adapter.second.type = netif_type_t::WIRED; - } else if ((cxx20::starts_with(adapter.first, "wl"))) { + } else if (adapter.first.starts_with("wl")) { adapter.second.type = netif_type_t::WIRELESS; - } else if ((cxx20::starts_with(adapter.first, "lo"))) { + } else if (adapter.first.starts_with("lo")) { adapter.second.type = netif_type_t::LOCAL; - } else if ((cxx20::starts_with(adapter.first, "veth"))) { + } else if (adapter.first.starts_with("veth")) { adapter.second.type = netif_type_t::VIRTUAL; - } else if ((cxx20::starts_with(adapter.first, "br") || - (cxx20::starts_with(adapter.first, "docker")))) { + } else if (adapter.first.starts_with("br") || adapter.first.starts_with("docker")) { adapter.second.type = netif_type_t::BRIDGE; } else { adapter.second.type = UNKNOWN; diff --git a/flunder/src/impl/flunder_client_impl.cpp b/flunder/src/impl/flunder_client_impl.cpp index 9077e8503..fa8dac651 100644 --- a/flunder/src/impl/flunder_client_impl.cpp +++ b/flunder/src/impl/flunder_client_impl.cpp @@ -18,8 +18,6 @@ #include -#include "util/cxx20/string.h" - namespace FLECS { namespace impl { @@ -128,7 +126,7 @@ auto encoding_from_string(std::string_view encoding) // } for (const auto& it : encodings) { - if (!it.first.empty() && cxx20::starts_with(encoding, it.first)) { + if (!it.first.empty() && encoding.starts_with(it.first)) { return {it.second, encoding.substr(it.first.length())}; } } @@ -271,7 +269,7 @@ auto flunder_client_t::publish( options.encoding = encoding; options.congestion_control = z_congestion_control_t::Z_CONGESTION_CONTROL_BLOCK; - const auto keyexpr = cxx20::starts_with(topic, '/') ? topic.data() + 1 : topic.data(); + const auto keyexpr = topic.starts_with('/') ? topic.data() + 1 : topic.data(); const auto res = z_put( z_session_loan(&_z_session), @@ -309,7 +307,7 @@ auto flunder_client_t::subscribe( const void* userp) // -> int { - const auto keyexpr = cxx20::starts_with(topic, '/') ? topic.data() + 1 : topic.data(); + const auto keyexpr = topic.starts_with('/') ? topic.data() + 1 : topic.data(); if (_subscriptions.count(keyexpr) > 0) { return -1; @@ -356,7 +354,7 @@ auto flunder_client_t::subscribe( auto flunder_client_t::unsubscribe(std::string_view topic) // -> int { - const auto keyexpr = cxx20::starts_with(topic, '/') ? topic.data() + 1 : topic.data(); + const auto keyexpr = topic.starts_with('/') ? topic.data() + 1 : topic.data(); auto it = _subscriptions.find(keyexpr); if (it == _subscriptions.cend()) { @@ -376,7 +374,7 @@ auto flunder_client_t::add_mem_storage(std::string name, std::string_view topic) return -1; } - const auto keyexpr = cxx20::starts_with(topic, '/') ? topic.data() + 1 : topic.data(); + const auto keyexpr = topic.starts_with('/') ? topic.data() + 1 : topic.data(); auto url = cpr::Url{std::string{"http://"} .append(_host) @@ -435,7 +433,7 @@ auto flunder_client_t::get(std::string_view topic) const // auto options = z_get_options_default(); options.target = Z_QUERY_TARGET_ALL; - auto keyexpr = z_keyexpr(cxx20::starts_with(topic, '/') ? topic.data() + 1 : topic.data()); + auto keyexpr = z_keyexpr(topic.starts_with('/') ? topic.data() + 1 : topic.data()); if (!z_keyexpr_is_initialized(&keyexpr)) { return {-1, vars}; } @@ -450,7 +448,7 @@ auto flunder_client_t::get(std::string_view topic) const // auto keyexpr = z_keyexpr_to_string(sample.keyexpr); auto keystr = std::string{"/"} + std::string{keyexpr._cstr}; z_str_drop(z_move(keyexpr)); - if (cxx20::starts_with(keystr, "/@")) { + if (keystr.starts_with("/@")) { continue; } @@ -477,7 +475,7 @@ auto flunder_client_t::get(std::string_view topic) const // auto flunder_client_t::erase(std::string_view topic) // -> int { - const auto keyexpr = cxx20::starts_with(topic, '/') ? topic.data() + 1 : topic.data(); + const auto keyexpr = topic.starts_with('/') ? topic.data() + 1 : topic.data(); auto options = z_delete_options_default(); const auto res = z_delete(z_session_loan(&_z_session), z_keyexpr(keyexpr), &options); diff --git a/util/archive/src/archive.cpp b/util/archive/src/archive.cpp index 0f90421c3..d5df9bcdf 100644 --- a/util/archive/src/archive.cpp +++ b/util/archive/src/archive.cpp @@ -26,7 +26,6 @@ #include "archive/impl/read_archive.h" #include "archive/impl/write_archive.h" #include "archive/impl/write_disk.h" -#include "util/cxx20/string.h" #include "util/string/literals.h" namespace FLECS { diff --git a/util/cxx20/string.h b/util/cxx20/string.h index 26d85a705..852966788 100644 --- a/util/cxx20/string.h +++ b/util/cxx20/string.h @@ -70,57 +70,5 @@ constexpr bool contains(const CharT* str, const CharT* s) return contains(std::basic_string_view{str}, s); } -template -constexpr bool starts_with( - std::basic_string_view sv, std::basic_string_view sv_cmp) noexcept -{ - return sv.substr(0, sv_cmp.size()) == sv_cmp; -} -template -constexpr bool starts_with(std::basic_string_view sv, CharT c) noexcept -{ - return !sv.empty() && Traits::eq(sv.front(), c); -} -template -constexpr bool starts_with(std::basic_string_view sv, const CharT* s) -{ - return starts_with(sv, std::basic_string_view{s}); -} - -template -constexpr bool starts_with( - const std::basic_string& str, std::basic_string_view sv) noexcept -{ - return starts_with(std::basic_string_view{str}, sv); -} -template -constexpr bool starts_with(const std::basic_string& str, CharT c) noexcept -{ - return starts_with(std::basic_string_view{str}, c); -} -template -constexpr bool starts_with(const std::basic_string& str, const CharT* s) -{ - return starts_with(std::basic_string_view{str}, s); -} - -template -constexpr bool starts_with(const CharT* str, std::basic_string_view sv) noexcept -{ - return starts_with(std::basic_string_view{str}, sv); -} - -template -constexpr bool starts_with(const CharT* str, CharT c) noexcept -{ - return starts_with(std::basic_string_view{str}, c); -} - -template -constexpr bool starts_with(const CharT* str, const CharT* s) noexcept -{ - return starts_with(std::basic_string_view{str}, s); -} - } // namespace cxx20 } // namespace FLECS diff --git a/util/cxx20/test/test_cxx20.cpp b/util/cxx20/test/test_cxx20.cpp index 0b1a12eb2..a5a8fef1e 100644 --- a/util/cxx20/test/test_cxx20.cpp +++ b/util/cxx20/test/test_cxx20.cpp @@ -39,16 +39,4 @@ TEST(cxx20, string) ASSERT_FALSE(FLECS::cxx20::contains(s, "123"sv)); ASSERT_FALSE(FLECS::cxx20::contains(str, "123"sv)); ASSERT_FALSE(FLECS::cxx20::contains(sv, "123"sv)); - - ASSERT_TRUE(FLECS::cxx20::starts_with(s, "This")); - ASSERT_TRUE(FLECS::cxx20::starts_with(str, "This")); - ASSERT_TRUE(FLECS::cxx20::starts_with(sv, "This")); - - ASSERT_TRUE(FLECS::cxx20::starts_with(s, "This"sv)); - ASSERT_TRUE(FLECS::cxx20::starts_with(str, "This"sv)); - ASSERT_TRUE(FLECS::cxx20::starts_with(sv, "This"sv)); - - ASSERT_TRUE(FLECS::cxx20::starts_with(s, 'T')); - ASSERT_TRUE(FLECS::cxx20::starts_with(str, 'T')); - ASSERT_TRUE(FLECS::cxx20::starts_with(sv, 'T')); } diff --git a/util/fs/fs.h b/util/fs/fs.h index fcc90eb32..1851bd696 100644 --- a/util/fs/fs.h +++ b/util/fs/fs.h @@ -16,8 +16,6 @@ #include -#include "util/cxx20/string.h" - namespace FLECS { namespace fs = std::filesystem; @@ -28,7 +26,7 @@ class tmpdir_t explicit tmpdir_t(fs::path dir) noexcept : _dir{std::move(dir)} { - if (!_dir.is_absolute() || !cxx20::starts_with(_dir.c_str(), "/var/lib/flecs/")) { + if (!_dir.is_absolute() || !_dir.string().starts_with("/var/lib/flecs/")) { _dir.clear(); return; } diff --git a/util/network/src/netif_type.cpp b/util/network/src/netif_type.cpp index a763db9cd..1f03f2228 100644 --- a/util/network/src/netif_type.cpp +++ b/util/network/src/netif_type.cpp @@ -18,8 +18,6 @@ #include #include -#include "util/cxx20/string.h" - namespace FLECS { namespace netif { @@ -74,15 +72,15 @@ auto from_string(std::string_view str) // auto from_adapter_name(std::string_view str) // -> type { - if ((cxx20::starts_with(str, "en") || (cxx20::starts_with(str, "eth")))) { + if (str.starts_with("en") || str.starts_with("eth")) { return type::Wired; - } else if ((cxx20::starts_with(str, "wl"))) { + } else if (str.starts_with("wl")) { return type::Wireless; - } else if ((cxx20::starts_with(str, "lo"))) { + } else if (str.starts_with("lo")) { return type::Local; - } else if ((cxx20::starts_with(str, "veth"))) { + } else if (str.starts_with("veth")) { return type::Virtual; - } else if ((cxx20::starts_with(str, "br") || (cxx20::starts_with(str, "docker")))) { + } else if (str.starts_with("br") || (str.starts_with("docker"))) { return type::Bridge; }