Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why is substr(string,0,1) the same as substr(string,1,1)?

38 views
Skip to first unread message

Marc de Bourget

unread,
Jan 3, 2017, 8:23:52 AM1/3/17
to
I have had an issue with a check:
if (substr(oldstr,s-1,1) ~ CONST_ALPHANUMERIC_ONESIGN)

If s is equal to 1, the result of substr(oldstr,0,1)
is the same as substr(oldstr,1,1) but it should not (in my case).
I would have expected an empty string as result for substr(string,0,1).

I have solved it by using
if ((s-1 > 0) && (substr(oldstr,s-1,1) ~ CONST_ALPHANUMERIC_ONESIGN))

However, there must be a reason why AWK developpers decided to do so?

Ed Morton

unread,
Jan 3, 2017, 10:20:00 AM1/3/17
to
It's undefined behavior per POSIX so gawk (and others?) treat any invalid start
position as 1. From the gawk manual
(https://www.gnu.org/software/gawk/manual/gawk.html#String-Functions):

"If start is less than one, substr() treats it as if it was one. (POSIX doesn’t
specify what to do in this case: BWK awk acts this way, and therefore gawk does
too.) If start is greater than the number of characters in the string, substr()
returns the null string. Similarly, if length is present but less than or equal
to zero, the null string is returned."

Ed.

Marc de Bourget

unread,
Jan 3, 2017, 10:29:50 AM1/3/17
to
Thank you Ed. GAWK and TAWK behave the same while MAWK returns the null string.
0 new messages