diff options
| author | Martin Hořeňovský | 2020-05-10 10:09:01 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2020-05-10 10:09:01 +0200 |
| commit | 9e498278be92ff209687a09b989f27d51aab7809 (patch) | |
| tree | 5d077a9dd6239cc228034ad644d3df47d34bf3d2 /src/catch2/internal | |
| parent | 14533f5bb6bb0b35036e1ae6fd9571463ee05902 (diff) | |
| download | catch3-9e498278be92ff209687a09b989f27d51aab7809.tar.gz | |
Move StringRef header to internals
Diffstat (limited to 'src/catch2/internal')
| -rw-r--r-- | src/catch2/internal/catch_decomposer.hpp | 2 | ||||
| -rw-r--r-- | src/catch2/internal/catch_list.hpp | 2 | ||||
| -rw-r--r-- | src/catch2/internal/catch_stream.cpp | 2 | ||||
| -rw-r--r-- | src/catch2/internal/catch_string_manip.cpp | 2 | ||||
| -rw-r--r-- | src/catch2/internal/catch_string_manip.hpp | 2 | ||||
| -rw-r--r-- | src/catch2/internal/catch_stringref.cpp | 2 | ||||
| -rw-r--r-- | src/catch2/internal/catch_stringref.hpp | 116 | ||||
| -rw-r--r-- | src/catch2/internal/catch_test_macro_impl.hpp | 2 | ||||
| -rw-r--r-- | src/catch2/internal/catch_test_registry.hpp | 2 |
9 files changed, 124 insertions, 8 deletions
diff --git a/src/catch2/internal/catch_decomposer.hpp b/src/catch2/internal/catch_decomposer.hpp index 6ed828b9..4c90091c 100644 --- a/src/catch2/internal/catch_decomposer.hpp +++ b/src/catch2/internal/catch_decomposer.hpp @@ -9,7 +9,7 @@ #define TWOBLUECUBES_CATCH_DECOMPOSER_H_INCLUDED #include <catch2/catch_tostring.hpp> -#include <catch2/catch_stringref.hpp> +#include <catch2/internal/catch_stringref.hpp> #include <catch2/internal/catch_meta.hpp> #include <iosfwd> diff --git a/src/catch2/internal/catch_list.hpp b/src/catch2/internal/catch_list.hpp index 14abea77..88e4c2c8 100644 --- a/src/catch2/internal/catch_list.hpp +++ b/src/catch2/internal/catch_list.hpp @@ -9,7 +9,7 @@ #define TWOBLUECUBES_CATCH_LIST_H_INCLUDED #include <catch2/catch_config.hpp> -#include <catch2/catch_stringref.hpp> +#include <catch2/internal/catch_stringref.hpp> #include <set> #include <string> diff --git a/src/catch2/internal/catch_stream.cpp b/src/catch2/internal/catch_stream.cpp index 5a29441b..809acc0b 100644 --- a/src/catch2/internal/catch_stream.cpp +++ b/src/catch2/internal/catch_stream.cpp @@ -11,7 +11,7 @@ #include <catch2/internal/catch_enforce.hpp> #include <catch2/internal/catch_stream.hpp> #include <catch2/internal/catch_debug_console.hpp> -#include <catch2/catch_stringref.hpp> +#include <catch2/internal/catch_stringref.hpp> #include <catch2/internal/catch_singletons.hpp> #include <cstdio> diff --git a/src/catch2/internal/catch_string_manip.cpp b/src/catch2/internal/catch_string_manip.cpp index 4e9322da..b24ec880 100644 --- a/src/catch2/internal/catch_string_manip.cpp +++ b/src/catch2/internal/catch_string_manip.cpp @@ -6,7 +6,7 @@ */ #include <catch2/internal/catch_string_manip.hpp> -#include <catch2/catch_stringref.hpp> +#include <catch2/internal/catch_stringref.hpp> #include <algorithm> #include <ostream> diff --git a/src/catch2/internal/catch_string_manip.hpp b/src/catch2/internal/catch_string_manip.hpp index f0b36a69..8570a8c0 100644 --- a/src/catch2/internal/catch_string_manip.hpp +++ b/src/catch2/internal/catch_string_manip.hpp @@ -7,7 +7,7 @@ #ifndef TWOBLUECUBES_CATCH_STRING_MANIP_H_INCLUDED #define TWOBLUECUBES_CATCH_STRING_MANIP_H_INCLUDED -#include <catch2/catch_stringref.hpp> +#include <catch2/internal/catch_stringref.hpp> #include <string> #include <iosfwd> diff --git a/src/catch2/internal/catch_stringref.cpp b/src/catch2/internal/catch_stringref.cpp index 53e224eb..4b0bbae6 100644 --- a/src/catch2/internal/catch_stringref.cpp +++ b/src/catch2/internal/catch_stringref.cpp @@ -6,7 +6,7 @@ */ #include <catch2/internal/catch_enforce.hpp> -#include <catch2/catch_stringref.hpp> +#include <catch2/internal/catch_stringref.hpp> #include <algorithm> #include <ostream> diff --git a/src/catch2/internal/catch_stringref.hpp b/src/catch2/internal/catch_stringref.hpp new file mode 100644 index 00000000..1e07b6ad --- /dev/null +++ b/src/catch2/internal/catch_stringref.hpp @@ -0,0 +1,116 @@ +/* + * 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 + +#include <cstddef> +#include <string> +#include <iosfwd> +#include <cassert> + +namespace Catch { + + /// A non-owning string class (similar to the forthcoming std::string_view) + /// Note that, because a StringRef may be a substring of another string, + /// it may not be null terminated. + class StringRef { + public: + using size_type = std::size_t; + using const_iterator = const char*; + + private: + static constexpr char const* const s_empty = ""; + + char const* m_start = s_empty; + size_type m_size = 0; + + public: // construction + constexpr StringRef() noexcept = default; + + StringRef( char const* rawChars ) noexcept; + + 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); + } + + public: // operators + auto operator == ( StringRef const& other ) const noexcept -> bool; + auto operator != (StringRef const& other) const noexcept -> bool { + return !(*this == other); + } + + constexpr auto operator[] ( size_type index ) const noexcept -> char { + assert(index < m_size); + return m_start[index]; + } + + bool operator<(StringRef const& rhs) const noexcept; + + public: // named queries + constexpr auto empty() const noexcept -> bool { + return m_size == 0; + } + constexpr auto size() const noexcept -> size_type { + return m_size; + } + + // Returns the current start pointer. If the StringRef is not + // null-terminated, throws std::domain_exception + auto c_str() const -> char const*; + + public: // substrings and searches + // Returns a substring of [start, start + length). + // If start + length > size(), then the substring is [start, start + size()). + // If start > size(), then the substring is empty. + 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. + constexpr char const* data() const noexcept { + return m_start; + } + + constexpr auto isNullTerminated() const noexcept -> bool { + return m_start[m_size] == '\0'; + } + + public: // iterators + constexpr const_iterator begin() const { return m_start; } + constexpr const_iterator end() const { return m_start + m_size; } + + + friend std::string& operator += (std::string& lhs, StringRef const& sr); + friend std::ostream& operator << (std::ostream& os, StringRef const& sr); + }; + + + constexpr auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef { + return StringRef( rawChars, size ); + } +} // namespace Catch + +constexpr auto operator "" _catch_sr( char const* rawChars, std::size_t size ) noexcept -> Catch::StringRef { + return Catch::StringRef( rawChars, size ); +} + +#endif // CATCH_STRINGREF_H_INCLUDED diff --git a/src/catch2/internal/catch_test_macro_impl.hpp b/src/catch2/internal/catch_test_macro_impl.hpp index ffb767ed..effd5884 100644 --- a/src/catch2/internal/catch_test_macro_impl.hpp +++ b/src/catch2/internal/catch_test_macro_impl.hpp @@ -10,7 +10,7 @@ #include <catch2/internal/catch_assertionhandler.hpp> #include <catch2/interfaces/catch_interfaces_capture.hpp> -#include <catch2/catch_stringref.hpp> +#include <catch2/internal/catch_stringref.hpp> // We need this suppression to leak, because it took until GCC 9 // for the front end to handle local suppression via _Pragma properly diff --git a/src/catch2/internal/catch_test_registry.hpp b/src/catch2/internal/catch_test_registry.hpp index def1de2d..8a027bd9 100644 --- a/src/catch2/internal/catch_test_registry.hpp +++ b/src/catch2/internal/catch_test_registry.hpp @@ -11,7 +11,7 @@ #include <catch2/internal/catch_common.hpp> #include <catch2/interfaces/catch_interfaces_testcase.hpp> #include <catch2/internal/catch_compiler_capabilities.hpp> -#include <catch2/catch_stringref.hpp> +#include <catch2/internal/catch_stringref.hpp> #include <memory> |