1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00

add string shift_concat operations

This commit is contained in:
bolero-MURAKAMI 2016-03-21 14:38:59 +09:00
parent a1be640b2f
commit c9cce4582e
4 changed files with 328 additions and 20 deletions

View file

@ -14,6 +14,7 @@
#include <sprout/string/string.hpp>
#include <sprout/string/comparison.hpp>
#include <sprout/string/concat.hpp>
#include <sprout/string/shift_concat.hpp>
#include <sprout/string/io.hpp>
#include <sprout/string/hash.hpp>
#include <sprout/string/tuple.hpp>

View file

@ -0,0 +1,199 @@
/*=============================================================================
Copyright (c) 2011-2016 Bolero MURAKAMI
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)
=============================================================================*/
#ifndef SPROUT_STRING_SHIFT_CONCAT_HPP
#define SPROUT_STRING_SHIFT_CONCAT_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
#include <sprout/index_tuple/metafunction.hpp>
#include <sprout/string/char_traits.hpp>
#include <sprout/string/string.hpp>
namespace sprout {
namespace detail {
template<std::size_t N>
inline SPROUT_CONSTEXPR std::size_t checked_length(std::size_t n) {
return n <= N ? n
: throw std::length_error("length exceeded")
;
}
} // namespace detail
namespace detail {
template<typename T, std::size_t N, typename Traits, sprout::index_t... Indexes>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N, Traits>
string_rshift(
sprout::basic_string<T, N, Traits> const& lhs, std::size_t lsize,
T const& rhs,
sprout::index_tuple<Indexes...>
)
{
typedef sprout::detail::string_construct_access<T, N, Traits> access_type;
return access_type::raw_construct(
sprout::detail::checked_length<N>(lsize + 1),
(Indexes < lsize ? lhs[Indexes]
: Indexes < lsize + 1 ? rhs
: T()
)...
);
}
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, Traits>
string_rshift(
sprout::basic_string<T, N, Traits> const& lhs, std::size_t lsize,
T const (& rhs)[M], std::size_t rsize,
sprout::index_tuple<Indexes...>
)
{
typedef sprout::detail::string_construct_access<T, N , Traits> access_type;
return access_type::raw_construct(
sprout::detail::checked_length<N>(lsize + rsize),
(Indexes < lsize ? lhs[Indexes]
: Indexes < lsize + rsize ? rhs[Indexes - lsize]
: T()
)...
);
}
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, Traits>
string_rshift(
sprout::basic_string<T, N1, Traits> const& lhs, std::size_t lsize,
sprout::basic_string<T, N2, Traits> const& rhs, std::size_t rsize,
sprout::index_tuple<Indexes...>
)
{
typedef sprout::detail::string_construct_access<T, N1, Traits> access_type;
return access_type::raw_construct(
sprout::detail::checked_length<N1>(lsize + rsize),
(Indexes < lsize ? lhs[Indexes]
: Indexes < lsize + rsize ? rhs[Indexes - lsize]
: T()
)...
);
}
} // namespace detail
//
// operator<<
//
template<typename T, std::size_t N, typename Traits>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N, Traits>
operator<<(sprout::basic_string<T, N, Traits> const& lhs, T const& rhs) {
return sprout::detail::string_rshift(
lhs, lhs.size(),
rhs,
sprout::make_index_tuple<N>::make()
);
}
template<typename T, std::size_t N, typename Traits, std::size_t M>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N, Traits>
operator<<(sprout::basic_string<T, N, Traits> const& lhs, T const (& rhs)[M]) {
typedef sprout::char_traits_helper<Traits> traits_type;
return sprout::detail::string_rshift(
lhs, lhs.size(),
rhs, traits_type::length(rhs, M - 1),
sprout::make_index_tuple<N>::make()
);
}
template<typename T, std::size_t N1, std::size_t N2, typename Traits>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N1, Traits>
operator<<(sprout::basic_string<T, N1, Traits> const& lhs, sprout::basic_string<T, N2, Traits> const& rhs) {
return sprout::detail::string_rshift(
lhs, lhs.size(),
rhs, rhs.size(),
sprout::make_index_tuple<N1>::make()
);
}
namespace detail {
template<typename T, std::size_t N, typename Traits, sprout::index_t... Indexes>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N, Traits>
string_lshift(
sprout::basic_string<T, N, Traits> const& lhs, std::size_t lsize,
T const& rhs,
sprout::index_tuple<Indexes...>
)
{
typedef sprout::detail::string_construct_access<T, N, Traits> access_type;
return access_type::raw_construct(
sprout::detail::checked_length<N>(1 + lsize),
(Indexes < 1 ? rhs
: Indexes < 1 + lsize ? lhs[Indexes - 1]
: T()
)...
);
}
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, Traits>
string_lshift(
sprout::basic_string<T, N, Traits> const& lhs, std::size_t lsize,
T const (& rhs)[M], std::size_t rsize,
sprout::index_tuple<Indexes...>
)
{
typedef sprout::detail::string_construct_access<T, N , Traits> access_type;
return access_type::raw_construct(
sprout::detail::checked_length<N>(rsize + lsize),
(Indexes < rsize ? rhs[Indexes]
: Indexes < rsize + lsize ? lhs[Indexes - rsize]
: T()
)...
);
}
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, Traits>
string_lshift(
sprout::basic_string<T, N1, Traits> const& lhs, std::size_t lsize,
sprout::basic_string<T, N2, Traits> const& rhs, std::size_t rsize,
sprout::index_tuple<Indexes...>
)
{
typedef sprout::detail::string_construct_access<T, N1, Traits> access_type;
return access_type::raw_construct(
sprout::detail::checked_length<N1>(rsize + lsize),
(Indexes < rsize ? rhs[Indexes]
: Indexes < rsize + lsize ? lhs[Indexes - rsize]
: T()
)...
);
}
//
// operator>>
//
template<typename T, std::size_t N, typename Traits>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N, Traits>
operator>>(sprout::basic_string<T, N, Traits> const& lhs, T const& rhs) {
return sprout::detail::string_rshift(
lhs, lhs.size(),
rhs,
sprout::make_index_tuple<N>::make()
);
}
template<typename T, std::size_t N, typename Traits, std::size_t M>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N, Traits>
operator>>(sprout::basic_string<T, N, Traits> const& lhs, T const (& rhs)[M]) {
typedef sprout::char_traits_helper<Traits> traits_type;
return sprout::detail::string_rshift(
lhs, lhs.size(),
rhs, traits_type::length(rhs, M - 1),
sprout::make_index_tuple<N>::make()
);
}
template<typename T, std::size_t N1, std::size_t N2, typename Traits>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N1, Traits>
operator>>(sprout::basic_string<T, N1, Traits> const& lhs, sprout::basic_string<T, N2, Traits> const& rhs) {
return sprout::detail::string_rshift(
lhs, lhs.size(),
rhs, rhs.size(),
sprout::make_index_tuple<N1>::make()
);
}
} // namespace detail
} // namespace sprout
#endif // #ifndef SPROUT_STRING_SHIFT_CONCAT_HPP

