mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 02:44:10 +00:00
Introduce new 'string_view' to match the one in the standard. Step #2 (up next) is to retire string_ref in favor of string_view
This commit is contained in:
parent
cf5ad341ed
commit
1caa745dd7
670
include/boost/utility/string_view.hpp
Normal file
670
include/boost/utility/string_view.hpp
Normal file
@ -0,0 +1,670 @@
|
||||
/*
|
||||
© Copyright (c) Marshall Clow 2012-2015.
|
||||
© Copyright Beman Dawes 2015
|
||||
|
||||
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)
|
||||
|
||||
For more information, see http://www.boost.org
|
||||
|
||||
Based on the StringRef implementation in LLVM (http://llvm.org) and
|
||||
N3422 by Jeffrey Yasskin
|
||||
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3442.html
|
||||
Updated July 2015 to reflect the Library Fundamentals TS
|
||||
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4480.html
|
||||
*/
|
||||
|
||||
#ifndef BOOST_STRING_VIEW_HPP
|
||||
#define BOOST_STRING_VIEW_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
#include <boost/utility/string_view_fwd.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <iosfwd>
|
||||
|
||||
namespace boost {
|
||||
|
||||
namespace detail {
|
||||
// A helper functor because sometimes we don't have lambdas
|
||||
template <typename charT, typename traits>
|
||||
class string_view_traits_eq {
|
||||
public:
|
||||
string_view_traits_eq ( charT ch ) : ch_(ch) {}
|
||||
bool operator()( charT val ) const { return traits::eq (ch_, val); }
|
||||
charT ch_;
|
||||
};
|
||||
}
|
||||
|
||||
template<typename charT, typename traits> // traits defaulted in string_view_fwd.hpp
|
||||
class basic_string_view {
|
||||
public:
|
||||
// types
|
||||
typedef traits traits_type;
|
||||
typedef charT value_type;
|
||||
typedef charT* pointer;
|
||||
typedef const charT* const_pointer;
|
||||
typedef charT& reference;
|
||||
typedef const charT& const_reference;
|
||||
typedef const_pointer const_iterator; // impl-defined
|
||||
typedef const_iterator iterator;
|
||||
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
typedef const_reverse_iterator reverse_iterator;
|
||||
typedef std::size_t size_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
static BOOST_CONSTEXPR_OR_CONST size_type npos = size_type(-1);
|
||||
|
||||
// construct/copy
|
||||
BOOST_CONSTEXPR basic_string_view() BOOST_NOEXCEPT
|
||||
: ptr_(NULL), len_(0) {}
|
||||
|
||||
BOOST_CONSTEXPR basic_string_view(const basic_string_view &rhs) BOOST_NOEXCEPT
|
||||
: ptr_(rhs.ptr_), len_(rhs.len_) {}
|
||||
|
||||
basic_string_view& operator=(const basic_string_view &rhs) BOOST_NOEXCEPT {
|
||||
ptr_ = rhs.ptr_;
|
||||
len_ = rhs.len_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename Allocator>
|
||||
basic_string_view(const std::basic_string<charT, traits,
|
||||
Allocator>& str) BOOST_NOEXCEPT
|
||||
: ptr_(str.data()), len_(str.length()) {}
|
||||
|
||||
BOOST_CONSTEXPR basic_string_view(const charT* str)
|
||||
: ptr_(str), len_(traits::length(str)) {}
|
||||
|
||||
BOOST_CONSTEXPR basic_string_view(const charT* str, size_type len)
|
||||
: ptr_(str), len_(len) {}
|
||||
|
||||
// iterators
|
||||
BOOST_CONSTEXPR const_iterator begin() const BOOST_NOEXCEPT { return ptr_; }
|
||||
BOOST_CONSTEXPR const_iterator cbegin() const BOOST_NOEXCEPT { return ptr_; }
|
||||
BOOST_CONSTEXPR const_iterator end() const BOOST_NOEXCEPT { return ptr_ + len_; }
|
||||
BOOST_CONSTEXPR const_iterator cend() const BOOST_NOEXCEPT { return ptr_ + len_; }
|
||||
const_reverse_iterator rbegin() const BOOST_NOEXCEPT { return const_reverse_iterator(end()); }
|
||||
const_reverse_iterator crbegin() const BOOST_NOEXCEPT { return const_reverse_iterator(end()); }
|
||||
const_reverse_iterator rend() const BOOST_NOEXCEPT { return const_reverse_iterator(begin()); }
|
||||
const_reverse_iterator crend() const BOOST_NOEXCEPT { return const_reverse_iterator(begin()); }
|
||||
|
||||
// capacity
|
||||
BOOST_CONSTEXPR size_type size() const BOOST_NOEXCEPT { return len_; }
|
||||
BOOST_CONSTEXPR size_type length() const BOOST_NOEXCEPT { return len_; }
|
||||
BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT { return len_; }
|
||||
BOOST_CONSTEXPR bool empty() const BOOST_NOEXCEPT { return len_ == 0; }
|
||||
|
||||
// element access
|
||||
BOOST_CONSTEXPR const_reference operator[](size_type pos) const BOOST_NOEXCEPT { return ptr_[pos]; }
|
||||
|
||||
BOOST_CONSTEXPR const_reference at(size_t pos) const {
|
||||
return pos >= len_ ? BOOST_THROW_EXCEPTION(std::out_of_range("boost::string_view::at")) : ptr_[pos];
|
||||
// if ( pos >= len_ )
|
||||
// BOOST_THROW_EXCEPTION( std::out_of_range ( "boost::string_view::at" ) );
|
||||
// return ptr_[pos];
|
||||
}
|
||||
|
||||
BOOST_CONSTEXPR const_reference front() const { return ptr_[0]; }
|
||||
BOOST_CONSTEXPR const_reference back() const { return ptr_[len_-1]; }
|
||||
BOOST_CONSTEXPR const_pointer data() const BOOST_NOEXCEPT { return ptr_; }
|
||||
|
||||
// modifiers
|
||||
void clear() BOOST_NOEXCEPT { len_ = 0; } // Boost extension
|
||||
|
||||
BOOST_CXX14_CONSTEXPR void remove_prefix(size_type n) {
|
||||
if ( n > len_ )
|
||||
n = len_;
|
||||
ptr_ += n;
|
||||
len_ -= n;
|
||||
}
|
||||
|
||||
BOOST_CXX14_CONSTEXPR void remove_suffix(size_type n) {
|
||||
if ( n > len_ )
|
||||
n = len_;
|
||||
len_ -= n;
|
||||
}
|
||||
|
||||
BOOST_CXX14_CONSTEXPR void swap(basic_string_view& s) BOOST_NOEXCEPT {
|
||||
std::swap(ptr_, s.ptr_);
|
||||
std::swap(len_, s.len_);
|
||||
}
|
||||
|
||||
// basic_string_view string operations
|
||||
#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
|
||||
template<typename Allocator>
|
||||
explicit operator std::basic_string<charT, traits, Allocator>() const {
|
||||
return std::basic_string<charT, traits, Allocator>(begin(), end());
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS
|
||||
template<typename Allocator = std::allocator<charT> >
|
||||
std::basic_string<charT, traits> to_string(const Allocator& a = Allocator()) const {
|
||||
return std::basic_string<charT, traits, Allocator>(begin(), end(), a);
|
||||
}
|
||||
#else
|
||||
std::basic_string<charT, traits> to_string() const {
|
||||
return std::basic_string<charT, traits>(begin(), end());
|
||||
}
|
||||
#endif
|
||||
|
||||
size_type copy(charT* s, size_type n, size_type pos=0) const {
|
||||
if (pos > size())
|
||||
BOOST_THROW_EXCEPTION(std::out_of_range("string_view::copy" ));
|
||||
size_type rlen = (std::min)(n, len_ - pos);
|
||||
// use std::copy(begin() + pos, begin() + pos + rlen, s) rather than
|
||||
// std::copy_n(begin() + pos, rlen, s) to support pre-C++11 standard libraries
|
||||
std::copy(begin() + pos, begin() + pos + rlen, s);
|
||||
return rlen;
|
||||
}
|
||||
|
||||
BOOST_CXX14_CONSTEXPR basic_string_view substr(size_type pos, size_type n=npos) const {
|
||||
if ( pos > size())
|
||||
BOOST_THROW_EXCEPTION( std::out_of_range ( "string_view::substr" ) );
|
||||
if (n == npos || pos + n > size())
|
||||
n = size () - pos;
|
||||
return basic_string_view(data() + pos, n);
|
||||
}
|
||||
|
||||
BOOST_CXX14_CONSTEXPR int compare(basic_string_view x) const BOOST_NOEXCEPT {
|
||||
const int cmp = traits::compare(ptr_, x.ptr_, (std::min)(len_, x.len_));
|
||||
return cmp != 0 ? cmp : (len_ == x.len_ ? 0 : len_ < x.len_ ? -1 : 1);
|
||||
}
|
||||
|
||||
BOOST_CXX14_CONSTEXPR int compare(size_type pos1, size_type n1, basic_string_view x)
|
||||
const BOOST_NOEXCEPT {
|
||||
return substr(pos1, n1).compare(x);
|
||||
}
|
||||
|
||||
BOOST_CXX14_CONSTEXPR int compare(size_type pos1, size_type n1,
|
||||
basic_string_view x, size_type pos2, size_type n2) const {
|
||||
return substr(pos1, n1).compare(x.substr(pos2, n2));
|
||||
}
|
||||
|
||||
BOOST_CXX14_CONSTEXPR int compare(const charT* x) const {
|
||||
return compare(basic_string_view(x));
|
||||
}
|
||||
|
||||
BOOST_CXX14_CONSTEXPR int compare(size_type pos1, size_type n1, const charT* x) const {
|
||||
return substr(pos1, n1).compare(basic_string_view(x));
|
||||
}
|
||||
|
||||
BOOST_CXX14_CONSTEXPR int compare(size_type pos1, size_type n1,
|
||||
const charT* x, size_type n2) const {
|
||||
return substr(pos1, n1).compare(basic_string_view(x, n2));
|
||||
}
|
||||
|
||||
// Searches
|
||||
BOOST_CONSTEXPR bool starts_with(charT c) const BOOST_NOEXCEPT { // Boost extension
|
||||
return !empty() && traits::eq(c, front());
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
BOOST_CONSTEXPR bool ends_with(charT c) const BOOST_NOEXCEPT { // Boost extension
|
||||
return !empty() && traits::eq(c, back());
|
||||
}
|
||||
|
||||
BOOST_CONSTEXPR bool ends_with(basic_string_view x) const BOOST_NOEXCEPT { // Boost extension
|
||||
return len_ >= x.len_ &&
|
||||
traits::compare(ptr_ + len_ - x.len_, x.ptr_, x.len_) == 0;
|
||||
}
|
||||
|
||||
// find
|
||||
BOOST_CXX14_CONSTEXPR size_type find(basic_string_view s, size_type pos = 0) const BOOST_NOEXCEPT {
|
||||
if (pos > size())
|
||||
return npos;
|
||||
if (s.empty())
|
||||
return pos;
|
||||
const_iterator iter = std::search(this->cbegin() + pos, this->cend(),
|
||||
s.cbegin (), s.cend (), traits::eq);
|
||||
return iter == this->cend () ? npos : std::distance(this->cbegin (), iter);
|
||||
}
|
||||
BOOST_CXX14_CONSTEXPR size_type find(charT c, size_type pos = 0) const BOOST_NOEXCEPT
|
||||
{ return find(basic_string_view(&c, 1), pos); }
|
||||
BOOST_CXX14_CONSTEXPR size_type find(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT
|
||||
{ return find(basic_string_view(s, n), pos); }
|
||||
BOOST_CXX14_CONSTEXPR size_type find(const charT* s, size_type pos = 0) const BOOST_NOEXCEPT
|
||||
{ return find(basic_string_view(s), pos); }
|
||||
|
||||
// rfind
|
||||
BOOST_CXX14_CONSTEXPR size_type rfind(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT {
|
||||
if (len_ < s.len_)
|
||||
return npos;
|
||||
if (pos > len_ - s.len_)
|
||||
pos = len_ - s.len_;
|
||||
if (s.len_ == 0u) // an empty string is always found
|
||||
return pos;
|
||||
for (const charT* cur = ptr_ + pos;; --cur) {
|
||||
if (traits::compare(cur, s.ptr_, s.len_) == 0)
|
||||
return cur - ptr_;
|
||||
if (cur == ptr_)
|
||||
return npos;
|
||||
};
|
||||
}
|
||||
BOOST_CXX14_CONSTEXPR size_type rfind(charT c, size_type pos = npos) const BOOST_NOEXCEPT
|
||||
{ return rfind(basic_string_view(&c, 1), pos); }
|
||||
BOOST_CXX14_CONSTEXPR size_type rfind(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT
|
||||
{ return rfind(basic_string_view(s, n), pos); }
|
||||
BOOST_CXX14_CONSTEXPR size_type rfind(const charT* s, size_type pos = npos) const BOOST_NOEXCEPT
|
||||
{ return rfind(basic_string_view(s), pos); }
|
||||
|
||||
// find_first_of
|
||||
BOOST_CXX14_CONSTEXPR size_type find_first_of(basic_string_view s, size_type pos = 0) const BOOST_NOEXCEPT {
|
||||
if (pos >= len_ || s.len_ == 0)
|
||||
return npos;
|
||||
const_iterator iter = std::find_first_of
|
||||
(this->cbegin () + pos, this->cend (), s.cbegin (), s.cend (), traits::eq);
|
||||
return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
|
||||
}
|
||||
BOOST_CXX14_CONSTEXPR size_type find_first_of(charT c, size_type pos = 0) const BOOST_NOEXCEPT
|
||||
{ return find_first_of(basic_string_view(&c, 1), pos); }
|
||||
BOOST_CXX14_CONSTEXPR size_type find_first_of(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT
|
||||
{ return find_first_of(basic_string_view(s, n), pos); }
|
||||
BOOST_CXX14_CONSTEXPR size_type find_first_of(const charT* s, size_type pos = 0) const BOOST_NOEXCEPT
|
||||
{ return find_first_of(basic_string_view(s), pos); }
|
||||
|
||||
// find_last_of
|
||||
BOOST_CXX14_CONSTEXPR size_type find_last_of(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT {
|
||||
if (s.len_ == 0u)
|
||||
return npos;
|
||||
if (pos >= len_)
|
||||
pos = 0;
|
||||
else
|
||||
pos = len_ - (pos+1);
|
||||
const_reverse_iterator iter = std::find_first_of
|
||||
( this->crbegin () + pos, this->crend (), s.cbegin (), s.cend (), traits::eq );
|
||||
return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter);
|
||||
}
|
||||
BOOST_CXX14_CONSTEXPR size_type find_last_of(charT c, size_type pos = npos) const BOOST_NOEXCEPT
|
||||
{ return find_last_of(basic_string_view(&c, 1), pos); }
|
||||
BOOST_CXX14_CONSTEXPR size_type find_last_of(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT
|
||||
{ return find_last_of(basic_string_view(s, n), pos); }
|
||||
BOOST_CXX14_CONSTEXPR size_type find_last_of(const charT* s, size_type pos = npos) const BOOST_NOEXCEPT
|
||||
{ return find_last_of(basic_string_view(s), pos); }
|
||||
|
||||
// find_first_not_of
|
||||
BOOST_CXX14_CONSTEXPR size_type find_first_not_of(basic_string_view s, size_type pos = 0) const BOOST_NOEXCEPT {
|
||||
if (pos >= len_)
|
||||
return npos;
|
||||
if (s.len_ == 0)
|
||||
return pos;
|
||||
const_iterator iter = find_not_of ( this->cbegin () + pos, this->cend (), s );
|
||||
return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter );
|
||||
}
|
||||
BOOST_CXX14_CONSTEXPR size_type find_first_not_of(charT c, size_type pos = 0) const BOOST_NOEXCEPT
|
||||
{ return find_first_not_of(basic_string_view(&c, 1), pos); }
|
||||
BOOST_CXX14_CONSTEXPR size_type find_first_not_of(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT
|
||||
{ return find_first_not_of(basic_string_view(s, n), pos); }
|
||||
BOOST_CXX14_CONSTEXPR size_type find_first_not_of(const charT* s, size_type pos = 0) const BOOST_NOEXCEPT
|
||||
{ return find_first_not_of(basic_string_view(s), pos); }
|
||||
|
||||
// find_last_not_of
|
||||
BOOST_CXX14_CONSTEXPR size_type find_last_not_of(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT {
|
||||
if (pos >= len_)
|
||||
pos = len_ - 1;;
|
||||
if (s.len_ == 0u)
|
||||
return pos;
|
||||
pos = len_ - (pos+1);
|
||||
const_reverse_iterator iter = find_not_of ( this->crbegin () + pos, this->crend (), s );
|
||||
return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter );
|
||||
}
|
||||
BOOST_CXX14_CONSTEXPR size_type find_last_not_of(charT c, size_type pos = npos) const BOOST_NOEXCEPT
|
||||
{ return find_last_not_of(basic_string_view(&c, 1), pos); }
|
||||
BOOST_CXX14_CONSTEXPR size_type find_last_not_of(const charT* s, size_type pos, size_type n) const BOOST_NOEXCEPT
|
||||
{ return find_last_not_of(basic_string_view(s, n), pos); }
|
||||
BOOST_CXX14_CONSTEXPR size_type find_last_not_of(const charT* s, size_type pos = npos) const BOOST_NOEXCEPT
|
||||
{ return find_last_not_of(basic_string_view(s), pos); }
|
||||
|
||||
private:
|
||||
template <typename r_iter>
|
||||
size_type reverse_distance(r_iter first, r_iter last) const BOOST_NOEXCEPT {
|
||||
// Portability note here: std::distance is not NOEXCEPT, but calling it with a string_view::reverse_iterator will not throw.
|
||||
return len_ - 1 - std::distance ( first, last );
|
||||
}
|
||||
|
||||
template <typename Iterator>
|
||||
Iterator find_not_of(Iterator first, Iterator last, basic_string_view s) const BOOST_NOEXCEPT {
|
||||
for (; first != last ; ++first)
|
||||
if ( 0 == traits::find(s.ptr_, s.len_, *first))
|
||||
return first;
|
||||
return last;
|
||||
}
|
||||
|
||||
const charT *ptr_;
|
||||
std::size_t len_;
|
||||
};
|
||||
|
||||
|
||||
// Comparison operators
|
||||
// Equality
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator==(basic_string_view<charT, traits> x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
if (x.size () != y.size ()) return false;
|
||||
return x.compare(y) == 0;
|
||||
}
|
||||
|
||||
// Inequality
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator!=(basic_string_view<charT, traits> x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
if ( x.size () != y.size ()) return true;
|
||||
return x.compare(y) != 0;
|
||||
}
|
||||
|
||||
// Less than
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator<(basic_string_view<charT, traits> x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return x.compare(y) < 0;
|
||||
}
|
||||
|
||||
// Greater than
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator>(basic_string_view<charT, traits> x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return x.compare(y) > 0;
|
||||
}
|
||||
|
||||
// Less than or equal to
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator<=(basic_string_view<charT, traits> x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return x.compare(y) <= 0;
|
||||
}
|
||||
|
||||
// Greater than or equal to
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator>=(basic_string_view<charT, traits> x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return x.compare(y) >= 0;
|
||||
}
|
||||
|
||||
// "sufficient additional overloads of comparison functions"
|
||||
template<typename charT, typename traits, typename Allocator>
|
||||
inline bool operator==(basic_string_view<charT, traits> x,
|
||||
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
|
||||
return x == basic_string_view<charT, traits>(y);
|
||||
}
|
||||
|
||||
template<typename charT, typename traits, typename Allocator>
|
||||
inline bool operator==(const std::basic_string<charT, traits, Allocator> & x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return basic_string_view<charT, traits>(x) == y;
|
||||
}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator==(basic_string_view<charT, traits> x,
|
||||
const charT * y) BOOST_NOEXCEPT {
|
||||
return x == basic_string_view<charT, traits>(y);
|
||||
}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator==(const charT * x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return basic_string_view<charT, traits>(x) == y;
|
||||
}
|
||||
|
||||
template<typename charT, typename traits, typename Allocator>
|
||||
inline bool operator!=(basic_string_view<charT, traits> x,
|
||||
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
|
||||
return x != basic_string_view<charT, traits>(y);
|
||||
}
|
||||
|
||||
template<typename charT, typename traits, typename Allocator>
|
||||
inline bool operator!=(const std::basic_string<charT, traits, Allocator> & x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return basic_string_view<charT, traits>(x) != y;
|
||||
}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator!=(basic_string_view<charT, traits> x,
|
||||
const charT * y) BOOST_NOEXCEPT {
|
||||
return x != basic_string_view<charT, traits>(y);
|
||||
}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator!=(const charT * x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return basic_string_view<charT, traits>(x) != y;
|
||||
}
|
||||
|
||||
template<typename charT, typename traits, typename Allocator>
|
||||
inline bool operator<(basic_string_view<charT, traits> x,
|
||||
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
|
||||
return x < basic_string_view<charT, traits>(y);
|
||||
}
|
||||
|
||||
template<typename charT, typename traits, typename Allocator>
|
||||
inline bool operator<(const std::basic_string<charT, traits, Allocator> & x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return basic_string_view<charT, traits>(x) < y;
|
||||
}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator<(basic_string_view<charT, traits> x,
|
||||
const charT * y) BOOST_NOEXCEPT {
|
||||
return x < basic_string_view<charT, traits>(y);
|
||||
}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator<(const charT * x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return basic_string_view<charT, traits>(x) < y;
|
||||
}
|
||||
|
||||
template<typename charT, typename traits, typename Allocator>
|
||||
inline bool operator>(basic_string_view<charT, traits> x,
|
||||
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
|
||||
return x > basic_string_view<charT, traits>(y);
|
||||
}
|
||||
|
||||
template<typename charT, typename traits, typename Allocator>
|
||||
inline bool operator>(const std::basic_string<charT, traits, Allocator> & x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return basic_string_view<charT, traits>(x) > y;
|
||||
}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator>(basic_string_view<charT, traits> x,
|
||||
const charT * y) BOOST_NOEXCEPT {
|
||||
return x > basic_string_view<charT, traits>(y);
|
||||
}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator>(const charT * x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return basic_string_view<charT, traits>(x) > y;
|
||||
}
|
||||
|
||||
template<typename charT, typename traits, typename Allocator>
|
||||
inline bool operator<=(basic_string_view<charT, traits> x,
|
||||
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
|
||||
return x <= basic_string_view<charT, traits>(y);
|
||||
}
|
||||
|
||||
template<typename charT, typename traits, typename Allocator>
|
||||
inline bool operator<=(const std::basic_string<charT, traits, Allocator> & x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return basic_string_view<charT, traits>(x) <= y;
|
||||
}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator<=(basic_string_view<charT, traits> x,
|
||||
const charT * y) BOOST_NOEXCEPT {
|
||||
return x <= basic_string_view<charT, traits>(y);
|
||||
}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator<=(const charT * x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return basic_string_view<charT, traits>(x) <= y;
|
||||
}
|
||||
|
||||
template<typename charT, typename traits, typename Allocator>
|
||||
inline bool operator>=(basic_string_view<charT, traits> x,
|
||||
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
|
||||
return x >= basic_string_view<charT, traits>(y);
|
||||
}
|
||||
|
||||
template<typename charT, typename traits, typename Allocator>
|
||||
inline bool operator>=(const std::basic_string<charT, traits, Allocator> & x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return basic_string_view<charT, traits>(x) >= y;
|
||||
}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator>=(basic_string_view<charT, traits> x,
|
||||
const charT * y) BOOST_NOEXCEPT {
|
||||
return x >= basic_string_view<charT, traits>(y);
|
||||
}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
inline bool operator>=(const charT * x,
|
||||
basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
|
||||
return basic_string_view<charT, traits>(x) >= y;
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<class charT, class traits>
|
||||
inline void insert_fill_chars(std::basic_ostream<charT, traits>& os, std::size_t n) {
|
||||
enum { chunk_size = 8 };
|
||||
charT fill_chars[chunk_size];
|
||||
std::fill_n(fill_chars, static_cast< std::size_t >(chunk_size), os.fill());
|
||||
for (; n >= chunk_size && os.good(); n -= chunk_size)
|
||||
os.write(fill_chars, static_cast< std::size_t >(chunk_size));
|
||||
if (n > 0 && os.good())
|
||||
os.write(fill_chars, n);
|
||||
}
|
||||
|
||||
template<class charT, class traits>
|
||||
void insert_aligned(std::basic_ostream<charT, traits>& os, const basic_string_view<charT,traits>& str) {
|
||||
const std::size_t size = str.size();
|
||||
const std::size_t alignment_size = static_cast< std::size_t >(os.width()) - size;
|
||||
const bool align_left = (os.flags() & std::basic_ostream<charT, traits>::adjustfield) == std::basic_ostream<charT, traits>::left;
|
||||
if (!align_left) {
|
||||
detail::insert_fill_chars(os, alignment_size);
|
||||
if (os.good())
|
||||
os.write(str.data(), size);
|
||||
}
|
||||
else {
|
||||
os.write(str.data(), size);
|
||||
if (os.good())
|
||||
detail::insert_fill_chars(os, alignment_size);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// Inserter
|
||||
template<class charT, class traits>
|
||||
inline std::basic_ostream<charT, traits>&
|
||||
operator<<(std::basic_ostream<charT, traits>& os,
|
||||
const basic_string_view<charT,traits>& str) {
|
||||
if (os.good()) {
|
||||
const std::size_t size = str.size();
|
||||
const std::size_t w = static_cast< std::size_t >(os.width());
|
||||
if (w <= size)
|
||||
os.write(str.data(), size);
|
||||
else
|
||||
detail::insert_aligned(os, str);
|
||||
os.width(0);
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
#if 0
|
||||
// numeric conversions
|
||||
//
|
||||
// These are short-term implementations.
|
||||
// In a production environment, I would rather avoid the copying.
|
||||
//
|
||||
inline int stoi (string_view str, size_t* idx=0, int base=10) {
|
||||
return std::stoi ( std::string(str), idx, base );
|
||||
}
|
||||
|
||||
inline long stol (string_view str, size_t* idx=0, int base=10) {
|
||||
return std::stol ( std::string(str), idx, base );
|
||||
}
|
||||
|
||||
inline unsigned long stoul (string_view str, size_t* idx=0, int base=10) {
|
||||
return std::stoul ( std::string(str), idx, base );
|
||||
}
|
||||
|
||||
inline long long stoll (string_view str, size_t* idx=0, int base=10) {
|
||||
return std::stoll ( std::string(str), idx, base );
|
||||
}
|
||||
|
||||
inline unsigned long long stoull (string_view str, size_t* idx=0, int base=10) {
|
||||
return std::stoull ( std::string(str), idx, base );
|
||||
}
|
||||
|
||||
inline float stof (string_view str, size_t* idx=0) {
|
||||
return std::stof ( std::string(str), idx );
|
||||
}
|
||||
|
||||
inline double stod (string_view str, size_t* idx=0) {
|
||||
return std::stod ( std::string(str), idx );
|
||||
}
|
||||
|
||||
inline long double stold (string_view str, size_t* idx=0) {
|
||||
return std::stold ( std::string(str), idx );
|
||||
}
|
||||
|
||||
inline int stoi (wstring_view str, size_t* idx=0, int base=10) {
|
||||
return std::stoi ( std::wstring(str), idx, base );
|
||||
}
|
||||
|
||||
inline long stol (wstring_view str, size_t* idx=0, int base=10) {
|
||||
return std::stol ( std::wstring(str), idx, base );
|
||||
}
|
||||
|
||||
inline unsigned long stoul (wstring_view str, size_t* idx=0, int base=10) {
|
||||
return std::stoul ( std::wstring(str), idx, base );
|
||||
}
|
||||
|
||||
inline long long stoll (wstring_view str, size_t* idx=0, int base=10) {
|
||||
return std::stoll ( std::wstring(str), idx, base );
|
||||
}
|
||||
|
||||
inline unsigned long long stoull (wstring_view str, size_t* idx=0, int base=10) {
|
||||
return std::stoull ( std::wstring(str), idx, base );
|
||||
}
|
||||
|
||||
inline float stof (wstring_view str, size_t* idx=0) {
|
||||
return std::stof ( std::wstring(str), idx );
|
||||
}
|
||||
|
||||
inline double stod (wstring_view str, size_t* idx=0) {
|
||||
return std::stod ( std::wstring(str), idx );
|
||||
}
|
||||
|
||||
inline long double stold (wstring_view str, size_t* idx=0) {
|
||||
return std::stold ( std::wstring(str), idx );
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
#if 0
|
||||
namespace std {
|
||||
// Hashing
|
||||
template<> struct hash<boost::string_view>;
|
||||
template<> struct hash<boost::u16string_view>;
|
||||
template<> struct hash<boost::u32string_view>;
|
||||
template<> struct hash<boost::wstring_view>;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
39
include/boost/utility/string_view_fwd.hpp
Normal file
39
include/boost/utility/string_view_fwd.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
Copyright (c) Marshall Clow 2012-2012.
|
||||
|
||||
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)
|
||||
|
||||
For more information, see http://www.boost.org
|
||||
|
||||
Based on the StringRef implementation in LLVM (http://llvm.org) and
|
||||
N3422 by Jeffrey Yasskin
|
||||
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3442.html
|
||||
Updated July 2015 to reflect the Library Fundamentals TS
|
||||
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4480.html
|
||||
|
||||
*/
|
||||
|
||||
#ifndef BOOST_STRING_VIEW_FWD_HPP
|
||||
#define BOOST_STRING_VIEW_FWD_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace boost {
|
||||
|
||||
template<typename charT, typename traits = std::char_traits<charT> > class basic_string_view;
|
||||
typedef basic_string_view<char, std::char_traits<char> > string_view;
|
||||
typedef basic_string_view<wchar_t, std::char_traits<wchar_t> > wstring_view;
|
||||
|
||||
#ifndef BOOST_NO_CXX11_CHAR16_T
|
||||
typedef basic_string_view<char16_t, std::char_traits<char16_t> > u16string_view;
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_NO_CXX11_CHAR32_T
|
||||
typedef basic_string_view<char32_t, std::char_traits<char32_t> > u32string_view;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -30,6 +30,9 @@ test-suite utility
|
||||
[ run string_ref_test1.cpp unit_test_framework ]
|
||||
[ run string_ref_test2.cpp unit_test_framework ]
|
||||
[ run string_ref_test_io.cpp unit_test_framework ]
|
||||
[ run string_view_test1.cpp unit_test_framework ]
|
||||
[ run string_view_test2.cpp unit_test_framework ]
|
||||
[ run string_view_test_io.cpp unit_test_framework ]
|
||||
[ run ../value_init_test.cpp ]
|
||||
[ run ../value_init_workaround_test.cpp ]
|
||||
[ run ../initialized_test.cpp ]
|
||||
|
@ -75,7 +75,6 @@ struct context
|
||||
BOOST_AUTO_TEST_CASE_TEMPLATE(string_ref_output, CharT, char_types)
|
||||
{
|
||||
typedef CharT char_type;
|
||||
typedef std::basic_string< char_type > string_type;
|
||||
typedef std::basic_ostringstream< char_type > ostream_type;
|
||||
typedef boost::basic_string_ref< char_type > string_ref_type;
|
||||
|
||||
@ -90,7 +89,6 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(string_ref_output, CharT, char_types)
|
||||
BOOST_AUTO_TEST_CASE_TEMPLATE(padding, CharT, char_types)
|
||||
{
|
||||
typedef CharT char_type;
|
||||
typedef std::basic_string< char_type > string_type;
|
||||
typedef std::basic_ostringstream< char_type > ostream_type;
|
||||
typedef boost::basic_string_ref< char_type > string_ref_type;
|
||||
|
||||
@ -134,7 +132,6 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(padding, CharT, char_types)
|
||||
BOOST_AUTO_TEST_CASE_TEMPLATE(padding_fill, CharT, char_types)
|
||||
{
|
||||
typedef CharT char_type;
|
||||
typedef std::basic_string< char_type > string_type;
|
||||
typedef std::basic_ostringstream< char_type > ostream_type;
|
||||
typedef boost::basic_string_ref< char_type > string_ref_type;
|
||||
|
||||
@ -153,7 +150,6 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(padding_fill, CharT, char_types)
|
||||
BOOST_AUTO_TEST_CASE_TEMPLATE(alignment, CharT, char_types)
|
||||
{
|
||||
typedef CharT char_type;
|
||||
typedef std::basic_string< char_type > string_type;
|
||||
typedef std::basic_ostringstream< char_type > ostream_type;
|
||||
typedef boost::basic_string_ref< char_type > string_ref_type;
|
||||
|
||||
|
109
test/string_view_test1.cpp
Normal file
109
test/string_view_test1.cpp
Normal file
@ -0,0 +1,109 @@
|
||||
/*
|
||||
Copyright (c) Marshall Clow 2012-2012.
|
||||
|
||||
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)
|
||||
|
||||
For more information, see http://www.boost.org
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
#include <boost/utility/string_view.hpp>
|
||||
|
||||
#define BOOST_TEST_MAIN
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
typedef boost::string_view string_view;
|
||||
|
||||
// Should be equal
|
||||
void interop ( const std::string &str, string_view ref ) {
|
||||
// BOOST_CHECK ( str == ref );
|
||||
BOOST_CHECK ( str.size () == ref.size ());
|
||||
BOOST_CHECK ( std::equal ( str.begin (), str.end (), ref.begin ()));
|
||||
BOOST_CHECK ( std::equal ( str.rbegin (), str.rend (), ref.rbegin ()));
|
||||
}
|
||||
|
||||
void null_tests ( const char *p ) {
|
||||
// All zero-length string-refs should be equal
|
||||
string_view sr1; // NULL, 0
|
||||
string_view sr2 ( NULL, 0 );
|
||||
string_view sr3 ( p, 0 );
|
||||
string_view sr4 ( p );
|
||||
sr4.clear ();
|
||||
|
||||
BOOST_CHECK ( sr1 == sr2 );
|
||||
BOOST_CHECK ( sr1 == sr3 );
|
||||
BOOST_CHECK ( sr2 == sr3 );
|
||||
BOOST_CHECK ( sr1 == sr4 );
|
||||
}
|
||||
|
||||
// make sure that substrings work just like strings
|
||||
void test_substr ( const std::string &str ) {
|
||||
const size_t sz = str.size ();
|
||||
string_view ref ( str );
|
||||
|
||||
// Substrings at the end
|
||||
for ( size_t i = 0; i <= sz; ++ i )
|
||||
interop ( str.substr ( i ), ref.substr ( i ));
|
||||
|
||||
// Substrings at the beginning
|
||||
for ( size_t i = 0; i <= sz; ++ i )
|
||||
interop ( str.substr ( 0, i ), ref.substr ( 0, i ));
|
||||
|
||||
// All possible substrings
|
||||
for ( size_t i = 0; i < sz; ++i )
|
||||
for ( size_t j = i; j < sz; ++j )
|
||||
interop ( str.substr ( i, j ), ref.substr ( i, j ));
|
||||
}
|
||||
|
||||
// make sure that removing prefixes and suffixes work just like strings
|
||||
void test_remove ( const std::string &str ) {
|
||||
const size_t sz = str.size ();
|
||||
std::string work;
|
||||
string_view ref;
|
||||
|
||||
for ( size_t i = 1; i <= sz; ++i ) {
|
||||
work = str;
|
||||
ref = str;
|
||||
while ( ref.size () >= i ) {
|
||||
interop ( work, ref );
|
||||
work.erase ( 0, i );
|
||||
ref.remove_prefix (i);
|
||||
}
|
||||
}
|
||||
|
||||
for ( size_t i = 1; i < sz; ++ i ) {
|
||||
work = str;
|
||||
ref = str;
|
||||
while ( ref.size () >= i ) {
|
||||
interop ( work, ref );
|
||||
work.erase ( work.size () - i, i );
|
||||
ref.remove_suffix (i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char *test_strings [] = {
|
||||
"",
|
||||
"1",
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
||||
"0123456789",
|
||||
NULL
|
||||
};
|
||||
|
||||
BOOST_AUTO_TEST_CASE( test_main )
|
||||
{
|
||||
const char **p = &test_strings[0];
|
||||
|
||||
while ( *p != NULL ) {
|
||||
interop ( *p, *p );
|
||||
test_substr ( *p );
|
||||
test_remove ( *p );
|
||||
null_tests ( *p );
|
||||
|
||||
p++;
|
||||
}
|
||||
}
|
318
test/string_view_test2.cpp
Normal file
318
test/string_view_test2.cpp
Normal file
@ -0,0 +1,318 @@
|
||||
/*
|
||||
Copyright (c) Marshall Clow 2012-2012.
|
||||
|
||||
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)
|
||||
|
||||
For more information, see http://www.boost.org
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstring> // for std::strchr
|
||||
|
||||
#include <boost/utility/string_view.hpp>
|
||||
|
||||
#define BOOST_TEST_MAIN
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
typedef boost::string_view string_view;
|
||||
|
||||
void ends_with ( const char *arg ) {
|
||||
const size_t sz = std::strlen ( arg );
|
||||
string_view sr ( arg );
|
||||
string_view sr2 ( arg );
|
||||
const char *p = arg;
|
||||
|
||||
while ( *p ) {
|
||||
BOOST_CHECK ( sr.ends_with ( p ));
|
||||
++p;
|
||||
}
|
||||
|
||||
while ( !sr2.empty ()) {
|
||||
BOOST_CHECK ( sr.ends_with ( sr2 ));
|
||||
sr2.remove_prefix (1);
|
||||
}
|
||||
|
||||
sr2 = arg;
|
||||
while ( !sr2.empty ()) {
|
||||
BOOST_CHECK ( sr.ends_with ( sr2 ));
|
||||
sr2.remove_prefix (1);
|
||||
}
|
||||
|
||||
char ch = sz == 0 ? '\0' : arg [ sz - 1 ];
|
||||
sr2 = arg;
|
||||
if ( sz > 0 )
|
||||
BOOST_CHECK ( sr2.ends_with ( ch ));
|
||||
BOOST_CHECK ( !sr2.ends_with ( ++ch ));
|
||||
BOOST_CHECK ( sr2.ends_with ( string_view()));
|
||||
}
|
||||
|
||||
void starts_with ( const char *arg ) {
|
||||
const size_t sz = std::strlen ( arg );
|
||||
string_view sr ( arg );
|
||||
string_view sr2 ( arg );
|
||||
const char *p = arg + std::strlen ( arg ) - 1;
|
||||
while ( p >= arg ) {
|
||||
std::string foo ( arg, p + 1 );
|
||||
BOOST_CHECK ( sr.starts_with ( foo ));
|
||||
--p;
|
||||
}
|
||||
|
||||
while ( !sr2.empty ()) {
|
||||
BOOST_CHECK ( sr.starts_with ( sr2 ));
|
||||
sr2.remove_suffix (1);
|
||||
}
|
||||
|
||||
char ch = *arg;
|
||||
sr2 = arg;
|
||||
if ( sz > 0 )
|
||||
BOOST_CHECK ( sr2.starts_with ( ch ));
|
||||
BOOST_CHECK ( !sr2.starts_with ( ++ch ));
|
||||
BOOST_CHECK ( sr2.starts_with ( string_view ()));
|
||||
}
|
||||
|
||||
void reverse ( const char *arg ) {
|
||||
// Round trip
|
||||
string_view sr1 ( arg );
|
||||
std::string string1 ( sr1.rbegin (), sr1.rend ());
|
||||
string_view sr2 ( string1 );
|
||||
std::string string2 ( sr2.rbegin (), sr2.rend ());
|
||||
|
||||
BOOST_CHECK ( std::equal ( sr2.rbegin (), sr2.rend (), arg ));
|
||||
BOOST_CHECK ( string2 == arg );
|
||||
BOOST_CHECK ( std::equal ( sr1.begin (), sr1.end (), string2.begin ()));
|
||||
}
|
||||
|
||||
// This helper function eliminates signed vs. unsigned warnings
|
||||
string_view::size_type ptr_diff ( const char *res, const char *base ) {
|
||||
BOOST_CHECK ( res >= base );
|
||||
return static_cast<string_view::size_type> ( res - base );
|
||||
}
|
||||
|
||||
void find ( const char *arg ) {
|
||||
string_view sr1;
|
||||
string_view sr2;
|
||||
const char *p;
|
||||
|
||||
// Look for each character in the string(searching from the start)
|
||||
p = arg;
|
||||
sr1 = arg;
|
||||
while ( *p ) {
|
||||
string_view::size_type pos = sr1.find(*p);
|
||||
BOOST_CHECK ( pos != string_view::npos && ( pos <= ptr_diff ( p, arg )));
|
||||
++p;
|
||||
}
|
||||
|
||||
// Look for each character in the string (searching from the end)
|
||||
p = arg;
|
||||
sr1 = arg;
|
||||
while ( *p ) {
|
||||
string_view::size_type pos = sr1.rfind(*p);
|
||||
BOOST_CHECK ( pos != string_view::npos && pos < sr1.size () && ( pos >= ptr_diff ( p, arg )));
|
||||
++p;
|
||||
}
|
||||
|
||||
// Look for pairs on characters (searching from the start)
|
||||
sr1 = arg;
|
||||
p = arg;
|
||||
while ( *p && *(p+1)) {
|
||||
string_view sr3 ( p, 2 );
|
||||
string_view::size_type pos = sr1.find ( sr3 );
|
||||
BOOST_CHECK ( pos != string_view::npos && pos <= static_cast<string_view::size_type>( p - arg ));
|
||||
p++;
|
||||
}
|
||||
|
||||
sr1 = arg;
|
||||
p = arg;
|
||||
// for all possible chars, see if we find them in the right place.
|
||||
// Note that strchr will/might do the _wrong_ thing if we search for NULL
|
||||
for ( int ch = 1; ch < 256; ++ch ) {
|
||||
string_view::size_type pos = sr1.find(ch);
|
||||
const char *strp = std::strchr ( arg, ch );
|
||||
BOOST_CHECK (( strp == NULL ) == ( pos == string_view::npos ));
|
||||
if ( strp != NULL )
|
||||
BOOST_CHECK ( ptr_diff ( strp, arg ) == pos );
|
||||
}
|
||||
|
||||
sr1 = arg;
|
||||
p = arg;
|
||||
// for all possible chars, see if we find them in the right place.
|
||||
// Note that strchr will/might do the _wrong_ thing if we search for NULL
|
||||
for ( int ch = 1; ch < 256; ++ch ) {
|
||||
string_view::size_type pos = sr1.rfind(ch);
|
||||
const char *strp = std::strrchr ( arg, ch );
|
||||
BOOST_CHECK (( strp == NULL ) == ( pos == string_view::npos ));
|
||||
if ( strp != NULL )
|
||||
BOOST_CHECK ( ptr_diff ( strp, arg ) == pos );
|
||||
}
|
||||
|
||||
|
||||
// Find everything at the start
|
||||
p = arg;
|
||||
sr1 = arg;
|
||||
while ( !sr1.empty ()) {
|
||||
string_view::size_type pos = sr1.find(*p);
|
||||
BOOST_CHECK ( pos == 0 );
|
||||
sr1.remove_prefix (1);
|
||||
++p;
|
||||
}
|
||||
|
||||
// Find everything at the end
|
||||
sr1 = arg;
|
||||
p = arg + std::strlen ( arg ) - 1;
|
||||
while ( !sr1.empty ()) {
|
||||
string_view::size_type pos = sr1.rfind(*p);
|
||||
BOOST_CHECK ( pos == sr1.size () - 1 );
|
||||
sr1.remove_suffix (1);
|
||||
--p;
|
||||
}
|
||||
|
||||
// Find everything at the start
|
||||
sr1 = arg;
|
||||
p = arg;
|
||||
while ( !sr1.empty ()) {
|
||||
string_view::size_type pos = sr1.find_first_of(*p);
|
||||
BOOST_CHECK ( pos == 0 );
|
||||
sr1.remove_prefix (1);
|
||||
++p;
|
||||
}
|
||||
|
||||
|
||||
// Find everything at the end
|
||||
sr1 = arg;
|
||||
p = arg + std::strlen ( arg ) - 1;
|
||||
while ( !sr1.empty ()) {
|
||||
string_view::size_type pos = sr1.find_last_of(*p);
|
||||
BOOST_CHECK ( pos == sr1.size () - 1 );
|
||||
sr1.remove_suffix (1);
|
||||
--p;
|
||||
}
|
||||
|
||||
// Basic sanity checking for "find_first_of / find_first_not_of"
|
||||
sr1 = arg;
|
||||
sr2 = arg;
|
||||
while ( !sr1.empty() ) {
|
||||
BOOST_CHECK ( sr1.find_first_of ( sr2 ) == 0 );
|
||||
BOOST_CHECK ( sr1.find_first_not_of ( sr2 ) == string_view::npos );
|
||||
sr1.remove_prefix ( 1 );
|
||||
}
|
||||
|
||||
p = arg;
|
||||
sr1 = arg;
|
||||
while ( *p ) {
|
||||
string_view::size_type pos1 = sr1.find_first_of(*p);
|
||||
string_view::size_type pos2 = sr1.find_first_not_of(*p);
|
||||
BOOST_CHECK ( pos1 != string_view::npos && pos1 < sr1.size () && pos1 <= ptr_diff ( p, arg ));
|
||||
if ( pos2 != string_view::npos ) {
|
||||
for ( size_t i = 0 ; i < pos2; ++i )
|
||||
BOOST_CHECK ( sr1[i] == *p );
|
||||
BOOST_CHECK ( sr1 [ pos2 ] != *p );
|
||||
}
|
||||
|
||||
BOOST_CHECK ( pos2 != pos1 );
|
||||
++p;
|
||||
}
|
||||
|
||||
// Basic sanity checking for "find_last_of / find_last_not_of"
|
||||
sr1 = arg;
|
||||
sr2 = arg;
|
||||
while ( !sr1.empty() ) {
|
||||
BOOST_CHECK ( sr1.find_last_of ( sr2 ) == ( sr1.size () - 1 ));
|
||||
BOOST_CHECK ( sr1.find_last_not_of ( sr2 ) == string_view::npos );
|
||||
sr1.remove_suffix ( 1 );
|
||||
}
|
||||
|
||||
p = arg;
|
||||
sr1 = arg;
|
||||
while ( *p ) {
|
||||
string_view::size_type pos1 = sr1.find_last_of(*p);
|
||||
string_view::size_type pos2 = sr1.find_last_not_of(*p);
|
||||
BOOST_CHECK ( pos1 != string_view::npos && pos1 < sr1.size () && pos1 >= ptr_diff ( p, arg ));
|
||||
BOOST_CHECK ( pos2 == string_view::npos || pos1 < sr1.size ());
|
||||
if ( pos2 != string_view::npos ) {
|
||||
for ( size_t i = sr1.size () -1 ; i > pos2; --i )
|
||||
BOOST_CHECK ( sr1[i] == *p );
|
||||
BOOST_CHECK ( sr1 [ pos2 ] != *p );
|
||||
}
|
||||
|
||||
BOOST_CHECK ( pos2 != pos1 );
|
||||
++p;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void to_string ( const char *arg ) {
|
||||
string_view sr1;
|
||||
std::string str1;
|
||||
std::string str2;
|
||||
|
||||
str1.assign ( arg );
|
||||
sr1 = arg;
|
||||
// str2 = sr1.to_string<std::allocator<char> > ();
|
||||
str2 = sr1.to_string ();
|
||||
BOOST_CHECK ( str1 == str2 );
|
||||
|
||||
#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
|
||||
std::string str3 = static_cast<std::string> ( sr1 );
|
||||
BOOST_CHECK ( str1 == str3 );
|
||||
#endif
|
||||
}
|
||||
|
||||
void compare ( const char *arg ) {
|
||||
string_view sr1;
|
||||
std::string str1;
|
||||
std::string str2 = str1;
|
||||
|
||||
str1.assign ( arg );
|
||||
sr1 = arg;
|
||||
BOOST_CHECK ( sr1 == sr1); // compare string_view and string_view
|
||||
BOOST_CHECK ( sr1 == str1); // compare string and string_view
|
||||
BOOST_CHECK ( str1 == sr1 ); // compare string_view and string
|
||||
BOOST_CHECK ( sr1 == arg ); // compare string_view and pointer
|
||||
BOOST_CHECK ( arg == sr1 ); // compare pointer and string_view
|
||||
|
||||
if ( sr1.size () > 0 ) {
|
||||
(*str1.rbegin())++;
|
||||
BOOST_CHECK ( sr1 != str1 );
|
||||
BOOST_CHECK ( str1 != sr1 );
|
||||
BOOST_CHECK ( sr1 < str1 );
|
||||
BOOST_CHECK ( sr1 <= str1 );
|
||||
BOOST_CHECK ( str1 > sr1 );
|
||||
BOOST_CHECK ( str1 >= sr1 );
|
||||
|
||||
(*str1.rbegin()) -= 2;
|
||||
BOOST_CHECK ( sr1 != str1 );
|
||||
BOOST_CHECK ( str1 != sr1 );
|
||||
BOOST_CHECK ( sr1 > str1 );
|
||||
BOOST_CHECK ( sr1 >= str1 );
|
||||
BOOST_CHECK ( str1 < sr1 );
|
||||
BOOST_CHECK ( str1 <= sr1 );
|
||||
}
|
||||
}
|
||||
|
||||
const char *test_strings [] = {
|
||||
"",
|
||||
"0",
|
||||
"abc",
|
||||
"AAA", // all the same
|
||||
"adsfadadiaef;alkdg;aljt;j agl;sjrl;tjs;lga;lretj;srg[w349u5209dsfadfasdfasdfadsf",
|
||||
"abc\0asdfadsfasf",
|
||||
NULL
|
||||
};
|
||||
|
||||
BOOST_AUTO_TEST_CASE( test_main )
|
||||
{
|
||||
const char **p = &test_strings[0];
|
||||
|
||||
while ( *p != NULL ) {
|
||||
starts_with ( *p );
|
||||
ends_with ( *p );
|
||||
reverse ( *p );
|
||||
find ( *p );
|
||||
to_string ( *p );
|
||||
compare ( *p );
|
||||
|
||||
p++;
|
||||
}
|
||||
}
|
179
test/string_view_test_io.cpp
Normal file
179
test/string_view_test_io.cpp
Normal file
@ -0,0 +1,179 @@
|
||||
/*
|
||||
* Copyright Andrey Semashev 2013.
|
||||
* 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)
|
||||
*/
|
||||
/*!
|
||||
* \file string_ref_test_io.cpp
|
||||
* \author Andrey Semashev
|
||||
* \date 26.05.2013
|
||||
*
|
||||
* \brief This header contains tests for stream operations of \c basic_string_ref.
|
||||
*/
|
||||
|
||||
#define BOOST_TEST_MODULE string_ref_test_io
|
||||
|
||||
#include <boost/utility/string_view.hpp>
|
||||
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/mpl/vector.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
typedef boost::mpl::vector<
|
||||
char
|
||||
#if !defined(BOOST_NO_STD_WSTRING) && !defined(BOOST_NO_STD_WSTREAMBUF) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
, wchar_t
|
||||
#endif
|
||||
/* Current implementations seem to be missing codecvt facets to convert chars to char16_t and char32_t even though the types are available.
|
||||
#if !defined(BOOST_NO_CXX11_CHAR16_T)
|
||||
, char16_t
|
||||
#endif
|
||||
#if !defined(BOOST_NO_CXX11_CHAR32_T)
|
||||
, char32_t
|
||||
#endif
|
||||
*/
|
||||
>::type char_types;
|
||||
|
||||
static const char* test_strings[] =
|
||||
{
|
||||
"begin",
|
||||
"abcd",
|
||||
"end"
|
||||
};
|
||||
|
||||
//! The context with test data for particular character type
|
||||
template< typename CharT >
|
||||
struct context
|
||||
{
|
||||
typedef CharT char_type;
|
||||
typedef std::basic_string< char_type > string_type;
|
||||
typedef std::basic_ostringstream< char_type > ostream_type;
|
||||
|
||||
string_type begin, abcd, end;
|
||||
|
||||
context()
|
||||
{
|
||||
boost::string_view str = test_strings[0];
|
||||
std::copy(str.begin(), str.end(), std::back_inserter(begin));
|
||||
|
||||
str = test_strings[1];
|
||||
std::copy(str.begin(), str.end(), std::back_inserter(abcd));
|
||||
|
||||
str = test_strings[2];
|
||||
std::copy(str.begin(), str.end(), std::back_inserter(end));
|
||||
}
|
||||
};
|
||||
|
||||
// Test regular output
|
||||
BOOST_AUTO_TEST_CASE_TEMPLATE(string_view_output, CharT, char_types)
|
||||
{
|
||||
typedef CharT char_type;
|
||||
typedef std::basic_ostringstream< char_type > ostream_type;
|
||||
typedef boost::basic_string_view< char_type > string_view_type;
|
||||
|
||||
context< char_type > ctx;
|
||||
|
||||
ostream_type strm;
|
||||
strm << string_view_type(ctx.abcd);
|
||||
BOOST_CHECK(strm.str() == ctx.abcd);
|
||||
}
|
||||
|
||||
// Test support for padding
|
||||
BOOST_AUTO_TEST_CASE_TEMPLATE(padding, CharT, char_types)
|
||||
{
|
||||
typedef CharT char_type;
|
||||
typedef std::basic_ostringstream< char_type > ostream_type;
|
||||
typedef boost::basic_string_view< char_type > string_view_type;
|
||||
|
||||
context< char_type > ctx;
|
||||
|
||||
// Test for padding
|
||||
{
|
||||
ostream_type strm_ref;
|
||||
strm_ref << ctx.begin << std::setw(8) << string_view_type(ctx.abcd) << ctx.end;
|
||||
|
||||
ostream_type strm_correct;
|
||||
strm_correct << ctx.begin << std::setw(8) << ctx.abcd << ctx.end;
|
||||
|
||||
BOOST_CHECK(strm_ref.str() == strm_correct.str());
|
||||
}
|
||||
|
||||
// Test for long padding
|
||||
{
|
||||
ostream_type strm_ref;
|
||||
strm_ref << ctx.begin << std::setw(100) << string_view_type(ctx.abcd) << ctx.end;
|
||||
|
||||
ostream_type strm_correct;
|
||||
strm_correct << ctx.begin << std::setw(100) << ctx.abcd << ctx.end;
|
||||
|
||||
BOOST_CHECK(strm_ref.str() == strm_correct.str());
|
||||
}
|
||||
|
||||
// Test that short width does not truncate the string
|
||||
{
|
||||
ostream_type strm_ref;
|
||||
strm_ref << ctx.begin << std::setw(1) << string_view_type(ctx.abcd) << ctx.end;
|
||||
|
||||
ostream_type strm_correct;
|
||||
strm_correct << ctx.begin << std::setw(1) << ctx.abcd << ctx.end;
|
||||
|
||||
BOOST_CHECK(strm_ref.str() == strm_correct.str());
|
||||
}
|
||||
}
|
||||
|
||||
// Test support for padding fill
|
||||
BOOST_AUTO_TEST_CASE_TEMPLATE(padding_fill, CharT, char_types)
|
||||
{
|
||||
typedef CharT char_type;
|
||||
typedef std::basic_ostringstream< char_type > ostream_type;
|
||||
typedef boost::basic_string_view< char_type > string_view_type;
|
||||
|
||||
context< char_type > ctx;
|
||||
|
||||
ostream_type strm_ref;
|
||||
strm_ref << ctx.begin << std::setfill(static_cast< char_type >('x')) << std::setw(8) << string_view_type(ctx.abcd) << ctx.end;
|
||||
|
||||
ostream_type strm_correct;
|
||||
strm_correct << ctx.begin << std::setfill(static_cast< char_type >('x')) << std::setw(8) << ctx.abcd << ctx.end;
|
||||
|
||||
BOOST_CHECK(strm_ref.str() == strm_correct.str());
|
||||
}
|
||||
|
||||
// Test support for alignment
|
||||
BOOST_AUTO_TEST_CASE_TEMPLATE(alignment, CharT, char_types)
|
||||
{
|
||||
typedef CharT char_type;
|
||||
typedef std::basic_ostringstream< char_type > ostream_type;
|
||||
typedef boost::basic_string_view< char_type > string_view_type;
|
||||
|
||||
context< char_type > ctx;
|
||||
|
||||
// Left alignment
|
||||
{
|
||||
ostream_type strm_ref;
|
||||
strm_ref << ctx.begin << std::left << std::setw(8) << string_view_type(ctx.abcd) << ctx.end;
|
||||
|
||||
ostream_type strm_correct;
|
||||
strm_correct << ctx.begin << std::left << std::setw(8) << ctx.abcd << ctx.end;
|
||||
|
||||
BOOST_CHECK(strm_ref.str() == strm_correct.str());
|
||||
}
|
||||
|
||||
// Right alignment
|
||||
{
|
||||
ostream_type strm_ref;
|
||||
strm_ref << ctx.begin << std::right << std::setw(8) << string_view_type(ctx.abcd) << ctx.end;
|
||||
|
||||
ostream_type strm_correct;
|
||||
strm_correct << ctx.begin << std::right << std::setw(8) << ctx.abcd << ctx.end;
|
||||
|
||||
BOOST_CHECK(strm_ref.str() == strm_correct.str());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user