mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-10-19 13:49:23 +00:00
fix string_ref
This commit is contained in:
parent
2e0bc89188
commit
84376c3f69
64 changed files with 204 additions and 128 deletions
|
@ -2,8 +2,8 @@
|
|||
#define SPROUT_STRING_CHAR_TRAITS_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/functional/bind2nd.hpp>
|
||||
#include <sprout/iterator/ptr_index_iterator.hpp>
|
||||
|
@ -13,6 +13,36 @@
|
|||
#include <sprout/cstring/strlen.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
template<typename Traits>
|
||||
class char_traits_eq {
|
||||
public:
|
||||
typedef Traits traits_type;
|
||||
typedef typename traits_type::char_type char_type;
|
||||
typedef bool result_type;
|
||||
typedef char_type first_argument_type;
|
||||
typedef char_type second_argument_type;
|
||||
public:
|
||||
SPROUT_CONSTEXPR bool operator()(char_type c1, char_type c2) const SPROUT_NOEXCEPT {
|
||||
return traits_type::eq(c1, c2);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Traits>
|
||||
class char_traits_lt {
|
||||
public:
|
||||
typedef Traits traits_type;
|
||||
typedef typename traits_type::char_type char_type;
|
||||
typedef bool result_type;
|
||||
typedef char_type first_argument_type;
|
||||
typedef char_type second_argument_type;
|
||||
public:
|
||||
SPROUT_CONSTEXPR bool operator()(char_type c1, char_type c2) const SPROUT_NOEXCEPT {
|
||||
return traits_type::lt(c1, c2);
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
//
|
||||
// char_traits
|
||||
//
|
||||
|
@ -26,19 +56,6 @@ namespace sprout {
|
|||
typedef typename impl_type::off_type off_type;
|
||||
typedef typename impl_type::pos_type pos_type;
|
||||
typedef typename impl_type::state_type state_type;
|
||||
private:
|
||||
struct eq_ {
|
||||
public:
|
||||
SPROUT_CONSTEXPR bool operator()(char_type c1, char_type c2) const SPROUT_NOEXCEPT {
|
||||
return eq(c1, c2);
|
||||
}
|
||||
};
|
||||
struct lt_ {
|
||||
public:
|
||||
SPROUT_CONSTEXPR bool operator()(char_type c1, char_type c2) const SPROUT_NOEXCEPT {
|
||||
return lt(c1, c2);
|
||||
}
|
||||
};
|
||||
private:
|
||||
static SPROUT_CONSTEXPR char_type const* find_impl(char_type const* found, char_type const* last) {
|
||||
return found == last ? nullptr
|
||||
|
@ -59,18 +76,18 @@ namespace sprout {
|
|||
return sprout::tristate_lexicographical_compare(
|
||||
sprout::as_iterator(s1), sprout::as_iterator(s1, n), char_type(),
|
||||
sprout::as_iterator(s2), sprout::as_iterator(s2, n), char_type(),
|
||||
lt_()
|
||||
sprout::detail::char_traits_lt<char_traits>()
|
||||
);
|
||||
}
|
||||
static SPROUT_CONSTEXPR std::size_t length(char_type const* s) {
|
||||
return sprout::detail::strlen(s);
|
||||
return sprout::strlen(s);
|
||||
}
|
||||
static SPROUT_CONSTEXPR char_type const* find(char_type const* s, std::size_t n, char_type const& a) {
|
||||
return find_impl(
|
||||
sprout::as_iterator_base(
|
||||
sprout::find_if(
|
||||
sprout::as_iterator(s), sprout::as_iterator(s, n),
|
||||
sprout::bind2nd(eq_(), a)
|
||||
sprout::bind2nd(sprout::detail::char_traits_eq<char_traits>(), a)
|
||||
)
|
||||
),
|
||||
s + n
|
||||
|
@ -106,7 +123,7 @@ namespace sprout {
|
|||
return sprout::tristate_lexicographical_compare(
|
||||
sprout::as_iterator(s1), sprout::as_iterator(s1, n), char_type(),
|
||||
s2, s2 + n, char_type(),
|
||||
lt_()
|
||||
sprout::detail::char_traits_lt<char_traits>()
|
||||
);
|
||||
}
|
||||
template<typename ConstIterator>
|
||||
|
@ -114,7 +131,7 @@ namespace sprout {
|
|||
return sprout::tristate_lexicographical_compare(
|
||||
s1, s1 + n, char_type(),
|
||||
sprout::as_iterator(s2), sprout::as_iterator(s2, n), char_type(),
|
||||
lt_()
|
||||
sprout::detail::char_traits_lt<char_traits>()
|
||||
);
|
||||
}
|
||||
template<typename ConstIterator1, typename ConstIterator2>
|
||||
|
@ -122,7 +139,7 @@ namespace sprout {
|
|||
return sprout::tristate_lexicographical_compare(
|
||||
s1, s1 + n, char_type(),
|
||||
s2, s2 + n, char_type(),
|
||||
lt_()
|
||||
sprout::detail::char_traits_lt<char_traits>()
|
||||
);
|
||||
}
|
||||
template<typename ConstIterator>
|
||||
|
@ -134,7 +151,7 @@ namespace sprout {
|
|||
return sprout::as_iterator_base(
|
||||
sprout::find_if(
|
||||
s, s + n,
|
||||
sprout::bind2nd(eq_(), a)
|
||||
sprout::bind2nd(sprout::detail::char_traits_eq<char_traits>(), a)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@ -168,25 +185,15 @@ namespace sprout {
|
|||
typedef typename traits_type::off_type off_type;
|
||||
typedef typename traits_type::pos_type pos_type;
|
||||
typedef typename traits_type::state_type state_type;
|
||||
private:
|
||||
struct eq_ {
|
||||
public:
|
||||
SPROUT_CONSTEXPR bool operator()(char_type c1, char_type c2) const SPROUT_NOEXCEPT {
|
||||
return traits_type::eq(c1, c2);
|
||||
}
|
||||
};
|
||||
public:
|
||||
static SPROUT_CONSTEXPR std::size_t length(char_type const* s, std::size_t n) {
|
||||
return sprout::distance(
|
||||
sprout::as_iterator(s),
|
||||
find(sprout::as_iterator(s), n, char_type())
|
||||
);
|
||||
return sprout::strlen(s, n);
|
||||
}
|
||||
static SPROUT_CONSTEXPR char_type const* find(char_type const* s, std::size_t n, char_type const& a) {
|
||||
return sprout::as_iterator_base(
|
||||
sprout::find_if(
|
||||
sprout::as_iterator(s), sprout::as_iterator(s, n),
|
||||
sprout::bind2nd(eq_(), a)
|
||||
sprout::bind2nd(sprout::detail::char_traits_eq<traits_type>(), a)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@ -196,16 +203,13 @@ namespace sprout {
|
|||
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
|
||||
template<typename ConstIterator>
|
||||
static SPROUT_CONSTEXPR std::size_t length(ConstIterator s, std::size_t n) {
|
||||
return sprout::distance(
|
||||
s,
|
||||
find(s, n, char_type())
|
||||
);
|
||||
return sprout::detail::strlen(s, n);
|
||||
}
|
||||
template<typename ConstIterator>
|
||||
static SPROUT_CONSTEXPR ConstIterator find(ConstIterator s, std::size_t n, char_type const& a) {
|
||||
return sprout::find_if(
|
||||
s, s + n,
|
||||
sprout::bind2nd(eq_(), a)
|
||||
sprout::bind2nd(sprout::detail::char_traits_eq<traits_type>(), a)
|
||||
);
|
||||
}
|
||||
template<typename ConstIterator>
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#define SPROUT_STRING_DETAIL_COMPARE_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT
|
||||
#include HDR_ALGORITHM_MIN_MAX_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
namespace string_detail {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/string/char_traits.hpp>
|
||||
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT
|
||||
#include HDR_ALGORITHM_MIN_MAX_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
namespace string_detail {
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
#include <sprout/detail/char_conversion.hpp>
|
||||
#include <sprout/detail/math/int.hpp>
|
||||
#include <sprout/detail/math/float.hpp>
|
||||
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT
|
||||
#include HDR_ALGORITHM_MIN_MAX_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include <sprout/string/char_traits.hpp>
|
||||
#include <sprout/string/string.hpp>
|
||||
#include <sprout/utility/value_holder/value_holder.hpp>
|
||||
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT
|
||||
#include HDR_ALGORITHM_MIN_MAX_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define SPROUT_STRING_STRING_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <stdexcept>
|
||||
|
@ -18,7 +19,7 @@
|
|||
#include <sprout/string/char_traits.hpp>
|
||||
#include <sprout/string/npos.hpp>
|
||||
#include <sprout/string/detail/operations.hpp>
|
||||
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT
|
||||
#include HDR_ALGORITHM_MIN_MAX_SSCRISK_CEL_OR_SPROUT
|
||||
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
|
||||
# include <sprout/iterator/index_iterator.hpp>
|
||||
#endif
|
||||
|
@ -89,15 +90,18 @@ namespace sprout {
|
|||
return sprout::basic_string<T, sizeof...(Indexes), Traits>{{(Indexes < M - 1 ? elems[Indexes] : T())...}, len};
|
||||
}
|
||||
public:
|
||||
static SPROUT_CONSTEXPR basic_string from_c_str(value_type const* s, size_type n) {
|
||||
static SPROUT_CONSTEXPR basic_string from_c_str(T const* s, size_type n) {
|
||||
return !(N < n)
|
||||
? from_c_str_impl(s, n, sprout::index_range<0, N>::make())
|
||||
: throw std::out_of_range("basic_string<>: index out of range")
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR basic_string from_c_str(value_type const* s) {
|
||||
static SPROUT_CONSTEXPR basic_string from_c_str(T const* s) {
|
||||
return from_c_str(s, traits_type::length(s));
|
||||
}
|
||||
static SPROUT_CONSTEXPR basic_string from_c_str(std::basic_string<T, Traits> const& s) {
|
||||
return from_c_str(s.data(), s.size());
|
||||
}
|
||||
public:
|
||||
value_type elems[static_size + 1];
|
||||
size_type len;
|
||||
|
@ -489,6 +493,12 @@ namespace sprout {
|
|||
sprout::index_range<0, N2>::make()
|
||||
);
|
||||
}
|
||||
#if SPROUT_USE_EXPLICIT_CONVERSION_OPERATORS
|
||||
template<typename Allocator>
|
||||
explicit operator std::basic_string<T, Traits, Allocator>() const {
|
||||
return std::basic_string<T, Traits, Allocator>(data(), size());
|
||||
}
|
||||
#endif
|
||||
pointer
|
||||
c_array() SPROUT_NOEXCEPT {
|
||||
return &elems[0];
|
||||
|
@ -693,18 +703,12 @@ namespace sprout {
|
|||
template<std::size_t M>
|
||||
static SPROUT_CONSTEXPR typename copied_type::size_type
|
||||
length_impl(sprout::array<T, M> const& arr) {
|
||||
return sprout::distance(
|
||||
arr.begin(),
|
||||
sprout::find(arr.begin(), arr.end(), T())
|
||||
);
|
||||
return sprout::distance(arr.begin(), sprout::find(arr.begin(), arr.end(), T()));
|
||||
}
|
||||
template<typename... Args, sprout::index_t... Indexes>
|
||||
static SPROUT_CONSTEXPR copied_type
|
||||
make_impl(typename copied_type::size_type size, sprout::index_tuple<Indexes...>, Args&&... args) {
|
||||
return copied_type{
|
||||
{(Indexes < size ? sprout::forward<Args>(args) : T())...},
|
||||
size
|
||||
};
|
||||
return copied_type{{(Indexes < size ? sprout::forward<Args>(args) : T())...}, size};
|
||||
}
|
||||
public:
|
||||
template<typename... Args>
|
||||
|
@ -743,11 +747,11 @@ namespace sprout {
|
|||
{
|
||||
return sprout::basic_string<T, N - 1>{{(Indexes < n ? arr[Indexes] : T())...}, n};
|
||||
}
|
||||
template<typename T, std::size_t N, sprout::index_t... Indexes>
|
||||
template<typename T, std::size_t N>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<T, N - 1>
|
||||
to_string_impl(T const(& arr)[N], sprout::index_tuple<Indexes...>) {
|
||||
typedef sprout::char_traits_helper<sprout::char_traits<T> > traits_type;
|
||||
return to_string_impl_1(arr, traits_type::length(arr, N - 1), sprout::index_tuple<Indexes...>());
|
||||
to_string_impl(T const(& arr)[N]) {
|
||||
typedef sprout::char_traits_helper<typename sprout::basic_string<T, N - 1>::traits_type> helper_type;
|
||||
return to_string_impl_1(arr, helper_type::length(arr, N - 1), sprout::index_range<0, N - 1>::make());
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
|
@ -761,7 +765,7 @@ namespace sprout {
|
|||
template<typename T, std::size_t N>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<T, N - 1>
|
||||
to_string(T const(& arr)[N]) {
|
||||
return sprout::detail::to_string_impl(arr, sprout::index_range<0, N - 1>::make());
|
||||
return sprout::detail::to_string_impl(arr);
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -777,6 +781,11 @@ namespace sprout {
|
|||
string_from_c_str(T const* s) {
|
||||
return sprout::basic_string<T, N>::from_c_str(s);
|
||||
}
|
||||
template<std::size_t N, typename T, typename Traits>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<T, N, Traits>
|
||||
string_from_c_str(std::basic_string<T, Traits> const& s) {
|
||||
return sprout::basic_string<T, N, Traits>::from_c_str(s);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_STRING_STRING_HPP
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue