added partial code to handle a numeric string better

This commit is contained in:
hyung-hwan 2009-06-15 07:22:00 +00:00
parent f9bf649924
commit 193afb0567
3 changed files with 64 additions and 35 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.h 199 2009-06-14 08:40:52Z hyunghwan.chung $ * $Id: awk.h 200 2009-06-14 13:22:00Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -55,11 +55,13 @@ typedef struct qse_awk_rtx_t qse_awk_rtx_t; /* (R)untime con(T)e(X)t */
#if QSE_SIZEOF_INT == 2 #if QSE_SIZEOF_INT == 2
# define QSE_AWK_VAL_HDR \ # define QSE_AWK_VAL_HDR \
unsigned int type: 3; \ unsigned int type: 3; \
unsigned int ref: 13 unsigned int ref: 11 \
unsigned int nstr: 2;
#else #else
# define QSE_AWK_VAL_HDR \ # define QSE_AWK_VAL_HDR \
unsigned int type: 3; \ unsigned int type: 3; \
unsigned int ref: 29 unsigned int ref: 27; \
unsigned int nstr: 2;
#endif #endif
#define QSE_AWK_VAL_TYPE(x) ((x)->type) #define QSE_AWK_VAL_TYPE(x) ((x)->type)

View File

@ -1,5 +1,5 @@
/* /*
* $Id: rec.c 198 2009-06-12 12:58:50Z hyunghwan.chung $ * $Id: rec.c 200 2009-06-14 13:22:00Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -31,6 +31,10 @@ int qse_awk_rtx_setrec (
if (idx == 0) if (idx == 0)
{ {
qse_long_t l;
qse_real_t r;
int x;
if (str == QSE_STR_PTR(&run->inrec.line) && if (str == QSE_STR_PTR(&run->inrec.line) &&
len == QSE_STR_LEN(&run->inrec.line)) len == QSE_STR_LEN(&run->inrec.line))
{ {
@ -49,13 +53,23 @@ int qse_awk_rtx_setrec (
} }
} }
x = qse_awk_rtx_strtonum (run, 1, str, len, &l, &r);
v = qse_awk_rtx_makestrval (run, str, len); v = qse_awk_rtx_makestrval (run, str, len);
if (v == QSE_NULL) if (v == QSE_NULL)
{ {
qse_awk_rtx_clrrec (run, QSE_FALSE); qse_awk_rtx_clrrec (run, QSE_FALSE);
return -1; return -1;
} }
if (x >= 0)
{
/* set the numeric string flag if a string
* can be converted to a number */
QSE_ASSERT (x == 0 || x == 1);
v->nstr = x + 1; /* long -> 1, real -> 2 */
}
QSE_ASSERT (run->inrec.d0->type == QSE_AWK_VAL_NIL); QSE_ASSERT (run->inrec.d0->type == QSE_AWK_VAL_NIL);
/* d0 should be cleared before the next line is reached /* d0 should be cleared before the next line is reached
* as it doesn't call qse_awk_rtx_refdownval on run->inrec.d0 */ * as it doesn't call qse_awk_rtx_refdownval on run->inrec.d0 */
@ -233,13 +247,7 @@ static int split_record (qse_awk_rtx_t* run)
run->inrec.flds[run->inrec.nflds].ptr = tok; run->inrec.flds[run->inrec.nflds].ptr = tok;
run->inrec.flds[run->inrec.nflds].len = tok_len; run->inrec.flds[run->inrec.nflds].len = tok_len;
/* this way of handling a record distorts a value.
x = qse_awk_rtx_strtonum (run, 1, tok, tok_len, &l, &r); x = qse_awk_rtx_strtonum (run, 1, tok, tok_len, &l, &r);
run->inrec.flds[run->inrec.nflds].val =
(x <= -1)? qse_awk_rtx_makestrval (run, tok, tok_len):
(x == 0)? qse_awk_rtx_makeintval (run, l):
qse_awk_rtx_makerealval (run, r);
*/
run->inrec.flds[run->inrec.nflds].val = run->inrec.flds[run->inrec.nflds].val =
qse_awk_rtx_makestrval (run, tok, tok_len); qse_awk_rtx_makestrval (run, tok, tok_len);
@ -249,6 +257,14 @@ static int split_record (qse_awk_rtx_t* run)
return -1; return -1;
} }
if (x >= 0)
{
/* set the numeric string flags if a string
* can be converted to a number */
QSE_ASSERT (x == 0 || x == 1);
run->inrec.flds[run->inrec.nflds].val->nstr = x + 1;
}
qse_awk_rtx_refupval (run, run->inrec.flds[run->inrec.nflds].val); qse_awk_rtx_refupval (run, run->inrec.flds[run->inrec.nflds].val);
run->inrec.nflds++; run->inrec.nflds++;

