bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gawk substr() problem


From: Stepan Kasal
Subject: Re: gawk substr() problem
Date: Tue, 19 Nov 2002 09:37:12 +0100
User-agent: Mutt/1.2.5.1i

Hello,

On Mon, Nov 18, 2002 at 02:05:40PM -0800, Paul Eggert wrote:
> Alas, --lint catches the zero length case as well; this is a bug,
> since it's quite reasonable to compute a zero-length substring ...

>   $ gawk --lint 'BEGIN {print substr("x", 1, 0);}' </dev/null
>   gawk: cmd. line:1: warning: substr: length 0 is <= 0

IMHO it's very similar to 

>   $ gawk --lint 'BEGIN {print substr("x", 1, 4);}' </dev/null
>   gawk: cmd. line:1: warning: substr: length 4 at start index 1 exceeds 
> length of first argument (1)

POSIX says:

   substr(s, m[, n  ])
          Return the _at most_ n-character substring of s  ...

so it's perfectly consistent to call substr this way.

In both cases this is not a bug in awk program, since it's well documented
behaviour.  So no awk implementation can issue an error.

OTOH, both of these can be easily filtered out, if you really need to
silence the lint:

function my_substr(s, m, n) {
        if (n == 0)
                return ""
        if (n + m - 1 > length(s))
                return substr(s, m)
        return substr(s,m,n)
}

As we have only one level of warnings, we have to make a choice whether to
issue a warning or not in both cases.  (I think that a complicated
mechanism for switching individual warnings on/off is not adequate here.)
Sure, there will always be situations when you mind/miss one of these
warnings...

IMHO the choice Arnold has made (both on) is not that bad.
Better to have more warnings, you can always ignore them or filter them
out if it matters for you.

Have a nice day,
        Stepan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]