[your code here] minimal hex viewer

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Jan 4 00:35:56 UTC 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 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 


More information about the Digitalmars-d mailing list