sclang: ServerShmInterface - avoid object duplication in deepCopy
[supercollider.git] / external_libraries / boost / libs / regex / src / cregex.cpp
blob8d69139f963e4c1222325209f4a39c6d6637dbaf
1 /*
3 * Copyright (c) 1998-2002
4 * John Maddock
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.
14 * FILE: cregex.cpp
15 * VERSION: see <boost/version.hpp>
16 * DESCRIPTION: Implements high level class boost::RexEx
20 #define BOOST_REGEX_SOURCE
22 #include <boost/regex.hpp>
23 #include <boost/cregex.hpp>
24 #if !defined(BOOST_NO_STD_STRING)
25 #include <map>
26 #include <list>
27 #include <boost/regex/v4/fileiter.hpp>
28 typedef boost::match_flag_type match_flag_type;
29 #include <cstdio>
31 #ifdef BOOST_MSVC
32 #pragma warning(disable:4309)
33 #endif
34 #ifdef BOOST_INTEL
35 #pragma warning(disable:981 383)
36 #endif
38 namespace boost{
40 #ifdef __BORLANDC__
41 #if __BORLANDC__ < 0x530
43 // we need to instantiate the vector classes we use
44 // since declaring a reference to type doesn't seem to
45 // do the job...
46 std::vector<std::size_t> inst1;
47 std::vector<std::string> inst2;
48 #endif
49 #endif
51 namespace{
53 template <class iterator>
54 std::string to_string(iterator i, iterator j)
56 std::string s;
57 while(i != j)
59 s.append(1, *i);
60 ++i;
62 return s;
65 inline std::string to_string(const char* i, const char* j)
67 return std::string(i, j);
71 namespace re_detail{
73 class RegExData
75 public:
76 enum type
78 type_pc,
79 type_pf,
80 type_copy
82 regex e;
83 cmatch m;
84 #ifndef BOOST_REGEX_NO_FILEITER
85 match_results<mapfile::iterator> fm;
86 #endif
87 type t;
88 const char* pbase;
89 #ifndef BOOST_REGEX_NO_FILEITER
90 mapfile::iterator fbase;
91 #endif
92 std::map<int, std::string, std::less<int> > strings;
93 std::map<int, std::ptrdiff_t, std::less<int> > positions;
94 void update();
95 void clean();
96 RegExData() : e(), m(),
97 #ifndef BOOST_REGEX_NO_FILEITER
98 fm(),
99 #endif
100 t(type_copy), pbase(0),
101 #ifndef BOOST_REGEX_NO_FILEITER
102 fbase(),
103 #endif
104 strings(), positions() {}
107 void RegExData::update()
109 strings.erase(strings.begin(), strings.end());
110 positions.erase(positions.begin(), positions.end());
111 if(t == type_pc)
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
120 else
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;
128 #endif
129 t = type_copy;
132 void RegExData::clean()
134 #ifndef BOOST_REGEX_NO_FILEITER
135 fbase = mapfile::iterator();
136 fm = match_results<mapfile::iterator>();
137 #endif
140 } // namespace
142 RegEx::RegEx()
144 pdata = new re_detail::RegExData();
147 RegEx::RegEx(const RegEx& o)
149 pdata = new re_detail::RegExData(*(o.pdata));
152 RegEx::~RegEx()
154 delete pdata;
157 RegEx::RegEx(const char* c, bool icase)
159 pdata = new re_detail::RegExData();
160 SetExpression(c, icase);
163 RegEx::RegEx(const std::string& s, bool icase)
165 pdata = new re_detail::RegExData();
166 SetExpression(s.c_str(), icase);
169 RegEx& RegEx::operator=(const RegEx& o)
171 *pdata = *(o.pdata);
172 return *this;
175 RegEx& RegEx::operator=(const char* p)
177 SetExpression(p, false);
178 return *this;
181 unsigned int RegEx::SetExpression(const char* p, bool icase)
183 boost::uint_fast32_t f = icase ? regex::normal | regex::icase : regex::normal;
184 return pdata->e.set_expression(p, f);
187 unsigned int RegEx::error_code()const
189 return pdata->e.error_code();
193 std::string RegEx::Expression()const
195 return pdata->e.expression();
199 // now matching operators:
201 bool RegEx::Match(const char* p, match_flag_type flags)
203 pdata->t = re_detail::RegExData::type_pc;
204 pdata->pbase = p;
205 const char* end = p;
206 while(*end)++end;
208 if(regex_match(p, end, pdata->m, pdata->e, flags))
210 pdata->update();
211 return true;
213 return false;
216 bool RegEx::Search(const char* p, match_flag_type flags)
218 pdata->t = re_detail::RegExData::type_pc;
219 pdata->pbase = p;
220 const char* end = p;
221 while(*end)++end;
223 if(regex_search(p, end, pdata->m, pdata->e, flags))
225 pdata->update();
226 return true;
228 return false;
230 namespace re_detail{
231 struct pred1
233 GrepCallback cb;
234 RegEx* pe;
235 pred1(GrepCallback c, RegEx* i) : cb(c), pe(i) {}
236 bool operator()(const cmatch& m)
238 pe->pdata->m = m;
239 return cb(*pe);
243 unsigned int RegEx::Grep(GrepCallback cb, const char* p, match_flag_type flags)
245 pdata->t = re_detail::RegExData::type_pc;
246 pdata->pbase = p;
247 const char* end = p;
248 while(*end)++end;
250 unsigned int result = regex_grep(re_detail::pred1(cb, this), p, end, pdata->e, flags);
251 if(result)
252 pdata->update();
253 return result;
255 namespace re_detail{
256 struct pred2
258 std::vector<std::string>& v;
259 RegEx* pe;
260 pred2(std::vector<std::string>& o, RegEx* e) : v(o), pe(e) {}
261 bool operator()(const cmatch& m)
263 pe->pdata->m = m;
264 v.push_back(std::string(m[0].first, m[0].second));
265 return true;
267 private:
268 pred2& operator=(const pred2&);
272 unsigned int RegEx::Grep(std::vector<std::string>& v, const char* p, match_flag_type flags)
274 pdata->t = re_detail::RegExData::type_pc;
275 pdata->pbase = p;
276 const char* end = p;
277 while(*end)++end;
279 unsigned int result = regex_grep(re_detail::pred2(v, this), p, end, pdata->e, flags);
280 if(result)
281 pdata->update();
282 return result;
284 namespace re_detail{
285 struct pred3
287 std::vector<std::size_t>& v;
288 const char* base;
289 RegEx* pe;
290 pred3(std::vector<std::size_t>& o, const char* pb, RegEx* p) : v(o), base(pb), pe(p) {}
291 bool operator()(const cmatch& m)
293 pe->pdata->m = m;
294 v.push_back(static_cast<std::size_t>(m[0].first - base));
295 return true;
297 private:
298 pred3& operator=(const pred3&);
301 unsigned int RegEx::Grep(std::vector<std::size_t>& v, const char* p, match_flag_type flags)
303 pdata->t = re_detail::RegExData::type_pc;
304 pdata->pbase = p;
305 const char* end = p;
306 while(*end)++end;
308 unsigned int result = regex_grep(re_detail::pred3(v, p, this), p, end, pdata->e, flags);
309 if(result)
310 pdata->update();
311 return result;
313 #ifndef BOOST_REGEX_NO_FILEITER
314 namespace re_detail{
315 struct pred4
317 GrepFileCallback cb;
318 RegEx* pe;
319 const char* file;
320 bool ok;
321 pred4(GrepFileCallback c, RegEx* i, const char* f) : cb(c), pe(i), file(f), ok(true) {}
322 bool operator()(const match_results<mapfile::iterator>& m)
324 pe->pdata->t = RegExData::type_pf;
325 pe->pdata->fm = m;
326 pe->pdata->update();
327 ok = cb(file, *pe);
328 return ok;
332 namespace{
333 void BuildFileList(std::list<std::string>* pl, const char* files, bool recurse)
335 file_iterator start(files);
336 file_iterator end;
337 if(recurse)
339 // go through sub directories:
340 char buf[MAX_PATH];
341 re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(buf, MAX_PATH, start.root()));
342 if(*buf == 0)
344 re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(buf, MAX_PATH, "."));
345 re_detail::overflow_error_if_not_zero(re_detail::strcat_s(buf, MAX_PATH, directory_iterator::separator()));
346 re_detail::overflow_error_if_not_zero(re_detail::strcat_s(buf, MAX_PATH, "*"));
348 else
350 re_detail::overflow_error_if_not_zero(re_detail::strcat_s(buf, MAX_PATH, directory_iterator::separator()));
351 re_detail::overflow_error_if_not_zero(re_detail::strcat_s(buf, MAX_PATH, "*"));
353 directory_iterator dstart(buf);
354 directory_iterator dend;
356 // now get the file mask bit of "files":
357 const char* ptr = files;
358 while(*ptr) ++ptr;
359 while((ptr != files) && (*ptr != *directory_iterator::separator()) && (*ptr != '/'))--ptr;
360 if(ptr != files) ++ptr;
362 while(dstart != dend)
364 // Verify that sprintf will not overflow:
365 if(std::strlen(dstart.path()) + std::strlen(directory_iterator::separator()) + std::strlen(ptr) >= MAX_PATH)
367 // Oops overflow, skip this item:
368 ++dstart;
369 continue;
371 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
372 int r = (::sprintf_s)(buf, sizeof(buf), "%s%s%s", dstart.path(), directory_iterator::separator(), ptr);
373 #else
374 int r = (std::sprintf)(buf, "%s%s%s", dstart.path(), directory_iterator::separator(), ptr);
375 #endif
376 if(r < 0)
378 // sprintf failed, skip this item:
379 ++dstart;
380 continue;
382 BuildFileList(pl, buf, recurse);
383 ++dstart;
386 while(start != end)
388 pl->push_back(*start);
389 ++start;
394 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();
403 while(start != 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);
410 result += r;
411 ++start;
412 pdata->clean();
413 if(pred.ok == false)
414 return result;
417 return result;
421 unsigned int RegEx::FindFiles(FindFilesCallback cb, const char* files, bool recurse, match_flag_type flags)
423 unsigned int result = 0;
424 std::list<std::string> file_list;
425 BuildFileList(&file_list, files, recurse);
426 std::list<std::string>::iterator start, end;
427 start = file_list.begin();
428 end = file_list.end();
430 while(start != end)
432 mapfile map((*start).c_str());
433 pdata->t = re_detail::RegExData::type_pf;
434 pdata->fbase = map.begin();
436 if(regex_search(map.begin(), map.end(), pdata->fm, pdata->e, flags))
438 ++result;
439 if(false == cb((*start).c_str()))
440 return result;
442 //pdata->update();
443 ++start;
444 //pdata->clean();
447 return result;
449 #endif
451 #ifdef BOOST_REGEX_V3
452 #define regex_replace regex_merge
453 #endif
455 std::string RegEx::Merge(const std::string& in, const std::string& fmt,
456 bool copy, match_flag_type flags)
458 std::string result;
459 re_detail::string_out_iterator<std::string> i(result);
460 if(!copy) flags |= format_no_copy;
461 regex_replace(i, in.begin(), in.end(), pdata->e, fmt.c_str(), flags);
462 return result;
465 std::string RegEx::Merge(const char* in, const char* fmt,
466 bool copy, match_flag_type flags)
468 std::string result;
469 if(!copy) flags |= format_no_copy;
470 re_detail::string_out_iterator<std::string> i(result);
471 regex_replace(i, in, in + std::strlen(in), pdata->e, fmt, flags);
472 return result;
475 std::size_t RegEx::Split(std::vector<std::string>& v,
476 std::string& s,
477 match_flag_type flags,
478 unsigned max_count)
480 return regex_split(std::back_inserter(v), s, pdata->e, flags, max_count);
486 // now operators for returning what matched in more detail:
488 std::size_t RegEx::Position(int i)const
490 switch(pdata->t)
492 case re_detail::RegExData::type_pc:
493 return pdata->m[i].matched ? pdata->m[i].first - pdata->pbase : RegEx::npos;
494 #ifndef BOOST_REGEX_NO_FILEITER
495 case re_detail::RegExData::type_pf:
496 return pdata->fm[i].matched ? pdata->fm[i].first - pdata->fbase : RegEx::npos;
497 #endif
498 case re_detail::RegExData::type_copy:
500 std::map<int, std::ptrdiff_t, std::less<int> >::iterator pos = pdata->positions.find(i);
501 if(pos == pdata->positions.end())
502 return RegEx::npos;
503 return (*pos).second;
506 return RegEx::npos;
509 std::size_t RegEx::Marks()const
511 return pdata->e.mark_count();
515 std::size_t RegEx::Length(int i)const
517 switch(pdata->t)
519 case re_detail::RegExData::type_pc:
520 return pdata->m[i].matched ? pdata->m[i].second - pdata->m[i].first : RegEx::npos;
521 #ifndef BOOST_REGEX_NO_FILEITER
522 case re_detail::RegExData::type_pf:
523 return pdata->fm[i].matched ? pdata->fm[i].second - pdata->fm[i].first : RegEx::npos;
524 #endif
525 case re_detail::RegExData::type_copy:
527 std::map<int, std::string, std::less<int> >::iterator pos = pdata->strings.find(i);
528 if(pos == pdata->strings.end())
529 return RegEx::npos;
530 return (*pos).second.size();
533 return RegEx::npos;
536 bool RegEx::Matched(int i)const
538 switch(pdata->t)
540 case re_detail::RegExData::type_pc:
541 return pdata->m[i].matched;
542 #ifndef BOOST_REGEX_NO_FILEITER
543 case re_detail::RegExData::type_pf:
544 return pdata->fm[i].matched;
545 #endif
546 case re_detail::RegExData::type_copy:
548 std::map<int, std::string, std::less<int> >::iterator pos = pdata->strings.find(i);
549 if(pos == pdata->strings.end())
550 return false;
551 return true;
554 return false;
558 std::string RegEx::What(int i)const
560 std::string result;
561 switch(pdata->t)
563 case re_detail::RegExData::type_pc:
564 if(pdata->m[i].matched)
565 result.assign(pdata->m[i].first, pdata->m[i].second);
566 break;
567 case re_detail::RegExData::type_pf:
568 if(pdata->m[i].matched)
569 result.assign(to_string(pdata->m[i].first, pdata->m[i].second));
570 break;
571 case re_detail::RegExData::type_copy:
573 std::map<int, std::string, std::less<int> >::iterator pos = pdata->strings.find(i);
574 if(pos != pdata->strings.end())
575 result = (*pos).second;
576 break;
579 return result;
582 const std::size_t RegEx::npos = ~static_cast<std::size_t>(0);
584 } // namespace boost
586 #if defined(__BORLANDC__) && (__BORLANDC__ >= 0x550) && (__BORLANDC__ <= 0x551) && !defined(_RWSTD_COMPILE_INSTANTIATE)
588 // this is an ugly hack to work around an ugly problem:
589 // by default this file will produce unresolved externals during
590 // linking unless _RWSTD_COMPILE_INSTANTIATE is defined (Borland bug).
591 // However if _RWSTD_COMPILE_INSTANTIATE is defined then we get separate
592 // copies of basic_string's static data in the RTL and this DLL, this messes
593 // with basic_string's memory management and results in run-time crashes,
594 // Oh sweet joy of Catch 22....
596 namespace std{
597 template<> template<>
598 basic_string<char>& BOOST_REGEX_DECL
599 basic_string<char>::replace<const char*>(char* f1, char* f2, const char* i1, const char* i2)
601 unsigned insert_pos = f1 - begin();
602 unsigned remove_len = f2 - f1;
603 unsigned insert_len = i2 - i1;
604 unsigned org_size = size();
605 if(insert_len > remove_len)
607 append(insert_len-remove_len, ' ');
608 std::copy_backward(begin() + insert_pos + remove_len, begin() + org_size, end());
609 std::copy(i1, i2, begin() + insert_pos);
611 else
613 std::copy(begin() + insert_pos + remove_len, begin() + org_size, begin() + insert_pos + insert_len);
614 std::copy(i1, i2, begin() + insert_pos);
615 erase(size() + insert_len - remove_len);
617 return *this;
619 template<> template<>
620 basic_string<wchar_t>& BOOST_REGEX_DECL
621 basic_string<wchar_t>::replace<const wchar_t*>(wchar_t* f1, wchar_t* f2, const wchar_t* i1, const wchar_t* i2)
623 unsigned insert_pos = f1 - begin();
624 unsigned remove_len = f2 - f1;
625 unsigned insert_len = i2 - i1;
626 unsigned org_size = size();
627 if(insert_len > remove_len)
629 append(insert_len-remove_len, ' ');
630 std::copy_backward(begin() + insert_pos + remove_len, begin() + org_size, end());
631 std::copy(i1, i2, begin() + insert_pos);
633 else
635 std::copy(begin() + insert_pos + remove_len, begin() + org_size, begin() + insert_pos + insert_len);
636 std::copy(i1, i2, begin() + insert_pos);
637 erase(size() + insert_len - remove_len);
639 return *this;
641 } // namespace std
642 #endif
644 #endif