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

summaryrefslogtreecommitdiffhomepage
path: root/src/Progmasoft/Catch3/Matchers/String.cpp
blob: 651f67e6c18fe7cdc08586a63a18732e36f7fd34 (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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

//              Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
//   (See accompanying file LICENSE.txt or copy at
//        https://www.boost.org/LICENSE_1_0.txt)

// SPDX-License-Identifier: BSL-1.0
#include <Progmasoft/Catch3/Matchers/String.hpp>
#include <Progmasoft/Catch3/Internal/StringManip.hpp>
#include <Progmasoft/Catch3/ToString.hpp>
#include <Progmasoft/Catch3/Internal/MoveAndForward.hpp>
#include <Progmasoft/Catch3/Internal/CaseInsensitiveComparisons.hpp>

#include <regex>

namespace Catch {

    namespace {
        constexpr StringRef caseSensitivitySuffix( CaseSensitive caseSensitivity ) {
            return caseSensitivity == CaseSensitive::Yes
                       ? StringRef{}
                       : " (case insensitive)"_sr;
        }
    } // namespace

namespace Matchers {

    StringMatcherBase::StringMatcherBase( std::string target,
                                          StringRef operation,
                                          CaseSensitive caseSensitivity ):
        m_target( CATCH_MOVE( target ) ),
        m_operation( operation ),
        m_caseSensitivity( caseSensitivity ) {}


    std::string StringMatcherBase::describe() const {
        std::string description;
        description.reserve(5 + m_operation.size() + m_target.size() +
                                    caseSensitivitySuffix(m_caseSensitivity).size());
        description += m_operation;
        description += ": \"";
        description += m_target;
        description += '"';
        description += caseSensitivitySuffix(m_caseSensitivity);
        return description;
    }


    StringEqualsMatcher::StringEqualsMatcher( std::string comparator, CaseSensitive caseSensitivity ):
        StringMatcherBase( CATCH_MOVE( comparator ), "equals"_sr, caseSensitivity ) {}

    bool StringEqualsMatcher::match( std::string const& source ) const {
        if (m_caseSensitivity == CaseSensitive::Yes) {
            return m_target == source;
        }
        if (m_target.size() != source.size()) { return false; }
        Catch::Detail::CaseInsensitiveEqualTo eq;
        return eq( m_target, source );
    }


    StringContainsMatcher::StringContainsMatcher(
        std::string comparator, CaseSensitive caseSensitivity ):
        StringMatcherBase( CATCH_MOVE( comparator ), "contains"_sr, caseSensitivity ) {}

    bool StringContainsMatcher::match( std::string const& source ) const {
        if ( m_caseSensitivity == CaseSensitive::Yes ) {
            return contains( source, m_target );
        }
        if ( source.size() < m_target.size() ) { return false; }
        StringRef as_ref( source );
        // The worst case of this is O(m*n), which is terrible, BUT:
        //  * The average case is much better, the worst case only happens rarely
        //  * We can implement BMH/other better searchers later if it matters
        Catch::Detail::CaseInsensitiveEqualTo eq;
        for (size_t i = 0; i < source.size(); ++i) {
            const auto substr = as_ref.substr( i, m_target.size() );
            bool found = eq( substr, m_target );
            if ( found ) { return true; }
        }
        return false;
    }


    StartsWithMatcher::StartsWithMatcher( std::string comparator,
                                          CaseSensitive caseSensitivity ):
        StringMatcherBase( CATCH_MOVE( comparator ), "starts with"_sr, caseSensitivity ) {}

    bool StartsWithMatcher::match( std::string const& source ) const {
        if ( m_caseSensitivity == CaseSensitive::Yes ) {
            return startsWith( source, m_target );
        }
        if (source.size() < m_target.size()) { return false; }
        Catch::Detail::CaseInsensitiveEqualTo eq;
        return eq(
            StringRef( source ).substr( 0, m_target.size() ), m_target );
    }


    EndsWithMatcher::EndsWithMatcher( std::string comparator,
                                      CaseSensitive caseSensitivity ):
        StringMatcherBase( CATCH_MOVE( comparator ), "ends with"_sr, caseSensitivity ) {}

    bool EndsWithMatcher::match( std::string const& source ) const {
        if ( m_caseSensitivity == CaseSensitive::Yes ) {
            return endsWith( source, m_target );
        }
        if ( source.size() < m_target.size() ) { return false; }
        Catch::Detail::CaseInsensitiveEqualTo eq;
        const size_t start_point = source.size() - m_target.size();
        return eq( StringRef( source ).substr( start_point, m_target.size() ), m_target );
    }



    RegexMatcher::RegexMatcher(std::string regex, CaseSensitive caseSensitivity): m_regex(CATCH_MOVE(regex)), m_caseSensitivity(caseSensitivity) {}

    bool RegexMatcher::match(std::string const& matchee) const {
        auto flags = std::regex::ECMAScript; // ECMAScript is the default syntax option anyway
        if (m_caseSensitivity == CaseSensitive::No) {
            flags |= std::regex::icase;
        }
        auto reg = std::regex(m_regex, flags);
        return std::regex_match(matchee, reg);
    }

    std::string RegexMatcher::describe() const {
        return "matches " + ::Catch::Detail::stringify(m_regex) + ((m_caseSensitivity == CaseSensitive::Yes)? " case sensitively" : " case insensitively");
    }


    StringEqualsMatcher Equals( std::string str, CaseSensitive caseSensitivity ) {
        return StringEqualsMatcher( CATCH_MOVE( str ), caseSensitivity );
    }
    StringContainsMatcher ContainsSubstring( std::string str, CaseSensitive caseSensitivity ) {
        return StringContainsMatcher( CATCH_MOVE( str ), caseSensitivity );
    }
    EndsWithMatcher EndsWith( std::string str, CaseSensitive caseSensitivity ) {
        return EndsWithMatcher( CATCH_MOVE( str ), caseSensitivity );
    }
    StartsWithMatcher StartsWith( std::string str, CaseSensitive caseSensitivity ) {
        return StartsWithMatcher( CATCH_MOVE( str ), caseSensitivity );
    }

    RegexMatcher Matches(std::string regex, CaseSensitive caseSensitivity) {
        return RegexMatcher( CATCH_MOVE( regex ), caseSensitivity );
    }

} // namespace Matchers
} // namespace Catch