Boost logo

Boost :

Subject: Re: [boost] [algorithm] split_copy
From: Chambers, Matthew (matt.chambers42_at_[hidden])
Date: 2016-12-13 17:06:30


On 12/13/2016 3:41 PM, Seth wrote:
> On 13-12-16 21:43, Chambers, Matthew wrote:
>> I like the simplicity and flexibility of boost::algorithm::split, but
>> whenever I do some one-off, trivial split with it where performance
>> isn't an issue, I find myself wishing that I didn't have to predeclare
>> the result container.
> In the interest in avoiding duplication, I'd say a range-adaptor would
> be in order, so you can just use `copy_range<T>` to achieve the copy you
> want without predeclaring.
>
> In fact, something very similar already exists:
> http://www.boost.org/doc/libs/1_62_0/libs/range/doc/html/range/reference/adaptors/reference/tokenized.html,
> although that is specifically for `regex_token_iterator`

Here is the context I used it today. A simple call to split the PATH environment variable into a vector<string>:

bfs::path exePath(args[0]);
if (!exePath.has_parent_path())
{
   vector<string> tokens;
   algorithm::split(tokens, algorithm::is_any_of(env_path_separator), environment::get("PATH"));
   exePath = findNameInPath(args[0], tokens);
}

I'd have loved to have instead written:

bfs::path exePath(args[0]);
if (!exePath.has_parent_path())
   exePath = findNameInPath(args[0], algorithm::split_copy(algorithm::is_any_of(env_path_separator), environment::get("PATH")));

Is there a way to use range adaptors to do that?

Thanks,
-Matt


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk