Skip to content

Commit

Permalink
Fix std::string_view support in transforming validators (#300)
Browse files Browse the repository at this point in the history
* Fix std::string_view support in transforming validators

* Fix single header

* Fix formatting

* Be more careful

* fix string_view test
  • Loading branch information
peterazmanov authored and henryiii committed Jul 25, 2019
1 parent 68c8b1b commit f0c0794
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
15 changes: 15 additions & 0 deletions include/CLI/TypeTools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/CLI/Validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
12 changes: 12 additions & 0 deletions tests/TransformTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit f0c0794

Please sign in to comment.