cregex.cpp

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

CPP
644
字号
/* * * Copyright (c) 1998-2002 * 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:        cregex.cpp  *   VERSION:     see <boost/version.hpp>  *   DESCRIPTION: Implements high level class boost::RexEx  */#define BOOST_REGEX_SOURCE#include <boost/regex.hpp>#include <boost/cregex.hpp>#if !defined(BOOST_NO_STD_STRING)#include <map>#include <list>#include <boost/regex/v4/fileiter.hpp>typedef boost::match_flag_type match_flag_type;#include <cstdio>namespace boost{#ifdef __BORLANDC__#if __BORLANDC__ < 0x530//// we need to instantiate the vector classes we use// since declaring a reference to type doesn't seem to// do the job...std::vector<std::size_t> inst1;std::vector<std::string> inst2;#endif#endifnamespace{template <class iterator>std::string to_string(iterator i, iterator j){   std::string s;   while(i != j)   {      s.append(1, *i);      ++i;   }   return s;}inline std::string to_string(const char* i, const char* j){   return std::string(i, j);}}namespace re_detail{class RegExData{public:   enum type   {      type_pc,      type_pf,      type_copy   };   regex e;   cmatch m;#ifndef BOOST_REGEX_NO_FILEITER   match_results<mapfile::iterator> fm;#endif   type t;   const char* pbase;#ifndef BOOST_REGEX_NO_FILEITER   mapfile::iterator fbase;#endif   std::map<int, std::string, std::less<int> > strings;   std::map<int, std::ptrdiff_t, std::less<int> > positions;   void update();   void clean();   RegExData() : e(), m(),#ifndef BOOST_REGEX_NO_FILEITER   fm(),#endif   t(type_copy), pbase(0),#ifndef BOOST_REGEX_NO_FILEITER   fbase(),#endif   strings(), positions() {}};void RegExData::update(){   strings.erase(strings.begin(), strings.end());   positions.erase(positions.begin(), positions.end());   if(t == type_pc)   {      for(unsigned int i = 0; i < m.size(); ++i)      {         if(m[i].matched) strings[i] = std::string(m[i].first, m[i].second);         positions[i] = m[i].matched ? m[i].first - pbase : -1;      }   }#ifndef BOOST_REGEX_NO_FILEITER   else   {      for(unsigned int i = 0; i < fm.size(); ++i)      {         if(fm[i].matched) strings[i] = to_string(fm[i].first, fm[i].second);         positions[i] = fm[i].matched ? fm[i].first - fbase : -1;      }   }#endif   t = type_copy;}void RegExData::clean(){#ifndef BOOST_REGEX_NO_FILEITER   fbase = mapfile::iterator();   fm = match_results<mapfile::iterator>();#endif}} // namespaceRegEx::RegEx(){   pdata = new re_detail::RegExData();}RegEx::RegEx(const RegEx& o){   pdata = new re_detail::RegExData(*(o.pdata));}RegEx::~RegEx(){   delete pdata;}RegEx::RegEx(const char* c, bool icase){   pdata = new re_detail::RegExData();   SetExpression(c, icase);}RegEx::RegEx(const std::string& s, bool icase){   pdata = new re_detail::RegExData();   SetExpression(s.c_str(), icase);}RegEx& RegEx::operator=(const RegEx& o){   *pdata = *(o.pdata);   return *this;}RegEx& RegEx::operator=(const char* p){   SetExpression(p, false);   return *this;}unsigned int RegEx::SetExpression(const char* p, bool icase){   boost::uint_fast32_t f = icase ? regex::normal | regex::icase : regex::normal;   return pdata->e.set_expression(p, f);}unsigned int RegEx::error_code()const{   return pdata->e.error_code();}std::string RegEx::Expression()const{   return pdata->e.expression();}//// now matching operators://bool RegEx::Match(const char* p, match_flag_type flags){   pdata->t = re_detail::RegExData::type_pc;   pdata->pbase = p;   const char* end = p;   while(*end)++end;   if(regex_match(p, end, pdata->m, pdata->e, flags))   {      pdata->update();      return true;   }   return false;}bool RegEx::Search(const char* p, match_flag_type flags){   pdata->t = re_detail::RegExData::type_pc;   pdata->pbase = p;   const char* end = p;   while(*end)++end;   if(regex_search(p, end, pdata->m, pdata->e, flags))   {      pdata->update();      return true;   }   return false;}namespace re_detail{struct pred1{   GrepCallback cb;   RegEx* pe;   pred1(GrepCallback c, RegEx* i) : cb(c), pe(i) {}   bool operator()(const cmatch& m)   {      pe->pdata->m = m;      return cb(*pe);   }};}unsigned int RegEx::Grep(GrepCallback cb, const char* p, match_flag_type flags){   pdata->t = re_detail::RegExData::type_pc;   pdata->pbase = p;   const char* end = p;   while(*end)++end;   unsigned int result = regex_grep(re_detail::pred1(cb, this), p, end, pdata->e, flags);   if(result)      pdata->update();   return result;}namespace re_detail{struct pred2{   std::vector<std::string>& v;   RegEx* pe;   pred2(std::vector<std::string>& o, RegEx* e) : v(o), pe(e) {}   bool operator()(const cmatch& m)   {      pe->pdata->m = m;      v.push_back(std::string(m[0].first, m[0].second));      return true;   }private:   pred2& operator=(const pred2&);};}unsigned int RegEx::Grep(std::vector<std::string>& v, const char* p, match_flag_type flags){   pdata->t = re_detail::RegExData::type_pc;   pdata->pbase = p;   const char* end = p;   while(*end)++end;   unsigned int result = regex_grep(re_detail::pred2(v, this), p, end, pdata->e, flags);   if(result)      pdata->update();   return result;}namespace re_detail{struct pred3{   std::vector<std::size_t>& v;   const char* base;   RegEx* pe;   pred3(std::vector<std::size_t>& o, const char* pb, RegEx* p) : v(o), base(pb), pe(p) {}   bool operator()(const cmatch& m)   {      pe->pdata->m = m;      v.push_back(static_cast<std::size_t>(m[0].first - base));      return true;   }private:   pred3& operator=(const pred3&);};}unsigned int RegEx::Grep(std::vector<std::size_t>& v, const char* p, match_flag_type flags){   pdata->t = re_detail::RegExData::type_pc;   pdata->pbase = p;   const char* end = p;   while(*end)++end;   unsigned int result = regex_grep(re_detail::pred3(v, p, this), p, end, pdata->e, flags);   if(result)      pdata->update();   return result;}#ifndef BOOST_REGEX_NO_FILEITERnamespace re_detail{struct pred4{   GrepFileCallback cb;   RegEx* pe;   const char* file;   bool ok;   pred4(GrepFileCallback c, RegEx* i, const char* f) : cb(c), pe(i), file(f), ok(true) {}   bool operator()(const match_results<mapfile::iterator>& m)   {      pe->pdata->t = RegExData::type_pf;      pe->pdata->fm = m;      pe->pdata->update();      ok = cb(file, *pe);      return ok;   }

⌨️ 快捷键说明

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