From 06d694ee8e44ed4c5e3033d8b8349a876d4c8bad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Musia=C5=82?= <111433005+SpectraL519@users.noreply.github.com> Date: Tue, 12 Mar 2024 16:04:19 +0100 Subject: [PATCH] using instead of --- change_log.md | 1 + include/ap/argument_parser.hpp | 73 ++++++++-------------------------- scripts/format/unix.sh | 4 +- scripts/format/windows.ps1 | 2 +- 4 files changed, 20 insertions(+), 60 deletions(-) diff --git a/change_log.md b/change_log.md index 9e9ce2f..ba9f9b3 100644 --- a/change_log.md +++ b/change_log.md @@ -24,3 +24,4 @@ * Aligned the `.clang-format` configuration file and moved formatting scripts to a new directory `/scripts` * Added the `install_clang_format_17.sh` env script * Added the `format` workflow +* Switched from the `` to the `` library for all current container operations diff --git a/include/ap/argument_parser.hpp b/include/ap/argument_parser.hpp index 15cb0f0..01d4fac 100644 --- a/include/ap/argument_parser.hpp +++ b/include/ap/argument_parser.hpp @@ -36,7 +36,6 @@ SOFTWARE. #pragma once -#include #include #include #include @@ -45,6 +44,7 @@ SOFTWARE. #include #include #include +#include #include #include #include @@ -801,8 +801,7 @@ class positional_argument : public detail::argument_interface { * @return True if the choice valid, false otherwise. */ [[nodiscard]] inline bool _is_valid_choice(const value_type& choice) const { - return this->_choices.empty() - or std::find(this->_choices.begin(), this->_choices.end(), choice) != this->_choices.end(); + return this->_choices.empty() or std::ranges::find(this->_choices, choice) != this->_choices.end(); } /** @@ -1102,8 +1101,7 @@ class optional_argument : public detail::argument_interface { * @return True if choice is valid, false otherwise. */ [[nodiscard]] inline bool _is_valid_choice(const value_type& choice) const { - return this->_choices.empty() - or std::find(this->_choices.begin(), this->_choices.end(), choice) != this->_choices.end(); + return this->_choices.empty() or std::ranges::find(this->_choices, choice) != this->_choices.end(); } /** @@ -1380,14 +1378,12 @@ class argument_parser { if (not arg.has_parsed_values() and arg.has_value()) return std::vector{ std::any_cast(arg.value()) }; - const auto& arg_values = arg.values(); - std::vector values; - std::transform( - std::begin(arg_values), - std::end(arg_values), - std::back_inserter(values), - [](const std::any& value) { return std::any_cast(value); } + std::ranges::copy( + std::views::transform( + arg.values(), [](const std::any& value) { return std::any_cast(value); } + ), + std::back_inserter(values) ); return values; } @@ -1551,10 +1547,10 @@ class argument_parser { [[nodiscard]] bool _is_arg_name_used(const std::string_view& name) const { const auto predicate = this->_name_eq_predicate(name); - if (this->_const_find_positional(predicate) != this->_positional_args.end()) + if (std::ranges::find_if(this->_positional_args, predicate) != this->_positional_args.end()) return true; - if (this->_const_find_optional(predicate) != this->_optional_args.end()) + if (std::ranges::find_if(this->_optional_args, predicate) != this->_optional_args.end()) return true; return false; @@ -1569,10 +1565,10 @@ class argument_parser { [[nodiscard]] bool _is_arg_name_used(const std::string_view& name, const std::string_view& short_name) const { const auto predicate = this->_name_eq_predicate(name, short_name); - if (this->_const_find_positional(predicate) != this->_positional_args.end()) + if (std::ranges::find_if(this->_positional_args, predicate) != this->_positional_args.end()) return true; - if (this->_const_find_optional(predicate) != this->_optional_args.end()) + if (std::ranges::find_if(this->_optional_args, predicate) != this->_optional_args.end()) return true; return false; @@ -1670,7 +1666,8 @@ class argument_parser { while (cmd_it != cmd_args.end()) { if (cmd_it->discriminator == cmd_argument::type_discriminator::flag) { - auto opt_arg_it = this->_find_optional(this->_name_eq_predicate(cmd_it->value)); + auto opt_arg_it = + std::ranges::find_if(this->_optional_args, this->_name_eq_predicate(cmd_it->value)); if (opt_arg_it == this->_optional_args.end()) throw error::argument_not_found_error(cmd_it->value); @@ -1733,12 +1730,12 @@ class argument_parser { argument_opt_type _get_argument(const std::string_view& name) const { const auto predicate = this->_name_eq_predicate(name); - if (auto pos_arg_it = this->_const_find_positional(predicate); + if (auto pos_arg_it = std::ranges::find_if(this->_positional_args, predicate); pos_arg_it != this->_positional_args.end()) { return std::ref(**pos_arg_it); } - if (auto opt_arg_it = this->_const_find_optional(predicate); + if (auto opt_arg_it = std::ranges::find_if(this->_optional_args, predicate); opt_arg_it != this->_optional_args.end()) { return std::ref(**opt_arg_it); } @@ -1746,44 +1743,6 @@ class argument_parser { return std::nullopt; } - /** - * @brief Find a positional argument based on the provided predicate. - * @param predicate The predicate for finding the argument. - * @return Iterator to the found positional argument. - */ - [[nodiscard]] inline argument_list_iterator_type _find_positional(const argument_predicate_type& predicate) { - return std::find_if(std::begin(this->_positional_args), std::end(this->_positional_args), predicate); - } - - /** - * @brief Find an optional argument based on the provided predicate. - * @param predicate The predicate for finding the argument. - * @return Iterator to the found optional argument. - */ - [[nodiscard]] inline argument_list_iterator_type _find_optional(const argument_predicate_type& predicate) { - return std::find_if(std::begin(this->_optional_args), std::end(this->_optional_args), predicate); - } - - /** - * @brief Find a positional argument based on the provided predicate (const version). - * @param predicate The predicate for finding the argument. - * @return Iterator to the found positional argument. - */ - [[nodiscard]] inline argument_list_const_iterator_type _const_find_positional(const argument_predicate_type& predicate - ) const { - return std::find_if(std::cbegin(this->_positional_args), std::cend(this->_positional_args), predicate); - } - - /** - * @brief Find an optional argument based on the provided predicate (const version). - * @param predicate The predicate for finding the argument. - * @return Iterator to the found optional argument. - */ - [[nodiscard]] inline argument_list_const_iterator_type _const_find_optional(const argument_predicate_type& predicate - ) const { - return std::find_if(std::cbegin(this->_optional_args), std::cend(this->_optional_args), predicate); - } - std::optional _program_name; std::optional _program_description; diff --git a/scripts/format/unix.sh b/scripts/format/unix.sh index a3b1e8a..f7873f9 100644 --- a/scripts/format/unix.sh +++ b/scripts/format/unix.sh @@ -15,7 +15,7 @@ run_clang_format() { } # Count the number of files to format -file_count=$(find . -type f \( -name "*.hpp" -o -name "*.cpp" \) | wc -l) +file_count=$(find . -type f \( -name "*.hpp" -o -name "*.cpp" \) ! -path "*/build/*" | wc -l) if [[ "$format_check" == true ]]; then echo "Files to check: $file_count" else @@ -25,7 +25,7 @@ echo # Iterate over the files and run format/check file_number=0 -find . -type f \( -name "*.hpp" -o -name "*.cpp" \) -print0 | while IFS= read -r -d '' file; do +find . -type f \( -name "*.hpp" -o -name "*.cpp" \) ! -path "*/build/*" -print0 | while IFS= read -r -d '' file; do ((file_number++)) echo "[$file_number/$file_count] $file" run_clang_format "$file" diff --git a/scripts/format/windows.ps1 b/scripts/format/windows.ps1 index 607488b..ab07c53 100644 --- a/scripts/format/windows.ps1 +++ b/scripts/format/windows.ps1 @@ -18,7 +18,7 @@ function Run-Clang-Format { } # Get the list of files to format -$files = Get-ChildItem -Recurse -Include *.hpp, *.cpp +$files = Get-ChildItem -Recurse -Include *.hpp, *.cpp | Where-Object { $_.DirectoryName -notmatch "\\build\\" } # Count the number of files to format $fileCount = $files.Count