How does one read file line by line / upto a specific delimeter of an MmFile?

Adnan relay.public.adnan at outlook.com
Mon Mar 16 13:17:21 UTC 2020


On Monday, 16 March 2020 at 13:09:08 UTC, Adnan wrote:
> On Sunday, 15 March 2020 at 00:37:35 UTC, H. S. Teoh wrote:
>> On Sat, Mar 14, 2020 at 10:37:37PM +0000, Adnan via 
>> Digitalmars-d-learn wrote:
>>> https://dlang.org/library/std/mmfile/mm_file.html doesn't 
>>> seem to
>>> specify anything similar to lines() or byLine() or 
>>> byLineCopy() etc.
>>
>> That's because a memory-mapped file appears directly in your 
>> program's memory address space as if it was an array of bytes 
>> (ubyte[]).  No interpretation is imposed upon the contents.  
>> If you want lines out of it, try casting the memory to 
>> const(char)[] and using std.algorithm.splitter to get a range 
>> of lines. For example:
>>
>> 	auto mmfile = new MmFile("myfile.txt");
>> 	auto data = cast(const(char)[]) mmfile[];
>> 	auto lines = data.splitter("\n");
>> 	foreach (line; lines) {
>> 		...
>> 	}
>>
>>
>> T
>
> Would it be wasteful to cast the entire content into a const 
> string? Can a memory mapped file be read with a buffer?

for more context here's the program

string lexHash(scope const ref string arg) {
	auto repeated = arg ~ arg;
	string result = arg;
	for (auto i = 1; i < arg.length; ++i) {
		const slice = repeated[i .. i + arg.length];
		if (slice < result)
			result = slice;
	}
	return result;
}

unittest {
	assert("cba".smallestRepr() == "acb");
}

void main(const string[] args) {
	import std.stdio : writeln, lines, File;
	import std.algorithm : splitter;
	import std.mmfile : MmFile;

	string[][const string] wordTable;
	scope auto mmfile = new MmFile(args[1]);
	auto data = cast(const string) mmfile[];

	foreach (string word; data.splitter()) {
		const string key = word.lexHash();
		wordTable.require(key, []) ~= word;
		if (wordTable[key].length == 4) {
			writeln(wordTable[key]);
			break;
		}
	}
}



More information about the Digitalmars-d-learn mailing list