www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [your code here] minimal hex viewer

reply Justin Whear <mrjnewt gmail.com> writes:
// 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
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
Jan 03 2018
parent reply Seb <seb wilzba.ch> writes:
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?
Jan 03 2018
parent Justin Whear <mrjnewt gmail.com> writes:
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, ;)
Jan 03 2018