]> git.lyx.org Git - features.git/blob - boost/boost/regex/v4/regex_format.hpp
0ca9baa7aed0d3f2ca9ead08ef07e4e0892dd727
[features.git] / boost / boost / regex / v4 / regex_format.hpp
1 /*
2  *
3  * Copyright (c) 1998-2009 John Maddock
4  * Copyright 2008 Eric Niebler. 
5  *
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)
9  *
10  */
11
12  /*
13   *   LOCATION:    see http://www.boost.org for most recent version.
14   *   FILE         regex_format.hpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: Provides formatting output routines for search and replace
17   *                operations.  Note this is an internal header file included
18   *                by regex.hpp, do not include on its own.
19   */
20
21 #ifndef BOOST_REGEX_FORMAT_HPP
22 #define BOOST_REGEX_FORMAT_HPP
23
24 #include <boost/type_traits/is_pointer.hpp>
25 #include <boost/type_traits/is_function.hpp>
26 #include <boost/type_traits/is_class.hpp>
27 #include <boost/type_traits/is_same.hpp>
28 #include <boost/type_traits/is_convertible.hpp>
29 #include <boost/type_traits/remove_pointer.hpp>
30 #include <boost/type_traits/remove_cv.hpp>
31 #include <boost/mpl/if.hpp>
32 #include <boost/mpl/and.hpp>
33 #include <boost/mpl/not.hpp>
34 #ifndef BOOST_NO_SFINAE
35 #include <boost/mpl/has_xxx.hpp>
36 #endif
37
38 namespace boost{
39
40 #ifdef BOOST_MSVC
41 #pragma warning(push)
42 #pragma warning(disable: 4103)
43 #endif
44 #ifdef BOOST_HAS_ABI_HEADERS
45 #  include BOOST_ABI_PREFIX
46 #endif
47 #ifdef BOOST_MSVC
48 #pragma warning(pop)
49 #endif
50
51 //
52 // Forward declaration:
53 //
54    template <class BidiIterator, class Allocator = BOOST_DEDUCED_TYPENAME std::vector<sub_match<BidiIterator> >::allocator_type >
55 class match_results;
56
57 namespace re_detail{
58
59 //
60 // struct trivial_format_traits:
61 // defines minimum localisation support for formatting
62 // in the case that the actual regex traits is unavailable.
63 //
64 template <class charT>
65 struct trivial_format_traits
66 {
67    typedef charT char_type;
68
69    static std::ptrdiff_t length(const charT* p)
70    {
71       return global_length(p);
72    }
73    static charT tolower(charT c)
74    {
75       return ::boost::re_detail::global_lower(c);
76    }
77    static charT toupper(charT c)
78    {
79       return ::boost::re_detail::global_upper(c);
80    }
81    static int value(const charT c, int radix)
82    {
83       int result = global_value(c);
84       return result >= radix ? -1 : result;
85    }
86    int toi(const charT*& p1, const charT* p2, int radix)const
87    {
88       return global_toi(p1, p2, radix, *this);
89    }
90 };
91
92 template <class OutputIterator, class Results, class traits, class ForwardIter>
93 class basic_regex_formatter
94 {
95 public:
96    typedef typename traits::char_type char_type;
97    basic_regex_formatter(OutputIterator o, const Results& r, const traits& t)
98       : m_traits(t), m_results(r), m_out(o), m_state(output_copy), m_restore_state(output_copy), m_have_conditional(false) {}
99    OutputIterator format(ForwardIter p1, ForwardIter p2, match_flag_type f);
100    OutputIterator format(ForwardIter p1, match_flag_type f)
101    {
102       return format(p1, p1 + m_traits.length(p1), f);
103    }
104 private:
105    typedef typename Results::value_type sub_match_type;
106    enum output_state
107    {
108       output_copy,
109       output_next_lower,
110       output_next_upper,
111       output_lower,
112       output_upper,
113       output_none
114    };
115
116    void put(char_type c);
117    void put(const sub_match_type& sub);
118    void format_all();
119    void format_perl();
120    void format_escape();
121    void format_conditional();
122    void format_until_scope_end();
123    bool handle_perl_verb(bool have_brace);
124
125    inline typename Results::value_type const& get_named_sub(ForwardIter i, ForwardIter j, const mpl::false_&)
126    {
127       std::vector<char_type> v(i, j);
128       return (i != j) ? this->m_results.named_subexpression(&v[0], &v[0] + v.size())
129          : this->m_results.named_subexpression(static_cast<const char_type*>(0), static_cast<const char_type*>(0));
130    }
131    inline typename Results::value_type const& get_named_sub(ForwardIter i, ForwardIter j, const mpl::true_&)
132    {
133       return this->m_results.named_subexpression(i, j);
134    }
135    inline typename Results::value_type const& get_named_sub(ForwardIter i, ForwardIter j)
136    {
137       typedef typename boost::is_convertible<ForwardIter, const char_type*>::type tag_type;
138       return get_named_sub(i, j, tag_type());
139    }
140    inline int get_named_sub_index(ForwardIter i, ForwardIter j, const mpl::false_&)
141    {
142       std::vector<char_type> v(i, j);
143       return (i != j) ? this->m_results.named_subexpression_index(&v[0], &v[0] + v.size())
144          : this->m_results.named_subexpression_index(static_cast<const char_type*>(0), static_cast<const char_type*>(0));
145    }
146    inline int get_named_sub_index(ForwardIter i, ForwardIter j, const mpl::true_&)
147    {
148       return this->m_results.named_subexpression_index(i, j);
149    }
150    inline int get_named_sub_index(ForwardIter i, ForwardIter j)
151    {
152       typedef typename boost::is_convertible<ForwardIter, const char_type*>::type tag_type;
153       return get_named_sub_index(i, j, tag_type());
154    }
155    inline int toi(ForwardIter& i, ForwardIter j, int base, const boost::mpl::false_&)
156    {
157       if(i != j)
158       {
159          std::vector<char_type> v(i, j);
160          const char_type* start = &v[0];
161          const char_type* pos = start;
162          int r = m_traits.toi(pos, &v[0] + v.size(), base);
163          std::advance(i, pos - start);
164          return r;
165       }
166       return -1;
167    }
168    inline int toi(ForwardIter& i, ForwardIter j, int base, const boost::mpl::true_&)
169    {
170       return m_traits.toi(i, j, base);
171    }
172    inline int toi(ForwardIter& i, ForwardIter j, int base)
173    {
174       typedef typename boost::is_convertible<ForwardIter, const char_type*&>::type tag_type;
175       return toi(i, j, base, tag_type());
176    }
177
178    const traits&    m_traits;       // the traits class for localised formatting operations
179    const Results&   m_results;     // the match_results being used.
180    OutputIterator   m_out;         // where to send output.
181    ForwardIter      m_position;  // format string, current position
182    ForwardIter      m_end;       // format string end
183    match_flag_type  m_flags;      // format flags to use
184    output_state     m_state;      // what to do with the next character
185    output_state     m_restore_state;  // what state to restore to.
186    bool             m_have_conditional; // we are parsing a conditional
187 private:
188    basic_regex_formatter(const basic_regex_formatter&);
189    basic_regex_formatter& operator=(const basic_regex_formatter&);
190 };
191
192 template <class OutputIterator, class Results, class traits, class ForwardIter>
193 OutputIterator basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::format(ForwardIter p1, ForwardIter p2, match_flag_type f)
194 {
195    m_position = p1;
196    m_end = p2;
197    m_flags = f;
198    format_all();
199    return m_out;
200 }
201
202 template <class OutputIterator, class Results, class traits, class ForwardIter>
203 void basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::format_all()
204 {
205    // over and over:
206    while(m_position != m_end)
207    {
208       switch(*m_position)
209       {
210       case '&':
211          if(m_flags & ::boost::regex_constants::format_sed)
212          {
213             ++m_position;
214             put(m_results[0]);
215             break;
216          }
217          put(*m_position++);
218          break;
219       case '\\':
220          format_escape();
221          break;
222       case '(':
223          if(m_flags & boost::regex_constants::format_all)
224          {
225             ++m_position;
226             bool have_conditional = m_have_conditional;
227             m_have_conditional = false;
228             format_until_scope_end();
229             m_have_conditional = have_conditional;
230             if(m_position == m_end)
231                return;
232             BOOST_ASSERT(*m_position == static_cast<char_type>(')'));
233             ++m_position;  // skip the closing ')'
234             break;
235          }
236          put(*m_position);
237          ++m_position;
238          break;
239       case ')':
240          if(m_flags & boost::regex_constants::format_all)
241          {
242             return;
243          }
244          put(*m_position);
245          ++m_position;
246          break;
247       case ':':
248          if((m_flags & boost::regex_constants::format_all) && m_have_conditional)
249          {
250             return;
251          }
252          put(*m_position);
253          ++m_position;
254          break;
255       case '?':
256          if(m_flags & boost::regex_constants::format_all)
257          {
258             ++m_position;
259             format_conditional();
260             break;
261          }
262          put(*m_position);
263          ++m_position;
264          break;
265       case '$':
266          if((m_flags & format_sed) == 0)
267          {
268             format_perl();
269             break;
270          }
271          // fall through, not a special character:
272       default:
273          put(*m_position);
274          ++m_position;
275          break;
276       }
277    }
278 }
279
280 template <class OutputIterator, class Results, class traits, class ForwardIter>
281 void basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::format_perl()
282 {
283    //
284    // On entry *m_position points to a '$' character
285    // output the information that goes with it:
286    //
287    BOOST_ASSERT(*m_position == '$');
288    //
289    // see if this is a trailing '$':
290    //
291    if(++m_position == m_end)
292    {
293       --m_position;
294       put(*m_position);
295       ++m_position;
296       return;
297    }
298    //
299    // OK find out what kind it is:
300    //
301    bool have_brace = false;
302    ForwardIter save_position = m_position;
303    switch(*m_position)
304    {
305    case '&':
306       ++m_position;
307       put(this->m_results[0]);
308       break;
309    case '`':
310       ++m_position;
311       put(this->m_results.prefix());
312       break;
313    case '\'':
314       ++m_position;
315       put(this->m_results.suffix());
316       break;
317    case '$':
318       put(*m_position++);
319       break;
320    case '+':
321       if((++m_position != m_end) && (*m_position == '{'))
322       {
323          ForwardIter base = ++m_position;
324          while((m_position != m_end) && (*m_position != '}')) ++m_position;
325          if(m_position != m_end)
326          {
327             // Named sub-expression:
328             put(get_named_sub(base, m_position));
329             ++m_position;
330             break;
331          }
332          else
333          {
334             m_position = --base;
335          }
336       }
337       put((this->m_results)[this->m_results.size() > 1 ? this->m_results.size() - 1 : 1]);
338       break;
339    case '{':
340       have_brace = true;
341       ++m_position;
342       // fall through....
343    default:
344       // see if we have a number:
345       {
346          std::ptrdiff_t len = ::boost::re_detail::distance(m_position, m_end);
347          //len = (std::min)(static_cast<std::ptrdiff_t>(2), len);
348          int v = this->toi(m_position, m_position + len, 10);
349          if((v < 0) || (have_brace && ((m_position == m_end) || (*m_position != '}'))))
350          {
351             // Look for a Perl-5.10 verb:
352             if(!handle_perl_verb(have_brace))
353             {
354                // leave the $ as is, and carry on:
355                m_position = --save_position;
356                put(*m_position);
357                ++m_position;
358             }
359             break;
360          }
361          // otherwise output sub v:
362          put(this->m_results[v]);
363          if(have_brace)
364             ++m_position;
365       }
366    }
367 }
368
369 template <class OutputIterator, class Results, class traits, class ForwardIter>
370 bool basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::handle_perl_verb(bool have_brace)
371 {
372    // 
373    // We may have a capitalised string containing a Perl action:
374    //
375    static const char_type MATCH[] = { 'M', 'A', 'T', 'C', 'H' };
376    static const char_type PREMATCH[] = { 'P', 'R', 'E', 'M', 'A', 'T', 'C', 'H' };
377    static const char_type POSTMATCH[] = { 'P', 'O', 'S', 'T', 'M', 'A', 'T', 'C', 'H' };
378    static const char_type LAST_PAREN_MATCH[] = { 'L', 'A', 'S', 'T', '_', 'P', 'A', 'R', 'E', 'N', '_', 'M', 'A', 'T', 'C', 'H' };
379    static const char_type LAST_SUBMATCH_RESULT[] = { 'L', 'A', 'S', 'T', '_', 'S', 'U', 'B', 'M', 'A', 'T', 'C', 'H', '_', 'R', 'E', 'S', 'U', 'L', 'T' };
380    static const char_type LAST_SUBMATCH_RESULT_ALT[] = { '^', 'N' };
381
382    if(m_position == m_end)
383       return false;
384    if(have_brace && (*m_position == '^'))
385       ++m_position;
386
387    int max_len = m_end - m_position;
388
389    if((max_len >= 5) && std::equal(m_position, m_position + 5, MATCH))
390    {
391       m_position += 5;
392       if(have_brace)
393       {
394          if((m_position != m_end) && (*m_position == '}'))
395             ++m_position;
396          else
397          {
398             m_position -= 5;
399             return false;
400          }
401       }
402       put(this->m_results[0]);
403       return true;
404    }
405    if((max_len >= 8) && std::equal(m_position, m_position + 8, PREMATCH))
406    {
407       m_position += 8;
408       if(have_brace)
409       {
410          if((m_position != m_end) && (*m_position == '}'))
411             ++m_position;
412          else
413          {
414             m_position -= 8;
415             return false;
416          }
417       }
418       put(this->m_results.prefix());
419       return true;
420    }
421    if((max_len >= 9) && std::equal(m_position, m_position + 9, POSTMATCH))
422    {
423       m_position += 9;
424       if(have_brace)
425       {
426          if((m_position != m_end) && (*m_position == '}'))
427             ++m_position;
428          else
429          {
430             m_position -= 9;
431             return false;
432          }
433       }
434       put(this->m_results.suffix());
435       return true;
436    }
437    if((max_len >= 16) && std::equal(m_position, m_position + 16, LAST_PAREN_MATCH))
438    {
439       m_position += 16;
440       if(have_brace)
441       {
442          if((m_position != m_end) && (*m_position == '}'))
443             ++m_position;
444          else
445          {
446             m_position -= 16;
447             return false;
448          }
449       }
450       put((this->m_results)[this->m_results.size() > 1 ? this->m_results.size() - 1 : 1]);
451       return true;
452    }
453    if((max_len >= 20) && std::equal(m_position, m_position + 20, LAST_SUBMATCH_RESULT))
454    {
455       m_position += 20;
456       if(have_brace)
457       {
458          if((m_position != m_end) && (*m_position == '}'))
459             ++m_position;
460          else
461          {
462             m_position -= 20;
463             return false;
464          }
465       }
466       put(this->m_results.get_last_closed_paren());
467       return true;
468    }
469    if((max_len >= 2) && std::equal(m_position, m_position + 2, LAST_SUBMATCH_RESULT_ALT))
470    {
471       m_position += 2;
472       if(have_brace)
473       {
474          if((m_position != m_end) && (*m_position == '}'))
475             ++m_position;
476          else
477          {
478             m_position -= 2;
479             return false;
480          }
481       }
482       put(this->m_results.get_last_closed_paren());
483       return true;
484    }
485    return false;
486 }
487
488 template <class OutputIterator, class Results, class traits, class ForwardIter>
489 void basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::format_escape()
490 {
491    // skip the escape and check for trailing escape:
492    if(++m_position == m_end)
493    {
494       put(static_cast<char_type>('\\'));
495       return;
496    }
497    // now switch on the escape type:
498    switch(*m_position)
499    {
500    case 'a':
501       put(static_cast<char_type>('\a'));
502       ++m_position;
503       break;
504    case 'f':
505       put(static_cast<char_type>('\f'));
506       ++m_position;
507       break;
508    case 'n':
509       put(static_cast<char_type>('\n'));
510       ++m_position;
511       break;
512    case 'r':
513       put(static_cast<char_type>('\r'));
514       ++m_position;
515       break;
516    case 't':
517       put(static_cast<char_type>('\t'));
518       ++m_position;
519       break;
520    case 'v':
521       put(static_cast<char_type>('\v'));
522       ++m_position;
523       break;
524    case 'x':
525       if(++m_position == m_end)
526       {
527          put(static_cast<char_type>('x'));
528          return;
529       }
530       // maybe have \x{ddd}
531       if(*m_position == static_cast<char_type>('{'))
532       {
533          ++m_position;
534          int val = this->toi(m_position, m_end, 16);
535          if(val < 0)
536          {
537             // invalid value treat everything as literals:
538             put(static_cast<char_type>('x'));
539             put(static_cast<char_type>('{'));
540             return;
541          }
542          if((m_position == m_end) || (*m_position != static_cast<char_type>('}')))
543          {
544             --m_position;
545             while(*m_position != static_cast<char_type>('\\'))
546                --m_position;
547             ++m_position;
548             put(*m_position++);
549             return;
550          }
551          ++m_position;
552          put(static_cast<char_type>(val));
553          return;
554       }
555       else
556       {
557          std::ptrdiff_t len = ::boost::re_detail::distance(m_position, m_end);
558          len = (std::min)(static_cast<std::ptrdiff_t>(2), len);
559          int val = this->toi(m_position, m_position + len, 16);
560          if(val < 0)
561          {
562             --m_position;
563             put(*m_position++);
564             return;
565          }
566          put(static_cast<char_type>(val));
567       }
568       break;
569    case 'c':
570       if(++m_position == m_end)
571       {
572          --m_position;
573          put(*m_position++);
574          return;
575       }
576       put(static_cast<char_type>(*m_position++ % 32));
577       break;
578    case 'e':
579       put(static_cast<char_type>(27));
580       ++m_position;
581       break;
582    default:
583       // see if we have a perl specific escape:
584       if((m_flags & boost::regex_constants::format_sed) == 0)
585       {
586          bool breakout = false;
587          switch(*m_position)
588          {
589          case 'l':
590             ++m_position;
591             m_restore_state = m_state;
592             m_state = output_next_lower;
593             breakout = true;
594             break;
595          case 'L':
596             ++m_position;
597             m_state = output_lower;
598             breakout = true;
599             break;
600          case 'u':
601             ++m_position;
602             m_restore_state = m_state;
603             m_state = output_next_upper;
604             breakout = true;
605             break;
606          case 'U':
607             ++m_position;
608             m_state = output_upper;
609             breakout = true;
610             break;
611          case 'E':
612             ++m_position;
613             m_state = output_copy;
614             breakout = true;
615             break;
616          }
617          if(breakout)
618             break;
619       }
620       // see if we have a \n sed style backreference:
621       std::ptrdiff_t len = ::boost::re_detail::distance(m_position, m_end);
622       len = (std::min)(static_cast<std::ptrdiff_t>(1), len);
623       int v = this->toi(m_position, m_position+len, 10);
624       if((v > 0) || ((v == 0) && (m_flags & ::boost::regex_constants::format_sed)))
625       {
626          put(m_results[v]);
627          break;
628       }
629       else if(v == 0)
630       {
631          // octal ecape sequence:
632          --m_position;
633          len = ::boost::re_detail::distance(m_position, m_end);
634          len = (std::min)(static_cast<std::ptrdiff_t>(4), len);
635          v = this->toi(m_position, m_position + len, 8);
636          BOOST_ASSERT(v >= 0);
637          put(static_cast<char_type>(v));
638          break;
639       }
640       // Otherwise output the character "as is":
641       put(*m_position++);
642       break;
643    }
644 }
645
646 template <class OutputIterator, class Results, class traits, class ForwardIter>
647 void basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::format_conditional()
648 {
649    if(m_position == m_end)
650    {
651       // oops trailing '?':
652       put(static_cast<char_type>('?'));
653       return;
654    }
655    int v;
656    if(*m_position == '{')
657    {
658       ForwardIter base = m_position;
659       ++m_position;
660       v = this->toi(m_position, m_end, 10);
661       if(v < 0)
662       {
663          // Try a named subexpression:
664          while((m_position != m_end) && (*m_position != '}'))
665             ++m_position;
666          v = this->get_named_sub_index(base + 1, m_position);
667       }
668       if((v < 0) || (*m_position != '}'))
669       {
670          m_position = base;
671          // oops trailing '?':
672          put(static_cast<char_type>('?'));
673          return;
674       }
675       // Skip trailing '}':
676       ++m_position;
677    }
678    else
679    {
680       std::ptrdiff_t len = ::boost::re_detail::distance(m_position, m_end);
681       len = (std::min)(static_cast<std::ptrdiff_t>(2), len);
682       v = this->toi(m_position, m_position + len, 10);
683    }
684    if(v < 0)
685    {
686       // oops not a number:
687       put(static_cast<char_type>('?'));
688       return;
689    }
690
691    // output varies depending upon whether sub-expression v matched or not:
692    if(m_results[v].matched)
693    {
694       m_have_conditional = true;
695       format_all();
696       m_have_conditional = false;
697       if((m_position != m_end) && (*m_position == static_cast<char_type>(':')))
698       {
699          // skip the ':':
700          ++m_position;
701          // save output state, then turn it off:
702          output_state saved_state = m_state;
703          m_state = output_none;
704          // format the rest of this scope:
705          format_until_scope_end();
706          // restore output state:
707          m_state = saved_state;
708       }
709    }
710    else
711    {
712       // save output state, then turn it off:
713       output_state saved_state = m_state;
714       m_state = output_none;
715       // format until ':' or ')':
716       m_have_conditional = true;
717       format_all();
718       m_have_conditional = false;
719       // restore state:
720       m_state = saved_state;
721       if((m_position != m_end) && (*m_position == static_cast<char_type>(':')))
722       {
723          // skip the ':':
724          ++m_position;
725          // format the rest of this scope:
726          format_until_scope_end();
727       }
728    }
729 }
730
731 template <class OutputIterator, class Results, class traits, class ForwardIter>
732 void basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::format_until_scope_end()
733 {
734    do
735    {
736       format_all();
737       if((m_position == m_end) || (*m_position == static_cast<char_type>(')')))
738          return;
739       put(*m_position++);
740    }while(m_position != m_end);
741 }
742
743 template <class OutputIterator, class Results, class traits, class ForwardIter>
744 void basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::put(char_type c)
745 {
746    // write a single character to output
747    // according to which case translation mode we are in:
748    switch(this->m_state)
749    {
750    case output_none:
751       return;
752    case output_next_lower:
753       c = m_traits.tolower(c);
754       this->m_state = m_restore_state;
755       break;
756    case output_next_upper:
757       c = m_traits.toupper(c);
758       this->m_state = m_restore_state;
759       break;
760    case output_lower:
761       c = m_traits.tolower(c);
762       break;
763    case output_upper:
764       c = m_traits.toupper(c);
765       break;
766    default:
767       break;
768    }
769    *m_out = c;
770    ++m_out;
771 }
772
773 template <class OutputIterator, class Results, class traits, class ForwardIter>
774 void basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::put(const sub_match_type& sub)
775 {
776    typedef typename sub_match_type::iterator iterator_type;
777    iterator_type i = sub.first;
778    while(i != sub.second)
779    {
780       put(*i);
781       ++i;
782    }
783 }
784
785 template <class S>
786 class string_out_iterator
787 #ifndef BOOST_NO_STD_ITERATOR
788    : public std::iterator<std::output_iterator_tag, typename S::value_type>
789 #endif
790 {
791    S* out;
792 public:
793    string_out_iterator(S& s) : out(&s) {}
794    string_out_iterator& operator++() { return *this; }
795    string_out_iterator& operator++(int) { return *this; }
796    string_out_iterator& operator*() { return *this; }
797    string_out_iterator& operator=(typename S::value_type v) 
798    { 
799       out->append(1, v); 
800       return *this; 
801    }
802
803 #ifdef BOOST_NO_STD_ITERATOR
804    typedef std::ptrdiff_t difference_type;
805    typedef typename S::value_type value_type;
806    typedef value_type* pointer;
807    typedef value_type& reference;
808    typedef std::output_iterator_tag iterator_category;
809 #endif
810 };
811
812 template <class OutputIterator, class Iterator, class Alloc, class ForwardIter, class traits>
813 OutputIterator regex_format_imp(OutputIterator out,
814                           const match_results<Iterator, Alloc>& m,
815                           ForwardIter p1, ForwardIter p2,
816                           match_flag_type flags,
817                           const traits& t
818                          )
819 {
820    if(flags & regex_constants::format_literal)
821    {
822       return re_detail::copy(p1, p2, out);
823    }
824
825    re_detail::basic_regex_formatter<
826       OutputIterator, 
827       match_results<Iterator, Alloc>, 
828       traits, ForwardIter> f(out, m, t);
829    return f.format(p1, p2, flags);
830 }
831
832 #ifndef BOOST_NO_SFINAE
833
834 BOOST_MPL_HAS_XXX_TRAIT_DEF(const_iterator)
835
836 struct any_type { any_type(...); };
837 typedef char no_type;
838 typedef char (&unary_type)[2];
839 typedef char (&binary_type)[3];
840 typedef char (&ternary_type)[4];
841
842 no_type check_is_formatter(unary_type, binary_type, ternary_type);
843 template<typename T>
844 unary_type check_is_formatter(T const &, binary_type, ternary_type);
845 template<typename T>
846 binary_type check_is_formatter(unary_type, T const &, ternary_type);
847 template<typename T, typename U>
848 binary_type check_is_formatter(T const &, U const &, ternary_type);
849 template<typename T>
850 ternary_type check_is_formatter(unary_type, binary_type, T const &);
851 template<typename T, typename U>
852 ternary_type check_is_formatter(T const &, binary_type, U const &);
853 template<typename T, typename U>
854 ternary_type check_is_formatter(unary_type, T const &, U const &);
855 template<typename T, typename U, typename V>
856 ternary_type check_is_formatter(T const &, U const &, V const &);
857
858 struct unary_binary_ternary
859 {
860     typedef unary_type (*unary_fun)(any_type);
861     typedef binary_type (*binary_fun)(any_type, any_type);
862     typedef ternary_type (*ternary_fun)(any_type, any_type, any_type);
863     operator unary_fun();
864     operator binary_fun();
865     operator ternary_fun();
866 };
867
868 template<typename Formatter, bool IsFunction = boost::is_function<Formatter>::value>
869 struct formatter_wrapper
870   : Formatter
871   , unary_binary_ternary
872 {
873    formatter_wrapper(){}
874 };
875
876 template<typename Formatter>
877 struct formatter_wrapper<Formatter, true>
878   : unary_binary_ternary
879 {
880     operator Formatter *();
881 };
882
883 template<typename Formatter>
884 struct formatter_wrapper<Formatter *, false>
885   : unary_binary_ternary
886 {
887     operator Formatter *();
888 };
889
890 template <class F, class M, class O>
891 struct format_traits_imp
892 {
893 private:
894    //
895    // F must be a pointer, a function, or a class with a function call operator:
896    //
897    BOOST_STATIC_ASSERT((::boost::is_pointer<F>::value || ::boost::is_function<F>::value || ::boost::is_class<F>::value));
898    static formatter_wrapper<F> f;
899    static M m;
900    static O out;
901    static boost::regex_constants::match_flag_type flags;
902 public:
903    BOOST_STATIC_CONSTANT(int, value = sizeof(check_is_formatter(f(m), f(m, out), f(m, out, flags))));
904 };
905
906 template <class F, class M, class O>
907 struct format_traits
908 {
909 public:
910    // 
911    // Type is mpl::int_<N> where N is one of:
912    //
913    // 0 : F is a pointer to a presumably null-terminated string.
914    // 1 : F is a character-container such as a std::string.
915    // 2 : F is a Unary Functor.
916    // 3 : F is a Binary Functor.
917    // 4 : F is a Ternary Functor.
918    //
919    typedef typename boost::mpl::if_<
920       boost::mpl::and_<boost::is_pointer<F>, boost::mpl::not_<boost::is_function<typename boost::remove_pointer<F>::type> > >,
921       boost::mpl::int_<0>,
922       typename boost::mpl::if_<
923          has_const_iterator<F>,
924          boost::mpl::int_<1>,
925          boost::mpl::int_<format_traits_imp<F, M, O>::value>
926       >::type
927    >::type type;
928    //
929    // This static assertion will fail if the functor passed does not accept
930    // the same type of arguments passed.
931    //
932    BOOST_STATIC_ASSERT( boost::is_class<F>::value && !has_const_iterator<F>::value ? (type::value > 1) : true);
933 };
934
935 #else // BOOST_NO_SFINAE
936
937 template <class F, class M, class O>
938 struct format_traits
939 {
940 public:
941    // 
942    // Type is mpl::int_<N> where N is one of:
943    //
944    // 0 : F is a pointer to a presumably null-terminated string.
945    // 1 : F is a character-container such as a std::string.
946    //
947    // Other options such as F being a Functor are not supported without
948    // SFINAE support.
949    //
950    typedef typename boost::mpl::if_<
951       boost::is_pointer<F>,
952       boost::mpl::int_<0>,
953       boost::mpl::int_<1>
954    >::type type;
955 };
956
957 #endif // BOOST_NO_SFINAE
958
959 template <class Base, class Match>
960 struct format_functor3
961 {
962    format_functor3(Base b) : func(b) {}
963    template <class OutputIter>
964    OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type f)
965    {
966       return func(m, i, f);
967    }
968    template <class OutputIter, class Traits>
969    OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type f, const Traits&)
970    {
971       return (*this)(m, i, f);
972    }
973 private:
974    Base func;
975    format_functor3(const format_functor3&);
976    format_functor3& operator=(const format_functor3&);
977 };
978
979 template <class Base, class Match>
980 struct format_functor2
981 {
982    format_functor2(Base b) : func(b) {}
983    template <class OutputIter>
984    OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type /*f*/)
985    {
986       return func(m, i);
987    }
988    template <class OutputIter, class Traits>
989    OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type f, const Traits&)
990    {
991       return (*this)(m, i, f);
992    }
993 private:
994    Base func;
995    format_functor2(const format_functor2&);
996    format_functor2& operator=(const format_functor2&);
997 };
998
999 template <class Base, class Match>
1000 struct format_functor1
1001 {
1002    format_functor1(Base b) : func(b) {}
1003
1004    template <class S, class OutputIter>
1005    OutputIter do_format_string(const S& s, OutputIter i)
1006    {
1007       return re_detail::copy(s.begin(), s.end(), i);
1008    }
1009    template <class S, class OutputIter>
1010    inline OutputIter do_format_string(const S* s, OutputIter i)
1011    {
1012       while(s && *s)
1013       {
1014          *i = *s;
1015          ++i;
1016          ++s;
1017       }
1018       return i;
1019    }
1020    template <class OutputIter>
1021    OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type /*f*/)
1022    {
1023       return do_format_string(func(m), i);
1024    }
1025    template <class OutputIter, class Traits>
1026    OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type f, const Traits&)
1027    {
1028       return (*this)(m, i, f);
1029    }
1030 private:
1031    Base func;
1032    format_functor1(const format_functor1&);
1033    format_functor1& operator=(const format_functor1&);
1034 };
1035
1036 template <class charT, class Match, class Traits>
1037 struct format_functor_c_string
1038 {
1039    format_functor_c_string(const charT* ps) : func(ps) {}
1040
1041    template <class OutputIter>
1042    OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type f, const Traits& t = Traits())
1043    {
1044       typedef typename Match::char_type char_type;
1045       const charT* end = func;
1046       while(*end) ++end;
1047       return regex_format_imp(i, m, func, end, f, t);
1048    }
1049 private:
1050    const charT* func;
1051    format_functor_c_string(const format_functor_c_string&);
1052    format_functor_c_string& operator=(const format_functor_c_string&);
1053 };
1054
1055 template <class Container, class Match, class Traits>
1056 struct format_functor_container
1057 {
1058    format_functor_container(const Container& c) : func(c) {}
1059
1060    template <class OutputIter>
1061    OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type f, const Traits& t = Traits())
1062    {
1063       typedef typename Match::char_type char_type;
1064       return re_detail::regex_format_imp(i, m, func.begin(), func.end(), f, t);
1065    }
1066 private:
1067    const Container& func;
1068    format_functor_container(const format_functor_container&);
1069    format_functor_container& operator=(const format_functor_container&);
1070 };
1071
1072 template <class Func, class Match, class OutputIterator, class Traits = re_detail::trivial_format_traits<typename Match::char_type> >
1073 struct compute_functor_type
1074 {
1075    typedef typename format_traits<Func, Match, OutputIterator>::type tag;
1076    typedef typename boost::remove_cv< typename boost::remove_pointer<Func>::type>::type maybe_char_type;
1077
1078    typedef typename mpl::if_<
1079       ::boost::is_same<tag, mpl::int_<0> >, format_functor_c_string<maybe_char_type, Match, Traits>,
1080       typename mpl::if_<
1081          ::boost::is_same<tag, mpl::int_<1> >, format_functor_container<Func, Match, Traits>,
1082          typename mpl::if_<
1083             ::boost::is_same<tag, mpl::int_<2> >, format_functor1<Func, Match>,
1084             typename mpl::if_<
1085                ::boost::is_same<tag, mpl::int_<3> >, format_functor2<Func, Match>, 
1086                format_functor3<Func, Match>
1087             >::type
1088          >::type
1089       >::type
1090    >::type type;
1091 };
1092
1093 } // namespace re_detail
1094
1095 template <class OutputIterator, class Iterator, class Allocator, class Functor>
1096 inline OutputIterator regex_format(OutputIterator out,
1097                           const match_results<Iterator, Allocator>& m,
1098                           Functor fmt,
1099                           match_flag_type flags = format_all
1100                          )
1101 {
1102    return m.format(out, fmt, flags);
1103 }
1104
1105 template <class Iterator, class Allocator, class Functor>
1106 inline std::basic_string<typename match_results<Iterator, Allocator>::char_type> regex_format(const match_results<Iterator, Allocator>& m, 
1107                                       Functor fmt, 
1108                                       match_flag_type flags = format_all)
1109 {
1110    return m.format(fmt, flags);
1111 }
1112
1113 #ifdef BOOST_MSVC
1114 #pragma warning(push)
1115 #pragma warning(disable: 4103)
1116 #endif
1117 #ifdef BOOST_HAS_ABI_HEADERS
1118 #  include BOOST_ABI_SUFFIX
1119 #endif
1120 #ifdef BOOST_MSVC
1121 #pragma warning(pop)
1122 #endif
1123
1124 } // namespace boost
1125
1126 #endif  // BOOST_REGEX_FORMAT_HPP
1127
1128
1129
1130
1131
1132