bug-gawk
[Top][All Lists]
Advanced

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

fatal: substr: length 0 is not >= 1


From: Roland Illig
Subject: fatal: substr: length 0 is not >= 1
Date: Thu, 7 May 2020 02:42:12 +0200
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Thunderbird/76.0

Dear GNU Awk developers,

In a real-world AWK program
(https://github.com/NetBSD/pkgsrc/blob/8075fc03e305003a/mk/scripts/subst-identity.awk)
I wrote the following code:

# Tests whether a single "s,from,to," is an identity substitution.
function is_identity_subst(
        s,
        len, i, sep, pat_from, pat_to, ch, subst
) {
        len = length(s);
        if (len < 6 || substr(s, 1, 1) != "s")
                return 0;

        sep = substr(s, 2, 1);
        i = 3;
        pat_to = "";
        while (i < len && substr(s, i, 1) != sep) {
                ch = identity_char(substr(s, i));
                if (ch == "")
                        break;
                pat_to = pat_to substr(ch, 1, 1);
                i += length(ch);
        }

        # The next 2 lines are only needed for GNU Awk 5.0.1
        # in -Lfatal mode.
        if (pat_to == "")
                return 0;

        pat_from = substr(s, 3, i - 3);

        subst = "s" sep pat_from sep pat_to sep;
        return s == subst || s == subst "g";
}


I ran this program with -Lfatal, and it presented me this "error":

fatal: substr: length 0 is not >= 1

I think it is ok to have length 0, which would always return the null
string. In my mind, only negative lengths should trigger this error
message. Was there a specific reason for making length 0 an error?

I know that in the above code, I could have written the equivalent "i ==
3" instead. But that would have repeated the number 3, and I didn't want
that. I thought was that the expression 'pat_to == ""' expressed my
thoughts more clearly.

Best,
Roland



reply via email to

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