Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r56259 - in trunk/boost/spirit/home: qi/char support
From: hartmut.kaiser_at_[hidden]
Date: 2009-09-16 23:37:34


Author: hkaiser
Date: 2009-09-16 23:37:33 EDT (Wed, 16 Sep 2009)
New Revision: 56259
URL: http://svn.boost.org/trac/boost/changeset/56259

Log:
Spirit: added optimization avoiding call to ischar() if not necessary
Text files modified:
   trunk/boost/spirit/home/qi/char/char.hpp | 15 +++++++++------
   trunk/boost/spirit/home/qi/char/char_class.hpp | 2 +-
   trunk/boost/spirit/home/support/char_class.hpp | 39 +++++++++++++++++++++++++++++++++++++++
   3 files changed, 49 insertions(+), 7 deletions(-)

Modified: trunk/boost/spirit/home/qi/char/char.hpp
==============================================================================
--- trunk/boost/spirit/home/qi/char/char.hpp (original)
+++ trunk/boost/spirit/home/qi/char/char.hpp 2009-09-16 23:37:33 EDT (Wed, 16 Sep 2009)
@@ -121,7 +121,8 @@
         template <typename CharParam, typename Context>
         bool test(CharParam ch_, Context&) const
         {
- return char_encoding::ischar(int(ch_)) && ch == char_type(ch_);
+ return traits::ischar<CharParam, char_encoding>::call(ch_) &&
+ ch == char_type(ch_);
         }
 
         template <typename Context>
@@ -158,7 +159,7 @@
         template <typename CharParam, typename Context>
         bool test(CharParam ch_, Context&) const
         {
- if (!char_encoding::ischar(int(ch_)))
+ if (!traits::ischar<CharParam, char_encoding>::call(ch_))
                 return false;
 
             char_type ch = char_type(ch_); // optimize for token based parsing
@@ -190,7 +191,7 @@
         template <typename CharParam, typename Context>
         bool test(CharParam ch_, Context&) const
         {
- if (!char_encoding::ischar(int(ch_)))
+ if (!traits::ischar<CharParam, char_encoding>::call(ch_))
                 return false;
 
             char_type ch = char_type(ch_); // optimize for token based parsing
@@ -226,7 +227,7 @@
         template <typename CharParam, typename Context>
         bool test(CharParam ch_, Context&) const
         {
- if (!char_encoding::ischar(int(ch_)))
+ if (!traits::ischar<CharParam, char_encoding>::call(ch_))
                 return false;
 
             char_type ch = char_type(ch_); // optimize for token based parsing
@@ -294,7 +295,8 @@
         template <typename CharParam, typename Context>
         bool test(CharParam ch, Context&) const
         {
- return char_encoding::ischar(int(ch)) && chset.test(char_type(ch));
+ return traits::ischar<CharParam, char_encoding>::call(ch) &&
+ chset.test(char_type(ch));
         }
 
         template <typename Context>
@@ -353,7 +355,8 @@
         template <typename CharParam, typename Context>
         bool test(CharParam ch, Context&) const
         {
- return char_encoding::ischar(int(ch)) && chset.test(char_type(ch));
+ return traits::ischar<CharParam, char_encoding>::call(ch) &&
+ chset.test(char_type(ch));
         }
 
         template <typename Context>

Modified: trunk/boost/spirit/home/qi/char/char_class.hpp
==============================================================================
--- trunk/boost/spirit/home/qi/char/char_class.hpp (original)
+++ trunk/boost/spirit/home/qi/char/char_class.hpp 2009-09-16 23:37:33 EDT (Wed, 16 Sep 2009)
@@ -57,7 +57,7 @@
         bool test(CharParam ch, Context&) const
         {
             using spirit::char_class::classify;
- return char_encoding::ischar(int(ch)) &&
+ return traits::ischar<CharParam, char_encoding>::call(ch) &&
                    classify<char_encoding>::is(classification(), ch);
         }
 

Modified: trunk/boost/spirit/home/support/char_class.hpp
==============================================================================
--- trunk/boost/spirit/home/support/char_class.hpp (original)
+++ trunk/boost/spirit/home/support/char_class.hpp 2009-09-16 23:37:33 EDT (Wed, 16 Sep 2009)
@@ -16,6 +16,7 @@
 
 #include <boost/proto/proto.hpp>
 #include <boost/config.hpp>
+#include <boost/mpl/bool.hpp>
 #include <boost/spirit/home/support/unused.hpp>
 
 #if defined(BOOST_MSVC)
@@ -308,6 +309,44 @@
     };
 }}}
 
+namespace boost { namespace spirit { namespace traits
+{
+ ///////////////////////////////////////////////////////////////////////////
+ // This meta-function evaluates to mpl::true_ if the function
+ // char_encoding::ischar() needs to be called to ensure correct matching.
+ // This happens mainly if the character type returned from the underlying
+ // iterator is larger than the character type of the used character
+ // encoding. Additionally, this meta-function provides a customization
+ // point for the lexer library to enforce this behavior while parsing
+ // a token stream.
+ template <typename Char, typename BaseChar>
+ struct mustcheck_ischar
+ : mpl::bool_<(sizeof(Char) > sizeof(BaseChar)) ? true : false> {};
+
+ ///////////////////////////////////////////////////////////////////////////
+ // The following template calls char_encoding::ischar, if necessary
+ template <typename CharParam, typename CharEncoding
+ , bool MustCheck = mustcheck_ischar<
+ CharParam, typename CharEncoding::char_type>::value>
+ struct ischar
+ {
+ static bool call(CharParam)
+ {
+ return true;
+ }
+ };
+
+ template <typename CharParam, typename CharEncoding>
+ struct ischar<CharParam, CharEncoding, true>
+ {
+ static bool call(CharParam const& ch)
+ {
+ return CharEncoding::ischar(int(ch));
+ }
+ };
+
+}}}
+
 #if defined(BOOST_MSVC)
 # pragma warning(pop)
 #endif


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk