splitterASCII

Non-decoding ASCII-separator-only variant of Phobos' splitter.

@trusted
splitterASCII
(
alias separatorPred
Range
)
(
return Range r
)
if (
is(typeof(Range.init[0 .. 0])) &&
is(typeof(Range.init[0]) : char)
&&
is(typeof(separatorPred(char.init)) : bool)
)

Examples

1 import std.algorithm.comparison : equal;
2 import std.algorithm.comparison : among;
3 import nxt.array_help : s;
4 
5 assert(``.splitterASCII!(_ => _ == ' ')
6          .empty);
7 
8 assert(` `.splitterASCII!(_ => _ == ' ')
9           .empty);
10 
11 assert(`   `.splitterASCII!(_ => _ == ' ')
12             .empty);
13 
14 assert(` - `.splitterASCII!(_ => _ == ' ')
15             .equal([`-`].s[]));
16 
17 assert(`a`.splitterASCII!(_ => _ == ' ')
18           .equal([`a`].s[]));
19 
20 assert(` a `.splitterASCII!(_ => _ == ' ')
21             .equal([`a`].s[]));
22 
23 assert(` a b `.splitterASCII!(_ => _ == ' ')
24               .equal([`a`, `b`].s[]));
25 
26 assert(` a_b `.splitterASCII!(_ => _ == ' ')
27               .equal([`a_b`].s[]));
28 
29 assert(` - aa   bb--c-_d--_e`.splitterASCII!(_ => _.among!(' ', '-', '_') != 0)
30                              .equal([`aa`, `bb`, `c`, `d`, `e`].s[]));

DIP-1000 return ref escape analysis

import nxt.dip_traits : isDIP1000;

static if (isDIP1000)
{
    // See_Also: https://forum.dlang.org/post/pzddsrwhfvcopfaamvak@forum.dlang.org
    static assert(!__traits(compiles, {
                char[] f()
                {
                    char[2] x;
                    return x[].splitterASCII!(_ => _ == ' ').front;
                }
            }));
}

Meta