Browse Source

Default to vector instead of basic_string for non-trivial types

master
Jessica James 3 years ago
parent
commit
f00e36bce2
  1. 6
      src/include/jessilib/split.hpp
  2. 6
      src/include/jessilib/word_split.hpp
  3. 2
      src/test/split_compilation.cpp

6
src/include/jessilib/split.hpp

@ -32,7 +32,7 @@ namespace jessilib {
template<template<typename...> typename ContainerT, typename ElementT, typename...> template<template<typename...> typename ContainerT, typename ElementT, typename...>
struct split_defaults { struct split_defaults {
using member_type = std::basic_string<ElementT>; using member_type = std::conditional_t<std::is_trivial_v<ElementT>, std::basic_string<ElementT>, std::vector<ElementT>>;
using container_type = ContainerT<member_type>; using container_type = ContainerT<member_type>;
}; };
@ -44,7 +44,7 @@ struct split_defaults<ContainerT, ElementT, FirstOptional, ContainerArgsT...> {
// Can probably be specialized for types which don't take in iterators _or_ // Can probably be specialized for types which don't take in iterators _or_
template<typename MemberT, typename ItrT, typename EndT, typename std::enable_if<!std::is_constructible<MemberT, ItrT, EndT>::value>::type* = nullptr> template<typename MemberT, typename ItrT, typename EndT, typename std::enable_if<!std::is_constructible<MemberT, ItrT, EndT>::value>::type* = nullptr>
MemberT make_split_member(ItrT in_itr, EndT in_end) { constexpr MemberT make_split_member(ItrT in_itr, EndT in_end) {
// Intended for string_view // Intended for string_view
if constexpr (std::is_pointer_v<MemberT>) { if constexpr (std::is_pointer_v<MemberT>) {
return { in_itr, static_cast<size_t>(in_end - in_itr) }; return { in_itr, static_cast<size_t>(in_end - in_itr) };
@ -58,7 +58,7 @@ MemberT make_split_member(ItrT in_itr, EndT in_end) {
} }
template<typename MemberT, typename ItrT, typename EndT, typename std::enable_if<std::is_constructible<MemberT, ItrT, EndT>::value>::type* = nullptr> template<typename MemberT, typename ItrT, typename EndT, typename std::enable_if<std::is_constructible<MemberT, ItrT, EndT>::value>::type* = nullptr>
MemberT make_split_member(ItrT in_itr, EndT in_end) { constexpr MemberT make_split_member(ItrT in_itr, EndT in_end) {
// Can construct with iterators, so construct with iterators // Can construct with iterators, so construct with iterators
return { in_itr, in_end }; return { in_itr, in_end };
} }

6
src/include/jessilib/word_split.hpp

@ -32,7 +32,7 @@ namespace jessilib {
template<template<typename...> typename ContainerT, typename ElementT, typename...> template<template<typename...> typename ContainerT, typename ElementT, typename...>
struct word_split_defaults { struct word_split_defaults {
using member_type = std::basic_string<ElementT>; using member_type = std::conditional_t<std::is_trivial_v<ElementT>, std::basic_string<ElementT>, std::vector<ElementT>>;
using container_type = ContainerT<member_type>; using container_type = ContainerT<member_type>;
}; };
@ -44,7 +44,7 @@ struct word_split_defaults<ContainerT, ElementT, FirstOptional, ContainerArgsT..
// Can probably be specialized for types which don't take in iterators _or_ // Can probably be specialized for types which don't take in iterators _or_
template<typename MemberT, typename ItrT, typename EndT, typename std::enable_if<!std::is_constructible<MemberT, ItrT, EndT>::value>::type* = nullptr> template<typename MemberT, typename ItrT, typename EndT, typename std::enable_if<!std::is_constructible<MemberT, ItrT, EndT>::value>::type* = nullptr>
MemberT make_word_split_member(ItrT in_itr, EndT in_end) { constexpr MemberT make_word_split_member(ItrT in_itr, EndT in_end) {
// Intended for string_view // Intended for string_view
if constexpr (std::is_pointer_v<MemberT>) { if constexpr (std::is_pointer_v<MemberT>) {
return { in_itr, static_cast<size_t>(in_end - in_itr) }; return { in_itr, static_cast<size_t>(in_end - in_itr) };
@ -58,7 +58,7 @@ MemberT make_word_split_member(ItrT in_itr, EndT in_end) {
} }
template<typename MemberT, typename ItrT, typename EndT, typename std::enable_if<std::is_constructible<MemberT, ItrT, EndT>::value>::type* = nullptr> template<typename MemberT, typename ItrT, typename EndT, typename std::enable_if<std::is_constructible<MemberT, ItrT, EndT>::value>::type* = nullptr>
MemberT make_word_split_member(ItrT in_itr, EndT in_end) { constexpr MemberT make_word_split_member(ItrT in_itr, EndT in_end) {
// Can construct with iterators, so construct with iterators // Can construct with iterators, so construct with iterators
return { in_itr, in_end }; return { in_itr, in_end };
} }

2
src/test/split_compilation.cpp

@ -105,6 +105,6 @@ TYPED_TEST(SplitCompilationTest, empty_list_long) {
TYPED_TEST(SplitCompilationTest, random_vector) { TYPED_TEST(SplitCompilationTest, random_vector) {
std::vector<std::vector<std::vector<TypeParam>>> empty; std::vector<std::vector<std::vector<TypeParam>>> empty;
std::vector<std::vector<TypeParam>> delim; std::vector<std::vector<TypeParam>> delim;
std::vector<std::vector<std::vector<std::vector<TypeParam>>>> split_result = split<std::vector, decltype(empty)>(empty, delim); auto split_result = split(empty, delim);
EXPECT_TRUE(split_result.empty()); EXPECT_TRUE(split_result.empty());
} }

Loading…
Cancel
Save