add sprout::shrink (convert from string to shorter string)

This commit is contained in:
bolero-MURAKAMI 2012-05-11 16:50:46 +09:00
parent 6d3f1e27c8
commit 83580a12c7
6 changed files with 67 additions and 1 deletions

View file

@ -25,6 +25,7 @@ namespace sprout {
: public sprout::range::range_container<
typename sprout::container_traits<Range>::iterator
>
, public sprout::detail::container_nosy_static_size<Range>
, public sprout::detail::container_nosy_fixed_size<Range>
{
public:

View file

@ -23,6 +23,7 @@ namespace sprout {
: public sprout::range::range_container<
sprout::sinusoid_iterator<Value>
>
, public sprout::detail::container_nosy_static_size<Range>
, public sprout::detail::container_nosy_fixed_size<Range>
{
public:

View file

@ -28,6 +28,7 @@ namespace sprout {
typename sprout::container_traits<RRange>::iterator
>
>
, public sprout::detail::container_nosy_static_size<LRange>
, public sprout::detail::container_nosy_fixed_size<LRange>
{
public:
@ -62,6 +63,7 @@ namespace sprout {
typename sprout::container_traits<Range>::iterator
>
>
, public sprout::detail::container_nosy_static_size<Range>
, public sprout::detail::container_nosy_fixed_size<Range>
{
public:

View file

@ -15,7 +15,7 @@ namespace sprout {
//
template<typename Iterator>
struct range_container
: public sprout::container_traits<Iterator>
: public sprout::container_traits_facade<Iterator>
{
public:
typedef Iterator iterator;

View file

@ -14,5 +14,6 @@
#include <sprout/string/conversion.hpp>
#include <sprout/string/type_traits.hpp>
#include <sprout/string/alias.hpp>
#include <sprout/string/shrink.hpp>
#endif // #ifndef SPROUT_STRING_HPP

61
sprout/string/shrink.hpp Normal file
View file

@ -0,0 +1,61 @@
#ifndef SPROUT_STRING_SHRINK_HPP
#define SPROUT_STRING_SHRINK_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/index_tuple.hpp>
#include <sprout/string/char_traits.hpp>
#include <sprout/string/string.hpp>
#include <sprout/utility/value_holder.hpp>
namespace sprout {
//
// shrink_string
//
template<typename T, std::size_t N, typename Traits = sprout::char_traits<T> >
class shrink_string {
public:
typedef sprout::basic_string<T, N, Traits> string_type;
private:
typedef sprout::value_holder<string_type const&> holder_type;
public:
typedef typename holder_type::param_type param_type;
private:
template<std::size_t M, sprout::index_t... Indexes>
static SPROUT_CONSTEXPR sprout::basic_string<T, sizeof...(Indexes), Traits> implicit_conversion_impl(
T const(& elems)[M],
std::size_t len,
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;
explicit SPROUT_CONSTEXPR shrink_string(param_type p)
: holder_(p)
{}
template<std::size_t N2>
SPROUT_CONSTEXPR operator sprout::basic_string<T, N2, Traits>() const {
return implicit_conversion_impl(
holder_.get().elems,
holder_.get().len,
sprout::index_range<0, (N2 < N ? N2 : N)>::make()
);
}
};
//
// shrink
//
template<typename T, std::size_t N, typename Traits>
SPROUT_CONSTEXPR sprout::shrink_string<T, N, Traits>
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