Карта сайта Kansoftware
НОВОСТИУСЛУГИРЕШЕНИЯКОНТАКТЫ
Разработка программного обеспечения

regex_grep (Deprecated)

Boost , Boost.Regex 5.1.2 , Deprecated Interfaces

Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

Алгоритм<regex_grep>обесценивается в пользу<regex_iterator>, что обеспечивает более удобный и стандартный интерфейс для библиотеки.

Следующая документация взята без изменений с предыдущего релиза и не будет обновляться в будущем.

#include <boost/regex.hpp>

<regex_grep>Позволяет осуществлять поиск по двунаправленному множителю и находить все (непересекающиеся) совпадения с заданным регулярным выражением. Функция объявляется как:

template <class Predicate, class iterator, class charT, class traits>
unsigned int regex_grep(Predicate foo,
                        iterator first,
                        iterator last,
                        const basic_regex<charT, traits>& e,
                        boost::match_flag_type flags = match_default)

Библиотека также определяет следующие удобные версии, которые принимают либо<constcharT*>, либо<const std::basic_string<>&>вместо пары итераторов.

template <class Predicate, class charT, class traits>
unsigned int regex_grep(Predicate foo,
            const charT* str,
            const basic_regex<charT, traits>& e,
            boost::match_flag_type flags = match_default);
template <class Predicate, class ST, class SA, class charT, class traits>
unsigned int regex_grep(Predicate foo,
            const std::basic_string<charT, ST, SA>& s,
            const basic_regex<charT, traits>& e,
            boost::match_flag_type flags = match_default);

Параметры для первичной версии<regex_grep>имеют следующие значения:

Фоо: Объект функции предиката или указатель функции, см. ниже для получения дополнительной информации.

Сначала: Начало диапазона для поиска.

Последнее: конец диапазона для поиска.

Е. Регулярное выражение для поиска.

Флаги: Флаги, которые определяют, как проводится сопоставление, один из переписчиков match_flags.

Алгоритм находит все непересекающиеся совпадения выраженияe, для каждого совпадения он заполняет структуру<match_results<iterator>>, которая содержит информацию о том, что совпало, и называет предикатfoo, передавая<match_results<iterator>>как единый аргумент. Если предикат возвращаетистинное, то греп-операция продолжается, иначе она прекращается без поиска дальнейших совпадений. Функция возвращает количество найденных совпадений.

Общая форма предиката:

struct grep_predicate
{
   bool operator()(const match_results<iterator_type>& m);
};

Например, обычное выражение «a*b» находит одно совпадение в строке «aaaab» и два в строке «aaabb».

Помните, что этот алгоритм может быть использован для гораздо большего, чем реализация версии grep, предикат может быть и делать все, что вы хотите, утилиты grep будут выводить результаты на экран, другая программа может индексировать файл на основе регулярного выражения и хранить набор закладок в списке, или утилита преобразования текстовых файлов будет выводить в файл. Результаты одного<regex_grep>могут быть даже прикованы к другому<regex_grep>для создания рекурсивных парсеров.

Алгоритм может выбросить<std::runtime_error>, если сложность сопоставления выражения с строкой символовNначинает превышать O (N2), или если программа исчерпает пространство стека при сопоставлении выражения (если Boost). Regex настраивается в рекурсивном режиме или, если спичечный аппарат исчерпает его, допускается выделение памяти (если Boost). Regex настроен в нерекурсивном режиме.

Пример: преобразуйте пример из<regex_search>в<regex_grep>:

#include <string>
#include <map>
#include <boost/regex.hpp>
// IndexClasses: 
// takes the contents of a file in the form of a string 
// and searches for all the C++ class definitions, storing 
// their locations in a map of strings/int's 
typedef std::map<std::string, int, std::less<std::string> > map_type;
const char* re =
   // possibly leading whitespace:   
   "^[[:space:]]*"
   // possible template declaration:
   "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
   // class or struct:
   "(class|struct)[[:space:]]*"
   // leading declspec macros etc:
   "("
      "\\<\\w+\\>"
      "("
         "[[:blank:]]*\\([^)]*\\)"
      ")?"
      "[[:space:]]*"
   ")*"
   // the class name
   "(\\<\\w*\\>)[[:space:]]*"
   // template specialisation parameters
   "(<[^;:{]+>)?[[:space:]]*"
   // terminate in { or :
   "(\\{|:[^;\\{()]*\\{)";
