3 * Copyright (c) 1998-2000
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_split.hpp
20 * DESCRIPTION: Implements regex_split and associated functions.
21 * Note this is an internal header file included
22 * by regex.hpp, do not include on its own.
25 #ifndef BOOST_REGEX_SPLIT_HPP
26 #define BOOST_REGEX_SPLIT_HPP
31 #if __BORLANDC__ == 0x530
32 #pragma option push -a4 -b -Ve
33 #elif __BORLANDC__ > 0x530
34 #pragma option push -a8 -b -Ve
40 template <class charT>
41 const reg_expression<charT>& get_default_expression(charT)
43 static const charT expression_text[] = { '\\', 's', '+', '\00', };
44 static const reg_expression<charT> e(expression_text);
48 template <class OutputIterator, class charT, class Traits1, class Alloc1, class Alloc2>
51 typedef std::basic_string<charT, Traits1, Alloc1> string_type;
52 typedef typename string_type::const_iterator iterator_type;
53 iterator_type* p_last;
54 OutputIterator* p_out;
56 std::size_t initial_max;
58 split_pred(iterator_type* a, OutputIterator* b, std::size_t* c)
59 : p_last(a), p_out(b), p_max(c), initial_max(*c) {}
61 bool operator()(const match_results<iterator_type, Alloc2>& what);
64 template <class OutputIterator, class charT, class Traits1, class Alloc1, class Alloc2>
65 bool split_pred<OutputIterator, charT, Traits1, Alloc1, Alloc2>::operator()
66 (const match_results<iterator_type, Alloc2>& what)
68 *p_last = what[0].second;
71 // output sub-expressions only:
72 for(unsigned i = 1; i < what.size(); ++i)
74 *(*p_out) = static_cast<string_type>(what[i]);
81 // output $` only if it's not-null or not at the start of the input:
82 const sub_match<iterator_type>& sub = what[-1];
83 if((sub.first != sub.second) || (*p_max != initial_max))
85 *(*p_out) = static_cast<string_type>(sub);
91 // initial null, do nothing:
95 } // namespace re_detail
97 template <class OutputIterator, class charT, class Traits1, class Alloc1, class Traits2, class Alloc2>
98 std::size_t regex_split(OutputIterator out,
99 std::basic_string<charT, Traits1, Alloc1>& s,
100 const reg_expression<charT, Traits2, Alloc2>& e,
102 std::size_t max_split)
104 typedef typename std::basic_string<charT, Traits1, Alloc1>::const_iterator ci_t;
105 ci_t last = s.begin();
106 std::size_t init_size = max_split;
107 re_detail::split_pred<OutputIterator, charT, Traits1, Alloc1, Alloc2> pred(&last, &out, &max_split);
111 regex_grep(pred, i, j, e, flags);
113 // if there is still input left, do a final push as long as max_split
114 // is not exhausted, and we're not splitting sub-expressions rather
116 if(max_split && (last != s.end()) && (e.mark_count() == 1))
118 *out = std::basic_string<charT, Traits1, Alloc1>((ci_t)last, (ci_t)s.end());
124 // delete from the string everything that has been processed so far:
125 s.erase(0, last - s.begin());
127 // return the number of new records pushed:
128 return init_size - max_split;
131 template <class OutputIterator, class charT, class Traits1, class Alloc1, class Traits2, class Alloc2>
132 inline std::size_t regex_split(OutputIterator out,
133 std::basic_string<charT, Traits1, Alloc1>& s,
134 const reg_expression<charT, Traits2, Alloc2>& e,
135 unsigned flags = match_default)
137 return regex_split(out, s, e, flags, UINT_MAX);
140 template <class OutputIterator, class charT, class Traits1, class Alloc1>
141 inline std::size_t regex_split(OutputIterator out,
142 std::basic_string<charT, Traits1, Alloc1>& s)
144 return regex_split(out, s, re_detail::get_default_expression(charT(0)), match_default, UINT_MAX);
148 #if __BORLANDC__ > 0x520