3 * Copyright (c) 1998-2002
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Dr John Maddock makes no representations
11 * about the suitability of this software for any purpose.
12 * It is provided "as is" without express or implied warranty.
17 * LOCATION: see http://www.boost.org for most recent version.
18 * FILE regex_format.hpp
19 * VERSION see <boost/version.hpp>
20 * DESCRIPTION: Provides formatting output routines for search and replace
21 * operations. Note this is an internal header file included
22 * by regex.hpp, do not include on its own.
25 #ifndef BOOST_REGEX_FORMAT_HPP
26 #define BOOST_REGEX_FORMAT_HPP
32 format_all = 0, // enable all extentions to sytax
33 format_sed = match_max << 1, // sed style replacement.
34 format_perl = format_sed << 1, // perl style replacement.
35 format_no_copy = format_perl << 1, // don't copy non-matching segments.
36 format_first_only = format_no_copy << 1, // Only replace first occurance.
37 format_is_if = format_first_only << 1 // internal use only.
43 #pragma option push -a4 -b -Ve -pc -w-8037
46 template <class O, class I>
47 O BOOST_REGEX_CALL re_copy_out(O out, I first, I last)
58 template <class charT, class traits_type>
59 void BOOST_REGEX_CALL re_skip_format(const charT*& fmt, const traits_type& traits_inst)
61 // dwa 9/13/00 - suppress incorrect unused parameter warning for MSVC
64 typedef typename traits_type::size_type traits_size_type;
65 typedef typename traits_type::uchar_type traits_uchar_type;
66 typedef typename traits_type::string_type traits_string_type;
68 unsigned int parens = 0;
72 c = traits_inst.syntax_type((traits_size_type)(traits_uchar_type)(*fmt));
73 if((c == traits_type::syntax_colon) && (parens == 0))
78 else if(c == traits_type::syntax_close_bracket)
87 else if(c == traits_type::syntax_open_bracket)
89 else if(c == traits_type::syntax_slash)
99 #ifdef BOOST_NO_STD_OUTPUT_ITERATOR_ASSIGN
102 // ugly hack for buggy output iterators
105 inline void oi_assign(T* p, T v)
107 ::boost::re_detail::pointer_destroy(p);
108 pointer_construct(p, v);
114 inline void oi_assign(T* p, T v)
117 // if you get a compile time error in here then you either
118 // need to rewrite your output iterator to make it assignable
119 // (as is required by the standard), or define
120 // BOOST_NO_STD_OUTPUT_ITERATOR_ASSIGN to use the ugly hack above
127 #if defined(BOOST_REGEX_NO_TEMPLATE_SWITCH_MERGE)
130 // template don't merge if they contain switch statements so declare these
131 // templates in unnamed namespace (ie with internal linkage), each translation
132 // unit then gets its own local copy, it works seemlessly but bloats the app.
137 // algorithm reg_format:
138 // takes the result of a match and a format string
139 // and merges them to produce a new string which
140 // is sent to an OutputIterator,
141 // _reg_format_aux does the actual work:
143 template <class OutputIterator, class iterator, class Allocator, class charT, class traits_type>
144 OutputIterator BOOST_REGEX_CALL _reg_format_aux(OutputIterator out,
145 const match_results<iterator, Allocator>& m,
147 unsigned flags, const traits_type& traits_inst)
149 const charT* fmt_end = fmt;
150 while(*fmt_end) ++ fmt_end;
152 typedef typename traits_type::size_type traits_size_type;
153 typedef typename traits_type::uchar_type traits_uchar_type;
154 typedef typename traits_type::string_type traits_string_type;
158 switch(traits_inst.syntax_type((traits_size_type)(traits_uchar_type)(*fmt)))
160 case traits_type::syntax_dollar:
161 if(flags & format_sed)
163 // no perl style replacement,
164 // $ is an ordinary character:
168 if(*fmt == 0) // oops trailing $
175 switch(traits_inst.syntax_type((traits_size_type)(traits_uchar_type)(*fmt)))
177 case traits_type::syntax_start_buffer:
178 oi_assign(&out, re_copy_out(out, iterator(m[-1].first), iterator(m[-1].second)));
181 case traits_type::syntax_end_buffer:
182 oi_assign(&out, re_copy_out(out, iterator(m[-2].first), iterator(m[-2].second)));
185 case traits_type::syntax_digit:
188 unsigned int index = traits_inst.toi(fmt, fmt_end, 10);
189 oi_assign(&out, re_copy_out(out, iterator(m[index].first), iterator(m[index].second)));
196 oi_assign(&out, re_copy_out(out, iterator(m[0].first), iterator(m[0].second)));
201 // probably an error, treat as a literal '$'
208 case traits_type::syntax_slash:
221 switch(traits_inst.syntax_type((traits_size_type)(traits_uchar_type)(*fmt)))
223 case traits_type::syntax_a:
227 case traits_type::syntax_f:
231 case traits_type::syntax_n:
235 case traits_type::syntax_r:
239 case traits_type::syntax_t:
243 case traits_type::syntax_v:
247 case traits_type::syntax_x:
255 // maybe have \x{ddd}
256 if(traits_inst.syntax_type((traits_size_type)(traits_uchar_type)(*fmt)) == traits_type::syntax_open_brace)
267 if(traits_inst.is_class(*fmt, traits_type::char_class_xdigit) == false)
275 c = (charT)traits_inst.toi(fmt, fmt_end, -16);
276 if(traits_inst.syntax_type((traits_size_type)(traits_uchar_type)(*fmt)) != traits_type::syntax_close_brace)
278 while(traits_inst.syntax_type((traits_size_type)(traits_uchar_type)(*fmt)) != traits_type::syntax_slash)
291 if(traits_inst.is_class(*fmt, traits_type::char_class_xdigit) == false)
299 c = (charT)traits_inst.toi(fmt, fmt_end, -16);
302 case traits_type::syntax_c:
311 if(((typename traits_type::uchar_type)(*fmt) < (typename traits_type::uchar_type)'@')
312 || ((typename traits_type::uchar_type)(*fmt) > (typename traits_type::uchar_type)127) )
320 c = (charT)((typename traits_type::uchar_type)(*fmt) - (typename traits_type::uchar_type)'@');
323 case traits_type::syntax_e:
327 case traits_type::syntax_digit:
328 if(flags & format_sed)
331 c = (charT)traits_inst.toi(fmt, fmt_end, -8);
341 case traits_type::syntax_open_bracket:
342 if(flags & (format_sed|format_perl))
352 oi_assign(&out, _reg_format_aux(out, m, fmt, flags, traits_inst));
355 case traits_type::syntax_close_bracket:
356 if(flags & (format_sed|format_perl))
365 ++fmt; // return from recursion
368 case traits_type::syntax_colon:
369 if(flags & format_is_if)
378 case traits_type::syntax_question:
380 if(flags & (format_sed|format_perl))
398 unsigned int id = traits_inst.toi(fmt, fmt_end, 10);
401 oi_assign(&out, _reg_format_aux(out, m, fmt, flags | format_is_if, traits_inst));
402 if(traits_inst.syntax_type((traits_size_type)(traits_uchar_type)(*(fmt-1))) == traits_type::syntax_colon)
403 re_skip_format(fmt, traits_inst);
407 re_skip_format(fmt, traits_inst);
408 if(traits_inst.syntax_type((traits_size_type)(traits_uchar_type)(*(fmt-1))) == traits_type::syntax_colon)
409 oi_assign(&out, _reg_format_aux(out, m, fmt, flags | format_is_if, traits_inst));
416 if((flags & format_sed) && (*fmt == '&'))
418 oi_assign(&out, re_copy_out(out, iterator(m[0].first), iterator(m[0].second)));
431 #if defined(BOOST_REGEX_NO_TEMPLATE_SWITCH_MERGE)
436 class string_out_iterator
440 string_out_iterator(S& s) : out(&s) {}
441 string_out_iterator& operator++() { return *this; }
442 string_out_iterator& operator++(int) { return *this; }
443 string_out_iterator& operator*() { return *this; }
444 string_out_iterator& operator=(typename S::value_type v)
451 template <class OutputIterator, class iterator, class charT, class Allocator, class traits_type>
452 class merge_out_predicate
458 const traits_type* pt;
461 merge_out_predicate(OutputIterator& o, iterator& pi, const charT* f, unsigned format_flags, const traits_type& p)
462 : out(&o), last(&pi), fmt(f), flags(format_flags), pt(&p){}
464 ~merge_out_predicate() {}
465 bool BOOST_REGEX_CALL operator()(const boost::match_results<iterator, Allocator>& m)
467 const charT* f = fmt;
468 if(0 == (flags & format_no_copy))
469 oi_assign(out, re_copy_out(*out, iterator(m[-1].first), iterator(m[-1].second)));
470 oi_assign(out, _reg_format_aux(*out, m, f, flags, *pt));
472 return flags & format_first_only ? false : true;
476 } // namespace re_detail
478 template <class OutputIterator, class iterator, class Allocator, class charT>
479 OutputIterator regex_format(OutputIterator out,
480 const match_results<iterator, Allocator>& m,
485 regex_traits<charT> t;
486 return re_detail::_reg_format_aux(out, m, fmt, flags, t);
489 template <class OutputIterator, class iterator, class Allocator, class charT>
490 OutputIterator regex_format(OutputIterator out,
491 const match_results<iterator, Allocator>& m,
492 const std::basic_string<charT>& fmt,
496 regex_traits<charT> t;
497 const charT* start = fmt.c_str();
498 return re_detail::_reg_format_aux(out, m, start, flags, t);
501 template <class iterator, class Allocator, class charT>
502 std::basic_string<charT> regex_format(const match_results<iterator, Allocator>& m, const charT* fmt, unsigned flags = 0)
504 std::basic_string<charT> result;
505 re_detail::string_out_iterator<std::basic_string<charT> > i(result);
506 regex_format(i, m, fmt, flags);
510 template <class iterator, class Allocator, class charT>
511 std::basic_string<charT> regex_format(const match_results<iterator, Allocator>& m, const std::basic_string<charT>& fmt, unsigned flags = 0)
513 std::basic_string<charT> result;
514 re_detail::string_out_iterator<std::basic_string<charT> > i(result);
515 regex_format(i, m, fmt.c_str(), flags);
519 template <class OutputIterator, class iterator, class traits, class Allocator, class charT>
520 OutputIterator regex_merge(OutputIterator out,
523 const reg_expression<charT, traits, Allocator>& e,
525 unsigned int flags = match_default)
528 re_detail::merge_out_predicate<OutputIterator, iterator, charT, Allocator, traits> oi(out, l, fmt, flags, e.get_traits());
529 regex_grep(oi, first, last, e, flags);
530 return (flags & format_no_copy) ? out : re_detail::re_copy_out(out, l, last);
533 template <class OutputIterator, class iterator, class traits, class Allocator, class charT>
534 inline OutputIterator regex_merge(OutputIterator out,
537 const reg_expression<charT, traits, Allocator>& e,
538 const std::basic_string<charT>& fmt,
539 unsigned int flags = match_default)
541 return regex_merge(out, first, last, e, fmt.c_str(), flags);
544 template <class traits, class Allocator, class charT>
545 std::basic_string<charT> regex_merge(const std::basic_string<charT>& s,
546 const reg_expression<charT, traits, Allocator>& e,
548 unsigned int flags = match_default)
550 std::basic_string<charT> result;
551 re_detail::string_out_iterator<std::basic_string<charT> > i(result);
552 regex_merge(i, s.begin(), s.end(), e, fmt, flags);
556 template <class traits, class Allocator, class charT>
557 std::basic_string<charT> regex_merge(const std::basic_string<charT>& s,
558 const reg_expression<charT, traits, Allocator>& e,
559 const std::basic_string<charT>& fmt,
560 unsigned int flags = match_default)
562 std::basic_string<charT> result;
563 re_detail::string_out_iterator<std::basic_string<charT> > i(result);
564 regex_merge(i, s.begin(), s.end(), e, fmt.c_str(), flags);
574 #endif // BOOST_REGEX_FORMAT_HPP