View file

@ -288,9 +288,16 @@ namespace sprout {
}
SPROUT_CXX14_CONSTEXPR void lengthcheck(size_type n) const {
if (n > static_size) {
throw std::length_error("basic_string<>: length error");
throw std::length_error("basic_string<>: length exceeded");
}
}
SPROUT_CXX14_CONSTEXPR void put_terminator() {
traits_type::assign(begin() + impl_.len, static_size - impl_.len, value_type());
}
SPROUT_CXX14_CONSTEXPR void put_terminator(size_type n) {
impl_.len = n;
put_terminator();
}
public:
// construct/copy/destroy:
SPROUT_CONSTEXPR basic_string() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL
@ -484,8 +491,7 @@ namespace sprout {
if (n > size()) {
traits_type::assign(end(), n - size(), c);
}
traits_type::assign(begin() + n, static_size - n, value_type());
impl_.len = n;
put_terminator(n);
}
SPROUT_CXX14_CONSTEXPR void
resize(size_type n) {
@ -493,8 +499,7 @@ namespace sprout {
}
SPROUT_CXX14_CONSTEXPR void
clear() SPROUT_NOEXCEPT {
traits_type::assign(begin(), static_size, value_type());
impl_.len = 0;
put_terminator(0);
}
SPROUT_CONSTEXPR bool
empty() const SPROUT_NOEXCEPT {
@ -557,10 +562,7 @@ namespace sprout {
for (size_type i = 0; i < n; ++i) {
traits_type::assign(impl_.elems[i], s[i]);
}
for (size_type i = n; i < static_size; ++i) {
traits_type::assign(impl_.elems[i], value_type());
}
impl_.len = n;
put_terminator(n);
return *this;
}
SPROUT_CXX14_CONSTEXPR basic_string&
@ -571,8 +573,7 @@ namespace sprout {
assign(size_type n, value_type c) {
maxcheck(n);
traits_type::assign(begin(), n, c);
traits_type::assign(begin() + n, static_size - n, value_type());
impl_.len = n;
put_terminator(n);
return *this;
}
template<typename InputIterator>
@ -582,10 +583,7 @@ namespace sprout {
for (; n < static_size || first != last; ++n, ++first) {
traits_type::assign(impl_.elems[n], *first);
}
for (size_type i = n; i < static_size; ++i) {
traits_type::assign(impl_.elems[i], value_type());
}
impl_.len = n;
put_terminator(n);
return *this;
}
SPROUT_CXX14_CONSTEXPR void
@ -787,7 +785,6 @@ namespace sprout {
operator=(StringConstIterator rhs) {
return assign(rhs);
}
// modifiers (for string iterator):
template<typename StringConstIterator>
SPROUT_CXX14_CONSTEXPR typename std::enable_if<
@ -799,10 +796,7 @@ namespace sprout {
for (size_type i = 0; i < n; ++i) {
traits_type::assign(impl_.elems[i], s[i]);
}
for (size_type i = n; i < static_size; ++i) {
traits_type::assign(impl_.elems[i], value_type());
}
impl_.len = n;
put_terminator(n);
return *this;
}
template<typename StringConstIterator>
@ -979,6 +973,52 @@ namespace sprout {
return sprout::distance(begin(), p);
}
#endif
SPROUT_CXX14_CONSTEXPR basic_string& operator<<=(T const& rhs) {
lengthcheck(size() + 1);
traits_type::assign(end(), 1, rhs);
put_terminator(impl_.len + 1);
return *this;
}
template<std::size_t M>
SPROUT_CXX14_CONSTEXPR basic_string& operator<<=(T const (& rhs)[M]) {
std::size_t rsize = traits_type::length(rhs, M - 1);
lengthcheck(size() + rsize);
traits_type::copy(end(), rhs, rsize);
put_terminator(impl_.len + rsize);
return *this;
}
template<std::size_t N2>
SPROUT_CXX14_CONSTEXPR basic_string& operator<<=(basic_string<T, N2> const& rhs) {
lengthcheck(size() + rhs.size());
traits_type::copy(end(), rhs.begin(), rhs.size());
put_terminator(impl_.len + rhs.size());
return *this;
}
SPROUT_CXX14_CONSTEXPR basic_string& operator>>=(T const& rhs) {
lengthcheck(size() + 1);
traits_type::move(begin() + 1, begin(), size());
traits_type::assign(begin(), 1, rhs);
put_terminator(impl_.len + 1);
return *this;
}
template<std::size_t M>
SPROUT_CXX14_CONSTEXPR basic_string& operator>>=(T const (& rhs)[M]) {
std::size_t rsize = traits_type::length(rhs, M - 1);
lengthcheck(size() + rsize);
traits_type::move(begin() + rsize, begin(), size());
traits_type::copy(begin(), rhs, rsize);
put_terminator(impl_.len + rsize);
return *this;
}
template<std::size_t N2>
SPROUT_CXX14_CONSTEXPR basic_string& operator>>=(basic_string<T, N2> const& rhs) {
lengthcheck(size() + rhs.size());
traits_type::move(begin() + rhs.size(), begin(), size());
traits_type::copy(begin(), rhs.begin(), rhs.size());
put_terminator(impl_.len + rhs.size());
return *this;
}
};
template<typename T, std::size_t N, typename Traits>
SPROUT_CONSTEXPR_OR_CONST typename sprout::basic_string<T, N, Traits>::size_type sprout::basic_string<T, N, Traits>::npos;

View file

@ -18,6 +18,7 @@
#include <sprout/iterator/operation.hpp>
#include <sprout/algorithm/cxx14/fill_n.hpp>
#include <sprout/utility/swap.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/utility/move.hpp>
namespace sprout {
@ -223,6 +224,7 @@ namespace sprout {
SPROUT_STATIC_CONSTEXPR bool is_reference = impl_type::is_reference;
SPROUT_STATIC_CONSTEXPR bool is_const = impl_type::is_const;
public:
typedef typename facade_type::value_type value_type;
typedef typename facade_type::iterator iterator;
typedef typename facade_type::const_iterator const_iterator;
typedef typename facade_type::reference reference;
@ -413,6 +415,41 @@ namespace sprout {
return impl_.to_last_ - sprout::size(get_internal());
}
SPROUT_CXX14_CONSTEXPR sub_array& window(difference_type to_first = 0) {
impl_.to_first_ = to_first;
impl_.to_last_ = size();
return *this;
}
SPROUT_CXX14_CONSTEXPR sub_array& window(difference_type to_first, difference_type to_last) {
impl_.to_first_ = to_first;
impl_.to_last_ = to_last;
return *this;
}
SPROUT_CONSTEXPR sub_array window(difference_type to_first = 0) const {
return sub_array(impl_.array_, to_first, size());
}
SPROUT_CONSTEXPR sub_array window(difference_type to_first, difference_type to_last) const {
return sub_array(impl_.array_, to_first, to_last);
}
SPROUT_CONSTEXPR sub_array cwindow(difference_type to_first = 0) const {
return window(to_first);
}
SPROUT_CONSTEXPR sub_array cwindow(difference_type to_first, difference_type to_last) const {
return window(to_first, to_last);
}
SPROUT_CXX14_CONSTEXPR sub_array& offset(difference_type from_begin = 0, difference_type from_end = 0) {
impl_.to_first_ += from_begin;
impl_.to_last_ += from_end;
return *this;
}
SPROUT_CONSTEXPR sub_array offset(difference_type from_begin = 0, difference_type from_end = 0) const {
return sub_array(impl_.array_, impl_.to_first_ + from_begin, impl_.to_last_ + from_end);
}
SPROUT_CONSTEXPR sub_array coffset(difference_type from_begin = 0, difference_type from_end = 0) const {
return offset(from_begin, from_end);
}
SPROUT_CXX14_CONSTEXPR iterator nth(size_type i) {
return i < size()
? sprout::next(begin(), i)
@ -431,6 +468,37 @@ namespace sprout {
SPROUT_CONSTEXPR size_type index_of(const_iterator p) const {
return sprout::distance(begin(), p);
}
SPROUT_CXX14_CONSTEXPR void push_back(value_type const& x) {
impl_.array_[impl_.to_last_] = x;
++impl_.to_last_;
}
SPROUT_CXX14_CONSTEXPR void push_back(value_type&& x) {
impl_.array_[impl_.to_last_] = sprout::move(x);
++impl_.to_last_;
}
SPROUT_CXX14_CONSTEXPR void pop_back() {
impl_.array_[impl_.to_last_ - 1] = value_type();
--impl_.to_last_;
}
SPROUT_CXX14_CONSTEXPR void drop_back() {
--impl_.to_last_;
}
SPROUT_CXX14_CONSTEXPR void push_front(value_type const& x) {
impl_.array_[impl_.to_first_ - 1] = x;
--impl_.to_first_;
}
SPROUT_CXX14_CONSTEXPR void push_front(value_type&& x) {
impl_.array_[impl_.to_first_ - 1] = sprout::move(x);
--impl_.to_first_;
}
SPROUT_CXX14_CONSTEXPR void pop_front() {
impl_.array_[impl_.to_first_] = value_type();
++impl_.to_first_;
}
SPROUT_CXX14_CONSTEXPR void drop_front() {
++impl_.to_first_;
}
};
template<typename Container>
SPROUT_CONSTEXPR_OR_CONST typename sprout::sub_array<Container>::size_type sprout::sub_array<Container>::enumerable_size;