Files
Catch2/src/catch2/internal/catch_stringref.hpp
T

117 lines
3.7 KiB
C++
Raw Normal View History

2017-06-29 11:18:14 +01:00
/*
* 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
2017-08-05 22:53:21 +01:00
#include <cstddef>
2017-08-14 08:39:14 +01:00
#include <string>
2017-08-14 08:50:44 +01:00
#include <iosfwd>
#include <cassert>
2017-08-05 22:53:21 +01:00
2017-06-29 11:18:14 +01:00
namespace Catch {
2017-06-29 11:18:14 +01:00
/// A non-owning string class (similar to the forthcoming std::string_view)
/// Note that, because a StringRef may be a substring of another string,
2019-10-21 17:44:11 +02:00
/// it may not be null terminated.
2017-06-29 11:18:14 +01:00
class StringRef {
public:
using size_type = std::size_t;
using const_iterator = const char*;
private:
static constexpr char const* const s_empty = "";
2019-10-21 17:44:11 +02:00
char const* m_start = s_empty;
size_type m_size = 0;
2019-10-21 17:44:11 +02:00
public: // construction
constexpr StringRef() noexcept = default;
StringRef( char const* rawChars ) noexcept;
2019-10-21 17:44:11 +02:00
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);
}
2017-06-29 11:18:14 +01:00
public: // operators
auto operator == ( StringRef const& other ) const noexcept -> bool;
2019-10-21 17:44:11 +02:00
auto operator != (StringRef const& other) const noexcept -> bool {
return !(*this == other);
}
2020-05-10 07:22:11 +02:00
constexpr auto operator[] ( size_type index ) const noexcept -> char {
assert(index < m_size);
return m_start[index];
}
bool operator<(StringRef const& rhs) const noexcept;
2017-06-29 11:18:14 +01:00
public: // named queries
2019-10-21 17:44:11 +02:00
constexpr auto empty() const noexcept -> bool {
return m_size == 0;
}
2019-10-21 17:44:11 +02:00
constexpr auto size() const noexcept -> size_type {
return m_size;
}
2019-10-21 17:44:11 +02:00
// Returns the current start pointer. If the StringRef is not
// null-terminated, throws std::domain_exception
2017-06-29 11:18:14 +01:00
auto c_str() const -> char const*;
2017-06-29 11:18:14 +01:00
public: // substrings and searches
2019-10-21 17:44:11 +02:00
// Returns a substring of [start, start + length).
2020-02-13 15:01:03 +01:00
// If start + length > size(), then the substring is [start, start + size()).
2019-10-21 17:44:11 +02:00
// If start > size(), then the substring is empty.
2020-02-13 15:01:03 +01:00
constexpr StringRef substr(size_type start, size_type length) const noexcept {
if (start < m_size) {
const auto shortened_size = m_size - start;
return StringRef(m_start + start, (shortened_size < length) ? shortened_size : length);
} else {
return StringRef();
}
}
2017-06-29 11:18:14 +01:00
2019-10-21 17:44:11 +02:00
// Returns the current start pointer. May not be null-terminated.
2020-02-13 15:01:03 +01:00
constexpr char const* data() const noexcept {
return m_start;
}
2019-10-21 17:44:11 +02:00
constexpr auto isNullTerminated() const noexcept -> bool {
return m_start[m_size] == '\0';
}
public: // iterators
2019-10-21 17:44:11 +02:00
constexpr const_iterator begin() const { return m_start; }
constexpr const_iterator end() const { return m_start + m_size; }
2020-05-10 07:22:11 +02:00
friend std::string& operator += (std::string& lhs, StringRef const& sr);
friend std::ostream& operator << (std::ostream& os, StringRef const& sr);
2017-06-29 11:18:14 +01:00
};
2019-10-21 17:44:11 +02:00
constexpr auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef {
return StringRef( rawChars, size );
}
2017-06-29 11:18:14 +01:00
} // namespace Catch
2019-10-21 17:44:11 +02:00
constexpr auto operator "" _catch_sr( char const* rawChars, std::size_t size ) noexcept -> Catch::StringRef {
2018-07-23 13:19:21 +02:00
return Catch::StringRef( rawChars, size );
}
2017-06-29 11:18:14 +01:00
#endif // CATCH_STRINGREF_H_INCLUDED