Reputation: 3874
If a string has been processed using a Boost tokenizer is it possible to get the position in the original string that a given token iterator is pointing to:
boost:tokenizer<> tok( "this is the original string" );
for(tokenizer<>::iterator it=tok.begin(); it!=tok.end();++it)
{
std::string strToken = *it;
int charPos = it.? /* IS THERE A METHOD? */
}
I realize I could create a specific char_separator with a defined list of 'kept delimiters' and specify keep_empty_tokens to try and track the progression of the iterator myself but I was hoping there was an easier way using just the iterator itself.
Upvotes: 3
Views: 1148
Reputation: 62985
This appears to be what you're looking for:
#include <string>
#include <iostream>
#include <boost/tokenizer.hpp>
int main()
{
typedef boost::tokenizer<> tok_t;
std::string const s = "this is the original string";
tok_t const tok(s);
for (tok_t::const_iterator it = tok.begin(), it_end = tok.end(); it != it_end; ++it)
{
std::string::difference_type const offset = it.base() - s.begin() - it->size();
std::cout << offset << "\t::\t" << *it << '\n';
}
}
Upvotes: 5
Reputation: 11669
If you need only the end of current token, base()
member function
might meet the purpose:
std::string s = "this is the original string";
boost::tokenizer<> tok(s);
for(boost::tokenizer<>::iterator it=tok.begin(); it!=tok.end();++it)
{
int charPos = it.base() - s.begin();
}
Unfortunately, there seems not to be the way to retrieve the beginning
of current token in boost::tokenizer
.
Upvotes: 1