test_regex_search.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 497 行 · 第 1/2 页

HPP
497
字号
/* * * Copyright (c) 2004 * John Maddock * * Use, modification and distribution are 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) * */ /*  *   LOCATION:    see http://www.boost.org for most recent version.  *   FILE         test_regex_search.hpp  *   VERSION      see <boost/version.hpp>  *   DESCRIPTION: Declares tests for regex search and iteration.  */#ifndef BOOST_REGEX_REGRESS_REGEX_SEARCH_HPP#define BOOST_REGEX_REGRESS_REGEX_SEARCH_HPP#include "info.hpp"#ifdef TEST_ROPE#include <rope>#endif//// this file implements a test for a regular expression that should compile,// followed by a search for that expression://struct test_regex_search_tag{};template <class BidirectionalIterator>void test_sub_match(const boost::sub_match<BidirectionalIterator>& sub, BidirectionalIterator base, const int* answer_table, int i){#ifdef BOOST_MSVC#pragma warning(push)#pragma warning(disable:4244)#endif   typedef typename boost::sub_match<BidirectionalIterator>::value_type charT;   if((sub.matched == 0)       &&          !((i == 0)          && (test_info<charT>::match_options() & boost::match_partial)) )   {      if(answer_table[2*i] >= 0)      {         BOOST_REGEX_TEST_ERROR(            "Sub-expression " << i             << " was not matched when it should have been.", charT);      }   }   else   {      if(boost::re_detail::distance(base, sub.first) != answer_table[2*i])      {         BOOST_REGEX_TEST_ERROR(            "Error in start location of sub-expression "             << i << ", found " << boost::re_detail::distance(base, sub.first)             << ", expected " << answer_table[2*i] << ".", charT);      }      if(boost::re_detail::distance(base, sub.second) != answer_table[1+ 2*i])      {         BOOST_REGEX_TEST_ERROR(            "Error in end location of sub-expression "             << i << ", found " << boost::re_detail::distance(base, sub.second)             << ", expected " << answer_table[1 + 2*i] << ".", charT);      }   }#ifdef BOOST_MSVC#pragma warning(pop)#endif}template <class BidirectionalIterator, class Allocator>void test_result(const boost::match_results<BidirectionalIterator, Allocator>& what, BidirectionalIterator base, const int* answer_table){   for(unsigned i = 0; i < what.size(); ++i)   {      test_sub_match(what[i], base, answer_table, i);   }}template<class charT, class traits>void test_simple_search(boost::basic_regex<charT, traits>& r){   typedef typename std::basic_string<charT>::const_iterator const_iterator;   const std::basic_string<charT>& search_text = test_info<charT>::search_text();   boost::regex_constants::match_flag_type opts = test_info<charT>::match_options();   const int* answer_table = test_info<charT>::answer_table();   boost::match_results<const_iterator> what;   if(boost::regex_search(      search_text.begin(),      search_text.end(),      what,      r,      opts))   {      test_result(what, search_text.begin(), answer_table);      // setting match_any should have no effect on the result returned:      if(!boost::regex_search(         search_text.begin(),         search_text.end(),         r,         opts|boost::regex_constants::match_any))      {         BOOST_REGEX_TEST_ERROR("Expected match was not found when using the match_any flag.", charT);      }   }   else   {      if(answer_table[0] >= 0)      {         // we should have had a match but didn't:         BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);      }      // setting match_any should have no effect on the result returned:      else if(boost::regex_search(         search_text.begin(),         search_text.end(),         r,         opts|boost::regex_constants::match_any))      {         BOOST_REGEX_TEST_ERROR("Unexpected match was found when using the match_any flag.", charT);      }   }#ifdef TEST_ROPE   std::rope<charT> rsearch_text;   for(unsigned i = 0; i < search_text.size(); ++i)   {      std::rope<charT> c(search_text[i]);      if(++i != search_text.size())      {         c.append(search_text[i]);         if(++i != search_text.size())         {            c.append(search_text[i]);         }      }      rsearch_text.append(c);   }   boost::match_results<std::rope<charT>::const_iterator> rwhat;   if(boost::regex_search(      rsearch_text.begin(),      rsearch_text.end(),      rwhat,      r,      opts))   {      test_result(rwhat, rsearch_text.begin(), answer_table);   }   else   {      if(answer_table[0] >= 0)      {         // we should have had a match but didn't:         BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);      }   }#endif}template<class charT, class traits>void test_regex_iterator(boost::basic_regex<charT, traits>& r){   typedef typename std::basic_string<charT>::const_iterator const_iterator;   typedef boost::regex_iterator<const_iterator, charT, traits> test_iterator;   const std::basic_string<charT>& search_text = test_info<charT>::search_text();   boost::regex_constants::match_flag_type opts = test_info<charT>::match_options();   const int* answer_table = test_info<charT>::answer_table();   test_iterator start(search_text.begin(), search_text.end(), r, opts), end;   test_iterator copy(start);   const_iterator last_end = search_text.begin();   while(start != end)   {      if(start != copy)      {         BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", charT);      }      if(!(start == copy))      {         BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", charT);      }      test_result(*start, search_text.begin(), answer_table);      // test $` and $' :      if(start->prefix().first != last_end)      {         BOOST_REGEX_TEST_ERROR("Incorrect position for start of $`", charT);      }      if(start->prefix().second != (*start)[0].first)      {         BOOST_REGEX_TEST_ERROR("Incorrect position for end of $`", charT);      }      if(start->prefix().matched != (start->prefix().first != start->prefix().second))      {         BOOST_REGEX_TEST_ERROR("Incorrect position for matched member of $`", charT);      }      if(start->suffix().first != (*start)[0].second)      {         BOOST_REGEX_TEST_ERROR("Incorrect position for start of $'", charT);      }      if(start->suffix().second != search_text.end())      {         BOOST_REGEX_TEST_ERROR("Incorrect position for end of $'", charT);      }      if(start->suffix().matched != (start->suffix().first != start->suffix().second))      {         BOOST_REGEX_TEST_ERROR("Incorrect position for matched member of $'", charT);      }      last_end = (*start)[0].second;      ++start;      ++copy;      // move on the answer table to next set of answers;      if(*answer_table != -2)         while(*answer_table++ != -2){}   }   if(answer_table[0] >= 0)   {      // we should have had a match but didn't:      BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);   }}template<class charT, class traits>void test_regex_token_iterator(boost::basic_regex<charT, traits>& r){   typedef typename std::basic_string<charT>::const_iterator const_iterator;   typedef boost::regex_token_iterator<const_iterator, charT, traits> test_iterator;   const std::basic_string<charT>& search_text = test_info<charT>::search_text();   boost::regex_constants::match_flag_type opts = test_info<charT>::match_options();   const int* answer_table = test_info<charT>::answer_table();   //   // we start by testing sub-expression 0:   //   test_iterator start(search_text.begin(), search_text.end(), r, 0, opts), end;   test_iterator copy(start);   while(start != end)   {      if(start != copy)      {         BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", charT);      }      if(!(start == copy))      {         BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", charT);      }      test_sub_match(*start, search_text.begin(), answer_table, 0);      ++start;      ++copy;      // move on the answer table to next set of answers;      if(*answer_table != -2)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?