Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:26

0001 /*
0002  *
0003  * Copyright (c) 1998-2002
0004  * John Maddock
0005  *
0006  * Use, modification and distribution are subject to the 
0007  * Boost Software License, Version 1.0. (See accompanying file 
0008  * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009  *
0010  */
0011  
0012  /*
0013   *   LOCATION:    see http://www.boost.org for most recent version.
0014   *   FILE         regex_split.hpp
0015   *   VERSION      see <boost/version.hpp>
0016   *   DESCRIPTION: Implements regex_split and associated functions.
0017   *                Note this is an internal header file included
0018   *                by regex.hpp, do not include on its own.
0019   */
0020 
0021 #ifndef BOOST_REGEX_SPLIT_HPP
0022 #define BOOST_REGEX_SPLIT_HPP
0023 
0024 namespace boost{
0025 
0026 #ifdef BOOST_MSVC
0027 #pragma warning(push)
0028 #pragma warning(disable: 4103)
0029 #endif
0030 #ifdef BOOST_HAS_ABI_HEADERS
0031 #  include BOOST_ABI_PREFIX
0032 #endif
0033 #ifdef BOOST_MSVC
0034 #pragma warning(pop)
0035 #endif
0036 
0037 #ifdef BOOST_MSVC
0038 #  pragma warning(push)
0039 #if BOOST_MSVC < 1910
0040 #pragma warning(disable:4800)
0041 #endif
0042 #endif
0043 
0044 namespace BOOST_REGEX_DETAIL_NS{
0045 
0046 template <class charT>
0047 const basic_regex<charT>& get_default_expression(charT)
0048 {
0049    static const charT expression_text[4] = { '\\', 's', '+', '\00', };
0050    static const basic_regex<charT> e(expression_text);
0051    return e;
0052 }
0053 
0054 template <class OutputIterator, class charT, class Traits1, class Alloc1>
0055 class split_pred
0056 {
0057    typedef std::basic_string<charT, Traits1, Alloc1> string_type;
0058    typedef typename string_type::const_iterator iterator_type;
0059    iterator_type* p_last;
0060    OutputIterator* p_out;
0061    std::size_t* p_max;
0062    std::size_t initial_max;
0063 public:
0064    split_pred(iterator_type* a, OutputIterator* b, std::size_t* c)
0065       : p_last(a), p_out(b), p_max(c), initial_max(*c) {}
0066 
0067    bool operator()(const match_results<iterator_type>& what);
0068 };
0069 
0070 template <class OutputIterator, class charT, class Traits1, class Alloc1>
0071 bool split_pred<OutputIterator, charT, Traits1, Alloc1>::operator()
0072    (const match_results<iterator_type>& what)
0073 {
0074    *p_last = what[0].second;
0075    if(what.size() > 1)
0076    {
0077       // output sub-expressions only:
0078       for(unsigned i = 1; i < what.size(); ++i)
0079       {
0080          *(*p_out) = what.str(i);
0081          ++(*p_out);
0082          if(0 == --*p_max) return false;
0083       }
0084       return *p_max != 0;
0085    }
0086    else
0087    {
0088       // output $` only if it's not-null or not at the start of the input:
0089       const sub_match<iterator_type>& sub = what[-1];
0090       if((sub.first != sub.second) || (*p_max != initial_max))
0091       {
0092          *(*p_out) = sub.str();
0093          ++(*p_out);
0094          return --*p_max;
0095       }
0096    }
0097    //
0098    // initial null, do nothing:
0099    return true;
0100 }
0101 
0102 } // namespace BOOST_REGEX_DETAIL_NS
0103 
0104 template <class OutputIterator, class charT, class Traits1, class Alloc1, class Traits2>
0105 std::size_t regex_split(OutputIterator out,
0106                    std::basic_string<charT, Traits1, Alloc1>& s, 
0107                    const basic_regex<charT, Traits2>& e,
0108                    match_flag_type flags,
0109                    std::size_t max_split)
0110 {
0111    typedef typename std::basic_string<charT, Traits1, Alloc1>::const_iterator  ci_t;
0112    //typedef typename match_results<ci_t>::allocator_type                        match_allocator;
0113    ci_t last = s.begin();
0114    std::size_t init_size = max_split;
0115    BOOST_REGEX_DETAIL_NS::split_pred<OutputIterator, charT, Traits1, Alloc1> pred(&last, &out, &max_split);
0116    ci_t i, j;
0117    i = s.begin();
0118    j = s.end();
0119    regex_grep(pred, i, j, e, flags);
0120    //
0121    // if there is still input left, do a final push as long as max_split
0122    // is not exhausted, and we're not splitting sub-expressions rather 
0123    // than whitespace:
0124    if(max_split && (last != s.end()) && (e.mark_count() == 0))
0125    {
0126       *out = std::basic_string<charT, Traits1, Alloc1>((ci_t)last, (ci_t)s.end());
0127       ++out;
0128       last = s.end();
0129       --max_split;
0130    }
0131    //
0132    // delete from the string everything that has been processed so far:
0133    s.erase(0, last - s.begin());
0134    //
0135    // return the number of new records pushed:
0136    return init_size - max_split;
0137 }
0138 
0139 template <class OutputIterator, class charT, class Traits1, class Alloc1, class Traits2>
0140 inline std::size_t regex_split(OutputIterator out,
0141                    std::basic_string<charT, Traits1, Alloc1>& s, 
0142                    const basic_regex<charT, Traits2>& e,
0143                    match_flag_type flags = match_default)
0144 {
0145    return regex_split(out, s, e, flags, UINT_MAX);
0146 }
0147 
0148 template <class OutputIterator, class charT, class Traits1, class Alloc1>
0149 inline std::size_t regex_split(OutputIterator out,
0150                    std::basic_string<charT, Traits1, Alloc1>& s)
0151 {
0152    return regex_split(out, s, BOOST_REGEX_DETAIL_NS::get_default_expression(charT(0)), match_default, UINT_MAX);
0153 }
0154 
0155 #ifdef BOOST_MSVC
0156 #  pragma warning(pop)
0157 #endif
0158 
0159 #ifdef BOOST_MSVC
0160 #pragma warning(push)
0161 #pragma warning(disable: 4103)
0162 #endif
0163 #ifdef BOOST_HAS_ABI_HEADERS
0164 #  include BOOST_ABI_SUFFIX
0165 #endif
0166 #ifdef BOOST_MSVC
0167 #pragma warning(pop)
0168 #endif
0169 
0170 } // namespace boost
0171 
0172 #endif
0173 
0174