Change how non-nullness is enforce in StringRef constructor

This commit is contained in:
Martin Hořeňovský 2017-11-13 12:57:55 +01:00
parent f80f28e09a
commit 0adb04807a

View File

@ -27,6 +27,13 @@ namespace Catch {
return s_emptyStringRef; return s_emptyStringRef;
} }
char const* enforceNonNull(char const* ptr) {
if (ptr == nullptr) {
std::abort();
}
return ptr;
}
} }
StringRef::StringRef() noexcept StringRef::StringRef() noexcept
@ -47,10 +54,8 @@ namespace Catch {
} }
StringRef::StringRef(char const* rawChars) noexcept StringRef::StringRef(char const* rawChars) noexcept
: m_start(rawChars), : m_start( enforceNonNull(rawChars) ),
m_size( (rawChars != nullptr)? m_size( static_cast<size_type>(std::strlen(rawChars)))
(static_cast<size_type>(std::strlen(rawChars))):
throw std::domain_error("Cannot create StringRef from nullptr"))
{} {}
StringRef::StringRef( char const* rawChars, size_type size ) noexcept StringRef::StringRef( char const* rawChars, size_type size ) noexcept