From: Joel Rosdahl Date: Sun, 19 Jun 2022 11:52:56 +0000 (+0200) Subject: refactor: Use structured binding declarations X-Git-Tag: v4.7~175 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4812396d96abb28892c4f8088a9d84724e5f4f23;p=thirdparty%2Fccache.git refactor: Use structured binding declarations --- diff --git a/src/Config.cpp b/src/Config.cpp index dae998fc0..9ac91a1d9 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -806,8 +806,8 @@ Config::visit_items(const ItemVisitor& item_visitor) const std::vector keys; keys.reserve(k_config_key_table.size()); - for (const auto& item : k_config_key_table) { - keys.emplace_back(item.first); + for (const auto& [key, value] : k_config_key_table) { + keys.emplace_back(key); } std::sort(keys.begin(), keys.end()); for (const auto& key : keys) { @@ -1014,21 +1014,21 @@ Config::set_item(const std::string& key, break; } - auto result = m_origins.emplace(key, origin); - if (!result.second) { - result.first->second = origin; + const auto& [element, inserted] = m_origins.emplace(key, origin); + if (!inserted) { + element->second = origin; } } void Config::check_key_tables_consistency() { - for (const auto& item : k_env_variable_table) { - if (k_config_key_table.find(item.second) == k_config_key_table.end()) { + for (const auto& [key, value] : k_env_variable_table) { + if (k_config_key_table.find(value) == k_config_key_table.end()) { throw core::Error( "env var {} mapped to {} which is missing from k_config_key_table", - item.first, - item.second); + key, + value); } } } diff --git a/src/ccache.cpp b/src/ccache.cpp index 1bc34eba5..5f95849d0 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -441,8 +441,8 @@ remember_include_file(Context& ctx, static void print_included_files(const Context& ctx, FILE* fp) { - for (const auto& item : ctx.included_files) { - PRINT(fp, "{}\n", item.first); + for (const auto& [path, digest] : ctx.included_files) { + PRINT(fp, "{}\n", path); } } diff --git a/src/core/Manifest.cpp b/src/core/Manifest.cpp index 003e54cfd..01adf50fc 100644 --- a/src/core/Manifest.cpp +++ b/src/core/Manifest.cpp @@ -179,9 +179,9 @@ Manifest::add_result( std::vector file_info_indexes; file_info_indexes.reserve(included_files.size()); - for (const auto& item : included_files) { - file_info_indexes.push_back(get_file_info_index(item.first, - item.second, + for (const auto& [path, digest] : included_files) { + file_info_indexes.push_back(get_file_info_index(path, + digest, mf_files, mf_file_infos, time_of_compilation, diff --git a/src/core/Statistics.cpp b/src/core/Statistics.cpp index 70c685aa1..06c5438e9 100644 --- a/src/core/Statistics.cpp +++ b/src/core/Statistics.cpp @@ -270,11 +270,8 @@ Statistics::format_human_readable(const Config& config, if (verbosity > 0) { auto uncacheable_stats = get_stats(FLAG_UNCACHEABLE, verbosity > 1); std::sort(uncacheable_stats.begin(), uncacheable_stats.end(), cmp_fn); - for (const auto& descr_count : uncacheable_stats) { - add_ratio_row(table, - FMT(" {}:", descr_count.first), - descr_count.second, - uncacheable); + for (const auto& [name, value] : uncacheable_stats) { + add_ratio_row(table, FMT(" {}:", name), value, uncacheable); } } } @@ -284,9 +281,8 @@ Statistics::format_human_readable(const Config& config, if (verbosity > 0) { auto error_stats = get_stats(FLAG_ERROR, verbosity > 1); std::sort(error_stats.begin(), error_stats.end(), cmp_fn); - for (const auto& descr_count : error_stats) { - add_ratio_row( - table, FMT(" {}:", descr_count.first), descr_count.second, errors); + for (const auto& [name, value] : error_stats) { + add_ratio_row(table, FMT(" {}:", name), value, errors); } } } diff --git a/src/storage/Storage.cpp b/src/storage/Storage.cpp index b6ea1e19a..2fafad7b9 100644 --- a/src/storage/Storage.cpp +++ b/src/storage/Storage.cpp @@ -137,9 +137,8 @@ parse_storage_config(const std::string_view entry) if (parts[i].empty()) { continue; } - const auto kv_pair = util::split_once(parts[i], '='); - const auto& key = kv_pair.first; - const auto& raw_value = kv_pair.second.value_or("true"); + const auto [key, right_hand_side] = util::split_once(parts[i], '='); + const auto& raw_value = right_hand_side.value_or("true"); const auto value = util::value_or_throw(util::percent_decode(raw_value)); if (key == "read-only") { diff --git a/src/storage/secondary/HttpStorage.cpp b/src/storage/secondary/HttpStorage.cpp index d95ba62c1..2276ce49a 100644 --- a/src/storage/secondary/HttpStorage.cpp +++ b/src/storage/secondary/HttpStorage.cpp @@ -98,13 +98,13 @@ HttpStorageBackend::HttpStorageBackend(const Params& params) m_http_client(get_url(params.url)) { if (!params.url.user_info().empty()) { - const auto pair = util::split_once(params.url.user_info(), ':'); - if (!pair.second) { + const auto [user, password] = util::split_once(params.url.user_info(), ':'); + if (!password) { throw core::Fatal("Expected username:password in URL but got \"{}\"", params.url.user_info()); } - m_http_client.set_basic_auth(std::string(pair.first).c_str(), - std::string(*pair.second).c_str()); + m_http_client.set_basic_auth(std::string(user).c_str(), + std::string(*password).c_str()); } m_http_client.set_default_headers({ @@ -278,9 +278,9 @@ void HttpStorage::redact_secrets(Backend::Params& params) const { auto& url = params.url; - const auto user_info = util::split_once(url.user_info(), ':'); - if (user_info.second) { - url.user_info(FMT("{}:{}", user_info.first, k_redacted_password)); + const auto [user, password] = util::split_once(url.user_info(), ':'); + if (password) { + url.user_info(FMT("{}:{}", user, k_redacted_password)); } auto bearer_token_attribute = diff --git a/src/storage/secondary/RedisStorage.cpp b/src/storage/secondary/RedisStorage.cpp index dcc98b579..cd5c100fa 100644 --- a/src/storage/secondary/RedisStorage.cpp +++ b/src/storage/secondary/RedisStorage.cpp @@ -92,16 +92,16 @@ to_timeval(const uint32_t ms) std::pair, std::optional> split_user_info(const std::string& user_info) { - const auto pair = util::split_once(user_info, ':'); - if (pair.first.empty()) { + const auto [left, right] = util::split_once(user_info, ':'); + if (left.empty()) { // redis://HOST return {std::nullopt, std::nullopt}; - } else if (pair.second) { + } else if (right) { // redis://USERNAME:PASSWORD@HOST - return {std::string(*pair.second), std::string(pair.first)}; + return {std::string(left), std::string(*right)}; } else { // redis://PASSWORD@HOST - return {std::string(pair.first), std::nullopt}; + return {std::nullopt, std::string(left)}; } } @@ -274,15 +274,15 @@ RedisStorageBackend::select_database(const Url& url) void RedisStorageBackend::authenticate(const Url& url) { - const auto password_username_pair = split_user_info(url.user_info()); - const auto& password = password_username_pair.first; + const auto [user, password] = split_user_info(url.user_info()); if (password) { - const auto& username = password_username_pair.second; - if (username) { - LOG("Redis AUTH {} {}", *username, k_redacted_password); + if (user) { + // redis://user:password@host + LOG("Redis AUTH {} {}", *user, k_redacted_password); util::value_or_throw( - redis_command("AUTH %s %s", username->c_str(), password->c_str())); + redis_command("AUTH %s %s", user->c_str(), password->c_str())); } else { + // redis://password@host LOG("Redis AUTH {}", k_redacted_password); util::value_or_throw(redis_command("AUTH %s", password->c_str())); } @@ -327,13 +327,15 @@ void RedisStorage::redact_secrets(Backend::Params& params) const { auto& url = params.url; - const auto user_info = util::split_once(url.user_info(), ':'); - if (user_info.second) { - // redis://username:password@host - url.user_info(FMT("{}:{}", user_info.first, k_redacted_password)); - } else if (!user_info.first.empty()) { - // redis://password@host - url.user_info(k_redacted_password); + const auto [user, password] = split_user_info(url.user_info()); + if (password) { + if (user) { + // redis://user:password@host + url.user_info(FMT("{}:{}", *user, k_redacted_password)); + } else { + // redis://password@host + url.user_info(k_redacted_password); + } } }