File Coverage

/usr/local/lib/perl5/site_perl/5.26.1/x86_64-linux/XS/libcatch.x/i/catch2/internal/catch_stringref.hpp
Criterion Covered Total %
statement 2 8 25.0
branch n/a
condition n/a
subroutine n/a
pod n/a
total 2 8 25.0


line stmt bran cond sub pod time code
1              
2             // Copyright Catch2 Authors
3             // Distributed under the Boost Software License, Version 1.0.
4             // (See accompanying file LICENSE_1_0.txt or copy at
5             // https://www.boost.org/LICENSE_1_0.txt)
6              
7             // SPDX-License-Identifier: BSL-1.0
8             #ifndef CATCH_STRINGREF_HPP_INCLUDED
9             #define CATCH_STRINGREF_HPP_INCLUDED
10              
11             #include
12             #include
13             #include
14             #include
15              
16             namespace Catch {
17              
18             /// A non-owning string class (similar to the forthcoming std::string_view)
19             /// Note that, because a StringRef may be a substring of another string,
20             /// it may not be null terminated.
21             class StringRef {
22             public:
23             using size_type = std::size_t;
24             using const_iterator = const char*;
25              
26             private:
27             static constexpr char const* const s_empty = "";
28              
29             char const* m_start = s_empty;
30             size_type m_size = 0;
31              
32             public: // construction
33 0           constexpr StringRef() noexcept = default;
34              
35             StringRef( char const* rawChars ) noexcept;
36              
37 0           constexpr StringRef( char const* rawChars, size_type size ) noexcept
38             : m_start( rawChars ),
39 0           m_size( size )
40 0           {}
41              
42             StringRef( std::string const& stdString ) noexcept
43             : m_start( stdString.c_str() ),
44             m_size( stdString.size() )
45             {}
46              
47             explicit operator std::string() const {
48             return std::string(m_start, m_size);
49             }
50              
51             public: // operators
52             auto operator == ( StringRef const& other ) const noexcept -> bool;
53             auto operator != (StringRef const& other) const noexcept -> bool {
54             return !(*this == other);
55             }
56              
57             constexpr auto operator[] ( size_type index ) const noexcept -> char {
58             assert(index < m_size);
59             return m_start[index];
60             }
61              
62             bool operator<(StringRef const& rhs) const noexcept;
63              
64             public: // named queries
65             constexpr auto empty() const noexcept -> bool {
66             return m_size == 0;
67             }
68             constexpr auto size() const noexcept -> size_type {
69             return m_size;
70             }
71              
72             // Returns a substring of [start, start + length).
73             // If start + length > size(), then the substring is [start, start + size()).
74             // If start > size(), then the substring is empty.
75             constexpr StringRef substr(size_type start, size_type length) const noexcept {
76             if (start < m_size) {
77             const auto shortened_size = m_size - start;
78             return StringRef(m_start + start, (shortened_size < length) ? shortened_size : length);
79             } else {
80             return StringRef();
81             }
82             }
83              
84             // Returns the current start pointer. May not be null-terminated.
85             constexpr char const* data() const noexcept {
86             return m_start;
87             }
88              
89             constexpr const_iterator begin() const { return m_start; }
90             constexpr const_iterator end() const { return m_start + m_size; }
91              
92              
93             friend std::string& operator += (std::string& lhs, StringRef const& sr);
94             friend std::ostream& operator << (std::ostream& os, StringRef const& sr);
95             friend std::string operator+(StringRef lhs, StringRef rhs);
96              
97             /**
98             * Provides a three-way comparison with rhs
99             *
100             * Returns negative number if lhs < rhs, 0 if lhs == rhs, and a positive
101             * number if lhs > rhs
102             */
103             int compare( StringRef rhs ) const;
104             };
105              
106              
107 0           constexpr auto operator ""_sr( char const* rawChars, std::size_t size ) noexcept -> StringRef {
108 0           return StringRef( rawChars, size );
109             }
110             } // namespace Catch
111              
112 50           constexpr auto operator ""_catch_sr( char const* rawChars, std::size_t size ) noexcept -> Catch::StringRef {
113 50           return Catch::StringRef( rawChars, size );
114             }
115              
116             #endif // CATCH_STRINGREF_HPP_INCLUDED