mirror of
https://github.com/boostorg/hana.git
synced 2026-02-01 08:32:11 +00:00
[String] Add conversion to char const* and minor refactoring
Also - remove useless value_type alias in String since it's not a Constant - document why the interface is so minimal
This commit is contained in:
@@ -17,10 +17,85 @@ using namespace boost::hana;
|
||||
|
||||
int main() {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Modeled concepts
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
{
|
||||
|
||||
//! [Comparable]
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
BOOST_HANA_STRING("abcdef") == BOOST_HANA_STRING("abcdef")
|
||||
);
|
||||
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
BOOST_HANA_STRING("abcdef") != BOOST_HANA_STRING("abef")
|
||||
);
|
||||
//! [Comparable]
|
||||
|
||||
}{
|
||||
|
||||
//! [Orderable]
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
BOOST_HANA_STRING("abc") < BOOST_HANA_STRING("bcd")
|
||||
);
|
||||
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
BOOST_HANA_STRING("abcd") > BOOST_HANA_STRING("abc")
|
||||
);
|
||||
//! [Orderable]
|
||||
|
||||
}{
|
||||
|
||||
//! [Foldable]
|
||||
auto sum_string = [](auto str) {
|
||||
return fold.left(str, int_<0>, [](auto sum, auto c) {
|
||||
constexpr int i = value(c) - 48; // convert character to decimal
|
||||
return sum + int_<i>;
|
||||
});
|
||||
};
|
||||
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
sum_string(BOOST_HANA_STRING("1234")) == int_<1 + 2 + 3 + 4>
|
||||
);
|
||||
//! [Foldable]
|
||||
|
||||
}{
|
||||
|
||||
//! [Iterable]
|
||||
BOOST_HANA_CONSTANT_CHECK(!is_empty(BOOST_HANA_STRING("abcd")));
|
||||
BOOST_HANA_CONSTANT_CHECK(is_empty(BOOST_HANA_STRING("")));
|
||||
|
||||
BOOST_HANA_CONSTANT_CHECK(BOOST_HANA_STRING("abcd")[int_<2>] == char_<'c'>);
|
||||
|
||||
auto is_vowel = [](auto c) {
|
||||
return c ^in^ BOOST_HANA_STRING("aeiouy");
|
||||
};
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
drop_while(BOOST_HANA_STRING("aioubcd"), is_vowel) == BOOST_HANA_STRING("bcd")
|
||||
);
|
||||
//! [Iterable]
|
||||
|
||||
}{
|
||||
|
||||
//! [Searchable]
|
||||
BOOST_HANA_CONSTANT_CHECK(char_<'c'> ^in^ BOOST_HANA_STRING("abcde"));
|
||||
BOOST_HANA_CONSTANT_CHECK(!(char_<'z'> ^in^ BOOST_HANA_STRING("abcde")));
|
||||
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
find(BOOST_HANA_STRING("abcxefg"), char_<'x'>) == just(char_<'x'>)
|
||||
);
|
||||
//! [Searchable]
|
||||
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Methods
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
{
|
||||
|
||||
//! [BOOST_HANA_STRING]
|
||||
BOOST_HANA_CONSTEXPR_LAMBDA auto str = BOOST_HANA_STRING("abcdef");
|
||||
BOOST_HANA_CONSTANT_CHECK(str == string<'a', 'b', 'c', 'd', 'e', 'f'>);
|
||||
static_assert(std::is_same<datatype_t<decltype(str)>, String>{}, "");
|
||||
//! [BOOST_HANA_STRING]
|
||||
|
||||
@@ -33,69 +108,11 @@ static_assert(std::is_same<datatype_t<decltype(str)>, String>{}, "");
|
||||
|
||||
}{
|
||||
|
||||
//! [comparable]
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
BOOST_HANA_STRING("abcdef") == BOOST_HANA_STRING("abcdef")
|
||||
);
|
||||
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
BOOST_HANA_STRING("abcdef") != BOOST_HANA_STRING("abef")
|
||||
);
|
||||
//! [comparable]
|
||||
|
||||
}{
|
||||
|
||||
//! [orderable]
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
BOOST_HANA_STRING("abc") < BOOST_HANA_STRING("bcd")
|
||||
);
|
||||
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
BOOST_HANA_STRING("abcd") > BOOST_HANA_STRING("abc")
|
||||
);
|
||||
//! [orderable]
|
||||
|
||||
}{
|
||||
|
||||
//! [foldable]
|
||||
auto sum_string = [](auto str) {
|
||||
return fold.left(str, int_<0>, [](auto sum, auto c) {
|
||||
constexpr int i = value(c) - 48; // convert character to decimal
|
||||
return sum + int_<i>;
|
||||
});
|
||||
};
|
||||
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
sum_string(BOOST_HANA_STRING("1234")) == int_<1 + 2 + 3 + 4>
|
||||
);
|
||||
//! [foldable]
|
||||
|
||||
}{
|
||||
|
||||
//! [iterable]
|
||||
BOOST_HANA_CONSTANT_CHECK(!is_empty(BOOST_HANA_STRING("abcd")));
|
||||
BOOST_HANA_CONSTANT_CHECK(is_empty(BOOST_HANA_STRING("")));
|
||||
|
||||
BOOST_HANA_CONSTANT_CHECK(BOOST_HANA_STRING("abcd")[int_<2>] == char_<'c'>);
|
||||
|
||||
auto is_vowel = [](auto c) {
|
||||
return c ^in^ BOOST_HANA_STRING("aeiouy");
|
||||
};
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
drop_while(BOOST_HANA_STRING("aioubcd"), is_vowel) == BOOST_HANA_STRING("bcd")
|
||||
);
|
||||
//! [iterable]
|
||||
|
||||
}{
|
||||
|
||||
//! [searchable]
|
||||
BOOST_HANA_CONSTANT_CHECK(char_<'c'> ^in^ BOOST_HANA_STRING("abcde"));
|
||||
BOOST_HANA_CONSTANT_CHECK(!(char_<'z'> ^in^ BOOST_HANA_STRING("abcde")));
|
||||
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
find(BOOST_HANA_STRING("abcxefg"), char_<'x'>) == just(char_<'x'>)
|
||||
);
|
||||
//! [searchable]
|
||||
//! [to<char const*>]
|
||||
constexpr auto str = string<'h', 'i'>;
|
||||
constexpr char const* s = to<char const*>(str);
|
||||
static_assert(s[0] == 'h' && s[1] == 'i' && s[2] == '\0', "");
|
||||
//! [to<char const*>]
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user