2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2015-01-10 10:13:57 +00:00
|
|
|
Copyright (c) 2011-2015 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2012-05-11 07:50:46 +00:00
|
|
|
#ifndef SPROUT_STRING_SHRINK_HPP
|
|
|
|
#define SPROUT_STRING_SHRINK_HPP
|
|
|
|
|
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2013-04-06 04:06:51 +00:00
|
|
|
#include <sprout/index_tuple/metafunction.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>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/utility/value_holder/value_holder.hpp>
|
2013-02-19 16:12:56 +00:00
|
|
|
#include HDR_ALGORITHM_MIN_MAX_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:
|
2013-08-09 10:04:37 +00:00
|
|
|
template<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(
|
2013-08-09 10:04:37 +00:00
|
|
|
string_type const& str, std::size_t len,
|
2012-05-11 07:50:46 +00:00
|
|
|
sprout::index_tuple<Indexes...>
|
|
|
|
)
|
|
|
|
{
|
2013-08-09 10:04:37 +00:00
|
|
|
typedef sprout::detail::string_construct_access<T, sizeof...(Indexes), Traits> access_type;
|
|
|
|
return access_type::raw_construct(len, (Indexes < N ? str[Indexes] : T())...);
|
2012-05-11 07:50:46 +00:00
|
|
|
}
|
|
|
|
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(
|
2013-08-09 10:04:37 +00:00
|
|
|
holder_.get(),
|
|
|
|
NS_SSCRISK_CEL_OR_SPROUT::min(N2, holder_.get().size()),
|
2013-03-31 06:14:10 +00:00
|
|
|
sprout::make_index_tuple<(N < N2 ? N : N2)>::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);
|
|
|
|
}
|
2012-12-19 14:06:08 +00:00
|
|
|
template<std::size_t To, typename T, std::size_t N, typename Traits>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<T, To, Traits>
|
|
|
|
shrink(sprout::basic_string<T, N, Traits> const& s) {
|
|
|
|
return sprout::shrink(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, std::size_t N>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::shrink_string<T, N>
|
|
|
|
shrink(T const(& arr)[N]) {
|
|
|
|
return sprout::shrink(sprout::to_string(arr));
|
|
|
|
}
|
|
|
|
template<std::size_t To, typename T, std::size_t N>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<T, To>
|
|
|
|
shrink(T const(& arr)[N]) {
|
|
|
|
return sprout::shrink<To>(sprout::to_string(arr));
|
|
|
|
}
|
2012-05-11 07:50:46 +00:00
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_STRING_SHRINK_HPP
|