[PATCH 2/2] bugfix: make ss-diff correctly handle tab expansion

Jason A. Donenfeld Jason at zx2c4.com
Tue Oct 30 17:56:33 CET 2012


I haven't reviewed this carefully yet, but off hand, it seems like a
tab is always between 1 and 8 spaces long.

Let the first column be column 1, and any column be column C. Then,

if C % 8 == 1, spaces := 8
if C % 8 == 2, spaces := 7
if C % 8 == 3, spaces := 6
if C % 8 == 4, spaces := 5
if C % 8 == 5, spaces := 4
if C % 8 == 6, spaces := 3
if C % 8 == 7, spaces := 2
if C % 8 == 0, spaces := 1

This is the intuitive approach visually confirmed by mucking around
inside of VIM. Now let's play with it for 0 indexed columns. We can
just rotate:

if C % 8 == 0, spaces := 8
if C % 8 == 1, spaces := 7
if C % 8 == 2, spaces := 6
if C % 8 == 3, spaces := 5
if C % 8 == 4, spaces := 4
if C % 8 == 5, spaces := 3
if C % 8 == 6, spaces := 2
if C % 8 == 7, spaces := 1


The formula is then just:

8 - ( C % 8 )

Which should be a pretty simple way of calculating it, since "the
0-based column we're at" is just the current length.

Doing it this way, you don't have to hard code the initial space,
since m % n < n.




More information about the CGit mailing list