Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorMartin Hořeňovský2020-02-13 15:01:03 +0100
committerMartin Hořeňovský2020-02-13 15:01:03 +0100
commit2945b80f61b8ad5b7e05c36fadcac8a35b0e3a8f (patch)
treef077b74334a42d08fa775a9f54ceef6cf7ddb837 /src
parent63b7d6f98ea5a86c0cc14e094bb66f704eaddfd1 (diff)
downloadcatch3-2945b80f61b8ad5b7e05c36fadcac8a35b0e3a8f.tar.gz
Add more constexpr to StringRef
Diffstat (limited to 'src')
-rw-r--r--src/catch2/catch_stringref.cpp10
-rw-r--r--src/catch2/catch_stringref.h15
2 files changed, 12 insertions, 13 deletions
diff --git a/src/catch2/catch_stringref.cpp b/src/catch2/catch_stringref.cpp
index 3402d1c8..1ae57047 100644
--- a/src/catch2/catch_stringref.cpp
+++ b/src/catch2/catch_stringref.cpp
@@ -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);
diff --git a/src/catch2/catch_stringref.h b/src/catch2/catch_stringref.h
index 9d5522a5..8ed1e731 100644
--- a/src/catch2/catch_stringref.h
+++ b/src/catch2/catch_stringref.h
@@ -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';