add string_ref user-defined literals

This commit is contained in:
bolero-MURAKAMI 2016-03-25 10:48:59 +09:00
parent 2fce5bd7f9
commit 5bdb045730
7 changed files with 398 additions and 199 deletions

View file

@ -77,6 +77,23 @@ namespace sprout {
)... )...
); );
} }
template<typename T, std::size_t N, typename Traits, sprout::index_t... Indexes>
inline SPROUT_INITIALIZER_LIST_CONSTEXPR sprout::basic_string<T, N, Traits>
string_rshift(
sprout::basic_string<T, N, Traits> const& lhs, std::size_t lsize,
std::initializer_list<T> rhs, 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.begin()[Indexes - lsize]
: T()
)...
);
}
} // namespace detail } // namespace detail
// //
// operator<< // operator<<
@ -108,6 +125,15 @@ namespace sprout {
sprout::make_index_tuple<N1>::make() sprout::make_index_tuple<N1>::make()
); );
} }
template<typename T, std::size_t N, typename Traits>
inline SPROUT_INITIALIZER_LIST_CONSTEXPR sprout::basic_string<T, N, Traits>
operator<<(sprout::basic_string<T, N, Traits> const& lhs, std::initializer_list<T> rhs) {
return sprout::detail::string_rshift(
lhs, lhs.size(),
rhs, rhs.size(),
sprout::make_index_tuple<N>::make()
);
}
namespace detail { namespace detail {
template<typename T, std::size_t N, typename Traits, sprout::index_t... Indexes> template<typename T, std::size_t N, typename Traits, sprout::index_t... Indexes>
@ -161,6 +187,23 @@ namespace sprout {
)... )...
); );
} }
template<typename T, std::size_t N, typename Traits, sprout::index_t... Indexes>
inline SPROUT_INITIALIZER_LIST_CONSTEXPR sprout::basic_string<T, N, Traits>
string_lshift(
sprout::basic_string<T, N, Traits> const& lhs, std::size_t lsize,
std::initializer_list<T> rhs, 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.begin()[Indexes]
: Indexes < rsize + lsize ? lhs[Indexes - rsize]
: T()
)...
);
}
} // namespace detail } // namespace detail
// //
// operator>> // operator>>
@ -192,6 +235,15 @@ namespace sprout {
sprout::make_index_tuple<N1>::make() sprout::make_index_tuple<N1>::make()
); );
} }
template<typename T, std::size_t N, typename Traits>
inline SPROUT_INITIALIZER_LIST_CONSTEXPR sprout::basic_string<T, N, Traits>
operator>>(sprout::basic_string<T, N, Traits> const& lhs, std::initializer_list<T> rhs) {
return sprout::detail::string_lshift(
lhs, lhs.size(),
rhs, rhs.size(),
sprout::make_index_tuple<N>::make()
);
}
} // namespace sprout } // namespace sprout
#endif // #ifndef SPROUT_STRING_SHIFT_CONCAT_HPP #endif // #ifndef SPROUT_STRING_SHIFT_CONCAT_HPP

View file

