1 /* 2 * Created by Phil on 13/7/2015. 3 * Copyright 2015 Two Blue Cubes Ltd. All rights reserved. 4 * 5 * Distributed under the Boost Software License, Version 1.0. (See accompanying 6 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 7 */ 8 #ifndef TWOBLUECUBES_CATCH_WILDCARD_PATTERN_HPP_INCLUDED 9 #define TWOBLUECUBES_CATCH_WILDCARD_PATTERN_HPP_INCLUDED 10 11 #include "catch_common.h" 12 13 #include <stdexcept> 14 15 16 namespace Catch 17 { 18 class WildcardPattern { 19 enum WildcardPosition { 20 NoWildcard = 0, 21 WildcardAtStart = 1, 22 WildcardAtEnd = 2, 23 WildcardAtBothEnds = WildcardAtStart | WildcardAtEnd 24 }; 25 26 public: 27 WildcardPattern(std::string const & pattern,CaseSensitive::Choice caseSensitivity)28 WildcardPattern( std::string const& pattern, CaseSensitive::Choice caseSensitivity ) 29 : m_caseSensitivity( caseSensitivity ), 30 m_wildcard( NoWildcard ), 31 m_pattern( adjustCase( pattern ) ) 32 { 33 if( startsWith( m_pattern, '*' ) ) { 34 m_pattern = m_pattern.substr( 1 ); 35 m_wildcard = WildcardAtStart; 36 } 37 if( endsWith( m_pattern, '*' ) ) { 38 m_pattern = m_pattern.substr( 0, m_pattern.size()-1 ); 39 m_wildcard = static_cast<WildcardPosition>( m_wildcard | WildcardAtEnd ); 40 } 41 } 42 virtual ~WildcardPattern(); matches(std::string const & str) const43 virtual bool matches( std::string const& str ) const { 44 switch( m_wildcard ) { 45 case NoWildcard: 46 return m_pattern == adjustCase( str ); 47 case WildcardAtStart: 48 return endsWith( adjustCase( str ), m_pattern ); 49 case WildcardAtEnd: 50 return startsWith( adjustCase( str ), m_pattern ); 51 case WildcardAtBothEnds: 52 return contains( adjustCase( str ), m_pattern ); 53 } 54 55 #ifdef __clang__ 56 #pragma clang diagnostic push 57 #pragma clang diagnostic ignored "-Wunreachable-code" 58 #endif 59 throw std::logic_error( "Unknown enum" ); 60 #ifdef __clang__ 61 #pragma clang diagnostic pop 62 #endif 63 } 64 private: adjustCase(std::string const & str) const65 std::string adjustCase( std::string const& str ) const { 66 return m_caseSensitivity == CaseSensitive::No ? toLower( str ) : str; 67 } 68 CaseSensitive::Choice m_caseSensitivity; 69 WildcardPosition m_wildcard; 70 std::string m_pattern; 71 }; 72 } 73 74 #endif // TWOBLUECUBES_CATCH_WILDCARD_PATTERN_HPP_INCLUDED 75