-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
catch_stringref.hpp
110 lines (85 loc) · 3.55 KB
/
catch_stringref.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
#ifndef CATCH_STRINGREF_HPP_INCLUDED
#define CATCH_STRINGREF_HPP_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;
}
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;
}
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);
friend std::string operator+(StringRef lhs, StringRef rhs);
};
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_HPP_INCLUDED