## include/boost/regex/v5/regex_split.hpp

97.4% Lines (37/38) 100.0% List of functions (9/9)
regex_split.hpp
f(x) Functions (9)
Function Calls Lines Blocks
boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const& boost::re_detail_600::get_default_expression<char>(char) :39 1x 100.0% 82.0% boost::re_detail_600::split_pred<std::back_insert_iterator<std::__cxx11::list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, char, std::char_traits<char>, std::allocator<char> >::split_pred(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >*, std::back_insert_iterator<std::__cxx11::list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >*, unsigned long*) :56 2x 100.0% 100.0% boost::re_detail_600::split_pred<std::ostream_iterator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char> >, char, std::char_traits<char>, std::allocator<char> >::split_pred(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >*, std::ostream_iterator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char> >*, unsigned long*) :56 99x 100.0% 100.0% boost::re_detail_600::split_pred<std::back_insert_iterator<std::__cxx11::list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, char, std::char_traits<char>, std::allocator<char> >::operator()(boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > const&) :63 103x 92.9% 44.0% boost::re_detail_600::split_pred<std::ostream_iterator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char> >, char, std::char_traits<char>, std::allocator<char> >::operator()(boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > const&) :63 98x 50.0% 42.0% unsigned long boost::regex_split<std::back_insert_iterator<std::__cxx11::list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, char, std::char_traits<char>, std::allocator<char>, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(std::back_insert_iterator<std::__cxx11::list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, unsigned long) :97 2x 100.0% 76.0% unsigned long boost::regex_split<std::ostream_iterator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char> >, char, std::char_traits<char>, std::allocator<char>, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(std::ostream_iterator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, unsigned long) :97 99x 73.3% 48.0% unsigned long boost::regex_split<std::back_insert_iterator<std::__cxx11::list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, char, std::char_traits<char>, std::allocator<char>, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(std::back_insert_iterator<std::__cxx11::list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags) :132 1x 100.0% 100.0% unsigned long boost::regex_split<std::back_insert_iterator<std::__cxx11::list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, char, std::char_traits<char>, std::allocator<char> >(std::back_insert_iterator<std::__cxx11::list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) :141 1x 100.0% 100.0%

