August 03, 2017
On Wednesday, 2 August 2017 at 22:02:49 UTC, Vladimir Panteleev wrote:
> On Wednesday, 2 August 2017 at 21:59:23 UTC, Vladimir Panteleev wrote:
>> Good idea! But I think it needs more ranges:
>
> The format call can be substituted with writefln directly:
>
> void main(string[] args)
> {
>     import std.algorithm, std.format, std.stdio;
>     enum cols = 16;
>     args[1].File("rb").byChunk(16).each!(chunk =>
>         writefln!"%(%02X %)%*s  %s"(
>             chunk,
>             3 * (cols - chunk.length), "",
>             chunk.map!(c =>
>                 c < 0x20 || c > 0x7E ? '.' : char(c))));
> }

Really cool:)

Kind regards
André
August 03, 2017
On Wednesday, 2 August 2017 at 22:02:49 UTC, Vladimir Panteleev wrote:
> On Wednesday, 2 August 2017 at 21:59:23 UTC, Vladimir Panteleev wrote:
>> Good idea! But I think it needs more ranges:
>
> The format call can be substituted with writefln directly:
>
> void main(string[] args)
> {
>     import std.algorithm, std.format, std.stdio;
>     enum cols = 16;
>     args[1].File("rb").byChunk(16).each!(chunk =>
>         writefln!"%(%02X %)%*s  %s"(
>             chunk,
>             3 * (cols - chunk.length), "",
>             chunk.map!(c =>
>                 c < 0x20 || c > 0x7E ? '.' : char(c))));
> }

Very cool!
Now you can remove: std.format,
and I would substitute: byChunk(16) with byChunk(col)

Can you point me to the explanation of this: %(%02X %)%*s  %s  ?

Best regards mt.
August 03, 2017
On Thursday, 3 August 2017 at 08:47:12 UTC, Martin Tschierschke wrote:
> On Wednesday, 2 August 2017 at 22:02:49 UTC, Vladimir Panteleev wrote:
>> On Wednesday, 2 August 2017 at 21:59:23 UTC, Vladimir Panteleev wrote:
>>> Good idea! But I think it needs more ranges:
>>
>> The format call can be substituted with writefln directly:
>>
>> void main(string[] args)
>> {
>>     import std.algorithm, std.format, std.stdio;
>>     enum cols = 16;
>>     args[1].File("rb").byChunk(16).each!(chunk =>
>>         writefln!"%(%02X %)%*s  %s"(
>>             chunk,
>>             3 * (cols - chunk.length), "",
>>             chunk.map!(c =>
>>                 c < 0x20 || c > 0x7E ? '.' : char(c))));
>> }
>
> Very cool!
> Now you can remove: std.format,
> and I would substitute: byChunk(16) with byChunk(col)
Error should be cols:  byChunk(cols)
>
> Can you point me to the explanation of this: %(%02X %)%*s  %s  ?
Ah I found it myself: https://dlang.org/library/std/format/formatted_write.html
The %(  %) called "nested array formatting" was new for me.


August 03, 2017
On Thursday, 3 August 2017 at 08:47:12 UTC, Martin Tschierschke wrote:
> Can you point me to the explanation of this: %(%02X %)%*s  %s  ?

https://dlang.org/phobos/std_format.html

Under "Example using array and nested array formatting:"

writefln("My items are %(%s %).", [1,2,3]);

So "%( %)" is an array format specifier, with the stuff between the two as the element format. In the case of "%(%02X %)%*s  %s", that reads:

"%("    // Start array format specifier for arg 1.
"%02X " // Format each element as a 2 digit hex number followed by a space.
"%)"    // End array format specifier.
"%*s"   // Right justify arg 3 (""), using width specified by arg 2.
"  %s"  // And print arg 4 using no special format.

--
  Biotronic
1 2
Next ›   Last »