[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[IC/Prog] Gawk's lint complains about empty strings
From: |
Jorge Stolfi |
Subject: |
[IC/Prog] Gawk's lint complains about empty strings |
Date: |
Sun, 29 Jun 2008 21:28:09 -0300 |
Hello,
Gawk's "lint" checker complains when the result of a {substr}
call would be an empty string. In particular
bash$ gawk --lint --source \
' BEGIN {
x="12345";
y = substr(x,3,0);
y = substr(x,6,0);
y = substr(x,6);
print y;
} '
gives the warnings
gawk: cmd. line:2: warning: substr: length 0 is not >= 1
gawk: cmd. line:3: warning: substr: length 0 is not >= 1
gawk: cmd. line:4: warning: substr: start index 6 is past end of string
gawk: cmd. line:5: warning: reference to uninitialized variable `y'
Note that the variable {y} gets incorrectly marked as "undefined"
because of the previous warnings. If one uses the "--lint=invalid"
option, things do not improve very much:
bash$ gawk --lint=invalid --source \
' BEGIN {
x="12345";
y = substr(x,3,0);
y = substr(x,6,0);
y = substr(x,6);
print y;
} '
the first two errors are supressed, but the last two remain:
gawk: cmd. line:4: warning: substr: start index 6 is past end of string
gawk: cmd. line:5: warning: reference to uninitialized variable `y'
However, all three {substr} calls are perfectly valid and are often used in
scripts. E.g.
# Extract the substring {y} of {x} between '<'and '>'
if (match(x, /[<].*[>]/)) { y = substr(x, RSTART+1, RLENGTH-2); }
# Note that the length of the substring may be 0.
# Remove everything from {x} up to the first ',' inclusive:
if (match(x, /[,]/)) { x = substr(x, RSTART+RLENGTH); }
# Note that the starting index may be {length(x)+1}.
I suggest that you fix gawk's "--lint" option so that
* the call {substr(x,i,k)} is considered OK as long as
{i >= 1}, {k >= 0}, and {i+k-1 <= length(x)}
* the call {substr(x,i)} is considered OK as long as
{i >= 1} and {i <= length(x)+1}.
Thank you,
--stolfi
Jorge Stolfi
Institute of Computing
State University of Campinas (UNICAMP)
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [IC/Prog] Gawk's lint complains about empty strings,
Jorge Stolfi <=