boost::regex expression(re);
class IndexClassesPred
{
   map_type& m;
   std::string::const_iterator base;
public:
   IndexClassesPred(map_type& a, std::string::const_iterator b) : m(a), base(b) {}
   bool operator()(const  smatch& what)
   {
      // what[0] contains the whole string 
      // what[5] contains the class name. 
      // what[6] contains the template specialisation if any. 
      // add class name and position to map: 
      m[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
               what[5].first - base;
      return true;
   }
};
void IndexClasses(map_type& m, const std::string& file)
{
   std::string::const_iterator start, end;
   start = file.begin();
   end = file.end();
   regex_grep(IndexClassesPred(m, start), start, end, expression);
}

Пример: Используйте<regex_grep>для вызова глобальной функции обратного вызова:

#include <string>
#include <map>
#include <boost/regex.hpp>
// purpose: 
// takes the contents of a file in the form of a string 
// and searches for all the C++ class definitions, storing 
// their locations in a map of strings/int's 
typedef std::map<std::string, int, std::less<std::string> > map_type;
const char* re =
   // possibly leading whitespace:   
   "^[[:space:]]*"
   // possible template declaration:
   "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
   // class or struct:
   "(class|struct)[[:space:]]*"
   // leading declspec macros etc:
   "("
      "\\<\\w+\\>"
      "("
         "[[:blank:]]*\\([^)]*\\)"
      ")?"
      "[[:space:]]*"
   ")*"
   // the class name
   "(\\<\\w*\\>)[[:space:]]*"
   // template specialisation parameters
   "(<[^;:{]+>)?[[:space:]]*"
   // terminate in { or :
   "(\\{|:[^;\\{()]*\\{)";
boost::regex expression(re);
map_type class_index;
std::string::const_iterator base;
bool grep_callback(const  boost::smatch& what)
{
   // what[0] contains the whole string 
   // what[5] contains the class name. 
   // what[6] contains the template specialisation if any. 
   // add class name and position to map: 
   class_index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
               what[5].first - base;
   return true;
}
void IndexClasses(const std::string& file)
{
   std::string::const_iterator start, end;
   start = file.begin();
   end = file.end();
   base = start;
   regex_grep(grep_callback, start, end, expression, match_default);
}

Пример: используйте<regex_grep>для вызова функции члена класса, используйте стандартные библиотечные адаптеры<std::mem_fun>и<std::bind1st>для преобразования функции члена в предикат:

#include <string>
#include <map>
#include <boost/regex.hpp>
#include <functional>
// purpose: 
// takes the contents of a file in the form of a string 
// and searches for all the C++ class definitions, storing 
// their locations in a map of strings/int's 
typedef std::map<std::string, int, std::less<std::string> > map_type;
class class_index
{
   boost::regex expression;
   map_type index;
   std::string::const_iterator base;
   bool  grep_callback(boost::smatch what);
public:
   void IndexClasses(const std::string& file);
   class_index()
      : index(),
      expression("^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
                  "(class|struct)[[:space:]]*(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?"
                  "[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?"
                  "(\\{|:[^;\\{()]*\\{)"
                  ){}
};
bool  class_index::grep_callback(boost::smatch what)
{
   // what[0] contains the whole string 
   // what[5] contains the class name. 
   // what[6] contains the template specialisation if any. 
   // add class name and position to map: 
   index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
               what[5].first - base;
   return true;
}
void class_index::IndexClasses(const std::string& file)
{
   std::string::const_iterator start, end;
   start = file.begin();
   end = file.end();
   base = start;
   regex_grep(std::bind1st(std::mem_fun(&class_index::grep_callback), this),
            start,
            end,
            expression);
}

Наконец, C++ Пользователи могут использовать C++ Тип закрытия строителя как аргумент обратного вызова:

#include <string>
#include <map>
#include <boost/regex.hpp>
#include <functional>
// purpose: 
// takes the contents of a file in the form of a string 
// and searches for all the C++ class definitions, storing 
// their locations in a map of strings/int's 
typedef std::map<std::string, int, std::less<std::string> > map_type;
class class_index
{
   boost::regex expression;
   map_type index;
   std::string::const_iterator base;
   typedef  boost::smatch arg_type;
   bool grep_callback(const arg_type& what);
public:
   typedef bool (__closure* grep_callback_type)(const arg_type&);
   void IndexClasses(const std::string& file);
   class_index()
      : index(),
      expression("^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
                  "(class|struct)[[:space:]]*(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?"
                  "[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?"
                  "(\\{|:[^;\\{()]*\\{)"
                  ){}
};
bool class_index::grep_callback(const arg_type& what)
{
   // what[0] contains the whole string    
// what[5] contains the class name.    
// what[6] contains the template specialisation if any.    
// add class name and position to map:    
index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
               what[5].first - base;
   return true;
}
void class_index::IndexClasses(const std::string& file)
{
   std::string::const_iterator start, end;
   start = file.begin();
   end = file.end();
   base = start;
   class_index::grep_callback_type cl = &(this->grep_callback);
   regex_grep(cl,
            start,
            end,
            expression);
}

PrevUpHomeNext

Статья regex_grep (Deprecated) раздела Boost.Regex 5.1.2 Deprecated Interfaces может быть полезна для разработчиков на c++ и boost.




Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.



:: Главная :: Deprecated Interfaces ::


реклама


©KANSoftWare (разработка программного обеспечения, создание программ, создание интерактивных сайтов), 2007
Top.Mail.Ru

Время компиляции файла: 2024-08-30 11:47:00
2025-05-19 19:27:38/0.029963970184326/1