1 /* 2 * Created by Martin on 19/07/2017. 3 * 4 * Distributed under the Boost Software License, Version 1.0. (See accompanying 5 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 */ 7 8 #include "catch_wildcard_pattern.h" 9 #include "catch_enforce.h" 10 #include "catch_string_manip.h" 11 12 #include <sstream> 13 14 namespace Catch { 15 WildcardPattern(std::string const & pattern,CaseSensitive::Choice caseSensitivity)16 WildcardPattern::WildcardPattern( std::string const& pattern, 17 CaseSensitive::Choice caseSensitivity ) 18 : m_caseSensitivity( caseSensitivity ), 19 m_pattern( adjustCase( pattern ) ) 20 { 21 if( startsWith( m_pattern, '*' ) ) { 22 m_pattern = m_pattern.substr( 1 ); 23 m_wildcard = WildcardAtStart; 24 } 25 if( endsWith( m_pattern, '*' ) ) { 26 m_pattern = m_pattern.substr( 0, m_pattern.size()-1 ); 27 m_wildcard = static_cast<WildcardPosition>( m_wildcard | WildcardAtEnd ); 28 } 29 } 30 matches(std::string const & str) const31 bool WildcardPattern::matches( std::string const& str ) const { 32 switch( m_wildcard ) { 33 case NoWildcard: 34 return m_pattern == adjustCase( str ); 35 case WildcardAtStart: 36 return endsWith( adjustCase( str ), m_pattern ); 37 case WildcardAtEnd: 38 return startsWith( adjustCase( str ), m_pattern ); 39 case WildcardAtBothEnds: 40 return contains( adjustCase( str ), m_pattern ); 41 default: 42 CATCH_INTERNAL_ERROR( "Unknown enum" ); 43 } 44 } 45 adjustCase(std::string const & str) const46 std::string WildcardPattern::adjustCase( std::string const& str ) const { 47 return m_caseSensitivity == CaseSensitive::No ? toLower( str ) : str; 48 } 49 } 50