[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: hard to understand code for the representation of tab
From: |
Gavin Smith |
Subject: |
Re: hard to understand code for the representation of tab |
Date: |
Sat, 12 Oct 2024 20:51:08 +0100 |
On Sat, Oct 12, 2024 at 09:15:10PM +0200, Patrice Dumas wrote:
> Hello,
>
> This is not important, but in the info code, in info/util.c in the
> printed_representation function, which returns a pointer to string that
> is the printed representation of character (or other logical unit) if it
> were printed at a given screen column.
>
> l 226 for tab there is a code that I do not understand at all,
> *pchars = ((pl_chars + 8) & 0xf8) - pl_chars;
>
> A bit more context for the variables:
> pl_chars: size_t the string column the printed representation of character
> should be printed at
> rep: text buffer corresponding to the returned value
> pchars: int gets the number of screen columns taken up by the return value
> pbytes: int gets the number of bytes in returned string
Here's an attempt at explaining it:
pl_chars is the current column. We want to find the next multiple of 8
after the current column, assuming a tab is 8 spaces. We add 8 to the
current column, and then subtract the remainder when this result is
divided by 8.
For example
A. 21 + 8 = 28,
B. Then 28 mod 8 = 4
C. 28 - 4 = 24.
D. Finally, we take away the starting point: 24 - 21 = 3.
So we output 3 spaces to get up to 24 columns.
(n & 0xf8) is supposed to do steps B and C combined
as the binary representation of 0xf8 is 1111 1000 so the result has
to be a multiple of 8.
However, this doesn't work if there are more than 255 columns (which is
unlikely, though).
> here is more context for the code:
>
> else if (*cur_ptr == '\t')
> {
> int i = 0;
>
> *pchars = ((pl_chars + 8) & 0xf8) - pl_chars;
> *pbytes = *pchars;
>
> /* We must output spaces instead of the tab because a tab may
> not clear characters already on the screen. */
> for (i = 0; i < *pbytes; i++)
> text_buffer_add_char (rep, ' ');
> return text_buffer_base (rep);
> }
>
> --
> Pat
>