Boost logo

Boost Users :

From: Pavol Droba (droba_at_[hidden])
Date: 2008-04-03 14:41:00


Hi,

Bruno Lalande wrote:
> Hi,
>
> I'm having a hard time trying to use the find_format() function
> provided in the Algorithm/String library.

Sorry to hear that, I'll try to explain.

> In the examples provided in the Concepts page, 2 examples of finder
> and formatter are provided: simple_finder and simple_formatter.
>
> The first problem is a typing error in simple_finder that prevents it
> form compiling, I've opened a ticket to fix this (#1761).
>

Thanks for spotting that out.

> The second problem is that simple_finder uses a function called
> make_range that I can't find anywhere in the Boost headers. What is
> this function? Am I supposed to write it myself?

It is called boost::make_iterator_range since it was moved to Boost.Range library.

I missed this when I was updating the documentation. I'll correct it.

>
> The third problem is that I can't find any example of actual use of
> those classes. I thought that this:
>
> find_format(string("foobar"), simple_finder(), simple_formatter());
>
> would compile and run (basically replacing the whole string by the
> same string), but after having tried to get rid of the second problem
> by de-templatizing the simple_finder to adapt it to string, it doesn't
> compile. But maybe I'm wrong in the way I did that:
>
> struct simple_finder
> {
> template<typename ForwardIteratorT>
> boost::iterator_range<ForwardIteratorT> operator()(
> ForwardIteratorT Begin,
> ForwardIteratorT End )
> {
> return string( Begin, End );
> }
> };
>

This is not correct. A "finder" must return an iterator_range, not a string. It's purpose is
to find a match in the input range (delimited by Begin and End iterators). As a result it should
return a pair of iterators (in form of iterator_range) that belong to [Begin,End) range.

make_iterator_range is actualy a simple wrapper over the contructor of the iterator_range. In the
formatter you can replace it with iterator_range<ForwardIteratorT>(Begin, End);

That should take care of finder.

Formatter, on the other hand, can return anything, that can be inserted into the resulting string.
So it might be std::string in your case.

And finaly, I have spotted another problem in your call. You are trying to call find_format with the
temporary string. Since find_format is a mutable algorithm, this approach will not work.
You shoud use find_format_copy or pass a regular string variable.

Best regards,
Pavol


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