View File

@ -1,5 +1,5 @@
/* /*
* $Id: val.c 197 2009-06-12 02:59:59Z hyunghwan.chung $ * $Id: val.c 200 2009-06-14 13:22:00Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -24,36 +24,36 @@
#define CHUNKSIZE QSE_AWK_VAL_CHUNK_SIZE #define CHUNKSIZE QSE_AWK_VAL_CHUNK_SIZE
static qse_awk_val_nil_t awk_nil = { QSE_AWK_VAL_NIL, 0 }; static qse_awk_val_nil_t awk_nil = { QSE_AWK_VAL_NIL, 0, 0 };
static qse_awk_val_str_t awk_zls = { QSE_AWK_VAL_STR, 0, QSE_T(""), 0 }; static qse_awk_val_str_t awk_zls = { QSE_AWK_VAL_STR, 0, 0, QSE_T(""), 0 };
qse_awk_val_t* qse_awk_val_nil = (qse_awk_val_t*)&awk_nil; qse_awk_val_t* qse_awk_val_nil = (qse_awk_val_t*)&awk_nil;
qse_awk_val_t* qse_awk_val_zls = (qse_awk_val_t*)&awk_zls; qse_awk_val_t* qse_awk_val_zls = (qse_awk_val_t*)&awk_zls;
static qse_awk_val_int_t awk_int[] = static qse_awk_val_int_t awk_int[] =
{ {
{ QSE_AWK_VAL_INT, 0, -1, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, -1, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 0, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 1, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 1, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 2, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 2, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 3, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 3, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 4, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 4, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 5, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 5, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 6, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 6, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 7, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 7, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 8, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 8, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 9, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 9, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 10, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 10, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 11, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 11, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 12, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 12, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 13, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 13, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 14, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 14, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 15, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 15, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 16, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 16, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 17, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 17, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 18, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 18, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 19, QSE_NULL }, { QSE_AWK_VAL_INT, 0, 0, 19, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 20, QSE_NULL } { QSE_AWK_VAL_INT, 0, 0, 20, QSE_NULL }
}; };
qse_awk_val_t* qse_awk_val_negone = (qse_awk_val_t*)&awk_int[0]; qse_awk_val_t* qse_awk_val_negone = (qse_awk_val_t*)&awk_int[0];
@ -132,6 +132,7 @@ qse_awk_val_t* qse_awk_rtx_makeintval (qse_awk_rtx_t* run, qse_long_t v)
val->type = QSE_AWK_VAL_INT; val->type = QSE_AWK_VAL_INT;
val->ref = 0; val->ref = 0;
val->nstr = 0;
val->val = v; val->val = v;
val->nde = QSE_NULL; val->nde = QSE_NULL;
@ -203,6 +204,7 @@ qse_awk_val_t* qse_awk_rtx_makerealval (qse_awk_rtx_t* run, qse_real_t v)
val->type = QSE_AWK_VAL_REAL; val->type = QSE_AWK_VAL_REAL;
val->ref = 0; val->ref = 0;
val->nstr = 0;
val->val = v; val->val = v;
val->nde = QSE_NULL; val->nde = QSE_NULL;
@ -257,6 +259,7 @@ init:
*/ */
val->type = QSE_AWK_VAL_STR; val->type = QSE_AWK_VAL_STR;
val->ref = 0; val->ref = 0;
val->nstr = 0;
val->len = len; val->len = len;
val->ptr = (qse_char_t*)(val + 1); val->ptr = (qse_char_t*)(val + 1);
/*qse_strxncpy (val->ptr, len+1, str, len);*/ /*qse_strxncpy (val->ptr, len+1, str, len);*/
@ -283,6 +286,7 @@ qse_awk_val_t* qse_awk_rtx_makestrval_nodup (
val->type = QSE_AWK_VAL_STR; val->type = QSE_AWK_VAL_STR;
val->ref = 0; val->ref = 0;
val->nstr = 0;
val->len = len; val->len = len;
val->ptr = str; val->ptr = str;
return (qse_awk_val_t*)val; return (qse_awk_val_t*)val;
@ -330,6 +334,7 @@ init:
*/ */
val->type = QSE_AWK_VAL_STR; val->type = QSE_AWK_VAL_STR;
val->ref = 0; val->ref = 0;
val->nstr = 0;
val->len = len1 + len2; val->len = len1 + len2;
val->ptr = (qse_char_t*)(val + 1); val->ptr = (qse_char_t*)(val + 1);
/*qse_strxncpy (val->ptr, len1+1, str1, len1); /*qse_strxncpy (val->ptr, len1+1, str1, len1);
@ -368,6 +373,7 @@ qse_awk_val_t* qse_awk_rtx_makerexval (
val->type = QSE_AWK_VAL_REX; val->type = QSE_AWK_VAL_REX;
val->ref = 0; val->ref = 0;
val->nstr = 0;
val->len = len; val->len = len;
val->ptr = (qse_char_t*)(val + 1); val->ptr = (qse_char_t*)(val + 1);
@ -419,6 +425,7 @@ qse_awk_val_t* qse_awk_rtx_makemapval (qse_awk_rtx_t* run)
val->type = QSE_AWK_VAL_MAP; val->type = QSE_AWK_VAL_MAP;
val->ref = 0; val->ref = 0;
val->nstr = 0;
val->map = qse_map_open ( val->map = qse_map_open (
run, 256, 70, free_mapval, same_mapval, run->awk->mmgr); run, 256, 70, free_mapval, same_mapval, run->awk->mmgr);
if (val->map == QSE_NULL) if (val->map == QSE_NULL)
@ -442,6 +449,7 @@ qse_awk_val_t* qse_awk_rtx_makemapval (qse_awk_rtx_t* run)
val->type = QSE_AWK_VAL_MAP; val->type = QSE_AWK_VAL_MAP;
val->ref = 0; val->ref = 0;
val->nstr = 0;
val->map = (qse_map_t*)(val + 1); val->map = (qse_map_t*)(val + 1);
val->map = qse_map_init (val->map, run->awk->mmgr, 256, 70); val->map = qse_map_init (val->map, run->awk->mmgr, 256, 70);
@ -489,6 +497,7 @@ qse_awk_val_t* qse_awk_rtx_makerefval (qse_awk_rtx_t* run, int id, qse_awk_val_t
val->type = QSE_AWK_VAL_REF; val->type = QSE_AWK_VAL_REF;
val->ref = 0; val->ref = 0;
val->nstr = 0;
val->id = id; val->id = id;
val->adr = adr; val->adr = adr;
@ -560,11 +569,13 @@ void qse_awk_rtx_freeval (qse_awk_rtx_t* rtx, qse_awk_val_t* val, qse_bool_t cac
rtx->scache32_count<QSE_COUNTOF(rtx->scache32)) rtx->scache32_count<QSE_COUNTOF(rtx->scache32))
{ {
rtx->scache32[rtx->scache32_count++] = v; rtx->scache32[rtx->scache32_count++] = v;
v->nstr = 0;
} }
else if (v->len <= 64 && else if (v->len <= 64 &&
rtx->scache64_count<QSE_COUNTOF(rtx->scache64)) rtx->scache64_count<QSE_COUNTOF(rtx->scache64))
{ {
rtx->scache64[rtx->scache64_count++] = v; rtx->scache64[rtx->scache64_count++] = v;
v->nstr = 0;
} }
else QSE_AWK_FREE (rtx->awk, val); else QSE_AWK_FREE (rtx->awk, val);
} }