Line | TLA | Hits | Source Code
--- | --- | --- | ---
1 |  |  | /*
2 |  |  | *
3 |  |  | * Copyright (c) 1998-2002
4 |  |  | * John Maddock
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_split.hpp
15 |  |  | * VERSION see <boost/version.hpp>
16 |  |  | * DESCRIPTION: Implements regex_split and associated functions.
17 |  |  | * Note this is an internal header file included
18 |  |  | * by regex.hpp, do not include on its own.
19 |  |  | */
20 |  |  |
21 |  |  | #ifndef BOOST_REGEX_SPLIT_HPP
22 |  |  | #define BOOST_REGEX_SPLIT_HPP
23 |  |  |
24 |  |  | #include <boost/regex/v5/basic_regex.hpp>
25 |  |  | #include <boost/regex/v5/match_results.hpp>
26 |  |  |
27 |  |  | namespace boost{
28 |  |  |
29 |  |  | #ifdef BOOST_REGEX_MSVC
30 |  |  | # pragma warning(push)
31 |  |  | #if BOOST_REGEX_MSVC < 1910
32 |  |  | #pragma warning(disable:4800)
33 |  |  | #endif
34 |  |  | #endif
35 |  |  |
36 |  |  | namespace BOOST_REGEX_DETAIL_NS{
37 |  |  |
38 |  |  | template <class charT>
39 |  | 1x | const basic_regex<charT>& get_default_expression(charT)
40 |  |  | {
41 |  |  | static const charT expression_text[4] = { '\\', 's', '+', '\00', };
42 |  | 1x | static const basic_regex<charT> e(expression_text);
43 |  | 1x | return e;
44 |  |  | }
45 |  |  |
46 |  |  | template <class OutputIterator, class charT, class Traits1, class Alloc1>
47 |  |  | class split_pred
48 |  |  | {
49 |  |  | typedef std::basic_string<charT, Traits1, Alloc1> string_type;
50 |  |  | typedef typename string_type::const_iterator iterator_type;
51 |  |  | iterator_type* p_last;
52 |  |  | OutputIterator* p_out;
53 |  |  | std::size_t* p_max;
54 |  |  | std::size_t initial_max;
55 |  |  | public:
56 |  | 101x | split_pred(iterator_type* a, OutputIterator* b, std::size_t* c)
57 |  | 101x | : p_last(a), p_out(b), p_max(c), initial_max(*c) {}
58 |  |  |
59 |  |  | bool operator()(const match_results<iterator_type>& what);
60 |  |  | };
61 |  |  |
62 |  |  | template <class OutputIterator, class charT, class Traits1, class Alloc1>
63 |  | 201x | bool split_pred<OutputIterator, charT, Traits1, Alloc1>::operator()
64 |  |  | (const match_results<iterator_type>& what)
65 |  |  | {
66 |  | 201x | *p_last = what[0].second;
67 |  | 201x | if(what.size() > 1)
68 |  |  | {
69 |  |  | // output sub-expressions only:
70 |  | 294x | for(unsigned i = 1; i < what.size(); ++i)
71 |  |  | {
72 |  | 196x | *(*p_out) = what.str(i);
73 |  | 196x | ++(*p_out);
74 |  | 196x | if(0 == --*p_max) return false;
75 |  |  | }
76 |  | 98x | return *p_max != 0;
77 |  |  | }
78 |  |  | else
79 |  |  | {
80 |  |  | // output $` only if it's not-null or not at the start of the input:
81 |  | 5x | const sub_match<iterator_type>& sub = what[-1];
82 |  | 5x | if((sub.first != sub.second) || (*p_max != initial_max))
83 |  |  | {
84 |  | 5x | *(*p_out) = sub.str();
85 |  | 5x | ++(*p_out);
86 |  | 5x | return --*p_max;
87 |  |  | }
88 |  |  | }
89 |  |  | //
90 |  |  | // initial null, do nothing:
91 |  | ✗ | return true;
92 |  |  | }
93 |  |  |
94 |  |  | } // namespace BOOST_REGEX_DETAIL_NS
95 |  |  |
96 |  |  | BOOST_REGEX_MODULE_EXPORT template <class OutputIterator, class charT, class Traits1, class Alloc1, class Traits2>
97 |  | 101x | std::size_t regex_split(OutputIterator out,
98 |  |  | std::basic_string<charT, Traits1, Alloc1>& s,
99 |  |  | const basic_regex<charT, Traits2>& e,
100 |  |  | match_flag_type flags,
101 |  |  | std::size_t max_split)
102 |  |  | {
103 |  |  | typedef typename std::basic_string<charT, Traits1, Alloc1>::const_iterator ci_t;
104 |  |  | //typedef typename match_results<ci_t>::allocator_type match_allocator;
105 |  | 101x | ci_t last = s.begin();
106 |  | 101x | std::size_t init_size = max_split;
107 |  | 101x | BOOST_REGEX_DETAIL_NS::split_pred<OutputIterator, charT, Traits1, Alloc1> pred(&last, &out, &max_split);
108 |  | 101x | ci_t i, j;
109 |  | 101x | i = s.begin();
110 |  | 101x | j = s.end();
111 |  | 101x | regex_grep(pred, i, j, e, flags);
112 |  |  | //
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
115 |  |  | // than whitespace:
116 |  | 101x | if(max_split && (last != s.end()) && (e.mark_count() == 0))
117 |  |  | {
118 |  | 1x | *out = std::basic_string<charT, Traits1, Alloc1>((ci_t)last, (ci_t)s.end());
119 |  | 1x | ++out;
120 |  | 1x | last = s.end();
121 |  | 1x | --max_split;
122 |  |  | }
123 |  |  | //
124 |  |  | // delete from the string everything that has been processed so far:
125 |  | 101x | s.erase(0, last - s.begin());
126 |  |  | //
127 |  |  | // return the number of new records pushed:
128 |  | 101x | return init_size - max_split;
129 |  |  | }
130 |  |  |
131 |  |  | BOOST_REGEX_MODULE_EXPORT template <class OutputIterator, class charT, class Traits1, class Alloc1, class Traits2>
132 |  | 1x | inline std::size_t regex_split(OutputIterator out,
133 |  |  | std::basic_string<charT, Traits1, Alloc1>& s,
134 |  |  | const basic_regex<charT, Traits2>& e,
135 |  |  | match_flag_type flags = match_default)
136 |  |  | {
137 |  | 1x | return regex_split(out, s, e, flags, UINT_MAX);
138 |  |  | }
139 |  |  |
140 |  |  | BOOST_REGEX_MODULE_EXPORT template <class OutputIterator, class charT, class Traits1, class Alloc1>
141 |  | 1x | inline std::size_t regex_split(OutputIterator out,
142 |  |  | std::basic_string<charT, Traits1, Alloc1>& s)
143 |  |  | {
144 |  | 1x | return regex_split(out, s, BOOST_REGEX_DETAIL_NS::get_default_expression(charT(0)), match_default, UINT_MAX);
145 |  |  | }
146 |  |  |
147 |  |  | #ifdef BOOST_REGEX_MSVC
148 |  |  | # pragma warning(pop)
149 |  |  | #endif
150 |  |  |
151 |  |  | } // namespace boost
152 |  |  |
153 |  |  | #endif
154 |  |  |
155 |  |  |
156 |  |  |