digitalmars.D - [your code here] minimal hex viewer
- Justin Whear (20/20) Jan 03 2018 // Reads bytes from stdin and writes a hexadecimal view like a
- H. S. Teoh (13/33) Jan 03 2018 Instead of cluttering the code with a whole bunch of local imports, for
- Seb (3/12) Jan 03 2018 @Justin: Thanks a lot for your suggestion, but don't we already
- Justin Whear (3/16) Jan 03 2018 Looks like we do, I had not noticed it previously. Naturally I
// 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()); }
Jan 03 2018
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 syntaxMmm, 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
Jan 03 2018
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:Justin: Thanks a lot for your suggestion, but don't we already have a "Print hex dump" example on dlang.org?// 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 syntaxMmm, I like this! Care to submit a PR for this in the dlang.org repo?
Jan 03 2018
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:Looks like we do, I had not noticed it previously. Naturally I prefer my implementation, ;)On Thu, Jan 04, 2018 at 12:25:59AM +0000, Justin Whear via Digitalmars-d wrote:Justin: Thanks a lot for your suggestion, but don't we already have a "Print hex dump" example on dlang.org?// 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 syntaxMmm, I like this! Care to submit a PR for this in the dlang.org repo?
Jan 03 2018