Boost logo

Boost Users :

From: Eric Niebler (eric_at_[hidden])
Date: 2007-10-18 16:26:47


Eric Niebler wrote:
> Ovanes Markarian wrote:
>> Hello Eric,
>>
>> sorry for bothering. I use xpressive in the following context:
>
> <snip>
>
> I can't get your code to compile. Please send a minimal and complete
> source file that demonstrates the problem.

After fiddling with your code a bit, I got it to compile, but I'm not
seeing any assertions, exceptions or crashes. What version are you using?

Below is the full source. I had to comment out the line to toks.size()
-- iterator_range::size() requires random access iterators, and
regex_token_iterator is not random access. I can't imagine how this
compiles for you.

Also, your example is highly suspect at this line:
     std::string includes = *++toks.begin();

iterator_range::begin() returns an iterator *by value*. The ++ is
mutating the copy, not the iterator stored in the range. Did you mean to
do that?

#include <string>
#include <iostream>
#include <boost/tr1/functional.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/xpressive/xpressive.hpp>

namespace x = boost::xpressive;

typedef x::sregex regex_type;

const regex_type non_semicolon = ~x::as_xpr(';');
const regex_type sem_separated = *non_semicolon;

typedef boost::iterator_range<x::sregex_token_iterator> regex_tokens;
typedef ::std::vector<regex_tokens>
regex_transformed_collection;

inline regex_tokens apply_regex(regex_type const& re, std::string const&
item)
{
     typedef x::sregex_token_iterator sre_iter;
     return boost::make_iterator_range(sre_iter(item.begin(),
item.end(), re), sre_iter());
}

// usage:
int main()
{
     std::vector<std::string> v; //vector is filled with strings of
type: some_part;some_other_part

     typedef regex_transformed_collection::const_iterator re_citer;

     regex_transformed_collection sep_strings;
     ::std::transform
         (
             v.begin(), v.end(), ::std::back_inserter(sep_strings)
             , ::std::tr1::bind(&apply_regex, sem_separated,
std::tr1::placeholders::_1)
         );

     //v is in the same scope and valid
     for(re_citer curr=sep_strings.begin(), end=sep_strings.end();
curr!=end; ++curr)
     {
         regex_tokens const& toks = *curr;
         std::string source = toks.front (); //this line is fine and
returns me the first part of a string...
         // all subsequent line will raise an assertion
         std::string includes = *++toks.begin();
         //size_t sz = toks.size();
     }

     return 0;
}

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net