Thread overview | ||||||
---|---|---|---|---|---|---|
|
January 04, 2018 [your code here] minimal hex viewer | ||||
---|---|---|---|---|
| ||||
// Reads bytes from stdin and writes a hexadecimal view like a no-frills xxd. // All the actual formatting work is done by format's sweet range syntax void main(string[] args) { import std.getopt; uint bytesPerLine = 8; args.getopt( "cols|c", &bytesPerLine ); import std.stdio; import std.range : chunks; import std.algorithm : map, copy; import std.string : format; import std.ascii : newline; stdin.byChunk(bytesPerLine) .map!(bytes => bytes.format!"%(%02X %)%s"(newline)) .copy(stdout.lockingTextWriter()); } |
January 03, 2018 Re: [your code here] minimal hex viewer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Whear | On Thu, Jan 04, 2018 at 12:25:59AM +0000, Justin Whear via Digitalmars-d wrote: > // Reads bytes from stdin and writes a hexadecimal view like a no-frills > xxd. > // All the actual formatting work is done by format's sweet range syntax Mmm, I like this! Care to submit a PR for this in the dlang.org repo? > void main(string[] args) > { > import std.getopt; > uint bytesPerLine = 8; > args.getopt( > "cols|c", &bytesPerLine > ); > > import std.stdio; > import std.range : chunks; > import std.algorithm : map, copy; > import std.string : format; > import std.ascii : newline; Instead of cluttering the code with a whole bunch of local imports, for such a short program I'd recommend just importing entire modules (skip the specific symbols) at the top of the file. > stdin.byChunk(bytesPerLine) > .map!(bytes => bytes.format!"%(%02X %)%s"(newline)) Is the `newline` part actually necessary? Doesn't "\n" automatically get translated into whatever it needs to be, assuming stdout is opened in text mode? > .copy(stdout.lockingTextWriter()); > } I would omit the (), but that's just being nitpicky. :-P T -- Having a smoking section in a restaurant is like having a peeing section in a swimming pool. -- Edward Burr |
January 04, 2018 Re: [your code here] minimal hex viewer | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Thursday, 4 January 2018 at 00:35:56 UTC, H. S. Teoh wrote:
> On Thu, Jan 04, 2018 at 12:25:59AM +0000, Justin Whear via Digitalmars-d wrote:
>> // Reads bytes from stdin and writes a hexadecimal view like a no-frills
>> xxd.
>> // All the actual formatting work is done by format's sweet range syntax
>
> Mmm, I like this! Care to submit a PR for this in the dlang.org repo?
@Justin: Thanks a lot for your suggestion, but don't we already have a "Print hex dump" example on dlang.org?
|
January 04, 2018 Re: [your code here] minimal hex viewer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seb | On Thursday, 4 January 2018 at 00:54:12 UTC, Seb wrote:
> On Thursday, 4 January 2018 at 00:35:56 UTC, H. S. Teoh wrote:
>> On Thu, Jan 04, 2018 at 12:25:59AM +0000, Justin Whear via Digitalmars-d wrote:
>>> // Reads bytes from stdin and writes a hexadecimal view like a no-frills
>>> xxd.
>>> // All the actual formatting work is done by format's sweet range syntax
>>
>> Mmm, I like this! Care to submit a PR for this in the dlang.org repo?
>
> @Justin: Thanks a lot for your suggestion, but don't we already have a "Print hex dump" example on dlang.org?
Looks like we do, I had not noticed it previously. Naturally I prefer my implementation, ;)
|
Copyright © 1999-2021 by the D Language Foundation