Skip to content

Commit

Permalink
Fix regex-disabled builds
Browse files Browse the repository at this point in the history
  • Loading branch information
eliaskosunen committed Jan 17, 2024
1 parent ffd2a13 commit 5bf72ea
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
14 changes: 12 additions & 2 deletions include/scn/detail/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ struct unscannable {};
struct unscannable_char : unscannable {};
struct unscannable_const : unscannable {};
struct unscannable_disabled : unscannable {
unscannable_disabled() = default;

template <typename T>
constexpr unscannable_disabled(T&&)
{
Expand Down Expand Up @@ -237,8 +239,6 @@ struct arg_mapper {
SCN_ARG_MAPPER(std::string)
SCN_ARG_MAPPER(std::wstring)

SCN_ARG_MAPPER(basic_regex_matches<char_type>)

#undef SCN_ARG_MAPPER

static decltype(auto) map(char& val)
Expand All @@ -256,6 +256,16 @@ struct arg_mapper {
}
}

static decltype(auto) map(basic_regex_matches<char_type>& val)
{
if constexpr (is_type_disabled<char_type>) {
return unscannable_disabled{val};
}
else {
return val;
}
}

static unscannable_char map(std::basic_string_view<other_char_type>&)
{
return {};
Expand Down
28 changes: 22 additions & 6 deletions src/scn/impl/reader/regex_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ namespace scn {
SCN_BEGIN_NAMESPACE

namespace impl {

// Forward declaration for C++17 compatibility with regex disabled
template <typename CharT, typename Input>
auto read_regex_matches_impl(std::basic_string_view<CharT> pattern,
detail::regex_flags flags,
Input input,
basic_regex_matches<CharT>& value)
-> scan_expected<ranges::iterator_t<Input>>;

#if !SCN_DISABLE_REGEX

#if SCN_REGEX_BACKEND == SCN_REGEX_BACKEND_STD
Expand Down Expand Up @@ -411,8 +420,6 @@ auto read_regex_matches_impl(std::basic_string_view<CharT> pattern,
#endif // SCN_REGEX_BACKEND == ...
}

#endif // !SCN_DISABLE_REGEX

inline std::string get_unescaped_regex_pattern(std::string_view pattern)
{
std::string result{pattern};
Expand All @@ -432,6 +439,8 @@ inline std::wstring get_unescaped_regex_pattern(std::wstring_view pattern)
return result;
}

#endif // !SCN_DISABLE_REGEX

template <typename SourceCharT>
struct regex_matches_reader
: public reader_base<regex_matches_reader<SourceCharT>, SourceCharT> {
Expand Down Expand Up @@ -497,11 +506,18 @@ struct regex_matches_reader
detail::regex_flags flags,
basic_regex_matches<DestCharT>& value)
{
if (is_escaped) {
return read_regex_matches_impl<SourceCharT>(
get_unescaped_regex_pattern(pattern), flags, input, value);
if constexpr (detail::is_type_disabled<
basic_regex_matches<DestCharT>>) {
SCN_EXPECT(false);
SCN_UNREACHABLE;
}
else {
if (is_escaped) {
return read_regex_matches_impl<SourceCharT>(
get_unescaped_regex_pattern(pattern), flags, input, value);
}
return read_regex_matches_impl(pattern, flags, input, value);
}
return read_regex_matches_impl(pattern, flags, input, value);
}
};

Expand Down
20 changes: 12 additions & 8 deletions tests/unittests/compilefail_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ set(buildfail_sources
locale_flag_with_string.cpp
negative_argument_id.cpp
regex_disabled.cpp
regex_empty.cpp
regex_flag_invalid.cpp
regex_flag_multiple.cpp
regex_matches_non_contiguous_source.cpp
regex_no_presentation.cpp
regex_non_contiguous_source.cpp
regex_unterminated.cpp
regex_wide_strings.cpp
string_view_non_contiguous_source.cpp
unterminated_argument_id.cpp
unterminated_format_specifier.cpp
)
if (NOT SCN_DISABLE_REGEX)
list(APPEND buildfail_sources
regex_empty.cpp
regex_flag_invalid.cpp
regex_flag_multiple.cpp
regex_matches_non_contiguous_source.cpp
regex_no_presentation.cpp
regex_non_contiguous_source.cpp
regex_unterminated.cpp
regex_wide_strings.cpp
)
endif ()

if (SCN_CXX_FRONTEND STREQUAL "GNU" OR SCN_CXX_FRONTEND STREQUAL "Clang")
icm_add_multiple_build_failure_tests(
Expand Down

0 comments on commit 5bf72ea

Please sign in to comment.