@ -281,28 +281,31 @@ namespace sprout {
sprout::detail::string_raw_construct_t(), n, SPROUT_FORWARD(Args, args)... sprout::detail::string_raw_construct_t(), n, SPROUT_FORWARD(Args, args)...
) )
{} {}
SPROUT_CXX14_CONSTEXPR void maxcheck(size_type n) const { SPROUT_CXX14_CONSTEXPR void
if (n > static_size) { maxcheck(size_type n) const {
if (n > max_size()) {
throw std::out_of_range("basic_string<>: index out of range"); throw std::out_of_range("basic_string<>: index out of range");
} }
} }
SPROUT_CXX14_CONSTEXPR void lengthcheck(size_type n) const { SPROUT_CXX14_CONSTEXPR void
if (n > static_size) { lengthcheck(size_type n) const {
if (n > max_size()) {
throw std::length_error("basic_string<>: length exceeded"); throw std::length_error("basic_string<>: length exceeded");
} }
} }
SPROUT_CXX14_CONSTEXPR void put_terminator() { SPROUT_CXX14_CONSTEXPR void
traits_type::assign(begin() + size(), static_size - size(), value_type()); put_terminator(size_type n) {
} if (n < size()) {
SPROUT_CXX14_CONSTEXPR void put_terminator(size_type n) { traits_type::assign(begin() + n, size() - n, value_type());
}
impl_.len = n; impl_.len = n;
put_terminator();
} }
template<typename InputIterator> template<typename InputIterator>
SPROUT_CXX14_CONSTEXPR basic_string& assign(InputIterator first, InputIterator last, std::input_iterator_tag*) { SPROUT_CXX14_CONSTEXPR basic_string&
assign(InputIterator first, InputIterator last, std::input_iterator_tag*) {
iterator it = begin(); iterator it = begin();
for (; first != last; ++first, ++it) { for (; first != last; ++first, ++it) {
if (sprout::distance(begin(), it) > static_size) { if (sprout::distance(begin(), it) > max_size()) {
throw std::length_error("basic_string<>: length exceeded"); throw std::length_error("basic_string<>: length exceeded");
} }
traits_type::assign(*it, *first); traits_type::assign(*it, *first);
@ -311,7 +314,8 @@ namespace sprout {
return *this; return *this;
} }
template<typename ForwardIterator> template<typename ForwardIterator>
SPROUT_CXX14_CONSTEXPR basic_string& assign(ForwardIterator first, ForwardIterator last, std::forward_iterator_tag*) { SPROUT_CXX14_CONSTEXPR basic_string&
assign(ForwardIterator first, ForwardIterator last, std::forward_iterator_tag*) {
lengthcheck(sprout::distance(first, last)); lengthcheck(sprout::distance(first, last));
iterator it = begin(); iterator it = begin();
for (; first != last; ++first, ++it) { for (; first != last; ++first, ++it) {
@ -321,10 +325,11 @@ namespace sprout {
return *this; return *this;
} }
template<typename InputIterator> template<typename InputIterator>
SPROUT_CXX14_CONSTEXPR basic_string& append(InputIterator first, InputIterator last, std::input_iterator_tag*) { SPROUT_CXX14_CONSTEXPR basic_string&
append(InputIterator first, InputIterator last, std::input_iterator_tag*) {
iterator it = end(); iterator it = end();
for (; first != last; ++first, ++it) { for (; first != last; ++first, ++it) {
if (sprout::distance(begin(), it) > static_size) { if (sprout::distance(begin(), it) > max_size()) {
throw std::length_error("basic_string<>: length exceeded"); throw std::length_error("basic_string<>: length exceeded");
} }
traits_type::assign(*it, *first); traits_type::assign(*it, *first);
@ -333,7 +338,8 @@ namespace sprout {
return *this; return *this;
} }
template<typename ForwardIterator> template<typename ForwardIterator>
SPROUT_CXX14_CONSTEXPR basic_string& append(ForwardIterator first, ForwardIterator last, std::forward_iterator_tag*) { SPROUT_CXX14_CONSTEXPR basic_string&
append(ForwardIterator first, ForwardIterator last, std::forward_iterator_tag*) {
lengthcheck(size() + sprout::distance(first, last)); lengthcheck(size() + sprout::distance(first, last));
iterator it = end(); iterator it = end();
for (; first != last; ++first, ++it) { for (; first != last; ++first, ++it) {
@ -437,6 +443,10 @@ namespace sprout {
operator=(value_type rhs) { operator=(value_type rhs) {
return assign(1, rhs); return assign(1, rhs);
} }
SPROUT_CXX14_CONSTEXPR basic_string&
operator=(std::initializer_list<value_type> il) {
return assign(il);
}
// iterators: // iterators:
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION #if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
SPROUT_CXX14_CONSTEXPR iterator SPROUT_CXX14_CONSTEXPR iterator
@ -541,6 +551,10 @@ namespace sprout {
resize(size_type n) { resize(size_type n) {
resize(n, value_type()); resize(n, value_type());
} }
SPROUT_CONSTEXPR size_type
capacity() const SPROUT_NOEXCEPT {
return static_size;
}
SPROUT_CXX14_CONSTEXPR void SPROUT_CXX14_CONSTEXPR void
clear() SPROUT_NOEXCEPT { clear() SPROUT_NOEXCEPT {
put_terminator(0); put_terminator(0);
@ -1063,63 +1077,80 @@ namespace sprout {
#endif #endif
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION #if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
SPROUT_CXX14_CONSTEXPR iterator nth(size_type i) { SPROUT_CXX14_CONSTEXPR iterator
nth(size_type i) {
return i < size() return i < size()
? iterator(*this, i) ? iterator(*this, i)
: (throw std::out_of_range("basic_string<>: index out of range"), iterator()) : (throw std::out_of_range("basic_string<>: index out of range"), iterator())
; ;
} }
SPROUT_CONSTEXPR const_iterator nth(size_type i) const { SPROUT_CONSTEXPR const_iterator
nth(size_type i) const {
return i < size() return i < size()
? const_iterator(*this, i) ? const_iterator(*this, i)
: (throw std::out_of_range("basic_string<>: index out of range"), const_iterator()) : (throw std::out_of_range("basic_string<>: index out of range"), const_iterator())
; ;
} }
SPROUT_CXX14_CONSTEXPR size_type index_of(iterator p) SPROUT_NOEXCEPT { SPROUT_CXX14_CONSTEXPR size_type
index_of(iterator p) SPROUT_NOEXCEPT {
return p.index(); return p.index();
} }
SPROUT_CONSTEXPR size_type index_of(const_iterator p) const SPROUT_NOEXCEPT { SPROUT_CONSTEXPR size_type
index_of(const_iterator p) const SPROUT_NOEXCEPT {
return p.index(); return p.index();
} }
#else #else
SPROUT_CXX14_CONSTEXPR iterator nth(size_type i) { SPROUT_CXX14_CONSTEXPR iterator
nth(size_type i) {
return i < size() return i < size()
? data() + i ? data() + i
: (throw std::out_of_range("basic_string<>: index out of range"), iterator()) : (throw std::out_of_range("basic_string<>: index out of range"), iterator())
; ;
} }
SPROUT_CONSTEXPR const_iterator nth(size_type i) const { SPROUT_CONSTEXPR const_iterator
th(size_type i) const {
return i < size() return i < size()
? data() + i ? data() + i
: (throw std::out_of_range("basic_string<>: index out of range"), const_iterator()) : (throw std::out_of_range("basic_string<>: index out of range"), const_iterator())
; ;
}SPROUT_CXX14_CONSTEXPR }SPROUT_CXX14_CONSTEXPR
SPROUT_CONSTEXPR size_type index_of(iterator p) SPROUT_NOEXCEPT { SPROUT_CONSTEXPR size_type
index_of(iterator p) SPROUT_NOEXCEPT {
return sprout::distance(begin(), p); return sprout::distance(begin(), p);
} }
SPROUT_CONSTEXPR size_type index_of(const_iterator p) const SPROUT_NOEXCEPT { SPROUT_CONSTEXPR size_type
index_of(const_iterator p) const SPROUT_NOEXCEPT {
return sprout::distance(begin(), p); return sprout::distance(begin(), p);
} }
#endif #endif
SPROUT_CXX14_CONSTEXPR basic_string& operator<<=(T const& rhs) { SPROUT_CXX14_CONSTEXPR basic_string&
operator<<=(T const& rhs) {
push_back(rhs); push_back(rhs);
return *this; return *this;
} }
SPROUT_CXX14_CONSTEXPR basic_string& operator<<=(value_type const* rhs) { SPROUT_CXX14_CONSTEXPR basic_string&
operator<<=(value_type const* rhs) {
return append(rhs); return append(rhs);
} }
template<std::size_t N2> template<std::size_t N2>
SPROUT_CXX14_CONSTEXPR basic_string& operator<<=(basic_string<T, N2, Traits> const& rhs) { SPROUT_CXX14_CONSTEXPR basic_string&
operator<<=(basic_string<T, N2, Traits> const& rhs) {
return append(rhs); return append(rhs);
} }
SPROUT_CXX14_CONSTEXPR basic_string& operator>>=(T const& rhs) { SPROUT_CXX14_CONSTEXPR basic_string&
operator<<=(std::initializer_list<value_type> rhs) {
return append(rhs);
}
SPROUT_CXX14_CONSTEXPR basic_string&
operator>>=(T const& rhs) {
lengthcheck(size() + 1); lengthcheck(size() + 1);
traits_type::move(begin() + 1, begin(), size()); traits_type::move(begin() + 1, begin(), size());
traits_type::assign(begin(), 1, rhs); traits_type::assign(begin(), 1, rhs);
put_terminator(size() + 1); put_terminator(size() + 1);
return *this; return *this;
} }
SPROUT_CXX14_CONSTEXPR basic_string& operator>>=(value_type const* rhs) { SPROUT_CXX14_CONSTEXPR basic_string&
operator>>=(value_type const* rhs) {
size_type rlen = traits_type::length(rhs); size_type rlen = traits_type::length(rhs);
lengthcheck(size() + rlen); lengthcheck(size() + rlen);
traits_type::move(begin() + rlen, begin(), size()); traits_type::move(begin() + rlen, begin(), size());
@ -1128,7 +1159,16 @@ namespace sprout {
return *this; return *this;
} }
template<std::size_t N2> template<std::size_t N2>
SPROUT_CXX14_CONSTEXPR basic_string& operator>>=(basic_string<T, N2, Traits> const& rhs) { SPROUT_CXX14_CONSTEXPR basic_string&
operator>>=(basic_string<T, N2, Traits> const& rhs) {
lengthcheck(size() + rhs.size());
traits_type::move(begin() + rhs.size(), begin(), size());
traits_type::move(begin(), rhs.begin(), rhs.size());
put_terminator(size() + rhs.size());
return *this;
}
SPROUT_CXX14_CONSTEXPR basic_string&
operator>>=(std::initializer_list<value_type> rhs) {
lengthcheck(size() + rhs.size()); lengthcheck(size() + rhs.size());
traits_type::move(begin() + rhs.size(), begin(), size()); traits_type::move(begin() + rhs.size(), begin(), size());
traits_type::move(begin(), rhs.begin(), rhs.size()); traits_type::move(begin(), rhs.begin(), rhs.size());

View file

@ -16,5 +16,7 @@
#include <sprout/utility/string_ref/conversion.hpp> #include <sprout/utility/string_ref/conversion.hpp>
#include <sprout/utility/string_ref/type_traits.hpp> #include <sprout/utility/string_ref/type_traits.hpp>
#include <sprout/utility/string_ref/alias.hpp> #include <sprout/utility/string_ref/alias.hpp>
#include <sprout/utility/string_ref/udl.hpp>
#include <sprout/utility/string_ref/string_view.hpp>
#endif // #ifndef SPROUT_UTILITY_STRING_REF_HPP #endif // #ifndef SPROUT_UTILITY_STRING_REF_HPP

View file

@ -9,9 +9,7 @@
#define SPROUT_UTILITY_STRING_REF_ALIAS_HPP #define SPROUT_UTILITY_STRING_REF_ALIAS_HPP
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/string/char_traits.hpp>
#include <sprout/utility/string_ref/string_ref.hpp> #include <sprout/utility/string_ref/string_ref.hpp>
#include <sprout/utility/string_ref/type_traits.hpp>
namespace sprout { namespace sprout {
// //
@ -26,150 +24,6 @@ namespace sprout {
typedef sprout::basic_string_ref<char16_t> u16string_ref; typedef sprout::basic_string_ref<char16_t> u16string_ref;
typedef sprout::basic_string_ref<char32_t> u32string_ref; typedef sprout::basic_string_ref<char32_t> u32string_ref;
#endif #endif
//
// string_view
// wstring_view
// u16string_view
// u32string_view
//
typedef sprout::basic_string_ref<char> string_view;
typedef sprout::basic_string_ref<wchar_t> wstring_view;
#if SPROUT_USE_UNICODE_LITERALS
typedef sprout::basic_string_ref<char16_t> u16string_view;
typedef sprout::basic_string_ref<char32_t> u32string_view;
#endif
#if SPROUT_USE_TEMPLATE_ALIASES
//
// basic_string_view
//
template<typename T, typename Traits = sprout::char_traits<T> >
using basic_string_view = sprout::basic_string_ref<T, Traits>;
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
//
// to_string_view
//
template<typename T, typename Traits>
inline SPROUT_CONSTEXPR sprout::basic_string_ref<T, Traits>
to_string_view(sprout::basic_string_ref<T, Traits> const& s) {
return sprout::to_string_ref(s);
}
template<typename T, std::size_t N, typename Traits>
inline SPROUT_CONSTEXPR sprout::basic_string_ref<T, Traits>
to_string_view(sprout::basic_string<T, N, Traits> const& s) {
return sprout::to_string_ref(s);
}
template<typename T, typename Traits>
inline SPROUT_NON_CONSTEXPR sprout::basic_string_ref<T, Traits>
to_string_view(std::basic_string<T, Traits> const& s) {
return sprout::to_string_ref(s);
}
template<typename T>
inline SPROUT_CONSTEXPR sprout::basic_string_ref<T>
to_string_view(T const* str) {
return sprout::to_string_ref(str);
}
template<typename T>
inline SPROUT_CONSTEXPR sprout::basic_string_ref<T>
to_string_view(T const* str, std::size_t len) {
return sprout::to_string_ref(str, len);
}
//
// is_basic_string_view
//
#if SPROUT_USE_TEMPLATE_ALIASES
template<typename T>
using is_basic_string_view = sprout::is_basic_string_ref<T>;
#else // #if SPROUT_USE_TEMPLATE_ALIASES
template<typename T>
struct is_basic_string_view
: public sprout::is_basic_string_ref<T>
{};
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
//
// is_string_view_of
//
#if SPROUT_USE_TEMPLATE_ALIASES
template<typename T, typename Elem>
using is_string_view_of = sprout::is_string_ref_of<T, Elem>;
#else // #if SPROUT_USE_TEMPLATE_ALIASES
template<typename T, typename Elem>
struct is_string_view_of
: public sprout::is_string_ref_of<T, Elem>
{};
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
//
// is_string_view
//
#if SPROUT_USE_TEMPLATE_ALIASES
template<typename T>
using is_string_view = sprout::is_string_ref<T>;
#else // #if SPROUT_USE_TEMPLATE_ALIASES
template<typename T, typename Elem>
struct is_string_view
: public sprout::is_string_ref<T>
{};
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
//
// is_wstring_view
//
#if SPROUT_USE_TEMPLATE_ALIASES
template<typename T>
using is_wstring_view = sprout::is_wstring_ref<T>;
#else // #if SPROUT_USE_TEMPLATE_ALIASES
template<typename T, typename Elem>
struct is_wstring_view
: public sprout::is_wstring_ref<T>
{};
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
#if SPROUT_USE_UNICODE_LITERALS
//
// is_u16string_view
//
#if SPROUT_USE_TEMPLATE_ALIASES
template<typename T>
using is_u16string_view = sprout::is_u16string_ref<T>;
#else // #if SPROUT_USE_TEMPLATE_ALIASES
template<typename T, typename Elem>
struct is_u16string_view
: public sprout::is_u16string_ref<T>
{};
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
//
// is_u32string_view
//
#if SPROUT_USE_TEMPLATE_ALIASES
template<typename T>
using is_u32string_view = sprout::is_u32string_ref<T>;
#else // #if SPROUT_USE_TEMPLATE_ALIASES
template<typename T, typename Elem>
struct is_u32string_view
: public sprout::is_u32string_ref<T>
{};
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
#endif
#if SPROUT_USE_VARIABLE_TEMPLATES
template<typename T>
SPROUT_STATIC_CONSTEXPR bool is_basic_string_view_v = sprout::is_basic_string_view<T>::value;
template<typename T, typename Elem>
SPROUT_STATIC_CONSTEXPR bool is_string_view_of_v = sprout::is_string_view_of<T, Elem>::value;
template<typename T>
SPROUT_STATIC_CONSTEXPR bool is_string_view_v = sprout::is_string_view<T>::value;
template<typename T>
SPROUT_STATIC_CONSTEXPR bool is_wstring_view_v = sprout::is_wstring_view<T>::value;
#if SPROUT_USE_UNICODE_LITERALS
template<typename T>
SPROUT_STATIC_CONSTEXPR bool is_u16string_view_v = sprout::is_u16string_view<T>::value;
template<typename T>
SPROUT_STATIC_CONSTEXPR bool is_u32string_view_v = sprout::is_u32string_view<T>::value;
#endif
#endif // #if SPROUT_USE_VARIABLE_TEMPLATES
} // namespace sprout } // namespace sprout
#endif // #ifndef SPROUT_UTILITY_STRING_REF_ALIAS_HPP #endif // #ifndef SPROUT_UTILITY_STRING_REF_ALIAS_HPP

View file

@ -9,6 +9,7 @@
#define SPROUT_UTILITY_STRING_REF_STRING_REF_HPP #define SPROUT_UTILITY_STRING_REF_STRING_REF_HPP
#include <string> #include <string>
#include <memory>
#include <stdexcept> #include <stdexcept>
#include <type_traits> #include <type_traits>
#include <sprout/config.hpp> #include <sprout/config.hpp>
@ -124,19 +125,19 @@ namespace sprout {
#else #else
SPROUT_CXX14_CONSTEXPR iterator SPROUT_CXX14_CONSTEXPR iterator
begin() SPROUT_NOEXCEPT { begin() SPROUT_NOEXCEPT {
return data(); return ptr_;
} }
SPROUT_CONSTEXPR const_iterator SPROUT_CONSTEXPR const_iterator
begin() const SPROUT_NOEXCEPT { begin() const SPROUT_NOEXCEPT {
return data(); return ptr_;
} }
SPROUT_CXX14_CONSTEXPR iterator SPROUT_CXX14_CONSTEXPR iterator
end() SPROUT_NOEXCEPT { end() SPROUT_NOEXCEPT {
return data() + size(); return ptr_ + size();
} }
SPROUT_CONSTEXPR const_iterator SPROUT_CONSTEXPR const_iterator
end() const SPROUT_NOEXCEPT { end() const SPROUT_NOEXCEPT {
return data() + size(); return ptr_ + size();
} }
#endif #endif
SPROUT_CONSTEXPR const_reverse_iterator SPROUT_CONSTEXPR const_reverse_iterator
@ -159,11 +160,11 @@ namespace sprout {
#else #else
SPROUT_CONSTEXPR const_iterator SPROUT_CONSTEXPR const_iterator
cbegin() const SPROUT_NOEXCEPT { cbegin() const SPROUT_NOEXCEPT {
return data(); return ptr_;
} }
SPROUT_CONSTEXPR const_iterator SPROUT_CONSTEXPR const_iterator
cend() const SPROUT_NOEXCEPT { cend() const SPROUT_NOEXCEPT {
return data() + size(); return ptr_ + size();
} }
#endif #endif
SPROUT_CONSTEXPR const_reverse_iterator SPROUT_CONSTEXPR const_reverse_iterator
@ -232,8 +233,8 @@ namespace sprout {
} }
SPROUT_CONSTEXPR basic_string_ref SPROUT_CONSTEXPR basic_string_ref
remove_prefix(size_type n) const { remove_prefix(size_type n) const {
return n > size() ? basic_string_ref(data() + size(), 0) return n > size() ? basic_string_ref(ptr_ + size(), 0)
: basic_string_ref(data() + n, size() - n) : basic_string_ref(ptr_ + n, size() - n)
; ;
} }
SPROUT_CXX14_CONSTEXPR void SPROUT_CXX14_CONSTEXPR void
@ -245,8 +246,8 @@ namespace sprout {
} }
SPROUT_CONSTEXPR basic_string_ref SPROUT_CONSTEXPR basic_string_ref
remove_suffix(size_type n) const { remove_suffix(size_type n) const {
return n > size() ? basic_string_ref(data(), 0) return n > size() ? basic_string_ref(ptr_, 0)
: basic_string_ref(data(), size() - n) : basic_string_ref(ptr_, size() - n)
; ;
} }
// string operations: // string operations:
@ -355,6 +356,13 @@ namespace sprout {
find_last_not_of(value_type c, size_type pos = npos) const { find_last_not_of(value_type c, size_type pos = npos) const {
return sprout::string_detail::find_last_not_of_c_impl<basic_string_ref>(begin(), size(), c, pos); return sprout::string_detail::find_last_not_of_c_impl<basic_string_ref>(begin(), size(), c, pos);
} }
SPROUT_CXX14_CONSTEXPR size_type
copy(value_type* s, size_type n, size_type pos = 0) const {
rangecheck(pos);
size_type llen = NS_SSCRISK_CEL_OR_SPROUT::min(n, size() - pos);
traits_type::copy(s, begin() + pos, llen);
return llen;
}
SPROUT_CONSTEXPR basic_string_ref SPROUT_CONSTEXPR basic_string_ref
substr(size_type pos = 0, size_type n = npos) const { substr(size_type pos = 0, size_type n = npos) const {
return !(size() < pos) ? n == npos return !(size() < pos) ? n == npos
@ -399,7 +407,7 @@ namespace sprout {
} }
SPROUT_CONSTEXPR bool SPROUT_CONSTEXPR bool
starts_with(basic_string_ref const& str) const { starts_with(basic_string_ref const& str) const {
return size() >= str.size() && traits_type::compare(data(), str.data(), str.size()) == 0; return size() >= str.size() && traits_type::compare(begin(), str.begin(), str.size()) == 0;
} }
SPROUT_CONSTEXPR bool SPROUT_CONSTEXPR bool
ends_with(value_type c) const { ends_with(value_type c) const {
@ -407,24 +415,30 @@ namespace sprout {
} }
SPROUT_CONSTEXPR bool SPROUT_CONSTEXPR bool
ends_with(basic_string_ref const& str) const { ends_with(basic_string_ref const& str) const {
return size() >= str.size() && traits_type::compare(data() + size() - str.size(), str.data(), str.size()) == 0; return size() >= str.size() && traits_type::compare(begin() + size() - str.size(), str.begin(), str.size()) == 0;
}
template<std::size_t N>
SPROUT_NON_CONSTEXPR sprout::basic_string<T, N, Traits>
to_string() const {
return sprout::basic_string<T, N, Traits>(ptr_, size());
}
template<typename Allocator = std::allocator<T> >
SPROUT_NON_CONSTEXPR std::basic_string<T, Traits, Allocator>
to_string() const {
return std::basic_string<T, Traits, Allocator>(ptr_, size());
}
template<std::size_t N>
SPROUT_EXPLICIT_CONVERSION SPROUT_NON_CONSTEXPR operator sprout::basic_string<T, N, Traits>() const {
return to_string<N>();
} }
// others:
template<typename Allocator> template<typename Allocator>
SPROUT_EXPLICIT_CONVERSION SPROUT_NON_CONSTEXPR operator std::basic_string<T, Traits, Allocator>() const { SPROUT_EXPLICIT_CONVERSION SPROUT_NON_CONSTEXPR operator std::basic_string<T, Traits, Allocator>() const {
return std::basic_string<T, Traits, Allocator>(data(), size()); return to_string<Allocator>();
}
SPROUT_CONSTEXPR const_pointer
c_array() const SPROUT_NOEXCEPT {
return data();
}
SPROUT_CXX14_CONSTEXPR pointer
c_array() SPROUT_NOEXCEPT {
return data();
} }
// others: // others:
SPROUT_CXX14_CONSTEXPR void rangecheck(size_type i) const { SPROUT_CXX14_CONSTEXPR void
return i >= size() ? throw std::out_of_range("uuid: index out of range") rangecheck(size_type i) const {
return i >= size() ? throw std::out_of_range("basic_string_ref<>: index out of range")
: (void)0 : (void)0
; ;
} }

View file

@ -0,0 +1,189 @@
/*=============================================================================
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_UTILITY_STRING_REF_STRING_VIEW_HPP
#define SPROUT_UTILITY_STRING_REF_STRING_VIEW_HPP
#include <sprout/config.hpp>
#include <sprout/string/char_traits.hpp>
#include <sprout/utility/string_ref/string_ref.hpp>
#include <sprout/utility/string_ref/type_traits.hpp>
namespace sprout {
//
// string_view
// wstring_view
// u16string_view
// u32string_view
//
typedef sprout::basic_string_ref<char> string_view;
typedef sprout::basic_string_ref<wchar_t> wstring_view;
#if SPROUT_USE_UNICODE_LITERALS
typedef sprout::basic_string_ref<char16_t> u16string_view;
typedef sprout::basic_string_ref<char32_t> u32string_view;
#endif
#if SPROUT_USE_TEMPLATE_ALIASES
//
// basic_string_view
//
template<typename T, typename Traits = sprout::char_traits<T> >
using basic_string_view = sprout::basic_string_ref<T, Traits>;
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
//
// to_string_view
//
template<typename T, typename Traits>
inline SPROUT_CONSTEXPR sprout::basic_string_ref<T, Traits>
to_string_view(sprout::basic_string_ref<T, Traits> const& s) {
return sprout::to_string_ref(s);
}
template<typename T, std::size_t N, typename Traits>
inline SPROUT_CONSTEXPR sprout::basic_string_ref<T, Traits>
to_string_view(sprout::basic_string<T, N, Traits> const& s) {
return sprout::to_string_ref(s);
}
template<typename T, typename Traits>
inline SPROUT_NON_CONSTEXPR sprout::basic_string_ref<T, Traits>
to_string_view(std::basic_string<T, Traits> const& s) {
return sprout::to_string_ref(s);
}
template<typename T>
inline SPROUT_CONSTEXPR sprout::basic_string_ref<T>
to_string_view(T const* str) {
return sprout::to_string_ref(str);
}
template<typename T>
inline SPROUT_CONSTEXPR sprout::basic_string_ref<T>
to_string_view(T const* str, std::size_t len) {
return sprout::to_string_ref(str, len);
}
//
// is_basic_string_view
//
#if SPROUT_USE_TEMPLATE_ALIASES
template<typename T>
using is_basic_string_view = sprout::is_basic_string_ref<T>;
#else // #if SPROUT_USE_TEMPLATE_ALIASES
template<typename T>
struct is_basic_string_view
: public sprout::is_basic_string_ref<T>
{};
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
//
// is_string_view_of
//
#if SPROUT_USE_TEMPLATE_ALIASES
template<typename T, typename Elem>
using is_string_view_of = sprout::is_string_ref_of<T, Elem>;
#else // #if SPROUT_USE_TEMPLATE_ALIASES
template<typename T, typename Elem>
struct is_string_view_of
: public sprout::is_string_ref_of<T, Elem>
{};
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
//
// is_string_view
//
#if SPROUT_USE_TEMPLATE_ALIASES
template<typename T>
using is_string_view = sprout::is_string_ref<T>;
#else // #if SPROUT_USE_TEMPLATE_ALIASES
template<typename T, typename Elem>
struct is_string_view
: public sprout::is_string_ref<T>
{};
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
//
// is_wstring_view
//
#if SPROUT_USE_TEMPLATE_ALIASES
template<typename T>
using is_wstring_view = sprout::is_wstring_ref<T>;
#else // #if SPROUT_USE_TEMPLATE_ALIASES
template<typename T, typename Elem>
struct is_wstring_view
: public sprout::is_wstring_ref<T>
{};
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
#if SPROUT_USE_UNICODE_LITERALS
//
// is_u16string_view
//
#if SPROUT_USE_TEMPLATE_ALIASES
template<typename T>
using is_u16string_view = sprout::is_u16string_ref<T>;
#else // #if SPROUT_USE_TEMPLATE_ALIASES
template<typename T, typename Elem>
struct is_u16string_view
: public sprout::is_u16string_ref<T>
{};
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
//
// is_u32string_view
//
#if SPROUT_USE_TEMPLATE_ALIASES
template<typename T>
using is_u32string_view = sprout::is_u32string_ref<T>;
#else // #if SPROUT_USE_TEMPLATE_ALIASES
template<typename T, typename Elem>
struct is_u32string_view
: public sprout::is_u32string_ref<T>
{};
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
#endif
#if SPROUT_USE_VARIABLE_TEMPLATES
template<typename T>
SPROUT_STATIC_CONSTEXPR bool is_basic_string_view_v = sprout::is_basic_string_view<T>::value;
template<typename T, typename Elem>
SPROUT_STATIC_CONSTEXPR bool is_string_view_of_v = sprout::is_string_view_of<T, Elem>::value;
template<typename T>
SPROUT_STATIC_CONSTEXPR bool is_string_view_v = sprout::is_string_view<T>::value;
template<typename T>
SPROUT_STATIC_CONSTEXPR bool is_wstring_view_v = sprout::is_wstring_view<T>::value;
#if SPROUT_USE_UNICODE_LITERALS
template<typename T>
SPROUT_STATIC_CONSTEXPR bool is_u16string_view_v = sprout::is_u16string_view<T>::value;
template<typename T>
SPROUT_STATIC_CONSTEXPR bool is_u32string_view_v = sprout::is_u32string_view<T>::value;
#endif
#endif // #if SPROUT_USE_VARIABLE_TEMPLATES
namespace udl {
//
// _sv
//
inline SPROUT_CONSTEXPR sprout::basic_string_ref<char>
operator"" _sv(char const* s, std::size_t size) {
return sprout::basic_string_ref<char>(s, size);
}
inline SPROUT_CONSTEXPR sprout::basic_string_ref<wchar_t>
operator"" _sv(wchar_t const* s, std::size_t size) {
return sprout::basic_string_ref<wchar_t>(s, size);
}
#if SPROUT_USE_UNICODE_LITERALS
inline SPROUT_CONSTEXPR sprout::basic_string_ref<char16_t>
operator"" _sv(char16_t const* s, std::size_t size) {
return sprout::basic_string_ref<char16_t>(s, size);
}
inline SPROUT_CONSTEXPR sprout::basic_string_ref<char32_t>
operator"" _sv(char32_t const* s, std::size_t size) {
return sprout::basic_string_ref<char32_t>(s, size);
}
#endif
} // namespace udl
using sprout::udl::operator"" _sv;
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_STRING_REF_STRING_VIEW_HPP

View file

@ -0,0 +1,48 @@
/*=============================================================================
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_UTILITY_STRING_REF_UDL_HPP
#define SPROUT_UTILITY_STRING_REF_UDL_HPP
#include <sprout/config.hpp>
#include <sprout/utility/string_ref/string_ref.hpp>
#if SPROUT_USE_USER_DEFINED_LITERALS
#include <sprout/workaround/std/cstddef.hpp>
namespace sprout {
namespace udl {
//
// _sr
//
inline SPROUT_CONSTEXPR sprout::basic_string_ref<char>
operator"" _sr(char const* s, std::size_t size) {
return sprout::basic_string_ref<char>(s, size);
}
inline SPROUT_CONSTEXPR sprout::basic_string_ref<wchar_t>
operator"" _sr(wchar_t const* s, std::size_t size) {
return sprout::basic_string_ref<wchar_t>(s, size);
}
#if SPROUT_USE_UNICODE_LITERALS
inline SPROUT_CONSTEXPR sprout::basic_string_ref<char16_t>
operator"" _sr(char16_t const* s, std::size_t size) {
return sprout::basic_string_ref<char16_t>(s, size);
}
inline SPROUT_CONSTEXPR sprout::basic_string_ref<char32_t>
operator"" _sr(char32_t const* s, std::size_t size) {
return sprout::basic_string_ref<char32_t>(s, size);
}
#endif
} // namespace udl
using sprout::udl::operator"" _sr;
} // namespace sprout
#endif // #if SPROUT_USE_USER_DEFINED_LITERALS
#endif // #ifndef SPROUT_UTILITY_STRING_REF_UDL_HPP