setting tab stops

Leo de Wit leo at ehviea.ine.philips.nl
Thu Jul 19 02:35:12 AEST 1990


In article <1037 at rossignol.Princeton.EDU> tr at samadams.princeton.edu (Tom Reingold) writes:
|This question is not relevant to C, but I saw a nice algorithm, and I
|think I saw it in comp.lang.c.  Someone posted a very terse way of
|calculating how many spaces to move given an input of tabs.  I think it
|used the modulo operator and some bitwise operators.  It was far too
|terse and elegant for me to think of.  Can someone express what I am
|thinking of?  The assumption is that the terminal tabs at every eighth
|position.
|--

If I understand you correctly, you want to know how many spaces to
output given an input of tabs (let's call it ntabs) and a current
column position (let's call it ccpos), so that the resulting column
position would be the same?
The current position is ccpos % 8 columns past the last tabstop, so you
must move - (ccpos % 8) columns to get back to the previous tabstop,
then 8 columns forward for each tab. This results in 8 * ntabs - (ccpos
% 8) spaces, which also could be expressed as (ntabs << 3) - (ccpos & 7).

    Leo.



More information about the Comp.lang.c mailing list