• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 ///////////////////////////////////////////////////////////////////////////////
2 // string_matcher.hpp
3 //
4 //  Copyright 2008 Eric Niebler. Distributed under the Boost
5 //  Software License, Version 1.0. (See accompanying file
6 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 
8 #ifndef BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_STRING_MATCHER_HPP_EAN_10_04_2005
9 #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_STRING_MATCHER_HPP_EAN_10_04_2005
10 
11 // MS compatible compilers support #pragma once
12 #if defined(_MSC_VER)
13 # pragma once
14 #endif
15 
16 #include <string>
17 #include <boost/mpl/bool.hpp>
18 #include <boost/xpressive/detail/detail_fwd.hpp>
19 #include <boost/xpressive/detail/core/quant_style.hpp>
20 #include <boost/xpressive/detail/core/state.hpp>
21 #include <boost/xpressive/detail/utility/algorithm.hpp>
22 #include <boost/xpressive/detail/utility/traits_utils.hpp>
23 
24 namespace boost { namespace xpressive { namespace detail
25 {
26     ///////////////////////////////////////////////////////////////////////////////
27     // string_matcher
28     //
29     template<typename Traits, typename ICase>
30     struct string_matcher
31       : quant_style_fixed_unknown_width
32     {
33         typedef typename Traits::char_type char_type;
34         typedef typename Traits::string_type string_type;
35         typedef ICase icase_type;
36         string_type str_;
37         char_type const *end_;
38 
string_matcherboost::xpressive::detail::string_matcher39         string_matcher(string_type const &str, Traits const &tr)
40           : str_(str)
41           , end_()
42         {
43             typename range_iterator<string_type>::type cur = boost::begin(this->str_);
44             typename range_iterator<string_type>::type end = boost::end(this->str_);
45             for(; cur != end; ++cur)
46             {
47                 *cur = detail::translate(*cur, tr, icase_type());
48             }
49             this->end_ = detail::data_end(str_);
50         }
51 
string_matcherboost::xpressive::detail::string_matcher52         string_matcher(string_matcher<Traits, ICase> const &that)
53           : str_(that.str_)
54           , end_(detail::data_end(str_))
55         {
56         }
57 
58         template<typename BidiIter, typename Next>
matchboost::xpressive::detail::string_matcher59         bool match(match_state<BidiIter> &state, Next const &next) const
60         {
61             BidiIter const tmp = state.cur_;
62             char_type const *begin = detail::data_begin(this->str_);
63             for(; begin != this->end_; ++begin, ++state.cur_)
64             {
65                 if(state.eos() ||
66                     (detail::translate(*state.cur_, traits_cast<Traits>(state), icase_type()) != *begin))
67                 {
68                     state.cur_ = tmp;
69                     return false;
70                 }
71             }
72 
73             if(next.match(state))
74             {
75                 return true;
76             }
77 
78             state.cur_ = tmp;
79             return false;
80         }
81 
get_widthboost::xpressive::detail::string_matcher82         detail::width get_width() const
83         {
84             return boost::size(this->str_);
85         }
86     };
87 
88 }}}
89 
90 #endif
91