📄 finder.hpp
字号:
// Boost string_algo library finder.hpp header file ---------------------------//// Copyright Pavol Droba 2002-2003. Use, modification and// distribution is subject to 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)// See http://www.boost.org for updates, documentation, and revision history.#ifndef BOOST_STRING_FINDER_DETAIL_HPP#define BOOST_STRING_FINDER_DETAIL_HPP#include <boost/algorithm/string/config.hpp>#include <boost/algorithm/string/constants.hpp>#include <boost/detail/iterator.hpp>#include <boost/algorithm/string/collection_traits.hpp>#include <boost/algorithm/string/iterator_range.hpp>namespace boost { namespace algorithm { namespace detail {// find first functor -----------------------------------------------// // find a subsequence in the sequence ( functor ) /* Returns a pair <begin,end> marking the subsequence in the sequence. If the find fails, functor returns <End,End> */ template<typename SearchIteratorT,typename PredicateT> struct first_finderF { typedef SearchIteratorT search_iterator_type; // Construction template< typename SearchT > first_finderF( const SearchT& Search, PredicateT Comp ) : m_Search(begin(Search), end(Search)), m_Comp(Comp) {} first_finderF( search_iterator_type SearchBegin, search_iterator_type SearchEnd, PredicateT Comp ) : m_Search(SearchBegin, SearchEnd), m_Comp(Comp) {} // Operation template< typename ForwardIteratorT > iterator_range<ForwardIteratorT> operator()( ForwardIteratorT Begin, ForwardIteratorT End ) const { typedef iterator_range<ForwardIteratorT> result_type; typedef ForwardIteratorT input_iterator_type; // Outer loop for(input_iterator_type OuterIt=Begin; OuterIt!=End; ++OuterIt) { // Sanity check if( empty(m_Search) ) return result_type( End, End ); input_iterator_type InnerIt=OuterIt; search_iterator_type SubstrIt=m_Search.begin(); for(; InnerIt!=End && SubstrIt!=m_Search.end(); ++InnerIt,++SubstrIt) { if( !( m_Comp(*InnerIt,*SubstrIt) ) ) break; } // Substring matching succeeded if ( SubstrIt==m_Search.end() ) return result_type( OuterIt, InnerIt ); } return result_type( End, End ); } private: iterator_range<search_iterator_type> m_Search; PredicateT m_Comp; };// find last functor -----------------------------------------------// // find the last match a subsequnce in the sequence ( functor ) /* Returns a pair <begin,end> marking the subsequence in the sequence. If the find fails, returns <End,End> */ template<typename SearchIteratorT, typename PredicateT> struct last_finderF { typedef SearchIteratorT search_iterator_type; typedef first_finderF< search_iterator_type, PredicateT> first_finder_type; // Construction template< typename SearchT > last_finderF( const SearchT& Search, PredicateT Comp ) : m_Search(begin(Search), end(Search)), m_Comp(Comp) {} last_finderF( search_iterator_type SearchBegin, search_iterator_type SearchEnd, PredicateT Comp ) : m_Search(SearchBegin, SearchEnd), m_Comp(Comp) {} // Operation template< typename ForwardIteratorT > iterator_range<ForwardIteratorT> operator()( ForwardIteratorT Begin, ForwardIteratorT End ) const { typedef iterator_range<ForwardIteratorT> result_type; if( empty(m_Search) ) return result_type( End, End ); typedef BOOST_STRING_TYPENAME boost::detail:: iterator_traits<ForwardIteratorT>::iterator_category category; return findit( Begin, End, category() ); } private: // forward iterator template< typename ForwardIteratorT > iterator_range<ForwardIteratorT> findit( ForwardIteratorT Begin, ForwardIteratorT End, std::forward_iterator_tag ) const { typedef ForwardIteratorT input_iterator_type; typedef iterator_range<ForwardIteratorT> result_type; first_finder_type first_finder( m_Search.begin(), m_Search.end(), m_Comp ); result_type M=first_finder( Begin, End ); result_type Last=M; while( M ) { Last=M; M=first_finder( end(M), End ); } return Last; } // bidirectional iterator template< typename ForwardIteratorT > iterator_range<ForwardIteratorT> findit( ForwardIteratorT Begin, ForwardIteratorT End, std::bidirectional_iterator_tag ) const { typedef iterator_range<ForwardIteratorT> result_type; typedef ForwardIteratorT input_iterator_type; // Outer loop for(input_iterator_type OuterIt=End; OuterIt!=Begin; ) { input_iterator_type OuterIt2=--OuterIt; input_iterator_type InnerIt=OuterIt2; search_iterator_type SubstrIt=m_Search.begin(); for(; InnerIt!=End && SubstrIt!=m_Search.end(); ++InnerIt,++SubstrIt) { if( !( m_Comp(*InnerIt,*SubstrIt) ) ) break; } // Substring matching succeeded if( SubstrIt==m_Search.end() ) return result_type( OuterIt2, InnerIt ); } return result_type( End, End ); } private: iterator_range<search_iterator_type> m_Search; PredicateT m_Comp; }; // find n-th functor -----------------------------------------------// // find the n-th match of a subsequnce in the sequence ( functor ) /* Returns a pair <begin,end> marking the subsequence in the sequence. If the find fails, returns <End,End> */ template<typename SearchIteratorT, typename PredicateT> struct nth_finderF { typedef SearchIteratorT search_iterator_type; typedef first_finderF< search_iterator_type, PredicateT> first_finder_type; // Construction template< typename SearchT > nth_finderF( const SearchT& Search, unsigned int Nth, PredicateT Comp) : m_Search(begin(Search), end(Search)), m_Nth(Nth), m_Comp(Comp) {} nth_finderF( search_iterator_type SearchBegin, search_iterator_type SearchEnd, unsigned int Nth, PredicateT Comp) : m_Search(SearchBegin, SearchEnd), m_Nth(Nth), m_Comp(Comp) {} // Operation template< typename ForwardIteratorT > iterator_range<ForwardIteratorT> operator()( ForwardIteratorT Begin, ForwardIteratorT End ) const { typedef ForwardIteratorT input_iterator_type; typedef iterator_range<ForwardIteratorT> result_type; // Sanity check if( empty(m_Search) ) return result_type( End, End ); // Instantiate find funtor first_finder_type first_finder( m_Search.begin(), m_Search.end(), m_Comp ); result_type M( Begin, Begin ); for( unsigned int n=0; n<=m_Nth; ++n ) { // find next match M=first_finder( end(M), End ); if ( !M ) { // Subsequence not found, return return M; } } return M; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -