Merge branch 'develop'

This commit is contained in:
Andrey Semashev
2016-09-02 18:34:58 +03:00
2 changed files with 136 additions and 44 deletions

View File

@ -157,13 +157,18 @@ namespace boost {
#ifndef BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS #ifndef BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS
template<typename Allocator = std::allocator<charT> > template<typename Allocator = std::allocator<charT> >
std::basic_string<charT, traits> to_string(const Allocator& a = Allocator()) const { std::basic_string<charT, traits, Allocator> to_string(const Allocator& a = Allocator()) const {
return std::basic_string<charT, traits, Allocator>(begin(), end(), a); return std::basic_string<charT, traits, Allocator>(begin(), end(), a);
} }
#else #else
std::basic_string<charT, traits> to_string() const { std::basic_string<charT, traits> to_string() const {
return std::basic_string<charT, traits>(begin(), end()); return std::basic_string<charT, traits>(begin(), end());
} }
template<typename Allocator>
std::basic_string<charT, traits, Allocator> to_string(const Allocator& a) const {
return std::basic_string<charT, traits, Allocator>(begin(), end(), a);
}
#endif #endif
size_type copy(charT* s, size_type n, size_type pos=0) const { size_type copy(charT* s, size_type n, size_type pos=0) const {
@ -215,7 +220,7 @@ namespace boost {
// Searches // Searches
BOOST_CONSTEXPR bool starts_with(charT c) const BOOST_NOEXCEPT { // Boost extension BOOST_CONSTEXPR bool starts_with(charT c) const BOOST_NOEXCEPT { // Boost extension
return !empty() && traits::eq(c, front()); return !empty() && traits::eq(c, front());
} }
BOOST_CONSTEXPR bool starts_with(basic_string_view x) const BOOST_NOEXCEPT { // Boost extension BOOST_CONSTEXPR bool starts_with(basic_string_view x) const BOOST_NOEXCEPT { // Boost extension
return len_ >= x.len_ && traits::compare(ptr_, x.ptr_, x.len_) == 0; return len_ >= x.len_ && traits::compare(ptr_, x.ptr_, x.len_) == 0;
@ -226,7 +231,7 @@ namespace boost {
} }
BOOST_CONSTEXPR bool ends_with(basic_string_view x) const BOOST_NOEXCEPT { // Boost extension BOOST_CONSTEXPR bool ends_with(basic_string_view x) const BOOST_NOEXCEPT { // Boost extension
return len_ >= x.len_ && return len_ >= x.len_ &&
traits::compare(ptr_ + len_ - x.len_, x.ptr_, x.len_) == 0; traits::compare(ptr_ + len_ - x.len_, x.ptr_, x.len_) == 0;
} }
@ -251,7 +256,7 @@ namespace boost {
BOOST_CXX14_CONSTEXPR size_type rfind(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT { BOOST_CXX14_CONSTEXPR size_type rfind(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT {
if (len_ < s.len_) if (len_ < s.len_)
return npos; return npos;
if (pos > len_ - s.len_) if (pos > len_ - s.len_)
pos = len_ - s.len_; pos = len_ - s.len_;
if (s.len_ == 0u) // an empty string is always found if (s.len_ == 0u) // an empty string is always found
return pos; return pos;
@ -368,7 +373,7 @@ namespace boost {
// Inequality // Inequality
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator!=(basic_string_view<charT, traits> x, inline bool operator!=(basic_string_view<charT, traits> x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
if ( x.size () != y.size ()) return true; if ( x.size () != y.size ()) return true;
return x.compare(y) != 0; return x.compare(y) != 0;
} }
@ -376,173 +381,173 @@ namespace boost {
// Less than // Less than
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator<(basic_string_view<charT, traits> x, inline bool operator<(basic_string_view<charT, traits> x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return x.compare(y) < 0; return x.compare(y) < 0;
} }
// Greater than // Greater than
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator>(basic_string_view<charT, traits> x, inline bool operator>(basic_string_view<charT, traits> x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return x.compare(y) > 0; return x.compare(y) > 0;
} }
// Less than or equal to // Less than or equal to
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator<=(basic_string_view<charT, traits> x, inline bool operator<=(basic_string_view<charT, traits> x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return x.compare(y) <= 0; return x.compare(y) <= 0;
} }
// Greater than or equal to // Greater than or equal to
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator>=(basic_string_view<charT, traits> x, inline bool operator>=(basic_string_view<charT, traits> x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return x.compare(y) >= 0; return x.compare(y) >= 0;
} }
// "sufficient additional overloads of comparison functions" // "sufficient additional overloads of comparison functions"
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator==(basic_string_view<charT, traits> x, inline bool operator==(basic_string_view<charT, traits> x,
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
return x == basic_string_view<charT, traits>(y); return x == basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator==(const std::basic_string<charT, traits, Allocator> & x, inline bool operator==(const std::basic_string<charT, traits, Allocator> & x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) == y; return basic_string_view<charT, traits>(x) == y;
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator==(basic_string_view<charT, traits> x, inline bool operator==(basic_string_view<charT, traits> x,
const charT * y) BOOST_NOEXCEPT { const charT * y) BOOST_NOEXCEPT {
return x == basic_string_view<charT, traits>(y); return x == basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator==(const charT * x, inline bool operator==(const charT * x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) == y; return basic_string_view<charT, traits>(x) == y;
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator!=(basic_string_view<charT, traits> x, inline bool operator!=(basic_string_view<charT, traits> x,
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
return x != basic_string_view<charT, traits>(y); return x != basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator!=(const std::basic_string<charT, traits, Allocator> & x, inline bool operator!=(const std::basic_string<charT, traits, Allocator> & x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) != y; return basic_string_view<charT, traits>(x) != y;
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator!=(basic_string_view<charT, traits> x, inline bool operator!=(basic_string_view<charT, traits> x,
const charT * y) BOOST_NOEXCEPT { const charT * y) BOOST_NOEXCEPT {
return x != basic_string_view<charT, traits>(y); return x != basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator!=(const charT * x, inline bool operator!=(const charT * x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) != y; return basic_string_view<charT, traits>(x) != y;
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator<(basic_string_view<charT, traits> x, inline bool operator<(basic_string_view<charT, traits> x,
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
return x < basic_string_view<charT, traits>(y); return x < basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator<(const std::basic_string<charT, traits, Allocator> & x, inline bool operator<(const std::basic_string<charT, traits, Allocator> & x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) < y; return basic_string_view<charT, traits>(x) < y;
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator<(basic_string_view<charT, traits> x, inline bool operator<(basic_string_view<charT, traits> x,
const charT * y) BOOST_NOEXCEPT { const charT * y) BOOST_NOEXCEPT {
return x < basic_string_view<charT, traits>(y); return x < basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator<(const charT * x, inline bool operator<(const charT * x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) < y; return basic_string_view<charT, traits>(x) < y;
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator>(basic_string_view<charT, traits> x, inline bool operator>(basic_string_view<charT, traits> x,
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
return x > basic_string_view<charT, traits>(y); return x > basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator>(const std::basic_string<charT, traits, Allocator> & x, inline bool operator>(const std::basic_string<charT, traits, Allocator> & x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) > y; return basic_string_view<charT, traits>(x) > y;
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator>(basic_string_view<charT, traits> x, inline bool operator>(basic_string_view<charT, traits> x,
const charT * y) BOOST_NOEXCEPT { const charT * y) BOOST_NOEXCEPT {
return x > basic_string_view<charT, traits>(y); return x > basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator>(const charT * x, inline bool operator>(const charT * x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) > y; return basic_string_view<charT, traits>(x) > y;
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator<=(basic_string_view<charT, traits> x, inline bool operator<=(basic_string_view<charT, traits> x,
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
return x <= basic_string_view<charT, traits>(y); return x <= basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator<=(const std::basic_string<charT, traits, Allocator> & x, inline bool operator<=(const std::basic_string<charT, traits, Allocator> & x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) <= y; return basic_string_view<charT, traits>(x) <= y;
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator<=(basic_string_view<charT, traits> x, inline bool operator<=(basic_string_view<charT, traits> x,
const charT * y) BOOST_NOEXCEPT { const charT * y) BOOST_NOEXCEPT {
return x <= basic_string_view<charT, traits>(y); return x <= basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator<=(const charT * x, inline bool operator<=(const charT * x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) <= y; return basic_string_view<charT, traits>(x) <= y;
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator>=(basic_string_view<charT, traits> x, inline bool operator>=(basic_string_view<charT, traits> x,
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
return x >= basic_string_view<charT, traits>(y); return x >= basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator>=(const std::basic_string<charT, traits, Allocator> & x, inline bool operator>=(const std::basic_string<charT, traits, Allocator> & x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) >= y; return basic_string_view<charT, traits>(x) >= y;
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator>=(basic_string_view<charT, traits> x, inline bool operator>=(basic_string_view<charT, traits> x,
const charT * y) BOOST_NOEXCEPT { const charT * y) BOOST_NOEXCEPT {
return x >= basic_string_view<charT, traits>(y); return x >= basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator>=(const charT * x, inline bool operator>=(const charT * x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) >= y; return basic_string_view<charT, traits>(x) >= y;
} }

View File

@ -7,10 +7,14 @@
For more information, see http://www.boost.org For more information, see http://www.boost.org
*/ */
#include <new> // for placement new
#include <iostream> #include <iostream>
#include <cstring> // for std::strchr #include <cstddef> // for NULL, std::size_t, std::ptrdiff_t
#include <cstring> // for std::strchr and std::strcmp
#include <cstdlib> // for std::malloc and std::free
#include <boost/utility/string_view.hpp> #include <boost/utility/string_view.hpp>
#include <boost/config.hpp>
#define BOOST_TEST_MAIN #define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
@ -83,7 +87,7 @@ void reverse ( const char *arg ) {
BOOST_CHECK ( std::equal ( sr1.begin (), sr1.end (), string2.begin ())); BOOST_CHECK ( std::equal ( sr1.begin (), sr1.end (), string2.begin ()));
} }
// This helper function eliminates signed vs. unsigned warnings // This helper function eliminates signed vs. unsigned warnings
string_view::size_type ptr_diff ( const char *res, const char *base ) { string_view::size_type ptr_diff ( const char *res, const char *base ) {
BOOST_CHECK ( res >= base ); BOOST_CHECK ( res >= base );
return static_cast<string_view::size_type> ( res - base ); return static_cast<string_view::size_type> ( res - base );
@ -112,7 +116,7 @@ void find ( const char *arg ) {
++p; ++p;
} }
// Look for pairs on characters (searching from the start) // Look for pairs on characters (searching from the start)
sr1 = arg; sr1 = arg;
p = arg; p = arg;
while ( *p && *(p+1)) { while ( *p && *(p+1)) {
@ -241,6 +245,86 @@ void find ( const char *arg ) {
} }
template <typename T>
class custom_allocator {
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef void* void_pointer;
typedef const void* const_void_pointer;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T& reference;
typedef const T& const_reference;
template<class U>
struct rebind {
typedef custom_allocator<U> other;
};
custom_allocator() BOOST_NOEXCEPT {}
template <typename U>
custom_allocator(custom_allocator<U> const&) BOOST_NOEXCEPT {}
pointer allocate(size_type n) const {
return static_cast<pointer>(std::malloc(sizeof(value_type) * n));
}
void deallocate(pointer p, size_type) const BOOST_NOEXCEPT {
std::free(p);
}
pointer address(reference value) const BOOST_NOEXCEPT {
return &value;
}
const_pointer address(const_reference value) const BOOST_NOEXCEPT {
return &value;
}
BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT {
return (~(size_type)0u) / sizeof(value_type);
}
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template <class U, class... Args>
void construct(U* ptr, Args&&... args) const {
::new((void*)ptr) U(static_cast<Args&&>(args)...);
}
#else
template <class U, class V>
void construct(U* ptr, V&& value) const {
::new((void*)ptr) U(static_cast<V&&>(value));
}
#endif
#else
template <class U, class V>
void construct(U* ptr, const V& value) const {
::new((void*)ptr) U(value);
}
#endif
template <class U>
void construct(U* ptr) const {
::new((void*)ptr) U();
}
template <class U>
void destroy(U* ptr) const {
(void)ptr;
ptr->~U();
}
};
template <typename T, typename U>
BOOST_CONSTEXPR bool operator==(const custom_allocator<T> &, const custom_allocator<U> &) BOOST_NOEXCEPT {
return true;
}
template <typename T, typename U>
BOOST_CONSTEXPR bool operator!=(const custom_allocator<T> &, const custom_allocator<U> &) BOOST_NOEXCEPT {
return false;
}
void to_string ( const char *arg ) { void to_string ( const char *arg ) {
string_view sr1; string_view sr1;
@ -249,13 +333,16 @@ void to_string ( const char *arg ) {
str1.assign ( arg ); str1.assign ( arg );
sr1 = arg; sr1 = arg;
// str2 = sr1.to_string<std::allocator<char> > (); // str2 = sr1.to_string<std::allocator<char> > ();
str2 = sr1.to_string (); str2 = sr1.to_string ();
BOOST_CHECK ( str1 == str2 ); BOOST_CHECK ( str1 == str2 );
std::basic_string<char, std::char_traits<char>, custom_allocator<char> > str3 = sr1.to_string(custom_allocator<char>());
BOOST_CHECK ( std::strcmp(str1.c_str(), str3.c_str()) == 0 );
#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
std::string str3 = static_cast<std::string> ( sr1 ); std::string str4 = static_cast<std::string> ( sr1 );
BOOST_CHECK ( str1 == str3 ); BOOST_CHECK ( str1 == str4 );
#endif #endif
} }
@ -266,11 +353,11 @@ void compare ( const char *arg ) {
str1.assign ( arg ); str1.assign ( arg );
sr1 = arg; sr1 = arg;
BOOST_CHECK ( sr1 == sr1); // compare string_view and string_view BOOST_CHECK ( sr1 == sr1); // compare string_view and string_view
BOOST_CHECK ( sr1 == str1); // compare string and string_view BOOST_CHECK ( sr1 == str1); // compare string and string_view
BOOST_CHECK ( str1 == sr1 ); // compare string_view and string BOOST_CHECK ( str1 == sr1 ); // compare string_view and string
BOOST_CHECK ( sr1 == arg ); // compare string_view and pointer BOOST_CHECK ( sr1 == arg ); // compare string_view and pointer
BOOST_CHECK ( arg == sr1 ); // compare pointer and string_view BOOST_CHECK ( arg == sr1 ); // compare pointer and string_view
if ( sr1.size () > 0 ) { if ( sr1.size () > 0 ) {
(*str1.rbegin())++; (*str1.rbegin())++;