Reading files using delimiters/terminators

Ali Çehreli acehreli at yahoo.com
Mon Dec 28 00:02:38 UTC 2020


On 12/27/20 3:12 PM, Rekel wrote:

 > is there a reason to use
 > either 'splitter' or 'split'? I'm not sure I see why the difference
 > would matter in the end.

splitter() is a lazy range algorithm. split() is a range algorithm as 
well but it is eager; it will put the results in an array that it grows. 
The string elements would not be copies of the original range; they will 
still be just the pair of .ptr and .length but it can be expensive if 
there are a lot of parts.

Further, if you want to process just a small number of the initial 
parts, then being eager would be wasteful.

As all lazy range algorithms, splitter() is just an iteration object 
waiting to be used. It does not allocate any array but serves the parts 
one by one. You can filter the parts as you iterate over or you can stop 
at any point. For example, the following would take the first 3 
non-empty lines:

import std.stdio;
import std.range;
import std.algorithm;

void main() {
   auto s = "hello\n\nworld\n\n\nand\nmoon";
   writefln!"%(%s, %)"(s.splitter('\n').filter!(part => 
!part.empty).take(3));
}

 > Sidetangent, don't mean to bash the learning tour, as it's been really
 > useful for getting started, but I'm surprised stuff like tuples and
 > files arent mentioned there.

Alternative place to search: :)

   http://ddili.org/ders/d.en/ix.html

 > Especially since the documentation tends to trip me up, with stuff like
 > 'isSomeString' mentioning 'built in string types', while I haven't been
 > able to find that concept elsewhere,

Built in strings are just arrays of character types: char[], wchar[], 
and dchar[]. Commonly used by their respective immutable aliases: 
string, wstring, and dstring.

 > 'countUntil' not being called 'indexOf'

countUntil() is more general because it works with any range while 
indexOf requires a string.

 > assumeUnique seems to be a thing?

That appears in the index I posted above as well. ;)

Ali



More information about the Digitalmars-d-learn mailing list