Skip to content

Commit

Permalink
Merge pull request #54 from smehringer/cpp20_back_inserter
Browse files Browse the repository at this point in the history
[MISC] Remove std::ranges::copy and std::cpp20::back_inserter.
  • Loading branch information
eseiler authored Feb 3, 2022
2 parents df7ee71 + 877b5ea commit 8170226
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 22 deletions.
3 changes: 1 addition & 2 deletions doc/tutorial/argument_parser/solution3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
template <typename number_type, typename range_type>
number_type to_number(range_type && range)
{
std::string str;
std::string str = [&range] () { std::string s; for (auto c : range) s.push_back(c); return s; }();
number_type num;
std::ranges::copy(range, std::cpp20::back_inserter(str));
auto res = std::from_chars(&str[0], &str[0] + str.size(), num);

if (res.ec != std::errc{})
Expand Down
3 changes: 1 addition & 2 deletions doc/tutorial/argument_parser/solution4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
template <typename number_type, typename range_type>
number_type to_number(range_type && range)
{
std::string str;
std::string str = [&range] () { std::string s; for (auto c : range) s.push_back(c); return s; }();
number_type num;
std::ranges::copy(range, std::cpp20::back_inserter(str));
auto res = std::from_chars(&str[0], &str[0] + str.size(), num);

if (res.ec != std::errc{})
Expand Down
3 changes: 1 addition & 2 deletions doc/tutorial/argument_parser/solution5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
template <typename number_type, typename range_type>
number_type to_number(range_type && range)
{
std::string str;
std::string str = [&range] () { std::string s; for (auto c : range) s.push_back(c); return s; }();
number_type num;
std::ranges::copy(range, std::cpp20::back_inserter(str));
auto res = std::from_chars(&str[0], &str[0] + str.size(), num);

if (res.ec != std::errc{})
Expand Down
3 changes: 1 addition & 2 deletions doc/tutorial/argument_parser/solution6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
template <typename number_type, typename range_type>
number_type to_number(range_type && range)
{
std::string str;
std::string str = [&range] () { std::string s; for (auto c : range) s.push_back(c); return s; }();
number_type num;
std::ranges::copy(range, std::cpp20::back_inserter(str));
auto res = std::from_chars(&str[0], &str[0] + str.size(), num);

if (res.ec != std::errc{})
Expand Down
2 changes: 1 addition & 1 deletion include/sharg/argument_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ class argument_parser
{
std::string arg{argv[i]};

if (std::ranges::find(subcommands, arg) != subcommands.end())
if (std::find(subcommands.begin(), subcommands.end(), arg) != subcommands.end())
{
sub_parser = std::make_unique<argument_parser>(info.app_name + "-" + arg,
argc - i,
Expand Down
7 changes: 2 additions & 5 deletions include/sharg/detail/format_help.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,14 @@ class format_help : public format_help_base<format_help>

// Tokenize the text.
std::istringstream iss(text.c_str());
std::vector<std::string> tokens;
std::ranges::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(),
std::cpp20::back_inserter(tokens));
std::vector<std::string> tokens{std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>()};

// Print the text.
assert(pos <= tab);
std::fill_n(out, tab - pos, ' '); // go to tab

pos = tab;
typedef std::vector<std::string>::const_iterator TConstIter;
for (TConstIter it = tokens.begin(); it != tokens.end(); ++it)
for (auto it = tokens.begin(); it != tokens.end(); ++it)
{
if (it == tokens.begin())
{
Expand Down
18 changes: 10 additions & 8 deletions include/sharg/validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#pragma once

#include <seqan3/std/algorithm>
#include <fstream>
#include <regex>

Expand Down Expand Up @@ -355,7 +354,7 @@ class file_validator_base
};

// Check if requested extension is present.
if (std::ranges::find_if(extensions, case_insensitive_ends_with) == extensions.end())
if (std::find_if(extensions.begin(), extensions.end(), case_insensitive_ends_with) == extensions.end())
{
throw validation_error{"Expected one of the following valid extensions: " + extensions_str + "! Got " +
all_extensions + " instead!"};
Expand Down Expand Up @@ -429,12 +428,15 @@ class file_validator_base
{
size_t const suffix_length{suffix.size()};
size_t const str_length{str.size()};
return suffix_length > str_length ?
false :
std::ranges::equal(str.substr(str_length - suffix_length), suffix, [] (char const chr1, char const chr2)
{
return std::tolower(chr1) == std::tolower(chr2);
});

if (suffix_length > str_length)
return false;

for (size_t j = 0, s_start = str_length - suffix_length; j < suffix_length; ++j)
if (std::tolower(str[s_start + j]) != std::tolower(suffix[j]))
return false;

return true;
}

//!\brief Creates a std::string from the extensions list, e.g. "[ext, ext2]".
Expand Down
7 changes: 7 additions & 0 deletions test/unit/format_parse_validators_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ TEST(validator_test, output_file)
EXPECT_THROW(my_validator(no_extension), sharg::validation_error);
}

{ // filename is shorter than extension.
std::filesystem::path filename{tmp_name.get_path()};
std::vector<std::string> long_extension{"super_duper_long_extension_longer_than_seqan_tmp_filename"};
sharg::output_file_validator my_validator{sharg::output_file_open_options::create_new, long_extension};
EXPECT_THROW(my_validator(filename), sharg::validation_error);
}

{ // filename starts with dot.
sharg::output_file_validator my_validator{sharg::output_file_open_options::create_new, formats};
EXPECT_NO_THROW(my_validator(hidden_name.get_path()));
Expand Down

1 comment on commit 8170226

@vercel
Copy link

@vercel vercel bot commented on 8170226 Feb 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.