bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] Handling hexadecimals in different modes


From: Jarno Suni
Subject: Re: [bug-gawk] Handling hexadecimals in different modes
Date: Wed, 9 Sep 2015 21:55:49 +0300

On Tue, 08 Sep 2015 07:34:12 -0600
address@hidden wrote:

> Jarno Suni <address@hidden> wrote:
> 
> > But if you want to write portable scripts, strtonum() is hard to
> > recommend as it is not found in other awk implementation AFAIK.
> 
> The gawk doc includes an awk implementatin of strtonum that can
> be used with other awks, so the above isn't an issue.

Well, there are few issues with the mystrtonum() implementation
available at
http://www.gnu.org/software/gawk/manual/gawk.html#Strtonum-Function :

Implicit decimal conversion "ret = str + 0" might not work, if awk
implementation follows locale setting concerning the decimal separator.

Using 
/^[-+]?([0-9]+([.][0-9]*([Ee][0-9]+)?)?|([.][0-9]+([Ee][-+]?[0-9]+)?))$/
for checking, if the string reprecents valid decimal number, is not
correct. It tells e.g. "3e-2" is invalid i.e. "NOT-A-NUMBER".

By the way, strtonum() seems to
convert /^[-+]?[0-9]*\.?[0-9]*([Ee][-+]?[0-9]+)?/ part and ignore the
rest of the source string. In posix mode or in use-lc-numeric mode
it uses the decimal separator of the current locale instead of period,
though. Still gawk doc says "Note also that strtonum() uses the current
locale’s decimal point for recognizing numbers".

Somewhat more restricting validation would
be /^[-+]?([0-9]+\.?|\.[0-9])[0-9]*([eE][-+]?[0-9]+)?$/ which matches
AWK numeric constants according to the manual page of mawk command. 

Character class [[:xdigit:]] is not understood by all awk
variants. That is the case with old widely used version (1.3.3) of mawk.
I guess [0-9a-fA-F] is more portable.

I am writing another mystrtonum() implementation to address these
issues and more.

-- 
Jarno Ilari Suni - http://www.iki.fi/8/



reply via email to

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