Please help me understand this function signature: std.stdio.File.byLine
jfondren
julian.fondren at gmail.com
Thu Jul 15 18:54:13 UTC 2021
On Thursday, 15 July 2021 at 18:08:45 UTC, Scotpip wrote:
> The relevant function appears to be
> [std.stdio.File.byLine](https://dlang.org/library/std/stdio/file.by_line.html).
>
> The default isn't breaking the lines properly, so I have to
> pass in the line ending. But the signature has me baffled:
>
> ```
> auto byLine(Terminator, Char) (
> KeepTerminator keepTerminator = No.keepTerminator,
> Terminator terminator = '\x0a'
> )
> if (isScalarType!Terminator);
>
> auto byLine(Terminator, Char) (
> KeepTerminator keepTerminator,
> Terminator terminator
> )
> if (is(immutable(ElementEncodingType!Terminator) ==
> immutable(Char)));
> ```
>
> To specify the line ending, it appears to be asking for a type
> "Terminator" which I can't find in the library.
Terminator appears a few times in the signature. The first
appearance, in `byLine(Terminator, ...)(...)`, is a template
parameter. So Terminator is whatever you want that otherwise
works.
> Doing the obvious doesn't work:
>
> ```
> // Err: cannot deduce function from argument types
> myFile.byLine(`\r\n`);
> ```
The first parameter is the keepTerminator flag. That wysiwyg
string is four bytes long and includes the literal slashes.
https://dlang.org/spec/lex.html#wysiwyg
```
$ rdmd --eval '`\r\n`.representation.writeln'
[92, 114, 92, 110]
```
Here's a simple dos2unix:
```d
import std.stdio, std.typecons;
void main() {
foreach (line; stdin.byLine!(string, char)(No.keepTerminator,
"\r\n"))
writeln(line);
}
```
> Also, how would you specify the Char type as a parameter?
The !(string, char) isn't necessary in the last example, but
that's how you'd provide it.
Usage:
```
$ echo -ne "hi\r\nthere\r\n" | dmd -run dosin|od -c
0000000 h i \n t h e r e \n
0000011
```
>
>
> The examples are unenlightening as they only show the default
> case, so any help would be much appreciated. Any wider tips on
> how to read these cryptic signatures would be a bonus!
More information about the Digitalmars-d-learn
mailing list