[comp.lang.c] setting tab stops

tr@samadams.princeton.edu (Tom Reingold) (07/13/90)

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.
--
                                        Tom Reingold
                                        tr@samadams.princeton.edu
                                        rutgers!princeton!samadams!tr
                                        201-577-5814
                                        "Brew strength depends upon the
                                         amount of coffee used." -Black&Decker

leo@ehviea.ine.philips.nl (Leo de Wit) (07/18/90)

In article <1037@rossignol.Princeton.EDU> tr@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.

scs@adam.mit.edu (Steve Summit) (07/20/90)

In article <1037@rossignol.Princeton.EDU> tr@samadams.princeton.edu (Tom Reingold) writes:
>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.
>The assumption is that the terminal tabs at every eighth
>position.

Just use an obvious algorithm.  It's more likely that you'll want
to allow tab stops at arbitrary positions (i.e. not a power of
two) than that you'll save significant time by using shifts and
bitwise operators.

                                            Steve Summit
                                            scs@adam.mit.edu