phobos and splitting things... but not with whitespace.

Chad J chadjoan at __spam.is.bad__gmail.com
Sat Jun 23 09:50:05 PDT 2012


On 06/23/2012 11:44 AM, simendsjo wrote:
> On Sat, 23 Jun 2012 17:39:55 +0200, Chad J
> <chadjoan at __spam.is.bad__gmail.com> wrote:
>
>> On 06/23/2012 11:31 AM, simendsjo wrote:
>>> On Sat, 23 Jun 2012 17:19:59 +0200, Chad J
>>> <chadjoan at __spam.is.bad__gmail.com> wrote:
>>>> http://dlang.org/phobos/std_array.html#splitter
>>>
>>>> The first thing I don't understand is why splitter is in /std.array/
>>>> and yet only works on /strings/. It is defined > in terms of
>>>> whitespace, and I don't understand how whitespace is well-defined for
>>>> things besides text. Why wouldn't > it be in std.string?
>>>
>>> See http://dlang.org/phobos/std_algorithm.html#splitter
>>>
>>>> I would expect these functions to exist:
>>>> auto getHead(C)(C[] s, C[] delim, ref C[] tail);
>>>> auto getHead(C)(C[] s, C[] delim);
>>>> auto getTail(C)(C[] s, C[] delim);
>>>
>>> As head is simply splitter(..)[0] and tail splitter(...)[1..$], extra
>>> functions could be implemented much like this
>>>
>>> @property T head(T[] arr) { return arr.front; }
>>> @property T[] tail(T[] arr) { return arr[1..$]; }
>>>
>>> ..and UFCS takes care of the rest:
>>> auto fields = splitter(...);
>>> auto head = fields.head;
>>> auto tail = fields.tail;
>>
>> But I don't want tail as an array. Assume that arr is HUGE and
>> scanning the rest of it is a bad idea. join(arr[1..$]) then becomes a
>> slow operation: O(n) when I could have O(1).
>
>
> Looking for findSplit? http://dlang.org/phobos/std_algorithm.html#findSplit

Cool, that's what I want!

Now if I could find the elegant way to remove exactly one line from the 
text without scanning the text after it...


More information about the Digitalmars-d-learn mailing list