3 * Copyright (c) 1998-2002
6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
13 * LOCATION: see http://www.boost.org for most recent version.
15 * VERSION: see <boost/version.hpp>
16 * DESCRIPTION: Implements high level class boost::RexEx
20 #define BOOST_REGEX_SOURCE
22 #include <boost/cregex.hpp>
23 #include <boost/regex.hpp>
24 #if !defined(BOOST_NO_STD_STRING)
28 #include <boost/regex/v3/fileiter.hpp>
29 typedef unsigned match_flag_type;
31 #include <boost/regex/v4/fileiter.hpp>
32 typedef boost::match_flag_type match_flag_type;
39 #if __BORLANDC__ < 0x530
41 // we need to instantiate the vector classes we use
42 // since declaring a reference to type doesn't seem to
44 std::vector<std::size_t> inst1;
45 std::vector<std::string> inst2;
51 template <class iterator>
52 std::string to_string(iterator i, iterator j)
64 inline std::string to_string(const char* i, const char* j)
66 return std::string(i, j);
83 #ifndef BOOST_REGEX_NO_FILEITER
84 match_results<mapfile::iterator> fm;
88 #ifndef BOOST_REGEX_NO_FILEITER
89 mapfile::iterator fbase;
91 std::map<int, std::string, std::less<int> > strings;
92 std::map<int, std::ptrdiff_t, std::less<int> > positions;
95 RegExData() : e(), m(),
96 #ifndef BOOST_REGEX_NO_FILEITER
99 t(type_copy), pbase(0),
100 #ifndef BOOST_REGEX_NO_FILEITER
103 strings(), positions() {}
106 void RegExData::update()
109 strings.erase(strings.begin(), strings.end());
110 positions.erase(positions.begin(), positions.end());
113 for(unsigned int i = 0; i < m.size(); ++i)
115 if(m[i].matched) strings[i] = std::string(m[i].first, m[i].second);
116 positions[i] = m[i].matched ? m[i].first - pbase : -1;
119 #ifndef BOOST_REGEX_NO_FILEITER
122 for(unsigned int i = 0; i < fm.size(); ++i)
124 if(fm[i].matched) strings[i] = to_string(fm[i].first, fm[i].second);
125 positions[i] = fm[i].matched ? fm[i].first - fbase : -1;
132 void RegExData::clean()
135 #ifndef BOOST_REGEX_NO_FILEITER
136 fbase = mapfile::iterator();
137 fm = match_results<mapfile::iterator>();
146 pdata = new re_detail::RegExData();
149 RegEx::RegEx(const RegEx& o)
152 pdata = new re_detail::RegExData(*(o.pdata));
161 RegEx::RegEx(const char* c, bool icase)
164 pdata = new re_detail::RegExData();
165 SetExpression(c, icase);
168 RegEx::RegEx(const std::string& s, bool icase)
171 pdata = new re_detail::RegExData();
172 SetExpression(s.c_str(), icase);
175 RegEx& RegEx::operator=(const RegEx& o)
182 RegEx& RegEx::operator=(const char* p)
185 SetExpression(p, false);
189 unsigned int RegEx::SetExpression(const char* p, bool icase)
192 boost::uint_fast32_t f = icase ? regex::normal | regex::use_except | regex::icase : regex::normal | regex::use_except;
193 return pdata->e.set_expression(p, f);
196 unsigned int RegEx::error_code()const
198 return pdata->e.error_code();
202 std::string RegEx::Expression()const
205 return pdata->e.expression();
209 // now matching operators:
211 bool RegEx::Match(const char* p, match_flag_type flags)
214 pdata->t = re_detail::RegExData::type_pc;
219 if(regex_match(p, end, pdata->m, pdata->e, flags))
227 bool RegEx::Search(const char* p, match_flag_type flags)
230 pdata->t = re_detail::RegExData::type_pc;
235 if(regex_search(p, end, pdata->m, pdata->e, flags))
247 pred1(GrepCallback c, RegEx* i) : cb(c), pe(i) {}
248 bool operator()(const cmatch& m)
255 unsigned int RegEx::Grep(GrepCallback cb, const char* p, match_flag_type flags)
258 pdata->t = re_detail::RegExData::type_pc;
263 unsigned int result = regex_grep(re_detail::pred1(cb, this), p, end, pdata->e, flags);
271 std::vector<std::string>& v;
273 pred2(std::vector<std::string>& o, RegEx* e) : v(o), pe(e) {}
274 bool operator()(const cmatch& m)
277 v.push_back(std::string(m[0].first, m[0].second));
281 pred2& operator=(const pred2&);
285 unsigned int RegEx::Grep(std::vector<std::string>& v, const char* p, match_flag_type flags)
288 pdata->t = re_detail::RegExData::type_pc;
293 unsigned int result = regex_grep(re_detail::pred2(v, this), p, end, pdata->e, flags);
301 std::vector<std::size_t>& v;
304 pred3(std::vector<std::size_t>& o, const char* pb, RegEx* p) : v(o), base(pb), pe(p) {}
305 bool operator()(const cmatch& m)
308 v.push_back(static_cast<std::size_t>(m[0].first - base));
312 pred3& operator=(const pred3&);
315 unsigned int RegEx::Grep(std::vector<std::size_t>& v, const char* p, match_flag_type flags)
318 pdata->t = re_detail::RegExData::type_pc;
323 unsigned int result = regex_grep(re_detail::pred3(v, p, this), p, end, pdata->e, flags);
328 #ifndef BOOST_REGEX_NO_FILEITER
336 pred4(GrepFileCallback c, RegEx* i, const char* f) : cb(c), pe(i), file(f), ok(true) {}
337 bool operator()(const match_results<mapfile::iterator>& m)
339 pe->pdata->t = RegExData::type_pf;
348 void BuildFileList(std::list<std::string>* pl, const char* files, bool recurse)
351 file_iterator start(files);
355 // go through sub directories:
357 std::strcpy(buf, start.root());
360 std::strcpy(buf, ".");
361 std::strcat(buf, directory_iterator::separator());
362 std::strcat(buf, "*");
366 std::strcat(buf, directory_iterator::separator());
367 std::strcat(buf, "*");
369 directory_iterator dstart(buf);
370 directory_iterator dend;
372 // now get the file mask bit of "files":
373 const char* ptr = files;
375 while((ptr != files) && (*ptr != *directory_iterator::separator()) && (*ptr != '/'))--ptr;
376 if(ptr != files) ++ptr;
378 while(dstart != dend)
380 std::sprintf(buf, "%s%s%s", dstart.path(), directory_iterator::separator(), ptr);
381 BuildFileList(pl, buf, recurse);
387 pl->push_back(*start);
393 unsigned int RegEx::GrepFiles(GrepFileCallback cb, const char* files, bool recurse, match_flag_type flags)
396 unsigned int result = 0;
397 std::list<std::string> file_list;
398 BuildFileList(&file_list, files, recurse);
399 std::list<std::string>::iterator start, end;
400 start = file_list.begin();
401 end = file_list.end();
405 mapfile map((*start).c_str());
406 pdata->t = re_detail::RegExData::type_pf;
407 pdata->fbase = map.begin();
408 re_detail::pred4 pred(cb, this, (*start).c_str());
409 int r = regex_grep(pred, map.begin(), map.end(), pdata->e, flags);
421 unsigned int RegEx::FindFiles(FindFilesCallback cb, const char* files, bool recurse, match_flag_type flags)
424 unsigned int result = 0;
425 std::list<std::string> file_list;
426 BuildFileList(&file_list, files, recurse);
427 std::list<std::string>::iterator start, end;
428 start = file_list.begin();
429 end = file_list.end();
433 mapfile map((*start).c_str());
434 pdata->t = re_detail::RegExData::type_pf;
435 pdata->fbase = map.begin();
437 if(regex_search(map.begin(), map.end(), pdata->fm, pdata->e, flags))
440 if(false == cb((*start).c_str()))
452 #ifdef BOOST_REGEX_V3
453 #define regex_replace regex_merge
456 std::string RegEx::Merge(const std::string& in, const std::string& fmt,
457 bool copy, match_flag_type flags)
460 re_detail::string_out_iterator<std::string> i(result);
461 if(!copy) flags |= format_no_copy;
462 regex_replace(i, in.begin(), in.end(), pdata->e, fmt.c_str(), flags);
466 std::string RegEx::Merge(const char* in, const char* fmt,
467 bool copy, match_flag_type flags)
470 if(!copy) flags |= format_no_copy;
471 re_detail::string_out_iterator<std::string> i(result);
472 regex_replace(i, in, in + std::strlen(in), pdata->e, fmt, flags);
476 std::size_t RegEx::Split(std::vector<std::string>& v,
478 match_flag_type flags,
481 return regex_split(std::back_inserter(v), s, pdata->e, flags, max_count);
487 // now operators for returning what matched in more detail:
489 std::size_t RegEx::Position(int i)const
494 case re_detail::RegExData::type_pc:
495 return pdata->m[i].matched ? pdata->m[i].first - pdata->pbase : RegEx::npos;
496 #ifndef BOOST_REGEX_NO_FILEITER
497 case re_detail::RegExData::type_pf:
498 return pdata->fm[i].matched ? pdata->fm[i].first - pdata->fbase : RegEx::npos;
500 case re_detail::RegExData::type_copy:
502 std::map<int, std::ptrdiff_t, std::less<int> >::iterator pos = pdata->positions.find(i);
503 if(pos == pdata->positions.end())
505 return (*pos).second;
511 unsigned int RegEx::Marks()const
514 return pdata->e.mark_count();
518 std::size_t RegEx::Length(int i)const
523 case re_detail::RegExData::type_pc:
524 return pdata->m[i].matched ? pdata->m[i].second - pdata->m[i].first : RegEx::npos;
525 #ifndef BOOST_REGEX_NO_FILEITER
526 case re_detail::RegExData::type_pf:
527 return pdata->fm[i].matched ? pdata->fm[i].second - pdata->fm[i].first : RegEx::npos;
529 case re_detail::RegExData::type_copy:
531 std::map<int, std::string, std::less<int> >::iterator pos = pdata->strings.find(i);
532 if(pos == pdata->strings.end())
534 return (*pos).second.size();
540 bool RegEx::Matched(int i)const
545 case re_detail::RegExData::type_pc:
546 return pdata->m[i].matched;
547 #ifndef BOOST_REGEX_NO_FILEITER
548 case re_detail::RegExData::type_pf:
549 return pdata->fm[i].matched;
551 case re_detail::RegExData::type_copy:
553 std::map<int, std::string, std::less<int> >::iterator pos = pdata->strings.find(i);
554 if(pos == pdata->strings.end())
563 std::string RegEx::What(int i)const
569 case re_detail::RegExData::type_pc:
570 if(pdata->m[i].matched)
571 result.assign(pdata->m[i].first, pdata->m[i].second);
573 case re_detail::RegExData::type_pf:
574 if(pdata->m[i].matched)
575 result.assign(to_string(pdata->m[i].first, pdata->m[i].second));
577 case re_detail::RegExData::type_copy:
579 std::map<int, std::string, std::less<int> >::iterator pos = pdata->strings.find(i);
580 if(pos != pdata->strings.end())
581 result = (*pos).second;
588 const unsigned int RegEx::npos = ~0u;
593 #if defined(__BORLANDC__) && (__BORLANDC__ >= 0x550) && (__BORLANDC__ <= 0x551) && !defined(_RWSTD_COMPILE_INSTANTIATE)
595 // this is an ugly hack to work around an ugly problem:
596 // by default this file will produce unresolved externals during
597 // linking unless _RWSTD_COMPILE_INSTANTIATE is defined (Borland bug).
598 // However if _RWSTD_COMPILE_INSTANTIATE is defined then we get separate
599 // copies of basic_string's static data in the RTL and this DLL, this messes
600 // with basic_string's memory management and results in run-time crashes,
601 // Oh sweet joy of Catch 22....
604 template<> template<>
606 basic_string<char>::replace<const char*>(char* f1, char* f2, const char* i1, const char* i2)
608 unsigned insert_pos = f1 - begin();
609 unsigned remove_len = f2 - f1;
610 unsigned insert_len = i2 - i1;
611 unsigned org_size = size();
612 if(insert_len > remove_len)
614 append(insert_len-remove_len, ' ');
615 std::copy_backward(begin() + insert_pos + remove_len, begin() + org_size, end());
616 std::copy(i1, i2, begin() + insert_pos);
620 std::copy(begin() + insert_pos + remove_len, begin() + org_size, begin() + insert_pos + insert_len);
621 std::copy(i1, i2, begin() + insert_pos);
622 erase(size() + insert_len - remove_len);