|
Boost Users : |
From: John Maddock (john_at_[hidden])
Date: 2006-03-21 11:54:36
> Hi all, I have Boost 1.33.1 and VC++ 7.1. I'm currently writing some
> apps that perform splitting, searching, replacing, etc. using Boost
> Regex++. I've nailed the searching, but am having trouble with the
> splitting. Basically I need to know how to convert the result of a
> regex_token_iterator to a C-style string (don't ask...) that I can
> display using the old-style printf() function. Can anyone out there
> give me an idea how to implement such a beast? Thanks in advance.
OK, the regex_token_iterator gives you iterators into the existing string,
which obviously aren't null-terminated. So you're going to have to call the
str() member of sub_match to get a std::string and then you can use the
c_ctr() member of that to get a null terminated string:
void PrintIterator(sregex_token_iterator it)
{
std::string tmp = it->str();
printf(a, b, tmp.c_str());
}
Note that while you can avoid the temporary in the above example by calling
it->str().c_str() directly: but be very careful if you do so in your actual
code: it's a common source of programming errors to call the c_str() member
on a temporary string object, and end up pointing at memory that's been
freed (because the temporary has gone out of scope).
Finally, if your app is *very* memory bound, you could avoid copying the
string by modifying the underlying sequence (basically remembering the
character at it->end() and replacing it with a NULL temporarily), but you
really don't want to go down that route unless performance is truely
unacceptable otherwise. Frankly you would be better off rewriting the
output routine to accept iterators, at least your code would be
const-correct then :-)
HTH, John.
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