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

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2/internal/catch_stringref.cpp
diff options
context:
space:
mode:
authorMartin Hořeňovský2021-09-27 18:30:31 +0200
committerMartin Hořeňovský2021-09-27 18:41:17 +0200
commit21b99d6f589090983136bf764514261dc863ed9a (patch)
treec5a6f5990179c0e4eecb6adbc90a7e7464fca22b /src/catch2/internal/catch_stringref.cpp
parentd42e7a23a0ca942e73b7065a1fbf28b6b2a817ce (diff)
downloadcatch3-21b99d6f589090983136bf764514261dc863ed9a.tar.gz
Add StringRef::compare for three way comparison
Diffstat (limited to 'src/catch2/internal/catch_stringref.cpp')
-rw-r--r--src/catch2/internal/catch_stringref.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/catch2/internal/catch_stringref.cpp b/src/catch2/internal/catch_stringref.cpp
index 36a6d246..0aa535db 100644
--- a/src/catch2/internal/catch_stringref.cpp
+++ b/src/catch2/internal/catch_stringref.cpp
@@ -29,6 +29,27 @@ namespace Catch {
return strncmp(m_start, rhs.m_start, rhs.m_size) < 0;
}
+ int StringRef::compare( StringRef rhs ) const {
+ auto cmpResult =
+ strncmp( m_start, rhs.m_start, std::min( m_size, rhs.m_size ) );
+
+ // This means that strncmp found a difference before the strings
+ // ended, and we can return it directly
+ if ( cmpResult != 0 ) {
+ return cmpResult;
+ }
+
+ // If strings are equal up to length, then their comparison results on
+ // their size
+ if ( m_size < rhs.m_size ) {
+ return -1;
+ } else if ( m_size > rhs.m_size ) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+
auto operator << ( std::ostream& os, StringRef const& str ) -> std::ostream& {
return os.write(str.data(), str.size());
}