2012-05-11 07:50:46 +00:00
|
|
|
#ifndef SPROUT_STRING_SHRINK_HPP
|
|
|
|
#define SPROUT_STRING_SHRINK_HPP
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/index_tuple.hpp>
|
2012-05-14 02:33:36 +00:00
|
|
|
#include <sprout/string/char_traits.hpp>
|
2012-05-11 07:50:46 +00:00
|
|
|
#include <sprout/string/string.hpp>
|
|
|
|
#include <sprout/utility/value_holder.hpp>
|
2012-05-11 16:49:15 +00:00
|
|
|
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT
|
2012-05-11 07:50:46 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
//
|
|
|
|
// shrink_string
|
|
|
|
//
|
2012-05-14 02:33:36 +00:00
|
|
|
template<typename T, std::size_t N, typename Traits = sprout::char_traits<T> >
|
2012-05-11 07:50:46 +00:00
|
|
|
class shrink_string {
|
|
|
|
public:
|
|
|
|
typedef sprout::basic_string<T, N, Traits> string_type;
|
|
|
|
private:
|
|
|
|
typedef sprout::value_holder<string_type const&> holder_type;
|
|
|
|
private:
|
|
|
|
template<std::size_t M, sprout::index_t... Indexes>
|
2012-10-05 15:58:56 +00:00
|
|
|
static SPROUT_CONSTEXPR sprout::basic_string<T, sizeof...(Indexes), Traits>
|
|
|
|
implicit_conversion_impl(
|
|
|
|
T const(& elems)[M], std::size_t len,
|
2012-05-11 07:50:46 +00:00
|
|
|
sprout::index_tuple<Indexes...>
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::basic_string<T, sizeof...(Indexes), Traits>{{(Indexes < M - 1 ? elems[Indexes] : T())...}, len};
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
holder_type holder_;
|
|
|
|
public:
|
|
|
|
shrink_string(shrink_string const&) = default;
|
|
|
|
shrink_string(shrink_string&&) = default;
|
2012-05-14 02:33:36 +00:00
|
|
|
SPROUT_CONSTEXPR shrink_string(string_type const& s)
|
|
|
|
: holder_(s)
|
2012-05-11 07:50:46 +00:00
|
|
|
{}
|
2012-05-14 02:33:36 +00:00
|
|
|
SPROUT_CONSTEXPR operator string_type const&() const {
|
|
|
|
return holder_.get();
|
|
|
|
}
|
2012-05-11 07:50:46 +00:00
|
|
|
template<std::size_t N2>
|
|
|
|
SPROUT_CONSTEXPR operator sprout::basic_string<T, N2, Traits>() const {
|
|
|
|
return implicit_conversion_impl(
|
|
|
|
holder_.get().elems,
|
2012-05-11 16:49:15 +00:00
|
|
|
NS_SSCRISK_CEL_OR_SPROUT::min(N2, holder_.get().len),
|
|
|
|
sprout::index_range<0, NS_SSCRISK_CEL_OR_SPROUT::min(N2, N)>::make()
|
2012-05-11 07:50:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// shrink
|
|
|
|
//
|
|
|
|
template<typename T, std::size_t N, typename Traits>
|
2012-07-15 07:04:05 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::shrink_string<T, N, Traits>
|
2012-05-11 07:50:46 +00:00
|
|
|
shrink(sprout::basic_string<T, N, Traits> const& s) {
|
|
|
|
return sprout::shrink_string<T, N, Traits>(s);
|
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_STRING_SHRINK_HPP
|