[StringAlgo] Need help to use find_format()
Hi, I'm having a hard time trying to use the find_format() function provided in the Algorithm/String library. 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). 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? 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 ); } }; Can anyone help me? Bruno
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
Hi,
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.
OK thanks, I had looked into the Boost.Range documentation but didn't look well enough. Now it compiles and runs perfectly.
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.
Yep, in my program it's an already existing string, I quickly replaced it by a temp to simplify the case in the mail, without realizing that the resulting statement didn't make sense... Maybe it could be good to have a very quick example of integration of the simple_finder and simple_formatter together with find_format in the docs? It can be obvious to you, but it would elude all the doubts I had about the right way to use them (I was even not sure they were "compatible" with each other). Thanks a lot for your help! Bruno
Hi again, There's another slight problem in the doc I think: when trying to use to same simple_finder with find_format_all(), I have an error due to the fact that the simple_finder's operator() is not const. I can't exactly figure out why this wasn't a problem with find_format() but I think it would be good to make it const in the example, in order to have it working with both algorithms. Bruno
participants (2)
-
Bruno Lalande
-
Pavol Droba