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

summaryrefslogtreecommitdiffhomepage
path: root/include/internal/catch_stringref.h
blob: 5870151c3b82385f704748f15d3f0b7c5bfc5743 (plain)
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
/*
 *  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>

namespace Catch {
    
    class StringData;

    /// 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. c_str() must return a null terminated
    /// string, however, and so the StringRef will internally take ownership
    /// (taking a copy), if necessary. In theory this ownership is not externally
    /// visible - but it does mean (substring) StringRefs should not be shared between
    /// threads.
    class StringRef {
        friend struct StringRefTestAccess;

        using size_type = size_t;
        
        char const* m_start;
        size_type m_size;
        
        char* m_data = nullptr;
        
        void takeOwnership();
        
    public: // construction/ assignment
        StringRef() noexcept;
        StringRef( StringRef const& other ) noexcept;
        StringRef( StringRef&& other ) noexcept;
        StringRef( char const* rawChars ) noexcept;
        StringRef( char const* rawChars, size_type size ) noexcept;
        StringRef( std::string const& stdString ) noexcept;
        ~StringRef() noexcept;
        
        auto operator = ( StringRef other ) noexcept -> StringRef&;
        operator std::string() const;

        void swap( StringRef& other ) noexcept;
        
    public: // operators
        auto operator == ( StringRef const& other ) const noexcept -> bool;
        auto operator != ( StringRef const& other ) const noexcept -> bool;
        
        auto operator[] ( size_type index ) const noexcept -> char;
        
    public: // named queries
        auto empty() const noexcept -> bool;
        auto size() const noexcept -> size_type;
        auto numberOfCharacters() const noexcept -> size_type;
        auto c_str() const -> char const*;
        
    public: // substrings and searches
        auto substr( size_type start, size_type size ) const noexcept -> StringRef;

    private: // ownership queries - may not be consistent between calls
        auto isOwned() const noexcept -> bool;
        auto isSubstring() const noexcept -> bool;
        auto data() const noexcept -> char const*;
    };

    auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> std::string;
    auto operator + ( StringRef const& lhs, char const* rhs ) -> std::string;
    auto operator + ( char const* lhs, StringRef const& rhs ) -> std::string;

    auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&;

} // namespace Catch

#endif // CATCH_STRINGREF_H_INCLUDED