diff --git a/libs/string/example/c_str_to_sprout_string.cpp b/libs/string/example/c_str_to_sprout_string.cpp new file mode 100644 index 00000000..43187bdb --- /dev/null +++ b/libs/string/example/c_str_to_sprout_string.cpp @@ -0,0 +1,44 @@ +// +// Sprout C++ Library +// +// Copyright (c) 2012 +// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ +// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ +// +// Readme: +// https://github.com/bolero-MURAKAMI/Sprout/blob/master/README +// +// License: +// Boost Software License - Version 1.0 +// +// +#include + +// +// C style string to Sprout.String +// +int +main(){ + + constexpr char const* c_str = "homu"; + + // + // String length is required + // + constexpr auto length = sprout::char_traits::length(c_str); + static_assert(length == 4, ""); + + // + // To Sprout.String + // + constexpr auto str = sprout::string_from_c_str( c_str ); + static_assert(std::is_same const, decltype(str)>{}, ""); + static_assert(str == "homu", ""); + + constexpr auto str2 = sprout::string_from_c_str( c_str, 2 ); + static_assert(std::is_same const, decltype(str2)>{}, ""); + static_assert(str2 == "ho", ""); + + + return 0; +} diff --git a/libs/weed/example/as_tuple.cpp b/libs/weed/example/as_tuple.cpp new file mode 100644 index 00000000..75b93582 --- /dev/null +++ b/libs/weed/example/as_tuple.cpp @@ -0,0 +1,67 @@ +// +// Sprout C++ Library +// +// Copyright (c) 2012 +// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ +// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ +// +// Readme: +// https://github.com/bolero-MURAKAMI/Sprout/blob/master/README +// +// License: +// Boost Software License - Version 1.0 +// +// +#include + + +int +main(){ + namespace w = sprout::weed; + + static constexpr auto source = sprout::to_string("homu:mado"); + + // String parser + static constexpr auto item = *w::lim<4>(w::char_ - ':'); + + + // + // If you do not want to use the as_tuple + // + { + static constexpr auto parser = item >> ':' >> item; + + static constexpr auto result = w::parse( + sprout::begin(source), sprout::end(source), parser + ); + static_assert(result.success(), ""); + + // !!!! + static_assert(result.attr() == "homumado", ""); + } + + + // + // You need to use the as_tuple + // + { + static constexpr auto parser = w::as_tuple[item] >> ':' >> w::as_tuple[item]; + + static constexpr auto result = w::parse( + sprout::begin(source), sprout::end(source), parser + ); + static_assert(result.success(), ""); + + static constexpr auto attr = result.attr(); + + // OK + static_assert(std::is_same< + sprout::tuple, sprout::string<4>> const, + decltype(attr) + >::value, ""); + static_assert(sprout::get<0>(attr) == "homu", ""); + static_assert(sprout::get<1>(attr) == "mado", ""); + } + + return 0; +}