catch_wildcard_pattern.cpp
1 2 // Copyright Catch2 Authors 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE.txt or copy at 5 // https://www.boost.org/LICENSE_1_0.txt) 6 7 // SPDX-License-Identifier: BSL-1.0 8 #include <catch2/internal/catch_wildcard_pattern.hpp> 9 #include <catch2/internal/catch_enforce.hpp> 10 #include <catch2/internal/catch_string_manip.hpp> 11 12 namespace Catch { 13 14 WildcardPattern::WildcardPattern( std::string const& pattern, 15 CaseSensitive caseSensitivity ) 16 : m_caseSensitivity( caseSensitivity ), 17 m_pattern( normaliseString( pattern ) ) 18 { 19 if( startsWith( m_pattern, '*' ) ) { 20 m_pattern = m_pattern.substr( 1 ); 21 m_wildcard = WildcardAtStart; 22 } 23 if( endsWith( m_pattern, '*' ) ) { 24 m_pattern = m_pattern.substr( 0, m_pattern.size()-1 ); 25 m_wildcard = static_cast<WildcardPosition>( m_wildcard | WildcardAtEnd ); 26 } 27 } 28 29 bool WildcardPattern::matches( std::string const& str ) const { 30 switch( m_wildcard ) { 31 case NoWildcard: 32 return m_pattern == normaliseString( str ); 33 case WildcardAtStart: 34 return endsWith( normaliseString( str ), m_pattern ); 35 case WildcardAtEnd: 36 return startsWith( normaliseString( str ), m_pattern ); 37 case WildcardAtBothEnds: 38 return contains( normaliseString( str ), m_pattern ); 39 default: 40 CATCH_INTERNAL_ERROR( "Unknown enum" ); 41 } 42 } 43 44 std::string WildcardPattern::normaliseString( std::string const& str ) const { 45 return trim( m_caseSensitivity == CaseSensitive::No ? toLower( str ) : str ); 46 } 47 }