Inconsistent coding style in code examples

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Tue Feb 10 14:25:01 PST 2015


On Tue, Feb 10, 2015 at 09:44:17PM +0000, Vladimir Panteleev via Digitalmars-d wrote:
> On Tuesday, 10 February 2015 at 21:21:14 UTC, Walter Bright wrote:
> >On 2/10/2015 12:57 PM, H. S. Teoh via Digitalmars-d wrote:
> >>Walter's reason for this is that 4-space indentation makes it look
> >>bad on Kindle and other small-screen readers.
> >
> >My Kindle 3 has a 37 character screen width when using a monospace
> >font (i.e. code font). Trying to read code formatted to 80 columns on
> >it is just awful. While I don't expect anyone to read Phobos source
> >code on a Kindle, I do expect the ereader version of the
> >documentation to be read on the Kindle (or phone or whatever
> >ereader).
> 
> Isn't this problem better solved mechanically?

That's what crossed my mind too... but:


> (If we used tabs for indentation, it would be as simple as setting the
> tab width...)

This would trigger another interminable thread about the merits/cons of
tabs vs. spaces. :-D

Given that current Phobos code standardized on 4 spaces, no tab
characters, I can't see tabs ever making it into Phobos code. (It would
be one giant blotch of a PR that reformats every single file in Phobos
and essentially be a black-out point of `git blame`... it would be a
prime example of what Andrei has been complaining about of late, that
much effort is spent on trivial things that only has marginal value.)

That leaves the mechanical solution, which is actually very easy. It
doesn't have to be a sophisticated solution involving a full-blown D
lexer. It could be as simple as substituting initial runs of spaces on
each line, since we already know our input to be formatted in a certain
way.

Proof of concept:

	import std.range.primitives;
	
	auto reIndent(R)(R range, size_t inputIndent, size_t outputIndent)
	    if (isInputRange!R && is(ElementType!R : const(char)[]))
	{
	    import std.algorithm.iteration : map;
	    import std.array;
	    import std.regex;
	
	    auto reSplitInitialSpace = regex(`^( +)(.*)`);
	    auto reReindent = regex(" ".replicate(inputIndent));
	    string outIndent = " ".replicate(outputIndent);
	
	    return range.map!((line) {
	        auto m = line.match(reSplitInitialSpace);
	        if (m)
	        {
	            auto newIndent = m.captures[1].replaceAll(reReindent, outIndent);
	            return newIndent ~ m.captures[2];
	        }
	        else
	            return line;
	    });
	}
	
	void main(string[] args)
	{
	    import std.algorithm.iteration : map;
	    import std.algorithm.mutation : copy;
	    import std.stdio;
	
	    stdin.byLine
	         .reIndent(4, 2)
	         .map!(line => line ~ "\n")
	         .copy(stdout.lockingTextWriter());
	}


Output of program, given its own source code as input:

	import std.range.primitives;
	
	auto reIndent(R)(R range, size_t inputIndent, size_t outputIndent)
	  if (isInputRange!R && is(ElementType!R : const(char)[]))
	{
	  import std.algorithm.iteration : map;
	  import std.array;
	  import std.regex;
	
	  auto reSplitInitialSpace = regex(`^( +)(.*)`);
	  auto reReindent = regex(" ".replicate(inputIndent));
	  string outIndent = " ".replicate(outputIndent);
	
	  return range.map!((line) {
	    auto m = line.match(reSplitInitialSpace);
	    if (m)
	    {
	      auto newIndent = m.captures[1].replaceAll(reReindent, outIndent);
	      return newIndent ~ m.captures[2];
	    }
	    else
	      return line;
	  });
	}
	
	void main(string[] args)
	{
	  import std.algorithm.iteration : map;
	  import std.algorithm.mutation : copy;
	  import std.stdio;
	
	  stdin.byLine
	     .reIndent(4, 2)
	     .map!(line => line ~ "\n")
	     .copy(stdout.lockingTextWriter());
	}


Q.E.D. :-D

So all we need to do, is to add this tool to the dlang.org repo, and run
each generated code example through it, and we're done. No need to
reformat the entire Phobos codebase, or any of that nonsense.


T

-- 
It won't be covered in the book. The source code has to be useful for something, after all. -- Larry Wall


More information about the Digitalmars-d mailing list