[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Groff] Re: .substring bug - indicies don't work as documented(?)
From: |
Bernd Warken |
Subject: |
Re: [Groff] Re: .substring bug - indicies don't work as documented(?) |
Date: |
Sun, 28 Oct 2001 15:05:41 +0100 |
User-agent: |
Mutt/1.2.5i |
On Sun, Oct 28, 2001 at 08:56:38AM +0000, Ralph Corderoy wrote:
>
> ... Python ..
>
Python starts the position counting with 0, like C, while groff starts
at 1. So Python positional calculation is fundamentally different and
cannot be taken over.
> Using zero for the end of the string and negative back from there makes
> it easy to refer to the last three characters without doing the maths
> based on string length.
>
> > Moreover, 0 means everything up to behind the last character; so the
> > last character should be -1 anyway.
>
> Don't quite get your point on this bit.
>
0 is meant to be the end of the string, i.e. the position _behind_ the last
character, while the number of the last character denotes the position
on the last character. This is comparable to the difference between
a dynamically defined '\0' terminated string and a character array of fixed
length.
Take for example ".ds s 12345".
Then ".substring s 4 5" could be viewed as printing all characters
from the 4th up to and including the 5th one including, so the pointer
is at position _on_ 5 when the program terminates.
On the other hand, ".substring s 4 0" could be viewed as printing every-
thing up to the end of the string, but this end lies _behind_ the last
character.
Both command return the same result "45", but they are functionally
different.
With the same argument, -1 means the position _on_ the last character,
because 0 means the position _behind_ it.
So ".substring s -1 -1" means `everything from the last one to and
including the last character', while ".substring s -1 0" means `everything
from the last one to the end of the string'. Tho both calls result in the
same substring, they are functionally different.
It is much easier to use ".substring s 1 -2" in the meaning `everything
from the first up to and including the second but last character'.
> > Also 2 args in reversed order could be made into reversing the
> > output.
>
> Yes, it could be. I guess it depends on whether reversing the string
> or having the indices in the wrong order is more common. This way
> avoids the `if (i1 > i2) swap(i1, i2)'. Another option would be
> Python's way of returning an empty string which is again sometimes what
> you want.
>
I agree to all possibilities, except the swapping.