Boost Regex 'Split' Question

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Boost Regex 'Split' Question

admin-100
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.

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Boost Regex 'Split' Question

John Maddock
> 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 mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Boost Regex 'Split' Question

admin-100

>> <snip> 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: <snip>

Thank you!  That's exactly the piece of the puzzle I was missing -- using
the str() member of sub_match.

> Frankly you would be better off rewriting the
> output routine to accept iterators, at least your code would be
> const-correct then :-)

I hear what you're saying about the output routine, but I'm actually
writing a few small tools to interact with some older commercial software
that exposes a bunch of old C style interfaces -- so I end up having to
use C-style strings and old C style coding for everything.

Thanks again!

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users