added qse_awk_wcsxtoint()/qse_awk_mbsxtoint() and made qse_awk_strtointo() a macro

added qse_awk_wcsxtoflt()/qse_awk_mbsxtoflt() and made qse_awk_strxtoflt() a macro
enhanced qse_awk_rtx_valtonum() to handle a byte array
This commit is contained in:
hyung-hwan 2019-04-15 04:30:49 +00:00
parent 5e0754e607
commit 81cbac9e4d
10 changed files with 798 additions and 636 deletions

View File

@ -1896,29 +1896,63 @@ QSE_EXPORT qse_char_t* qse_awk_cstrdup (
);
/**
* The qse_awk_strxtoint() function converts a string to an integer.
* The qse_awk_mbsxtoint() function converts a multi-byte string to an integer.
*/
QSE_EXPORT qse_awk_int_t qse_awk_strxtoint (
QSE_EXPORT qse_awk_int_t qse_awk_mbsxtoint (
qse_awk_t* awk,
const qse_char_t* str,
const qse_mchar_t* str,
qse_size_t len,
int base,
const qse_char_t** endptr
const qse_mchar_t** endptr
);
/**
* The qse_awk_strxtoflt() function converts a string to a floating-point
* The qse_awk_wcsxtoint() function converts a wide character string to an integer.
*/
QSE_EXPORT qse_awk_int_t qse_awk_wcsxtoint (
qse_awk_t* awk,
const qse_wchar_t* str,
qse_size_t len,
int base,
const qse_wchar_t** endptr
);
#if defined(QSE_CHAR_IS_MCHAR)
# define qse_awk_strxtoint(awk,str,len,base,endptr) qse_awk_mbsxtoint(awk,str,len,base,endptr)
#else
# define qse_awk_strxtoint(awk,str,len,base,endptr) qse_awk_wcsxtoint(awk,str,len,base,endptr)
#endif
/**
* The qse_awk_mbsxtoflt() function converts a multi-byte string to a floating-point
* number.
*/
QSE_EXPORT qse_awk_flt_t qse_awk_strxtoflt (
QSE_EXPORT qse_awk_flt_t qse_awk_mbsxtoflt (
qse_awk_t* awk,
const qse_char_t* str,
const qse_mchar_t* str,
qse_size_t len,
const qse_char_t** endptr
const qse_mchar_t** endptr
);
/**
* The qse_awk_longtostr() functon converts an integer to a string.
* The qse_awk_wcsxtoflt() function converts a wide character string to a floating-point
* number.
*/
QSE_EXPORT qse_awk_flt_t qse_awk_wcsxtoflt (
qse_awk_t* awk,
const qse_wchar_t* str,
qse_size_t len,
const qse_wchar_t** endptr
);
#if defined(QSE_CHAR_IS_MCHAR)
# define qse_awk_strxtoflt(awk,str,len,endptr) qse_awk_mbsxtoflt(awk,str,len,endptr)
#else
# define qse_awk_strxtoflt(awk,str,len,endptr) qse_awk_wcsxtoflt(awk,str,len,endptr)
#endif
/**
* The qse_awk_inttostr() functon converts an integer to a string.
*/
QSE_EXPORT qse_size_t qse_awk_inttostr (
qse_awk_t* awk,
@ -2811,15 +2845,29 @@ QSE_EXPORT int qse_awk_rtx_valtoflt (
#define QSE_AWK_RTX_STRTONUM_GET_OPTION_STRICT(option) ((option) & 1)
#define QSE_AWK_RTX_STRTONUN_GET_OPTION_BASE(option) ((option) >> 8)
QSE_EXPORT int qse_awk_rtx_strtonum (
QSE_EXPORT int qse_awk_rtx_mbstonum (
qse_awk_rtx_t* rtx, /**< runtime context */
int strict, /**< determines to perform strict check */
const qse_char_t* ptr, /**< points to a string to convert */
const qse_mchar_t* ptr, /**< points to a string to convert */
qse_size_t len, /**< number of characters in a string */
qse_awk_int_t* l, /**< stores a converted integer */
qse_awk_flt_t* r /**< stores a converted floating-poing number */
);
QSE_EXPORT int qse_awk_rtx_wcstonum (
qse_awk_rtx_t* rtx, /**< runtime context */
int strict, /**< determines to perform strict check */
const qse_wchar_t* ptr, /**< points to a string to convert */
qse_size_t len, /**< number of characters in a string */
qse_awk_int_t* l, /**< stores a converted integer */
qse_awk_flt_t* r /**< stores a converted floating-poing number */
);
#if defined(QSE_CHAR_IS_MCHAR)
# define qse_awk_rtx_strtonum(rtx,strict,ptr,len,l,r) qse_awk_rtx_mbstonum(rtx,strict,ptr,len,l,r)
#else
# define qse_awk_rtx_strtonum(rtx,strict,ptr,len,l,r) qse_awk_rtx_wcstonum(rtx,strict,ptr,len,l,r)
#endif
/**
* The qse_awk_rtx_hashval() function hashes a simple value
* to a positive integer. It returns -1 for a inhashable value.

View File

@ -76,6 +76,34 @@ typedef struct qse_awk_tree_t qse_awk_tree_t;
#define QSE_AWK_TOUPPER(awk,c) QSE_TOUPPER(c)
#define QSE_AWK_TOLOWER(awk,c) QSE_TOLOWER(c)
#define QSE_AWK_ISMUPPER(awk,c) QSE_ISMUPPER(c)
#define QSE_AWK_ISMLOWER(awk,c) QSE_ISMLOWER(c)
#define QSE_AWK_ISMALPHA(awk,c) QSE_ISMALPHA(c)
#define QSE_AWK_ISMDIGIT(awk,c) QSE_ISMDIGIT(c)
#define QSE_AWK_ISMXDIGIT(awk,c) QSE_ISMXDIGIT(c)
#define QSE_AWK_ISMALNUM(awk,c) QSE_ISMALNUM(c)
#define QSE_AWK_ISMSPACE(awk,c) QSE_ISMSPACE(c)
#define QSE_AWK_ISMPRINT(awk,c) QSE_ISMPRINT(c)
#define QSE_AWK_ISMGRAPH(awk,c) QSE_ISMGRAPH(c)
#define QSE_AWK_ISMCNTRL(awk,c) QSE_ISMCNTRL(c)
#define QSE_AWK_ISMPUNCT(awk,c) QSE_ISMPUNCT(c)
#define QSE_AWK_TOMUPPER(awk,c) QSE_TOMUPPER(c)
#define QSE_AWK_TOMLOWER(awk,c) QSE_TOMLOWER(c)
#define QSE_AWK_ISWUPPER(awk,c) QSE_ISWUPPER(c)
#define QSE_AWK_ISWLOWER(awk,c) QSE_ISWLOWER(c)
#define QSE_AWK_ISWALPHA(awk,c) QSE_ISWALPHA(c)
#define QSE_AWK_ISWDIGIT(awk,c) QSE_ISWDIGIT(c)
#define QSE_AWK_ISWXDIGIT(awk,c) QSE_ISWXDIGIT(c)
#define QSE_AWK_ISWALNUM(awk,c) QSE_ISWALNUM(c)
#define QSE_AWK_ISWSPACE(awk,c) QSE_ISWSPACE(c)
#define QSE_AWK_ISWPRINT(awk,c) QSE_ISWPRINT(c)
#define QSE_AWK_ISWGRAPH(awk,c) QSE_ISWGRAPH(c)
#define QSE_AWK_ISWCNTRL(awk,c) QSE_ISWCNTRL(c)
#define QSE_AWK_ISWPUNCT(awk,c) QSE_ISWPUNCT(c)
#define QSE_AWK_TOWUPPER(awk,c) QSE_TOWUPPER(c)
#define QSE_AWK_TOWLOWER(awk,c) QSE_TOWLOWER(c)
#define QSE_AWK_STRDUP(awk,str) (qse_strdup(str,(awk)->mmgr))
#define QSE_AWK_STRXDUP(awk,str,len) (qse_strxdup(str,len,(awk)->mmgr))

503
qse/lib/awk/misc-imp.h Normal file
View File

@ -0,0 +1,503 @@
qse_awk_int_t awk_strxtoint (qse_awk_t* awk, const char_t* str, qse_size_t len, int base, const char_t** endptr)
{
qse_awk_int_t n = 0;
const char_t* p;
const char_t* end;
qse_size_t rem;
int digit, negative = 0;
QSE_ASSERT (base < 37);
p = str;
end = str + len;
if (awk->opt.trait & QSE_AWK_STRIPSTRSPC)
{
/* strip off leading spaces */
while (p < end && AWK_ISSPACE(awk,*p)) p++;
}
/* check for a sign */
while (p < end)
{
if (*p == _T('-'))
{
negative = ~negative;
p++;
}
else if (*p == _T('+')) p++;
else break;
}
/* check for a binary/octal/hexadecimal notation */
rem = end - p;
if (base == 0)
{
if (rem >= 1 && *p == _T('0'))
{
p++;
if (rem == 1) base = 8;
else if (*p == _T('x') || *p == _T('X'))
{
p++; base = 16;
}
else if (*p == _T('b') || *p == _T('B'))
{
p++; base = 2;
}
else base = 8;
}
else base = 10;
}
else if (rem >= 2 && base == 16)
{
if (*p == _T('0') &&
(*(p+1) == _T('x') || *(p+1) == _T('X'))) p += 2;
}
else if (rem >= 2 && base == 2)
{
if (*p == _T('0') &&
(*(p+1) == _T('b') || *(p+1) == _T('B'))) p += 2;
}
/* process the digits */
while (p < end)
{
if (*p >= _T('0') && *p <= _T('9'))
digit = *p - _T('0');
else if (*p >= _T('A') && *p <= _T('Z'))
digit = *p - _T('A') + 10;
else if (*p >= _T('a') && *p <= _T('z'))
digit = *p - _T('a') + 10;
else break;
if (digit >= base) break;
n = n * base + digit;
p++;
}
if (endptr) *endptr = p;
return (negative)? -n: n;
}
/*
* qse_awk_strtoflt is almost a replica of strtod.
*
* strtod.c --
*
* Source code for the "strtod" library procedure.
*
* Copyright (c) 1988-1993 The Regents of the University of California.
* Copyright (c) 1994 Sun Microsystems, Inc.
*
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies. The University of California
* makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without
* express or implied warranty.
*/
/*
* double(64bits) extended(80-bits) quadruple(128-bits)
* exponent 11 bits 15 bits 15 bits
* fraction 52 bits 63 bits 112 bits
* sign 1 bit 1 bit 1 bit
* integer 1 bit
*/
#define MAX_EXPONENT 511
qse_awk_flt_t awk_strtoflt (qse_awk_t* awk, const char_t* str)
{
/*
* Table giving binary powers of 10. Entry is 10^2^i.
* Used to convert decimal exponents into floating-point numbers.
*/
static qse_awk_flt_t powers_of_10[] =
{
10., 100., 1.0e4, 1.0e8, 1.0e16,
1.0e32, 1.0e64, 1.0e128, 1.0e256
};
qse_awk_flt_t fraction, dbl_exp, * d;
const char_t* p;
cint_t c;
int exp = 0; /* Exponent read from "EX" field */
/*
* Exponent that derives from the fractional part. Under normal
* circumstatnces, it is the negative of the number of digits in F.
* However, if I is very long, the last digits of I get dropped
* (otherwise a long I with a large negative exponent could cause an
* unnecessary overflow on I alone). In this case, frac_exp is
* incremented one for each dropped digit.
*/
int frac_exp;
int mant_size; /* Number of digits in mantissa. */
int dec_pt; /* Number of mantissa digits BEFORE decimal point */
const char_t *pexp; /* Temporarily holds location of exponent in string */
int negative = 0, exp_negative = 0;
p = str;
if (awk->opt.trait & QSE_AWK_STRIPSTRSPC)
{
/* strip off leading spaces */
while (AWK_ISSPACE(awk,*p)) p++;
}
/* check for a sign */
while (*p != _T('\0'))
{
if (*p == _T('-'))
{
negative = ~negative;
p++;
}
else if (*p == _T('+')) p++;
else break;
}
/* Count the number of digits in the mantissa (including the decimal
* point), and also locate the decimal point. */
dec_pt = -1;
for (mant_size = 0; ; mant_size++)
{
c = *p;
if (!AWK_ISDIGIT(awk, c))
{
if ((c != _T('.')) || (dec_pt >= 0)) break;
dec_pt = mant_size;
}
p++;
}
/*
* Now suck up the digits in the mantissa. Use two integers to
* collect 9 digits each (this is faster than using floating-point).
* If the mantissa has more than 18 digits, ignore the extras, since
* they can't affect the value anyway.
*/
pexp = p;
p -= mant_size;
if (dec_pt < 0)
{
dec_pt = mant_size;
}
else
{
mant_size--; /* One of the digits was the point */
}
if (mant_size > 18)
{
frac_exp = dec_pt - 18;
mant_size = 18;
}
else
{
frac_exp = dec_pt - mant_size;
}
if (mant_size == 0)
{
fraction = 0.0;
/*p = str;*/
p = pexp;
goto done;
}
else
{
int frac1, frac2;
frac1 = 0;
for ( ; mant_size > 9; mant_size--)
{
c = *p;
p++;
if (c == _T('.'))
{
c = *p;
p++;
}
frac1 = 10 * frac1 + (c - _T('0'));
}
frac2 = 0;
for (; mant_size > 0; mant_size--) {
c = *p;
p++;
if (c == _T('.'))
{
c = *p;
p++;
}
frac2 = 10*frac2 + (c - _T('0'));
}
fraction = (1.0e9 * frac1) + frac2;
}
/* Skim off the exponent */
p = pexp;
if ((*p == _T('E')) || (*p == _T('e')))
{
p++;
if (*p == _T('-'))
{
exp_negative = 1;
p++;
}
else
{
if (*p == _T('+')) p++;
exp_negative = 0;
}
if (!AWK_ISDIGIT(awk, *p))
{
/* p = pexp; */
/* goto done; */
goto no_exp;
}
while (AWK_ISDIGIT(awk, *p))
{
exp = exp * 10 + (*p - _T('0'));
p++;
}
}
no_exp:
if (exp_negative) exp = frac_exp - exp;
else exp = frac_exp + exp;
/*
* Generate a floating-point number that represents the exponent.
* Do this by processing the exponent one bit at a time to combine
* many powers of 2 of 10. Then combine the exponent with the
* fraction.
*/
if (exp < 0)
{
exp_negative = 1;
exp = -exp;
}
else exp_negative = 0;
if (exp > MAX_EXPONENT) exp = MAX_EXPONENT;
dbl_exp = 1.0;
for (d = powers_of_10; exp != 0; exp >>= 1, d++)
{
if (exp & 01) dbl_exp *= *d;
}
if (exp_negative) fraction /= dbl_exp;
else fraction *= dbl_exp;
done:
return (negative)? -fraction: fraction;
}
qse_awk_flt_t awk_strxtoflt (qse_awk_t* awk, const char_t* str, qse_size_t len, const char_t** endptr)
{
/*
* Table giving binary powers of 10. Entry is 10^2^i.
* Used to convert decimal exponents into floating-point numbers.
*/
static qse_awk_flt_t powers_of_10[] =
{
10., 100., 1.0e4, 1.0e8, 1.0e16,
1.0e32, 1.0e64, 1.0e128, 1.0e256
};
qse_awk_flt_t fraction, dbl_exp, * d;
const char_t* p, * end;
cint_t c;
int exp = 0; /* Exponent read from "EX" field */
/*
* Exponent that derives from the fractional part. Under normal
* circumstatnces, it is the negative of the number of digits in F.
* However, if I is very long, the last digits of I get dropped
* (otherwise a long I with a large negative exponent could cause an
* unnecessary overflow on I alone). In this case, frac_exp is
* incremented one for each dropped digit.
*/
int frac_exp;
int mant_size; /* Number of digits in mantissa. */
int dec_pt; /* Number of mantissa digits BEFORE decimal point */
const char_t *pexp; /* Temporarily holds location of exponent in string */
int negative = 0, exp_negative = 0;
p = str;
end = str + len;
/* Strip off leading blanks and check for a sign */
/*while (AWK_ISSPACE(awk,*p)) p++;*/
/*while (*p != _T('\0')) */
while (p < end)
{
if (*p == _T('-'))
{
negative = ~negative;
p++;
}
else if (*p == _T('+')) p++;
else break;
}
/* Count the number of digits in the mantissa (including the decimal
* point), and also locate the decimal point. */
dec_pt = -1;
/*for (mant_size = 0; ; mant_size++) */
for (mant_size = 0; p < end; mant_size++)
{
c = *p;
if (!AWK_ISDIGIT(awk, c))
{
if (c != _T('.') || dec_pt >= 0) break;
dec_pt = mant_size;
}
p++;
}
/*
* Now suck up the digits in the mantissa. Use two integers to
* collect 9 digits each (this is faster than using floating-point).
* If the mantissa has more than 18 digits, ignore the extras, since
* they can't affect the value anyway.
*/
pexp = p;
p -= mant_size;
if (dec_pt < 0)
{
dec_pt = mant_size;
}
else
{
mant_size--; /* One of the digits was the point */
}
if (mant_size > 18) /* TODO: is 18 correct for qse_awk_flt_t??? */
{
frac_exp = dec_pt - 18;
mant_size = 18;
}
else
{
frac_exp = dec_pt - mant_size;
}
if (mant_size == 0)
{
fraction = 0.0;
/*p = str;*/
p = pexp;
goto done;
}
else
{
int frac1, frac2;
frac1 = 0;
for ( ; mant_size > 9; mant_size--)
{
c = *p;
p++;
if (c == _T('.'))
{
c = *p;
p++;
}
frac1 = 10 * frac1 + (c - _T('0'));
}
frac2 = 0;
for (; mant_size > 0; mant_size--) {
c = *p++;
if (c == _T('.'))
{
c = *p;
p++;
}
frac2 = 10 * frac2 + (c - _T('0'));
}
fraction = (1.0e9 * frac1) + frac2;
}
/* Skim off the exponent */
p = pexp;
if (p < end && (*p == _T('E') || *p == _T('e')))
{
p++;
if (p < end)
{
if (*p == _T('-'))
{
exp_negative = 1;
p++;
}
else
{
if (*p == _T('+')) p++;
exp_negative = 0;
}
}
else exp_negative = 0;
if (!(p < end && AWK_ISDIGIT(awk, *p)))
{
/*p = pexp;*/
/*goto done;*/
goto no_exp;
}
while (p < end && AWK_ISDIGIT(awk, *p))
{
exp = exp * 10 + (*p - _T('0'));
p++;
}
}
no_exp:
if (exp_negative) exp = frac_exp - exp;
else exp = frac_exp + exp;
/*
* Generate a floating-point number that represents the exponent.
* Do this by processing the exponent one bit at a time to combine
* many powers of 2 of 10. Then combine the exponent with the
* fraction.
*/
if (exp < 0)
{
exp_negative = 1;
exp = -exp;
}
else exp_negative = 0;
if (exp > MAX_EXPONENT) exp = MAX_EXPONENT;
dbl_exp = 1.0;
for (d = powers_of_10; exp != 0; exp >>= 1, d++)
{
if (exp & 01) dbl_exp *= *d;
}
if (exp_negative) fraction /= dbl_exp;
else fraction *= dbl_exp;
done:
if (endptr != QSE_NULL) *endptr = p;
return (negative)? -fraction: fraction;
}

View File

@ -82,510 +82,57 @@ qse_char_t* qse_awk_cstrdup (qse_awk_t* awk, const qse_cstr_t* s)
return ptr;
}
qse_awk_int_t qse_awk_strxtoint (
qse_awk_t* awk, const qse_char_t* str, qse_size_t len,
int base, const qse_char_t** endptr)
{
qse_awk_int_t n = 0;
const qse_char_t* p;
const qse_char_t* end;
qse_size_t rem;
int digit, negative = 0;
/* ========================================================================= */
#undef awk_strxtoint
#undef awk_strtoflt
#undef awk_strxtoflt
#undef char_t
#undef cint_t
#undef AWK_ISSPACE
#undef AWK_ISDIGIT
#undef _T
QSE_ASSERT (base < 37);
#define awk_strxtoint qse_awk_mbsxtoint
#define awk_strtoflt qse_awk_mbstoflt
#define awk_strxtoflt qse_awk_mbsxtoflt
#define char_t qse_mchar_t
#define cint_t qse_mcint_t
#define AWK_ISSPACE QSE_AWK_ISMSPACE
#define AWK_ISDIGIT QSE_AWK_ISMDIGIT
#define _T QSE_MT
#include "misc-imp.h"
p = str;
end = str + len;
/* ------------------------------------------------------------------------- */
#undef awk_strxtoint
#undef awk_strtoflt
#undef awk_strxtoflt
#undef char_t
#undef cint_t
#undef AWK_ISSPACE
#undef AWK_ISDIGIT
#undef _T
/* ------------------------------------------------------------------------- */
if (awk->opt.trait & QSE_AWK_STRIPSTRSPC)
{
/* strip off leading spaces */
while (p < end && QSE_AWK_ISSPACE(awk,*p)) p++;
}
#define awk_strxtoint qse_awk_wcsxtoint
#define awk_strtoflt qse_awk_wcstoflt
#define awk_strxtoflt qse_awk_wcsxtoflt
#define char_t qse_wchar_t
#define cint_t qse_wcint_t
#define AWK_ISSPACE QSE_AWK_ISWSPACE
#define AWK_ISDIGIT QSE_AWK_ISWDIGIT
#define _T QSE_WT
#include "misc-imp.h"
/* check for a sign */
while (p < end)
{
if (*p == QSE_T('-'))
{
negative = ~negative;
p++;
}
else if (*p == QSE_T('+')) p++;
else break;
}
#undef awk_strxtoint
#undef awk_strtoflt
#undef awk_strxtoflt
#undef char_t
#undef cint_t
#undef AWK_ISSPACE
#undef AWK_ISDIGIT
#undef _T
/* ========================================================================= */
/* check for a binary/octal/hexadecimal notation */
rem = end - p;
if (base == 0)
{
if (rem >= 1 && *p == QSE_T('0'))
{
p++;
if (rem == 1) base = 8;
else if (*p == QSE_T('x') || *p == QSE_T('X'))
{
p++; base = 16;
}
else if (*p == QSE_T('b') || *p == QSE_T('B'))
{
p++; base = 2;
}
else base = 8;
}
else base = 10;
}
else if (rem >= 2 && base == 16)
{
if (*p == QSE_T('0') &&
(*(p+1) == QSE_T('x') || *(p+1) == QSE_T('X'))) p += 2;
}
else if (rem >= 2 && base == 2)
{
if (*p == QSE_T('0') &&
(*(p+1) == QSE_T('b') || *(p+1) == QSE_T('B'))) p += 2;
}
/* process the digits */
while (p < end)
{
if (*p >= QSE_T('0') && *p <= QSE_T('9'))
digit = *p - QSE_T('0');
else if (*p >= QSE_T('A') && *p <= QSE_T('Z'))
digit = *p - QSE_T('A') + 10;
else if (*p >= QSE_T('a') && *p <= QSE_T('z'))
digit = *p - QSE_T('a') + 10;
else break;
if (digit >= base) break;
n = n * base + digit;
p++;
}
if (endptr) *endptr = p;
return (negative)? -n: n;
}
/*
* qse_awk_strtoflt is almost a replica of strtod.
*
* strtod.c --
*
* Source code for the "strtod" library procedure.
*
* Copyright (c) 1988-1993 The Regents of the University of California.
* Copyright (c) 1994 Sun Microsystems, Inc.
*
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies. The University of California
* makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without
* express or implied warranty.
*/
/*
* double(64bits) extended(80-bits) quadruple(128-bits)
* exponent 11 bits 15 bits 15 bits
* fraction 52 bits 63 bits 112 bits
* sign 1 bit 1 bit 1 bit
* integer 1 bit
*/
#define MAX_EXPONENT 511
qse_awk_flt_t qse_awk_strtoflt (qse_awk_t* awk, const qse_char_t* str)
{
/*
* Table giving binary powers of 10. Entry is 10^2^i.
* Used to convert decimal exponents into floating-point numbers.
*/
static qse_awk_flt_t powers_of_10[] =
{
10., 100., 1.0e4, 1.0e8, 1.0e16,
1.0e32, 1.0e64, 1.0e128, 1.0e256
};
qse_awk_flt_t fraction, dbl_exp, * d;
const qse_char_t* p;
qse_cint_t c;
int exp = 0; /* Exponent read from "EX" field */
/*
* Exponent that derives from the fractional part. Under normal
* circumstatnces, it is the negative of the number of digits in F.
* However, if I is very long, the last digits of I get dropped
* (otherwise a long I with a large negative exponent could cause an
* unnecessary overflow on I alone). In this case, frac_exp is
* incremented one for each dropped digit.
*/
int frac_exp;
int mant_size; /* Number of digits in mantissa. */
int dec_pt; /* Number of mantissa digits BEFORE decimal point */
const qse_char_t *pexp; /* Temporarily holds location of exponent in string */
int negative = 0, exp_negative = 0;
p = str;
if (awk->opt.trait & QSE_AWK_STRIPSTRSPC)
{
/* strip off leading spaces */
while (QSE_AWK_ISSPACE(awk,*p)) p++;
}
/* check for a sign */
while (*p != QSE_T('\0'))
{
if (*p == QSE_T('-'))
{
negative = ~negative;
p++;
}
else if (*p == QSE_T('+')) p++;
else break;
}
/* Count the number of digits in the mantissa (including the decimal
* point), and also locate the decimal point. */
dec_pt = -1;
for (mant_size = 0; ; mant_size++)
{
c = *p;
if (!QSE_AWK_ISDIGIT (awk, c))
{
if ((c != QSE_T('.')) || (dec_pt >= 0)) break;
dec_pt = mant_size;
}
p++;
}
/*
* Now suck up the digits in the mantissa. Use two integers to
* collect 9 digits each (this is faster than using floating-point).
* If the mantissa has more than 18 digits, ignore the extras, since
* they can't affect the value anyway.
*/
pexp = p;
p -= mant_size;
if (dec_pt < 0)
{
dec_pt = mant_size;
}
else
{
mant_size--; /* One of the digits was the point */
}
if (mant_size > 18)
{
frac_exp = dec_pt - 18;
mant_size = 18;
}
else
{
frac_exp = dec_pt - mant_size;
}
if (mant_size == 0)
{
fraction = 0.0;
/*p = str;*/
p = pexp;
goto done;
}
else
{
int frac1, frac2;
frac1 = 0;
for ( ; mant_size > 9; mant_size--)
{
c = *p;
p++;
if (c == QSE_T('.'))
{
c = *p;
p++;
}
frac1 = 10 * frac1 + (c - QSE_T('0'));
}
frac2 = 0;
for (; mant_size > 0; mant_size--) {
c = *p;
p++;
if (c == QSE_T('.'))
{
c = *p;
p++;
}
frac2 = 10*frac2 + (c - QSE_T('0'));
}
fraction = (1.0e9 * frac1) + frac2;
}
/* Skim off the exponent */
p = pexp;
if ((*p == QSE_T('E')) || (*p == QSE_T('e')))
{
p++;
if (*p == QSE_T('-'))
{
exp_negative = 1;
p++;
}
else
{
if (*p == QSE_T('+')) p++;
exp_negative = 0;
}
if (!QSE_AWK_ISDIGIT (awk, *p))
{
/* p = pexp; */
/* goto done; */
goto no_exp;
}
while (QSE_AWK_ISDIGIT (awk, *p))
{
exp = exp * 10 + (*p - QSE_T('0'));
p++;
}
}
no_exp:
if (exp_negative) exp = frac_exp - exp;
else exp = frac_exp + exp;
/*
* Generate a floating-point number that represents the exponent.
* Do this by processing the exponent one bit at a time to combine
* many powers of 2 of 10. Then combine the exponent with the
* fraction.
*/
if (exp < 0)
{
exp_negative = 1;
exp = -exp;
}
else exp_negative = 0;
if (exp > MAX_EXPONENT) exp = MAX_EXPONENT;
dbl_exp = 1.0;
for (d = powers_of_10; exp != 0; exp >>= 1, d++)
{
if (exp & 01) dbl_exp *= *d;
}
if (exp_negative) fraction /= dbl_exp;
else fraction *= dbl_exp;
done:
return (negative)? -fraction: fraction;
}
qse_awk_flt_t qse_awk_strxtoflt (
qse_awk_t* awk, const qse_char_t* str, qse_size_t len,
const qse_char_t** endptr)
{
/*
* Table giving binary powers of 10. Entry is 10^2^i.
* Used to convert decimal exponents into floating-point numbers.
*/
static qse_awk_flt_t powers_of_10[] =
{
10., 100., 1.0e4, 1.0e8, 1.0e16,
1.0e32, 1.0e64, 1.0e128, 1.0e256
};
qse_awk_flt_t fraction, dbl_exp, * d;
const qse_char_t* p, * end;
qse_cint_t c;
int exp = 0; /* Exponent read from "EX" field */
/*
* Exponent that derives from the fractional part. Under normal
* circumstatnces, it is the negative of the number of digits in F.
* However, if I is very long, the last digits of I get dropped
* (otherwise a long I with a large negative exponent could cause an
* unnecessary overflow on I alone). In this case, frac_exp is
* incremented one for each dropped digit.
*/
int frac_exp;
int mant_size; /* Number of digits in mantissa. */
int dec_pt; /* Number of mantissa digits BEFORE decimal point */
const qse_char_t *pexp; /* Temporarily holds location of exponent in string */
int negative = 0, exp_negative = 0;
p = str;
end = str + len;
/* Strip off leading blanks and check for a sign */
/*while (QSE_AWK_ISSPACE(awk,*p)) p++;*/
/*while (*p != QSE_T('\0')) */
while (p < end)
{
if (*p == QSE_T('-'))
{
negative = ~negative;
p++;
}
else if (*p == QSE_T('+')) p++;
else break;
}
/* Count the number of digits in the mantissa (including the decimal
* point), and also locate the decimal point. */
dec_pt = -1;
/*for (mant_size = 0; ; mant_size++) */
for (mant_size = 0; p < end; mant_size++)
{
c = *p;
if (!QSE_AWK_ISDIGIT (awk, c))
{
if (c != QSE_T('.') || dec_pt >= 0) break;
dec_pt = mant_size;
}
p++;
}
/*
* Now suck up the digits in the mantissa. Use two integers to
* collect 9 digits each (this is faster than using floating-point).
* If the mantissa has more than 18 digits, ignore the extras, since
* they can't affect the value anyway.
*/
pexp = p;
p -= mant_size;
if (dec_pt < 0)
{
dec_pt = mant_size;
}
else
{
mant_size--; /* One of the digits was the point */
}
if (mant_size > 18) /* TODO: is 18 correct for qse_awk_flt_t??? */
{
frac_exp = dec_pt - 18;
mant_size = 18;
}
else
{
frac_exp = dec_pt - mant_size;
}
if (mant_size == 0)
{
fraction = 0.0;
/*p = str;*/
p = pexp;
goto done;
}
else
{
int frac1, frac2;
frac1 = 0;
for ( ; mant_size > 9; mant_size--)
{
c = *p;
p++;
if (c == QSE_T('.'))
{
c = *p;
p++;
}
frac1 = 10 * frac1 + (c - QSE_T('0'));
}
frac2 = 0;
for (; mant_size > 0; mant_size--) {
c = *p++;
if (c == QSE_T('.'))
{
c = *p;
p++;
}
frac2 = 10 * frac2 + (c - QSE_T('0'));
}
fraction = (1.0e9 * frac1) + frac2;
}
/* Skim off the exponent */
p = pexp;
if (p < end && (*p == QSE_T('E') || *p == QSE_T('e')))
{
p++;
if (p < end)
{
if (*p == QSE_T('-'))
{
exp_negative = 1;
p++;
}
else
{
if (*p == QSE_T('+')) p++;
exp_negative = 0;
}
}
else exp_negative = 0;
if (!(p < end && QSE_AWK_ISDIGIT (awk, *p)))
{
/*p = pexp;*/
/*goto done;*/
goto no_exp;
}
while (p < end && QSE_AWK_ISDIGIT (awk, *p))
{
exp = exp * 10 + (*p - QSE_T('0'));
p++;
}
}
no_exp:
if (exp_negative) exp = frac_exp - exp;
else exp = frac_exp + exp;
/*
* Generate a floating-point number that represents the exponent.
* Do this by processing the exponent one bit at a time to combine
* many powers of 2 of 10. Then combine the exponent with the
* fraction.
*/
if (exp < 0)
{
exp_negative = 1;
exp = -exp;
}
else exp_negative = 0;
if (exp > MAX_EXPONENT) exp = MAX_EXPONENT;
dbl_exp = 1.0;
for (d = powers_of_10; exp != 0; exp >>= 1, d++)
{
if (exp & 01) dbl_exp *= *d;
}
if (exp_negative) fraction /= dbl_exp;
else fraction *= dbl_exp;
done:
if (endptr != QSE_NULL) *endptr = p;
return (negative)? -fraction: fraction;
}
qse_size_t qse_awk_inttostr (
qse_awk_t* awk, qse_awk_int_t value,

49
qse/lib/awk/val-imp.h Normal file
View File

@ -0,0 +1,49 @@
int awk_rtx_strtonum (qse_awk_rtx_t* rtx, int option, const char_t* ptr, qse_size_t len, qse_awk_int_t* l, qse_awk_flt_t* r)
{
const char_t* endptr;
const char_t* end;
int strict = QSE_AWK_RTX_STRTONUM_GET_OPTION_STRICT(option);
int base = QSE_AWK_RTX_STRTONUN_GET_OPTION_BASE(option);
end = ptr + len;
*l = awk_strxtoint(rtx->awk, ptr, len, base, &endptr);
if (endptr < end)
{
if (*endptr == _T('.') || *endptr == _T('E') || *endptr == _T('e'))
{
handle_float:
*r = awk_strxtoflt(rtx->awk, ptr, len, &endptr);
if (strict && endptr < end) return -1;
return 1; /* flt */
}
else if (AWK_ISDIGIT(awk, *endptr))
{
const char_t* p = endptr;
do { p++; } while (p < end && AWK_ISDIGIT(awk, *p));
if (p < end && (*p == _T('.') || *p == _T('E') || *p == _T('e')))
{
/* it's probably an floating-point number.
*
* BEGIN { b=99; printf "%f\n", (0 b 1.112); }
*
* for the above code, this function gets '0991.112'.
* and endptr points to '9' after qse_awk_strxtoint() as
* the numeric string beginning with 0 is treated
* as an octal number.
*
* getting side-tracked,
* BEGIN { b=99; printf "%f\n", (0 b 1.000); }
* the above code cause this function to get 0991, not 0991.000
* because of the default CONVFMT '%.6g' which doesn't produce '.000'.
* so such a number is not treated as a floating-point number.
*/
goto handle_float;
}
}
}
if (strict && endptr < end) return -1;
return 0; /* int */
}

View File

@ -1420,8 +1420,7 @@ int qse_awk_rtx_valtostr (
return -1;
}
qse_char_t* qse_awk_rtx_valtostrdup (
qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
qse_char_t* qse_awk_rtx_valtostrdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
{
qse_awk_rtx_valtostr_out_t out;
@ -1432,8 +1431,7 @@ qse_char_t* qse_awk_rtx_valtostrdup (
return out.u.cpldup.ptr;
}
qse_mchar_t* qse_awk_rtx_valtombsdup (
qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
qse_mchar_t* qse_awk_rtx_valtombsdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
{
#if defined(QSE_CHAR_IS_MCHAR)
qse_awk_rtx_valtostr_out_t out;
@ -1456,8 +1454,7 @@ qse_mchar_t* qse_awk_rtx_valtombsdup (
#endif
}
qse_wchar_t* qse_awk_rtx_valtowcsdup (
qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
qse_wchar_t* qse_awk_rtx_valtowcsdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
{
#if defined(QSE_CHAR_IS_MCHAR)
qse_awk_rtx_valtostr_out_t out;
@ -1480,8 +1477,7 @@ qse_wchar_t* qse_awk_rtx_valtowcsdup (
#endif
}
qse_char_t* qse_awk_rtx_getvalstr (
qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
qse_char_t* qse_awk_rtx_getvalstr (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
{
if (QSE_AWK_RTX_GETVALTYPE(rtx, v) == QSE_AWK_VAL_STR)
{
@ -1494,8 +1490,7 @@ qse_char_t* qse_awk_rtx_getvalstr (
}
}
void qse_awk_rtx_freevalstr (
qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_char_t* str)
void qse_awk_rtx_freevalstr (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_char_t* str)
{
if (QSE_AWK_RTX_GETVALTYPE(rtx, v) != QSE_AWK_VAL_STR ||
str != ((qse_awk_val_str_t*)v)->val.ptr)
@ -1504,8 +1499,7 @@ void qse_awk_rtx_freevalstr (
}
}
static int val_ref_to_num (
qse_awk_rtx_t* rtx, const qse_awk_val_ref_t* ref, qse_awk_int_t* l, qse_awk_flt_t* r)
static int val_ref_to_num (qse_awk_rtx_t* rtx, const qse_awk_val_ref_t* ref, qse_awk_int_t* l, qse_awk_flt_t* r)
{
switch (ref->id)
{
@ -1564,8 +1558,7 @@ static int val_ref_to_num (
}
int qse_awk_rtx_valtonum (
qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_awk_int_t* l, qse_awk_flt_t* r)
int qse_awk_rtx_valtonum (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_awk_int_t* l, qse_awk_flt_t* r)
{
qse_awk_val_type_t vtype = QSE_AWK_RTX_GETVALTYPE (rtx, v);
@ -1600,8 +1593,6 @@ int qse_awk_rtx_valtonum (
);
}
#if 0
/* TODO: */
case QSE_AWK_VAL_BYTEARR:
{
return qse_awk_rtx_mbstonum (
@ -1612,7 +1603,6 @@ int qse_awk_rtx_valtonum (
l, r
);
}
#endif
case QSE_AWK_VAL_MAP:
{
@ -1640,8 +1630,7 @@ int qse_awk_rtx_valtonum (
return -1; /* error */
}
int qse_awk_rtx_valtoint (
qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_awk_int_t* l)
int qse_awk_rtx_valtoint (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_awk_int_t* l)
{
int n;
qse_awk_flt_t r;
@ -1656,8 +1645,7 @@ int qse_awk_rtx_valtoint (
return n;
}
int qse_awk_rtx_valtoflt (
qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_awk_flt_t* r)
int qse_awk_rtx_valtoflt (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_awk_flt_t* r)
{
int n;
qse_awk_int_t l;
@ -1669,57 +1657,46 @@ int qse_awk_rtx_valtoflt (
return n;
}
int qse_awk_rtx_strtonum (
qse_awk_rtx_t* rtx, int option,
const qse_char_t* ptr, qse_size_t len,
qse_awk_int_t* l, qse_awk_flt_t* r)
{
const qse_char_t* endptr;
const qse_char_t* end;
/* ========================================================================== */
#undef awk_rtx_strtonum
#undef awk_strxtoint
#undef awk_strxtoflt
#undef char_t
#undef AWK_ISDIGIT
#undef _T
int strict = QSE_AWK_RTX_STRTONUM_GET_OPTION_STRICT(option);
int base = QSE_AWK_RTX_STRTONUN_GET_OPTION_BASE(option);
#define awk_rtx_strtonum qse_awk_rtx_mbstonum
#define awk_strxtoint qse_awk_mbsxtoint
#define awk_strxtoflt qse_awk_mbsxtoflt
#define char_t qse_mchar_t
#define AWK_ISDIGIT QSE_AWK_ISMDIGIT
#define _T QSE_MT
#include "val-imp.h"
end = ptr + len;
*l = qse_awk_strxtoint(rtx->awk, ptr, len, base, &endptr);
if (endptr < end)
{
if (*endptr == QSE_T('.') || *endptr == QSE_T('E') || *endptr == QSE_T('e'))
{
handle_float:
*r = qse_awk_strxtoflt (rtx->awk, ptr, len, &endptr);
if (strict && endptr < end) return -1;
return 1; /* flt */
}
else if (QSE_AWK_ISDIGIT(awk, *endptr))
{
const qse_char_t* p = endptr;
do { p++; } while (p < end && QSE_AWK_ISDIGIT(awk, *p));
if (p < end && (*p == QSE_T('.') || *p == QSE_T('E') || *p == QSE_T('e')))
{
/* it's probably an floating-point number.
*
* BEGIN { b=99; printf "%f\n", (0 b 1.112); }
*
* for the above code, this function gets '0991.112'.
* and endptr points to '9' after qse_awk_strxtoint() as
* the numeric string beginning with 0 is treated
* as an octal number.
*
* getting side-tracked,
* BEGIN { b=99; printf "%f\n", (0 b 1.000); }
* the above code cause this function to get 0991, not 0991.000
* because of the default CONVFMT '%.6g' which doesn't produce '.000'.
* so such a number is not treated as a floating-point number.
*/
goto handle_float;
}
}
}
/* ------------------------------------------------------------------------- */
#undef awk_rtx_strtonum
#undef awk_strxtoint
#undef awk_strxtoflt
#undef char_t
#undef AWK_ISDIGIT
#undef _T
/* ------------------------------------------------------------------------- */
if (strict && endptr < end) return -1;
return 0; /* int */
}
#define awk_rtx_strtonum qse_awk_rtx_wcstonum
#define awk_strxtoint qse_awk_wcsxtoint
#define awk_strxtoflt qse_awk_wcsxtoflt
#define char_t qse_wchar_t
#define AWK_ISDIGIT QSE_AWK_ISWDIGIT
#define _T QSE_WT
#include "val-imp.h"
#undef awk_rtx_strtonum
#undef awk_strxtoint
#undef awk_strxtoflt
#undef char_t
#undef AWK_ISDIGIT
#undef _T
/* ========================================================================== */
static QSE_INLINE qse_awk_uint_t hash (qse_uint8_t* ptr, qse_size_t len)
{
@ -1748,16 +1725,25 @@ qse_awk_int_t qse_awk_rtx_hashval (qse_awk_rtx_t* rtx, qse_awk_val_t* v)
}
case QSE_AWK_VAL_FLT:
hv = (qse_awk_int_t)hash(
(qse_uint8_t*)&((qse_awk_val_flt_t*)v)->val,
QSE_SIZEOF(((qse_awk_val_flt_t*)v)->val));
{
qse_awk_val_flt_t* dv = (qse_awk_val_flt_t*)v;
hv = (qse_awk_int_t)hash((qse_uint8_t*)&dv->val, QSE_SIZEOF(dv->val));
break;
}
case QSE_AWK_VAL_STR:
hv = (qse_awk_int_t)hash(
(qse_uint8_t*)((qse_awk_val_str_t*)v)->val.ptr,
((qse_awk_val_str_t*)v)->val.len * QSE_SIZEOF(qse_char_t));
{
qse_awk_val_str_t* dv = (qse_awk_val_str_t*)v;
hv = (qse_awk_int_t)hash((qse_uint8_t*)dv->val.ptr, dv->val.len * QSE_SIZEOF(*dv->val.ptr));
break;
}
case QSE_AWK_VAL_BYTEARR:
{
qse_awk_val_bytearr_t* dv = (qse_awk_val_bytearr_t*)v;
hv = (qse_awk_int_t)hash((qse_uint8_t*)dv->val.ptr, dv->val.len * QSE_SIZEOF(*dv->val.ptr));
break;
}
default:
@ -1806,19 +1792,20 @@ int qse_awk_rtx_setrefval (qse_awk_rtx_t* rtx, qse_awk_val_ref_t* ref, qse_awk_v
case QSE_AWK_VAL_STR:
{
int x;
/* handle this separately from the default case
* for no duplication. jumping to the default case
* and callingqse_awk_rtx_valtostrdup() would also work, anyway. */
qse_awk_rtx_refupval (rtx, val);
x = qse_awk_rtx_setrec (
rtx, (qse_size_t)ref->adr,
&((qse_awk_val_str_t*)val)->val
);
x = qse_awk_rtx_setrec (rtx, (qse_size_t)ref->adr, &((qse_awk_val_str_t*)val)->val);
qse_awk_rtx_refdownval (rtx, val);
return x;
}
#if 0
/* TODO: */
case QSE_AWK_VAL_BYTEARR:
#endif
default:
{
qse_cstr_t str;