mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-30 09:13:29 +01:00
50cc14c94c
Now it no longer tries to be this weird hybrid between an owning and non-owning reference, and is only ever non-owning. This is also reflected in its interface, for example `StringRef::isNullTerminated` is now public, and `StringRef::c_str()` has the precondition that it is true. Overview of the changes: * The `StringRef::m_data` member has been completely removed, as it had no more uses. * `StringRef::isSubstring()` has been made public and renamed to `StringRef::isNullTerminated()`, so that the name reflects what the method actually does. * `StringRef::currentData()` has been renamed to `StringRef::data()`, to be in line with common C++ containers and container-alikes. * `StringRef::c_str()` will no longer silently make copies. It instead has a precondition that `isNullTerminated()` is true. * If the user needs a null-terminated string, they should use the `std::string` conversion operator and call `c_str()` on the resulting `std::string`. * Some small optimizations in various places. * Basic functionality is now `constexpr`.
105 lines
3.4 KiB
C++
105 lines
3.4 KiB
C++
/*
|
|
* Copyright 2016 Two Blue Cubes Ltd. All rights reserved.
|
|
*
|
|
* 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 CATCH_STRINGREF_H_INCLUDED
|
|
#define CATCH_STRINGREF_H_INCLUDED
|
|
|
|
#include <cstddef>
|
|
#include <string>
|
|
#include <iosfwd>
|
|
#include <cassert>
|
|
|
|
namespace Catch {
|
|
|
|
/// A non-owning string class (similar to the forthcoming std::string_view)
|
|
/// Note that, because a StringRef may be a substring of another string,
|
|
/// it may not be null terminated.
|
|
class StringRef {
|
|
public:
|
|
using size_type = std::size_t;
|
|
using const_iterator = const char*;
|
|
|
|
private:
|
|
static constexpr char const* const s_empty = "";
|
|
|
|
char const* m_start = s_empty;
|
|
size_type m_size = 0;
|
|
|
|
public: // construction
|
|
constexpr StringRef() noexcept = default;
|
|
|
|
StringRef( char const* rawChars ) noexcept;
|
|
|
|
constexpr StringRef( char const* rawChars, size_type size ) noexcept
|
|
: m_start( rawChars ),
|
|
m_size( size )
|
|
{}
|
|
|
|
StringRef( std::string const& stdString ) noexcept
|
|
: m_start( stdString.c_str() ),
|
|
m_size( stdString.size() )
|
|
{}
|
|
|
|
explicit operator std::string() const {
|
|
return std::string(m_start, m_size);
|
|
}
|
|
|
|
public: // operators
|
|
auto operator == ( StringRef const& other ) const noexcept -> bool;
|
|
auto operator != (StringRef const& other) const noexcept -> bool {
|
|
return !(*this == other);
|
|
}
|
|
|
|
auto operator[] ( size_type index ) const noexcept -> char {
|
|
assert(index < m_size);
|
|
return m_start[index];
|
|
}
|
|
|
|
public: // named queries
|
|
constexpr auto empty() const noexcept -> bool {
|
|
return m_size == 0;
|
|
}
|
|
constexpr auto size() const noexcept -> size_type {
|
|
return m_size;
|
|
}
|
|
|
|
// Returns the current start pointer. If the StringRef is not
|
|
// null-terminated, throws std::domain_exception
|
|
auto c_str() const -> char const*;
|
|
|
|
public: // substrings and searches
|
|
// Returns a substring of [start, start + length).
|
|
// If start + length > size(), then the substring is [start, size()).
|
|
// If start > size(), then the substring is empty.
|
|
auto substr( size_type start, size_type length ) const noexcept -> StringRef;
|
|
|
|
// Returns the current start pointer. May not be null-terminated.
|
|
auto data() const noexcept -> char const*;
|
|
|
|
constexpr auto isNullTerminated() const noexcept -> bool {
|
|
return m_start[m_size] == '\0';
|
|
}
|
|
|
|
public: // iterators
|
|
constexpr const_iterator begin() const { return m_start; }
|
|
constexpr const_iterator end() const { return m_start + m_size; }
|
|
};
|
|
|
|
auto operator += ( std::string& lhs, StringRef const& sr ) -> std::string&;
|
|
auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&;
|
|
|
|
|
|
constexpr auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef {
|
|
return StringRef( rawChars, size );
|
|
}
|
|
} // namespace Catch
|
|
|
|
constexpr auto operator "" _catch_sr( char const* rawChars, std::size_t size ) noexcept -> Catch::StringRef {
|
|
return Catch::StringRef( rawChars, size );
|
|
}
|
|
|
|
#endif // CATCH_STRINGREF_H_INCLUDED
|