Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions std/stdio.d
Original file line number Diff line number Diff line change
Expand Up @@ -2242,6 +2242,24 @@ $(XREF file,readText)
assert(blc.front is blc.front);
}

/**
Creates an input range set up to parse one line at a time from the file
into a tuple.

Range primitives may throw $(D StdioException) on I/O error.

Params:
file = file handle to parse from
format = tuple record $(REF_ALTTEXT _format, formattedRead, std, _format)

Returns:
The input range set up to parse one line at a time into a record tuple.

See_Also:

It is similar to $(LREF byLine) and uses
$(REF_ALTTEXT _format, formattedRead, std, _format) under the hood.
*/
template byRecord(Fields...)
{
ByRecord!(Fields) byRecord(string format)
Expand All @@ -2250,24 +2268,26 @@ $(XREF file,readText)
}
}

///
unittest
{
// static import std.file;
//
// auto deleteme = testFilename();
// rndGen.popFront();
// scope(failure) printf("Failed test at line %d\n", __LINE__);
// std.file.write(deleteme, "1 2\n4 1\n5 100");
// scope(exit) std.file.remove(deleteme);
// File f = File(deleteme);
// scope(exit) f.close();
// auto t = [ tuple(1, 2), tuple(4, 1), tuple(5, 100) ];
// uint i;
// foreach (e; f.byRecord!(int, int)("%s %s"))
// {
// //.writeln(e);
// assert(e == t[i++]);
// }
static import std.file;

// prepare test file
auto testFile = testFilename();
scope(failure) printf("Failed test at line %d\n", __LINE__);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

printf seems to be a workaround against an overloading problem with writeln.

std.file.write(testFile, "1 2\n4 1\n5 100");
scope(exit) std.file.remove(testFile);

File f = File(testFile);
scope(exit) f.close();

auto expected = [tuple(1, 2), tuple(4, 1), tuple(5, 100)];
uint i;
foreach (e; f.byRecord!(int, int)("%s %s"))
{
assert(e == expected[i++]);
}
}

// Note: This was documented until 2013/08
Expand Down