On 06/23/2012 01:24 PM, Chad J wrote:
On 06/23/2012 01:02 PM, simendsjo wrote:
On Sat, 23 Jun 2012 18:56:24 +0200, simendsjo <simend...@gmail.com>
wrote:
On Sat, 23 Jun 2012 18:50:05 +0200, Chad J
<chadjoan@__spam.is.bad__gmail.com> wrote:
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...
Isn't that exactly what findSplit does? It doesn't have to search the
rest of the string after the match, it just returns a slice of the
rest of the array (I guess - haven't read the code)
import std.stdio, std.algorithm;
void main() {
auto text = "1\n2\n3\n4";
auto res = text.findSplit("\n");
auto pre = res[0];
assert(pre.ptr == text.ptr); // no copy for pre match
auto match = res[1];
assert(match.ptr == &text[1]); // no copy for needle
auto post = res[2];
assert(post.ptr == &text[2]); // no copy for post match
assert(post.length == 5);
}
Close... the reason findSplit doesn't work is because a new line could
be "\n" or it could be "\r\n" or it could be "\r".
As an additional note: I could probably do this easily if I had a
function like findSplit where the predicate is used /instead/ of a
delimiter. So like this:
auto findSplit(alias pred = "a", R)(R haystack);
...
auto tuple = findSplit!(`a == "\n" || a == "\r\n" || a == "\r"`)(text);
return tuple[2];