Sprout/sprout/string/concat.hpp

160 lines
5.7 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-01-08 07:48:12 +00:00
Copyright (c) 2011-2014 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-04-14 10:06:21 +00:00
#ifndef SPROUT_STRING_CONCAT_HPP
#define SPROUT_STRING_CONCAT_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
2013-04-06 04:06:51 +00:00
#include <sprout/index_tuple/metafunction.hpp>
2012-04-18 04:37:35 +00:00
#include <sprout/string/char_traits.hpp>
2012-04-14 10:06:21 +00:00
#include <sprout/string/string.hpp>
namespace sprout {
2012-04-18 04:37:35 +00:00
namespace detail {
template<typename T, std::size_t N, typename Traits, sprout::index_t... Indexes>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N + 1, Traits>
string_concat(
2012-10-05 15:58:56 +00:00
sprout::basic_string<T, N, Traits> const& lhs, std::size_t lsize,
2012-04-18 04:37:35 +00:00
T const& rhs,
sprout::index_tuple<Indexes...>
)
{
typedef sprout::detail::string_construct_access<T, N + 1, Traits> access_type;
return access_type::raw_construct(
lsize + 1,
(Indexes < lsize ? lhs[Indexes]
: Indexes < lsize + 1 ? rhs
: T()
)...
);
2012-04-18 04:37:35 +00:00
}
template<typename T, std::size_t N, typename Traits, sprout::index_t... Indexes>
inline SPROUT_CONSTEXPR sprout::basic_string<T, 1 + N, Traits>
string_concat(
T const& lhs,
2012-10-05 15:58:56 +00:00
sprout::basic_string<T, N, Traits> const& rhs, std::size_t rsize,
2012-04-18 04:37:35 +00:00
sprout::index_tuple<Indexes...>
)
{
typedef sprout::detail::string_construct_access<T, 1 + N, Traits> access_type;
return access_type::raw_construct(
1 + rsize,
(Indexes < 1 ? lhs
: Indexes < 1 + rsize ? rhs[Indexes - 1]
: T()
)...
);
2012-04-18 04:37:35 +00:00
}
template<typename T, std::size_t N, typename Traits, std::size_t M, sprout::index_t... Indexes>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N + (M - 1), Traits>
string_concat(
2012-10-05 15:58:56 +00:00
sprout::basic_string<T, N, Traits> const& lhs, std::size_t lsize,
T const (& rhs)[M], std::size_t rsize,
2012-04-18 04:37:35 +00:00
sprout::index_tuple<Indexes...>
)
{
typedef sprout::detail::string_construct_access<T, N + (M - 1), Traits> access_type;
return access_type::raw_construct(
lsize + rsize,
(Indexes < lsize ? lhs[Indexes]
: Indexes < lsize + rsize ? rhs[Indexes - lsize]
: T()
)...
);
2012-04-18 04:37:35 +00:00
}
template<typename T, std::size_t N, typename Traits, std::size_t M, sprout::index_t... Indexes>
inline SPROUT_CONSTEXPR sprout::basic_string<T, (M - 1) + N, Traits>
string_concat(
2012-10-05 15:58:56 +00:00
T const (& lhs)[M], std::size_t lsize,
sprout::basic_string<T, N, Traits> const& rhs, std::size_t rsize,
2012-04-18 04:37:35 +00:00
sprout::index_tuple<Indexes...>
)
{
typedef sprout::detail::string_construct_access<T, (M - 1) + N, Traits> access_type;
return access_type::raw_construct(
lsize + rsize,
2013-08-09 11:04:27 +00:00
(Indexes < lsize ? *(lhs + Indexes)
: Indexes < lsize + rsize ? rhs[Indexes - lsize]
: T()
)...
);
2012-04-18 04:37:35 +00:00
}
template<typename T, std::size_t N1, std::size_t N2, typename Traits, sprout::index_t... Indexes>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N1 + N2, Traits>
string_concat(
2012-10-05 15:58:56 +00:00
sprout::basic_string<T, N1, Traits> const& lhs, std::size_t lsize,
sprout::basic_string<T, N2, Traits> const& rhs, std::size_t rsize,
2012-04-18 04:37:35 +00:00
sprout::index_tuple<Indexes...>
)
{
typedef sprout::detail::string_construct_access<T, N1 + N2, Traits> access_type;
return access_type::raw_construct(
lsize + rsize,
(Indexes < lsize ? lhs[Indexes]
: Indexes < lsize + rsize ? rhs[Indexes - lsize]
: T()
)...
);
2012-04-18 04:37:35 +00:00
}
} // namespace detail
2012-04-14 10:06:21 +00:00
//
// operator+
//
template<typename T, std::size_t N, typename Traits>
2012-04-18 04:37:35 +00:00
inline SPROUT_CONSTEXPR sprout::basic_string<T, N + 1, Traits>
operator+(sprout::basic_string<T, N, Traits> const& lhs, T const& rhs) {
return sprout::detail::string_concat(
lhs, lhs.size(),
rhs,
2013-03-31 06:14:10 +00:00
sprout::make_index_tuple<N + 1>::make()
2012-04-18 04:37:35 +00:00
);
2012-04-14 10:06:21 +00:00
}
template<typename T, std::size_t N, typename Traits>
2012-04-18 04:37:35 +00:00
inline SPROUT_CONSTEXPR sprout::basic_string<T, 1 + N, Traits>
operator+(T const& lhs, sprout::basic_string<T, N, Traits> const& rhs) {
return sprout::detail::string_concat(
lhs,
rhs, rhs.size(),
2013-03-31 06:14:10 +00:00
sprout::make_index_tuple<1 + N>::make()
2012-04-18 04:37:35 +00:00
);
2012-04-14 10:06:21 +00:00
}
2012-04-18 04:37:35 +00:00
template<typename T, std::size_t N, typename Traits, std::size_t M>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N + (M - 1), Traits>
operator+(sprout::basic_string<T, N, Traits> const& lhs, T const (& rhs)[M]) {
typedef sprout::char_traits_helper<Traits> traits_type;
2012-04-18 04:37:35 +00:00
return sprout::detail::string_concat(
lhs, lhs.size(),
rhs, traits_type::length(rhs, M - 1),
2013-03-31 06:14:10 +00:00
sprout::make_index_tuple<N + (M - 1)>::make()
2012-04-18 04:37:35 +00:00
);
2012-04-14 10:06:21 +00:00
}
2012-04-18 04:37:35 +00:00
template<typename T, std::size_t N, typename Traits, std::size_t M>
inline SPROUT_CONSTEXPR sprout::basic_string<T, (M - 1) + N, Traits>
operator+(T const (& lhs)[M], sprout::basic_string<T, N, Traits> const& rhs) {
typedef sprout::char_traits_helper<Traits> traits_type;
2012-04-18 04:37:35 +00:00
return sprout::detail::string_concat(
lhs, traits_type::length(lhs, M - 1),
2012-04-18 04:37:35 +00:00
rhs, rhs.size(),
2013-03-31 06:14:10 +00:00
sprout::make_index_tuple<(M - 1) + N>::make()
2012-04-18 04:37:35 +00:00
);
2012-04-14 10:06:21 +00:00
}
2012-04-18 04:37:35 +00:00
template<typename T, std::size_t N1, std::size_t N2, typename Traits>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N1 + N2, Traits>
operator+(sprout::basic_string<T, N1, Traits> const& lhs, sprout::basic_string<T, N2, Traits> const& rhs) {
return sprout::detail::string_concat(
lhs, lhs.size(),
rhs, rhs.size(),
2013-03-31 06:14:10 +00:00
sprout::make_index_tuple<N1 + N2>::make()
2012-04-18 04:37:35 +00:00
);
2012-04-14 10:06:21 +00:00
}
} // namespace sprout
#endif // #ifndef SPROUT_STRING_CONCAT_HPP