mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-02 13:25:41 +02:00
Add more constexpr to StringRef
This commit is contained in:
@@ -22,17 +22,7 @@ namespace Catch {
|
||||
CATCH_ENFORCE(isNullTerminated(), "Called StringRef::c_str() on a non-null-terminated instance");
|
||||
return m_start;
|
||||
}
|
||||
auto StringRef::data() const noexcept -> char const* {
|
||||
return m_start;
|
||||
}
|
||||
|
||||
auto StringRef::substr( size_type start, size_type size ) const noexcept -> StringRef {
|
||||
if (start < m_size) {
|
||||
return StringRef(m_start + start, (std::min)(m_size - start, size));
|
||||
} else {
|
||||
return StringRef();
|
||||
}
|
||||
}
|
||||
auto StringRef::operator == ( StringRef const& other ) const noexcept -> bool {
|
||||
return m_size == other.m_size
|
||||
&& (std::memcmp( m_start, other.m_start, m_size ) == 0);
|
||||
|
@@ -74,12 +74,21 @@ namespace Catch {
|
||||
|
||||
public: // substrings and searches
|
||||
// Returns a substring of [start, start + length).
|
||||
// If start + length > size(), then the substring is [start, size()).
|
||||
// If start + length > size(), then the substring is [start, start + size()).
|
||||
// If start > size(), then the substring is empty.
|
||||
auto substr( size_type start, size_type length ) const noexcept -> StringRef;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the current start pointer. May not be null-terminated.
|
||||
auto data() const noexcept -> char const*;
|
||||
constexpr char const* data() const noexcept {
|
||||
return m_start;
|
||||
}
|
||||
|
||||
constexpr auto isNullTerminated() const noexcept -> bool {
|
||||
return m_start[m_size] == '\0';
|
||||
|
Reference in New Issue
Block a user