1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 12:13:52 +00:00

Fix std::string_view support in transforming validators (#300)

* Fix std::string_view support in transforming validators

* Fix single header

* Fix formatting

* Be more careful

* fix string_view test
This commit is contained in:
Peter Azmanov 2019-07-25 23:14:32 +03:00 committed by Henry Schreiner
parent 68c8b1b789
commit f0c0794aa7
3 changed files with 28 additions and 1 deletions

View File

@ -10,6 +10,17 @@
#include <type_traits>
#include <vector>
// [CLI11:verbatim]
#if defined(CLI11_CPP17)
#if defined(__has_include)
#if __has_include(<string_view>)
#include <string_view>
#define CLI11_HAS_STRING_VIEW
#endif
#endif
#endif
// [CLI11:verbatim]
namespace CLI {
// Type tools
@ -72,6 +83,10 @@ template <typename T> struct IsMemberType { using type = T; };
/// The main custom type needed here is const char * should be a string.
template <> struct IsMemberType<const char *> { using type = std::string; };
#ifdef CLI11_HAS_STRING_VIEW
template <> struct IsMemberType<std::string_view> { using type = std::string; };
#endif
namespace detail {
// These are utilities for IsMember

View File

@ -503,7 +503,7 @@ auto search(const T &set, const V &val, const std::function<V(V)> &filter_functi
// if we haven't found it do the longer linear search with all the element translations
auto &setref = detail::smart_deref(set);
auto it = std::find_if(std::begin(setref), std::end(setref), [&](decltype(*std::begin(setref)) v) {
V a = detail::pair_adaptor<element_t>::first(v);
V a{detail::pair_adaptor<element_t>::first(v)};
a = filter_function(a);
return (a == val);
});

View File

@ -102,6 +102,18 @@ TEST_F(TApp, SimpleTransformFn) {
EXPECT_EQ(value, 1);
}
#if defined(CLI11_HAS_STRING_VIEW)
TEST_F(TApp, StringViewTransformFn) {
std::string value;
std::map<std::string_view, std::string_view> map = {// key length > std::string().capacity() [SSO length]
{"a-rather-long-argument", "mapped"}};
app.add_option("-s", value)->transform(CLI::CheckedTransformer(map));
args = {"-s", "a-rather-long-argument"};
run();
EXPECT_EQ(value, "mapped");
}
#endif
TEST_F(TApp, SimpleNumericalTransformFn) {
int value;
auto opt =