From bae06804efdbbfa6cc6648b40d41e46484b28ad0 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 10 Mar 2020 08:36:31 +0000 Subject: [PATCH] added hawk_rtx_matchvalwithucs() and hawk_rtx_matchvalwithbcs() renamed hawk_rtx_matchval() to hawk_rtx_matchvalwithoocs() updated code concerning the call to hawk_rtx_setrefval() --- hawk/lib/fnc.c | 229 ++++++++++++++++++++++++-------------------- hawk/lib/hawk-prv.h | 2 +- hawk/lib/hawk.h | 8 +- hawk/lib/misc-prv.h | 13 ++- hawk/lib/misc.c | 60 ++++++++++-- hawk/lib/run.c | 22 ++--- hawk/lib/std.c | 2 +- hawk/lib/val.c | 120 ++++++++++++++++------- 8 files changed, 290 insertions(+), 166 deletions(-) diff --git a/hawk/lib/fnc.c b/hawk/lib/fnc.c index 9e0f8592..86404297 100644 --- a/hawk/lib/fnc.c +++ b/hawk/lib/fnc.c @@ -800,7 +800,7 @@ int hawk_fnc_substr (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) int hawk_fnc_split (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) { hawk_oow_t nargs; - hawk_val_t* a0, * a1, * a2, * t1, * t2; + hawk_val_t* a0, * a2, * t1, * t2; hawk_val_type_t a1_vtype, a2_vtype, t1_vtype; hawk_oocs_t str; @@ -813,8 +813,6 @@ int hawk_fnc_split (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) hawk_oocs_t tok; hawk_int_t nflds; - - hawk_errnum_t errnum; int x; str.ptr = HAWK_NULL; @@ -824,12 +822,8 @@ int hawk_fnc_split (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) HAWK_ASSERT (nargs >= 2 && nargs <= 3); a0 = hawk_rtx_getarg(rtx, 0); - a1 = hawk_rtx_getarg(rtx, 1); a2 = (nargs >= 3)? hawk_rtx_getarg (rtx, 2): HAWK_NULL; - a1_vtype = HAWK_RTX_GETVALTYPE (rtx, a1); - HAWK_ASSERT (a1_vtype == HAWK_VAL_REF); - str.ptr = hawk_rtx_getvaloocstr(rtx, a0, &str.len); if (str.ptr == HAWK_NULL) goto oops; @@ -900,18 +894,18 @@ int hawk_fnc_split (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) } t1 = hawk_rtx_makemapval(rtx); - if (t1 == HAWK_NULL) goto oops; + if (HAWK_UNLIKELY(!t1)) goto oops; hawk_rtx_refupval (rtx, t1); - x = hawk_rtx_setrefval (rtx, (hawk_val_ref_t*)a1, t1); + x = hawk_rtx_setrefval(rtx, (hawk_val_ref_t*)hawk_rtx_getarg(rtx, 1), t1); hawk_rtx_refdownval (rtx, t1); - if (x <= -1) goto oops; + if (HAWK_UNLIKELY(x <= -1)) goto oops; /* fill the map with actual values */ p = str.ptr; str_left = str.len; org_len = str.len; nflds = 0; - while (p != HAWK_NULL) + while (p) { hawk_ooch_t key_buf[HAWK_SIZEOF(hawk_int_t)*8+2]; hawk_oow_t key_len; @@ -1246,7 +1240,7 @@ oops: static int __substitute (hawk_rtx_t* rtx, hawk_oow_t max_count) { hawk_oow_t nargs; - hawk_val_t* a0, * a1, * a2, * v; + hawk_val_t* a0, * a1, * r2, * v; hawk_val_type_t a0_vtype; hawk_oocs_t s0; @@ -1258,7 +1252,6 @@ static int __substitute (hawk_rtx_t* rtx, hawk_oow_t max_count) hawk_tre_t* rex = HAWK_NULL; hawk_tre_t* rex_free = HAWK_NULL; - hawk_oow_t sub_count; s0.ptr = HAWK_NULL; @@ -1268,14 +1261,10 @@ static int __substitute (hawk_rtx_t* rtx, hawk_oow_t max_count) s1.len = 0; nargs = hawk_rtx_getnargs(rtx); - HAWK_ASSERT (nargs >= 2 && nargs <= 3); - a0 = hawk_rtx_getarg(rtx, 0); a1 = hawk_rtx_getarg(rtx, 1); - a2 = (nargs >= 3)? hawk_rtx_getarg(rtx, 2): HAWK_NULL; a0_vtype = HAWK_RTX_GETVALTYPE(rtx, a0); - HAWK_ASSERT (a2 == HAWK_NULL || HAWK_RTX_GETVALTYPE(rtx, a2) == HAWK_VAL_REF); /* the first argument - pattern */ if (a0_vtype == HAWK_VAL_REX) @@ -1289,7 +1278,7 @@ static int __substitute (hawk_rtx_t* rtx, hawk_oow_t max_count) } /* the optional third argument - string to manipulate */ - if (a2 == HAWK_NULL) + if (nargs < 3) { /* is this correct? any needs to use inrec.d0? */ s2.ptr = HAWK_OOECS_PTR(&rtx->inrec.line); @@ -1299,23 +1288,28 @@ static int __substitute (hawk_rtx_t* rtx, hawk_oow_t max_count) s1.ptr = hawk_rtx_getvaloocstr(rtx, a1, &s1.len); s1_free = 1; } - else if (hawk_rtx_getrefvaltype(rtx, (hawk_val_ref_t*)a2) == HAWK_VAL_MBS) + else { - s2.ptr = hawk_rtx_getvalbcstr(rtx, a2, &s2.len); - s2_free = 2; + r2 = hawk_rtx_getrefval(rtx, (hawk_val_ref_t*)hawk_rtx_getarg(rtx, 2)); - /* the second argument - substitute */ - s1.ptr = hawk_rtx_getvalbcstr(rtx, a1, &s1.len); - s1_free = 2; - } - else - { - s2.ptr = hawk_rtx_getvaloocstr(rtx, a2, &s2.len); - s2_free = 1; + if (HAWK_RTX_GETVALTYPE(rtx, r2) == HAWK_VAL_MBS) + { + s2.ptr = hawk_rtx_getvalbcstr(rtx, r2, &s2.len); + s2_free = 2; - /* the second argument - substitute */ - s1.ptr = hawk_rtx_getvaloocstr(rtx, a1, &s1.len); - s1_free = 1; + /* the second argument - substitute */ + s1.ptr = hawk_rtx_getvalbcstr(rtx, a1, &s1.len); + s1_free = 2; + } + else + { + s2.ptr = hawk_rtx_getvaloocstr(rtx, r2, &s2.len); + s2_free = 1; + + /* the second argument - substitute */ + s1.ptr = hawk_rtx_getvaloocstr(rtx, a1, &s1.len); + s1_free = 1; + } } if (HAWK_UNLIKELY(!s1.ptr || !s2.ptr)) goto oops; @@ -1335,13 +1329,13 @@ static int __substitute (hawk_rtx_t* rtx, hawk_oow_t max_count) sub_count = max_count; if (s2_free == 2) { - hawk_becs_clear(&rtx->subst.bout); - if (__substitute_bcs(rtx, &sub_count, rex, (hawk_bcs_t*)&s1, (hawk_bcs_t*)&s2, &rtx->subst.bout) <= -1) goto oops; + hawk_becs_clear(&rtx->fnc.bout); + if (__substitute_bcs(rtx, &sub_count, rex, (hawk_bcs_t*)&s1, (hawk_bcs_t*)&s2, &rtx->fnc.bout) <= -1) goto oops; } else { - hawk_ooecs_clear(&rtx->subst.oout); - if (__substitute_oocs(rtx, &sub_count, rex, (hawk_oocs_t*)&s1, (hawk_oocs_t*)&s2, &rtx->subst.oout) <= -1) goto oops; + hawk_ooecs_clear(&rtx->fnc.oout); + if (__substitute_oocs(rtx, &sub_count, rex, (hawk_oocs_t*)&s1, (hawk_oocs_t*)&s2, &rtx->fnc.oout) <= -1) goto oops; } if (rex_free) @@ -1353,37 +1347,16 @@ static int __substitute (hawk_rtx_t* rtx, hawk_oow_t max_count) rex_free = HAWK_NULL; } - if (sub_count > 0) - { - int n; - if (a2 == HAWK_NULL) - { - n = hawk_rtx_setrec(rtx, 0, HAWK_OOECS_OOCS(&rtx->subst.oout), 0); - if (HAWK_UNLIKELY(n <= -1)) goto oops; - } - else - { - v = (s2_free == 2)? - hawk_rtx_makembsvalwithbcs(rtx, HAWK_BECS_BCS(&rtx->subst.bout)): - hawk_rtx_makestrvalwithoocs(rtx, HAWK_OOECS_OOCS(&rtx->subst.oout)); - if (HAWK_UNLIKELY(!v)) goto oops; - - hawk_rtx_refupval (rtx, v); - n = hawk_rtx_setrefval(rtx, (hawk_val_ref_t*)a2, v); - hawk_rtx_refdownval (rtx, v); - } - if (HAWK_UNLIKELY(n <= -1)) goto oops; - } - switch (s2_free) { case 1: - hawk_rtx_freevaloocstr (rtx, a2, s2.ptr); + hawk_rtx_freevaloocstr (rtx, r2, s2.ptr); break; case 2: - hawk_rtx_freevalbcstr (rtx, a2, s2.ptr); + hawk_rtx_freevalbcstr (rtx, r2, s2.ptr); break; } + s2.ptr = HAWK_NULL; switch (s1_free) { @@ -1394,8 +1367,34 @@ static int __substitute (hawk_rtx_t* rtx, hawk_oow_t max_count) hawk_rtx_freevalbcstr (rtx, a1, s1.ptr); break; } + s1.ptr = HAWK_NULL; - if (s0.ptr) hawk_rtx_freevaloocstr (rtx, a0, s0.ptr); + if (s0.ptr) + { + hawk_rtx_freevaloocstr (rtx, a0, s0.ptr); + s0.ptr = HAWK_NULL; + } + + if (sub_count > 0) + { + int n; + if (nargs < 3) + { + n = hawk_rtx_setrec(rtx, 0, HAWK_OOECS_OOCS(&rtx->fnc.oout), 0); + } + else + { + v = (s2_free == 2)? + hawk_rtx_makembsvalwithbcs(rtx, HAWK_BECS_BCS(&rtx->fnc.bout)): + hawk_rtx_makestrvalwithoocs(rtx, HAWK_OOECS_OOCS(&rtx->fnc.oout)); + if (HAWK_UNLIKELY(!v)) goto oops; + + hawk_rtx_refupval (rtx, v); + n = hawk_rtx_setrefval(rtx, (hawk_val_ref_t*)hawk_rtx_getarg(rtx, 2), v); + hawk_rtx_refdownval (rtx, v); + } + if (HAWK_UNLIKELY(n <= -1)) goto oops; + } v = hawk_rtx_makeintval(rtx, sub_count); if (HAWK_UNLIKELY(!v)) return -1; @@ -1417,10 +1416,10 @@ oops: switch (s2_free) { case 1: - hawk_rtx_freevaloocstr (rtx, a2, s2.ptr); + hawk_rtx_freevaloocstr (rtx, r2, s2.ptr); break; case 2: - hawk_rtx_freevalbcstr (rtx, a2, s2.ptr); + hawk_rtx_freevalbcstr (rtx, r2, s2.ptr); break; } } @@ -1458,15 +1457,25 @@ int hawk_fnc_sub (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) int hawk_fnc_match (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) { + /* + match("abcdefg", "cde"); + match("abcdefgdefx", "def", 7); + ------------------------------------ + match("abcdefgdefx", /(def)g(.+)/, 1, x); + q = length(x) / 2; + for (i = 1; i <= q; i++) print x[i,"start"], x[i,"length"]; + print RSTART, RLENGTH; + */ + hawk_oow_t nargs; - hawk_val_t* a0, * a1, * a3; + hawk_val_t* a0, * a1; + hawk_val_type_t a0_type; hawk_ooch_t* str0; hawk_oow_t len0; hawk_int_t idx, start = 1; hawk_val_t* x0 = HAWK_NULL, * x1 = HAWK_NULL, * x2 = HAWK_NULL; int n; hawk_oocs_t mat, submat[9]; - hawk_ooecs_t* tmpbuf = HAWK_NULL; nargs = hawk_rtx_getnargs(rtx); HAWK_ASSERT (nargs >= 2 && nargs <= 4); @@ -1483,21 +1492,19 @@ int hawk_fnc_match (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) * it is treated as a match start index */ n = hawk_rtx_valtoint(rtx, a2, &start); if (n <= -1) return -1; - - if (nargs >= 4) a3 = hawk_rtx_getarg(rtx, 3); } /* TODO: match support MBS */ -#if 0 - if (HAWK_RTX_GETVALTYPE(rtx, a0) == HAWK_VAL_MBS) + a0_type = HAWK_RTX_GETVALTYPE(rtx, a0); + /*if (a0_type == HAWK_VAL_MBS) { - str0 = ((hawk_val_mbs_t*)a0)->val.ptr; - len0 = ((hawk_val_mbs_t*)a0)->val.len; + str0 = hawk_rtx_getvalbcstr(rtx, a0, &len0); } -#endif - - str0 = hawk_rtx_getvaloocstr(rtx, a0, &len0); - if (str0 == HAWK_NULL) return -1; + else + {*/ + str0 = hawk_rtx_getvaloocstr(rtx, a0, &len0); + /*}*/ + if (HAWK_UNLIKELY(!str0)) return -1; if (start == 0) start = 1; else if (start < 0) start = len0 + start + 1; @@ -1508,15 +1515,21 @@ int hawk_fnc_match (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) { hawk_oocs_t tmp; - /*TODO: must use str0,len0? */ tmp.ptr = str0 + start - 1; tmp.len = len0 - start + 1; - n = hawk_rtx_matchval(rtx, a1, &tmp, &tmp, &mat, (nargs >= 4? submat: HAWK_NULL)); + n = hawk_rtx_matchvalwithoocs(rtx, a1, &tmp, &tmp, &mat, (nargs >= 4? submat: HAWK_NULL)); if (n <= -1) return -1; } - hawk_rtx_freevaloocstr (rtx, a0, str0); + /*if (a0_type == HAWK_VAL_MBS) + { + hawk_rtx_freevalbcstr (rtx, a0, str0); + } + else + {*/ + hawk_rtx_freevaloocstr (rtx, a0, str0); + /*}*/ /* RSTART: 0 on no match */ idx = (n == 0)? 0: ((hawk_int_t)(mat.ptr - str0) + 1); @@ -1537,41 +1550,40 @@ int hawk_fnc_match (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) hawk_oow_t i, xlen; hawk_val_t* tv; - tmpbuf = hawk_ooecs_open(hawk_rtx_getgem(rtx), 0, 64); - if (!tmpbuf) goto oops; + hawk_ooecs_clear (&rtx->fnc.oout); x2 = hawk_rtx_makemapval(rtx); - if (!x2) goto oops; + if (HAWK_UNLIKELY(!x2)) goto oops; hawk_rtx_refupval (rtx, x2); - submatcount =0; - subsep = hawk_rtx_getsubsep (rtx); + submatcount = 0; + subsep = hawk_rtx_getsubsep(rtx); for (i = 0; i < HAWK_COUNTOF(submat); i++) { if (!submat[i].ptr) break; submatcount++; - if (hawk_ooecs_fmt(tmpbuf, HAWK_T("%d"), (int)submatcount) == (hawk_oow_t)-1 || - hawk_ooecs_ncat(tmpbuf, subsep->ptr, subsep->len) == (hawk_oow_t)-1) goto oops; - xlen = HAWK_OOECS_LEN(tmpbuf); - if (hawk_ooecs_ncat(tmpbuf, HAWK_T("start"), 5) == (hawk_oow_t)-1) goto oops; + if (hawk_ooecs_fmt(&rtx->fnc.oout, HAWK_T("%d"), (int)submatcount) == (hawk_oow_t)-1 || + hawk_ooecs_ncat(&rtx->fnc.oout, subsep->ptr, subsep->len) == (hawk_oow_t)-1) goto oops; + xlen = HAWK_OOECS_LEN(&rtx->fnc.oout); + if (hawk_ooecs_ncat(&rtx->fnc.oout, HAWK_T("start"), 5) == (hawk_oow_t)-1) goto oops; tv = hawk_rtx_makeintval(rtx, submat[i].ptr - str0 + 1); if (!tv) goto oops; - if (!hawk_rtx_setmapvalfld(rtx, x2, HAWK_OOECS_PTR(tmpbuf), HAWK_OOECS_LEN(tmpbuf), tv)) + if (!hawk_rtx_setmapvalfld(rtx, x2, HAWK_OOECS_PTR(&rtx->fnc.oout), HAWK_OOECS_LEN(&rtx->fnc.oout), tv)) { hawk_rtx_refupval (rtx, tv); hawk_rtx_refdownval (rtx, tv); goto oops; } - if (hawk_ooecs_setlen(tmpbuf, xlen) == (hawk_oow_t)-1 || - hawk_ooecs_ncat(tmpbuf, HAWK_T("length"), 6) == (hawk_oow_t)-1) goto oops; + if (hawk_ooecs_setlen(&rtx->fnc.oout, xlen) == (hawk_oow_t)-1 || + hawk_ooecs_ncat(&rtx->fnc.oout, HAWK_T("length"), 6) == (hawk_oow_t)-1) goto oops; tv = hawk_rtx_makeintval(rtx, submat[i].len); if (!tv) goto oops; - if (!hawk_rtx_setmapvalfld(rtx, x2, HAWK_OOECS_PTR(tmpbuf), HAWK_OOECS_LEN(tmpbuf), tv)) + if (!hawk_rtx_setmapvalfld(rtx, x2, HAWK_OOECS_PTR(&rtx->fnc.oout), HAWK_OOECS_LEN(&rtx->fnc.oout), tv)) { hawk_rtx_refupval (rtx, tv); hawk_rtx_refdownval (rtx, tv); @@ -1581,7 +1593,7 @@ int hawk_fnc_match (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) /* the caller of this function must be able to get the submatch count by * dividing the array size by 2 */ - if (hawk_rtx_setrefval(rtx, (hawk_val_ref_t*)a3, x2) <= -1) goto oops; + if (hawk_rtx_setrefval(rtx, (hawk_val_ref_t*)hawk_rtx_getarg(rtx, 3), x2) <= -1) goto oops; } if (hawk_rtx_setgbl(rtx, HAWK_GBL_RSTART, x0) <= -1 || @@ -1592,14 +1604,12 @@ int hawk_fnc_match (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) hawk_rtx_setretval (rtx, x0); - if (tmpbuf) hawk_ooecs_close (tmpbuf); if (x2) hawk_rtx_refdownval (rtx, x2); hawk_rtx_refdownval (rtx, x1); hawk_rtx_refdownval (rtx, x0); return 0; oops: - if (tmpbuf) hawk_ooecs_close (tmpbuf); if (x2) hawk_rtx_refdownval (rtx, x2); if (x1) hawk_rtx_refdownval (rtx, x1); if (x0) hawk_rtx_refdownval (rtx, x0); @@ -1629,11 +1639,11 @@ int hawk_fnc_sprintf (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) fbu_inited = 1; cs0.ptr = hawk_rtx_getvalbcstr(rtx, a0, &cs0.len); - if (cs0.ptr == HAWK_NULL) goto oops_mbs; + if (HAWK_UNLIKELY(!cs0.ptr)) goto oops_mbs; x.ptr = hawk_rtx_formatmbs(rtx, &out, &fbu, cs0.ptr, cs0.len, nargs, HAWK_NULL, &x.len); hawk_rtx_freevalbcstr (rtx, a0, cs0.ptr); - if (!x.ptr) goto oops_mbs; + if (HAWK_UNLIKELY(!x.ptr)) goto oops_mbs; a0 = hawk_rtx_makembsvalwithbcs(rtx, &x); if (a0 == HAWK_NULL) goto oops_mbs; @@ -1777,8 +1787,25 @@ static HAWK_INLINE int asort_compare_ud (const void* x1, const void* x2, void* c static HAWK_INLINE int __fnc_asort (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi, int sort_keys) { + /* + a["tt"]=20; a[2]=10; a[3]=99; + asort(a); + for (i in a) print i, a[i]; + --------------------------------------------- + a["tt"]=20; a[2]=10; a[3]=99; + asort(a, b); + for (i in b) print i, b[i]; + ---------------------------------------------- + function cmp(a, b) { return b - a; } + BEGIN { + a["tt"]=20; a[2]=10; a[3]=99; a[4]=66; + asort(a, b, cmp); + for (i in b) print i, b[i]; + } + */ + hawk_oow_t nargs; - hawk_val_t* a0, * a0_val, * a1, * a2; + hawk_val_t* a0, * a0_val, * a2; hawk_val_type_t a0_type, v_type; hawk_val_t* r, * rmap = HAWK_NULL; hawk_int_t rv = 0; /* as if no element in the map */ @@ -1812,8 +1839,6 @@ static HAWK_INLINE int __fnc_asort (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi, if (nargs >= 2) { - a1 = hawk_rtx_getarg(rtx, 1); /* destination map */ - if (nargs >= 3) { a2 = hawk_rtx_getarg(rtx, 2); @@ -1832,10 +1857,6 @@ static HAWK_INLINE int __fnc_asort (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi, } } } - else - { - a1 = a0; /* let a0 be the destination. a0 is both source and destination */ - } if (!hawk_rtx_getfirstmapvalitr(rtx, a0_val, &itr)) goto done; /* map empty */ @@ -1940,7 +1961,7 @@ done: /* rmap can be NULL when a jump has been made for an empty source * at the beginning of this fucntion */ hawk_rtx_refupval (rtx, rmap); - x = hawk_rtx_setrefval (rtx, (hawk_val_ref_t*)a1, rmap); + x = hawk_rtx_setrefval(rtx, (hawk_val_ref_t*)hawk_rtx_getarg(rtx, (nargs >= 2)), rmap); hawk_rtx_refdownval (rtx, rmap); if (x <= -1) { diff --git a/hawk/lib/hawk-prv.h b/hawk/lib/hawk-prv.h index 78afc856..e99f0052 100644 --- a/hawk/lib/hawk-prv.h +++ b/hawk/lib/hawk-prv.h @@ -463,7 +463,7 @@ struct hawk_rtx_t { hawk_becs_t bout; hawk_ooecs_t oout; - } subst; /* output buffer for gsub and sub */ + } fnc; /* output buffer simple functions like gsub, sub, match*/ struct { diff --git a/hawk/lib/hawk.h b/hawk/lib/hawk.h index 31d2e73c..0d009987 100644 --- a/hawk/lib/hawk.h +++ b/hawk/lib/hawk.h @@ -3127,7 +3127,7 @@ HAWK_EXPORT hawk_uch_t* hawk_rtx_valtoucstrdupwithcmgr ( */ HAWK_EXPORT hawk_ooch_t* hawk_rtx_getvaloocstrwithcmgr ( hawk_rtx_t* rtx, /**< runtime context */ - const hawk_val_t* val, /**< value to convert */ + hawk_val_t* val, /**< value to convert */ hawk_oow_t* len, /**< result length */ hawk_cmgr_t* cmgr ); @@ -3142,14 +3142,14 @@ HAWK_EXPORT hawk_ooch_t* hawk_rtx_getvaloocstrwithcmgr ( */ HAWK_EXPORT void hawk_rtx_freevaloocstr ( hawk_rtx_t* rtx, /**< runtime context */ - const hawk_val_t* val, /**< value to convert */ + hawk_val_t* val, /**< value to convert */ hawk_ooch_t* str /**< string pointer */ ); HAWK_EXPORT hawk_bch_t* hawk_rtx_getvalbcstrwithcmgr ( hawk_rtx_t* rtx, /**< runtime context */ - const hawk_val_t* val, /**< value to convert */ + hawk_val_t* val, /**< value to convert */ hawk_oow_t* len, /**< result length */ hawk_cmgr_t* cmgr ); @@ -3158,7 +3158,7 @@ HAWK_EXPORT hawk_bch_t* hawk_rtx_getvalbcstrwithcmgr ( HAWK_EXPORT void hawk_rtx_freevalbcstr ( hawk_rtx_t* rtx, /**< runtime context */ - const hawk_val_t* val, /**< value to convert */ + hawk_val_t* val, /**< value to convert */ hawk_bch_t* str /**< string pointer */ ); diff --git a/hawk/lib/misc-prv.h b/hawk/lib/misc-prv.h index ae5dc9b8..60148835 100644 --- a/hawk/lib/misc-prv.h +++ b/hawk/lib/misc-prv.h @@ -70,12 +70,17 @@ hawk_ooch_t* hawk_rtx_strxnfld ( hawk_oocs_t* tok ); -int hawk_rtx_matchval ( +int hawk_rtx_matchvalwithucs ( hawk_rtx_t* rtx, hawk_val_t* val, - const hawk_oocs_t* str, const hawk_oocs_t* substr, - hawk_oocs_t* match, hawk_oocs_t submat[9] + const hawk_ucs_t* str, const hawk_ucs_t* substr, + hawk_ucs_t* match, hawk_ucs_t submat[9] ); +int hawk_rtx_matchvalwithbcs ( + hawk_rtx_t* rtx, hawk_val_t* val, + const hawk_bcs_t* str, const hawk_bcs_t* substr, + hawk_bcs_t* match, hawk_bcs_t submat[9] +); int hawk_rtx_matchrexwithucs ( hawk_rtx_t* rtx, hawk_tre_t* code, @@ -90,8 +95,10 @@ int hawk_rtx_matchrexwithbcs ( ); #if defined(HAWK_OOCH_IS_UCH) +# define hawk_rtx_matchvalwithoocs hawk_rtx_matchvalwithucs # define hawk_rtx_matchrexwithoocs hawk_rtx_matchrexwithucs #else +# define hawk_rtx_matchvalwithoocs hawk_rtx_matchvalwithbcs # define hawk_rtx_matchrexwithoocs hawk_rtx_matchrexwithbcs #endif diff --git a/hawk/lib/misc.c b/hawk/lib/misc.c index 986931ed..eec6fc45 100644 --- a/hawk/lib/misc.c +++ b/hawk/lib/misc.c @@ -510,15 +510,17 @@ static int matchtre_bcs (hawk_tre_t* tre, int opt, const hawk_bcs_t* str, hawk_b return 1; } -int hawk_rtx_matchval (hawk_rtx_t* rtx, hawk_val_t* val, const hawk_oocs_t* str, const hawk_oocs_t* substr, hawk_oocs_t* match, hawk_oocs_t submat[9]) +int hawk_rtx_matchvalwithucs (hawk_rtx_t* rtx, hawk_val_t* val, const hawk_ucs_t* str, const hawk_ucs_t* substr, hawk_ucs_t* match, hawk_ucs_t submat[9]) { int ignorecase, x; int opt = HAWK_TRE_BACKTRACKING; /* TODO: option... HAWK_TRE_BACKTRACKING ??? */ hawk_tre_t* code; + hawk_val_type_t v_type; ignorecase = rtx->gbl.ignorecase; - if (HAWK_RTX_GETVALTYPE(rtx, val) == HAWK_VAL_REX) + v_type = HAWK_RTX_GETVALTYPE(rtx, val); + if (v_type == HAWK_VAL_REX) { code = ((hawk_val_rex_t*)val)->code[ignorecase]; } @@ -536,16 +538,13 @@ int hawk_rtx_matchval (hawk_rtx_t* rtx, hawk_val_t* val, const hawk_oocs_t* str, if (x <= -1) return -1; } -#if defined(HAWK_OOCH_IS_UCH) + x = matchtre_ucs( -#else - x = matchtre_bcs( -#endif code, ((str->ptr == substr->ptr)? opt: (opt | HAWK_TRE_NOTBOL)), substr, match, submat, hawk_rtx_getgem(rtx) ); - if (HAWK_RTX_GETVALTYPE(rtx, val) == HAWK_VAL_REX) + if (v_type == HAWK_VAL_REX) { /* nothing to free */ } @@ -557,6 +556,53 @@ int hawk_rtx_matchval (hawk_rtx_t* rtx, hawk_val_t* val, const hawk_oocs_t* str, return x; } + +int hawk_rtx_matchvalwithbcs (hawk_rtx_t* rtx, hawk_val_t* val, const hawk_bcs_t* str, const hawk_bcs_t* substr, hawk_bcs_t* match, hawk_bcs_t submat[9]) +{ + int ignorecase, x; + int opt = HAWK_TRE_BACKTRACKING; /* TODO: option... HAWK_TRE_BACKTRACKING ??? */ + hawk_tre_t* code; + hawk_val_type_t v_type; + + ignorecase = rtx->gbl.ignorecase; + + v_type = HAWK_RTX_GETVALTYPE(rtx, val); + if (v_type == HAWK_VAL_REX) + { + code = ((hawk_val_rex_t*)val)->code[ignorecase]; + } + else + { + /* convert to a string and build a regular expression */ + hawk_oocs_t tmp; + + tmp.ptr = hawk_rtx_getvaloocstr(rtx, val, &tmp.len); + if (HAWK_UNLIKELY(!tmp.ptr)) return -1; + + x = ignorecase? hawk_rtx_buildrex(rtx, tmp.ptr, tmp.len, HAWK_NULL, &code): + hawk_rtx_buildrex(rtx, tmp.ptr, tmp.len, &code, HAWK_NULL); + hawk_rtx_freevaloocstr (rtx, val, tmp.ptr); + if (x <= -1) return -1; + } + + x = matchtre_bcs( + code, ((str->ptr == substr->ptr)? opt: (opt | HAWK_TRE_NOTBOL)), + substr, match, submat, hawk_rtx_getgem(rtx) + ); + + if (v_type == HAWK_VAL_REX) + { + /* nothing to free */ + } + else + { + hawk_tre_close (code); + } + + return x; +} + + int hawk_rtx_matchrexwithucs (hawk_rtx_t* rtx, hawk_tre_t* code, const hawk_ucs_t* str, const hawk_ucs_t* substr, hawk_ucs_t* match, hawk_ucs_t submat[9]) { int opt = HAWK_TRE_BACKTRACKING; /* TODO: option... HAWK_TRE_BACKTRACKING or others??? */ diff --git a/hawk/lib/run.c b/hawk/lib/run.c index 4fc380db..209d84bf 100644 --- a/hawk/lib/run.c +++ b/hawk/lib/run.c @@ -1055,8 +1055,8 @@ static int init_rtx (hawk_rtx_t* rtx, hawk_t* awk, hawk_rio_cbs_t* rio) if (hawk_becs_init(&rtx->formatmbs.out, hawk_rtx_getgem(rtx), 256) <= -1) goto oops_7; if (hawk_becs_init(&rtx->formatmbs.fmt, hawk_rtx_getgem(rtx), 256) <= -1) goto oops_8; - if (hawk_becs_init(&rtx->subst.bout, hawk_rtx_getgem(rtx), 256) <= -1) goto oops_9; - if (hawk_ooecs_init(&rtx->subst.oout, hawk_rtx_getgem(rtx), 256) <= -1) goto oops_10; + if (hawk_becs_init(&rtx->fnc.bout, hawk_rtx_getgem(rtx), 256) <= -1) goto oops_9; + if (hawk_ooecs_init(&rtx->fnc.oout, hawk_rtx_getgem(rtx), 256) <= -1) goto oops_10; rtx->named = hawk_htb_open(hawk_rtx_getgem(rtx), HAWK_SIZEOF(rtx), 1024, 70, HAWK_SIZEOF(hawk_ooch_t), 1); @@ -1107,9 +1107,9 @@ oops_13: oops_12: hawk_htb_close (rtx->named); oops_11: - hawk_becs_fini (&rtx->subst.oout); + hawk_becs_fini (&rtx->fnc.oout); oops_10: - hawk_becs_fini (&rtx->subst.bout); + hawk_becs_fini (&rtx->fnc.bout); oops_9: hawk_becs_fini (&rtx->formatmbs.fmt); oops_8: @@ -1196,8 +1196,8 @@ static void fini_rtx (hawk_rtx_t* rtx, int fini_globals) rtx->gbl.subsep.len = 0; } - hawk_ooecs_fini (&rtx->subst.oout); - hawk_becs_fini (&rtx->subst.bout); + hawk_ooecs_fini (&rtx->fnc.oout); + hawk_becs_fini (&rtx->fnc.bout); hawk_rtx_freemem (rtx, rtx->formatmbs.tmp.ptr); rtx->formatmbs.tmp.ptr = HAWK_NULL; @@ -3369,7 +3369,7 @@ static hawk_val_t* eval_expression (hawk_rtx_t* rtx, hawk_nde_t* nde) #endif } - n = hawk_rtx_matchval(rtx, v, &vs, &vs, HAWK_NULL, HAWK_NULL); + n = hawk_rtx_matchvalwithoocs(rtx, v, &vs, &vs, HAWK_NULL, HAWK_NULL); hawk_rtx_refdownval (rtx, v); if (free_vs) hawk_rtx_freevaloocstr (rtx, rtx->inrec.d0, vs.ptr); if (n <= -1) @@ -5389,17 +5389,17 @@ static hawk_val_t* eval_binop_match0 ( out.ptr = hawk_rtx_getvaloocstr (rtx, left, &out.len); if (out.ptr == HAWK_NULL) return HAWK_NULL; - n = hawk_rtx_matchval(rtx, right, &out, &out, HAWK_NULL, HAWK_NULL); + n = hawk_rtx_matchvalwithoocs(rtx, right, &out, &out, HAWK_NULL, HAWK_NULL); hawk_rtx_freevaloocstr (rtx, left, out.ptr); - if (n <= -1) + if (HAWK_UNLIKELY(n <= -1)) { ADJERR_LOC (rtx, lloc); return HAWK_NULL; } - res = hawk_rtx_makeintval (rtx, (n == ret)); - if (res == HAWK_NULL) + res = hawk_rtx_makeintval(rtx, (n == ret)); + if (HAWK_UNLIKELY(!res)) { ADJERR_LOC (rtx, lloc); return HAWK_NULL; diff --git a/hawk/lib/std.c b/hawk/lib/std.c index 788c0075..21824a0f 100644 --- a/hawk/lib/std.c +++ b/hawk/lib/std.c @@ -3147,7 +3147,7 @@ done: if (rv) { hawk_rtx_refupval (rtx, rv); - ret = hawk_rtx_setrefval (rtx, (hawk_val_ref_t*)hawk_rtx_getarg (rtx, 2), rv); + ret = hawk_rtx_setrefval (rtx, (hawk_val_ref_t*)hawk_rtx_getarg(rtx, 2), rv); hawk_rtx_refdownval (rtx, rv); if (ret >= 0) hawk_rtx_setretval (rtx, HAWK_VAL_ZERO); } diff --git a/hawk/lib/val.c b/hawk/lib/val.c index b8186069..f6a2d0e6 100644 --- a/hawk/lib/val.c +++ b/hawk/lib/val.c @@ -302,7 +302,6 @@ hawk_val_t* hawk_rtx_makestrvalwithbchars2 (hawk_rtx_t* rtx, const hawk_bch_t* b hawk_val_t* hawk_rtx_makenumorstrvalwithuchars (hawk_rtx_t* rtx, const hawk_uch_t* ptr, hawk_oow_t len) { - hawk_t* hawk = hawk_rtx_gethawk(rtx); int x; hawk_int_t l; hawk_flt_t r; @@ -316,7 +315,6 @@ hawk_val_t* hawk_rtx_makenumorstrvalwithuchars (hawk_rtx_t* rtx, const hawk_uch_ hawk_val_t* hawk_rtx_makenumorstrvalwithbchars (hawk_rtx_t* rtx, const hawk_bch_t* ptr, hawk_oow_t len) { - hawk_t* hawk = hawk_rtx_gethawk(rtx); int x; hawk_int_t l; hawk_flt_t r; @@ -333,7 +331,6 @@ hawk_val_t* hawk_rtx_makenumorstrvalwithbchars (hawk_rtx_t* rtx, const hawk_bch_ hawk_val_t* hawk_rtx_makenstrvalwithuchars (hawk_rtx_t* rtx, const hawk_uch_t* ptr, hawk_oow_t len) { - hawk_t* hawk = hawk_rtx_gethawk(rtx); int x; hawk_val_t* v; hawk_int_t l; @@ -357,7 +354,6 @@ hawk_val_t* hawk_rtx_makenstrvalwithuchars (hawk_rtx_t* rtx, const hawk_uch_t* p hawk_val_t* hawk_rtx_makenstrvalwithbchars (hawk_rtx_t* rtx, const hawk_bch_t* ptr, hawk_oow_t len) { - hawk_t* hawk = hawk_rtx_gethawk(rtx); int x; hawk_val_t* v; hawk_int_t l; @@ -460,7 +456,6 @@ hawk_val_t* hawk_rtx_makembsvalwithucs (hawk_rtx_t* rtx, const hawk_ucs_t* ucs) hawk_val_t* hawk_rtx_makenumormbsvalwithuchars (hawk_rtx_t* rtx, const hawk_uch_t* ptr, hawk_oow_t len) { - hawk_t* hawk = hawk_rtx_gethawk(rtx); int x; hawk_int_t l; hawk_flt_t r; @@ -474,7 +469,6 @@ hawk_val_t* hawk_rtx_makenumormbsvalwithuchars (hawk_rtx_t* rtx, const hawk_uch_ hawk_val_t* hawk_rtx_makenumormbsvalwithbchars (hawk_rtx_t* rtx, const hawk_bch_t* ptr, hawk_oow_t len) { - hawk_t* hawk = hawk_rtx_gethawk(rtx); int x; hawk_int_t l; hawk_flt_t r; @@ -1362,7 +1356,7 @@ static int val_ref_to_str (hawk_rtx_t* rtx, const hawk_val_ref_t* ref, hawk_rtx_ case HAWK_VAL_REF_POS: { hawk_oow_t idx; - + /* special case when the reference value is * pointing to the positional */ @@ -1594,54 +1588,110 @@ hawk_uch_t* hawk_rtx_valtoucstrdupwithcmgr (hawk_rtx_t* rtx, const hawk_val_t* v return wcs; } -hawk_ooch_t* hawk_rtx_getvaloocstrwithcmgr (hawk_rtx_t* rtx, const hawk_val_t* v, hawk_oow_t* len, hawk_cmgr_t* cmgr) +hawk_ooch_t* hawk_rtx_getvaloocstrwithcmgr (hawk_rtx_t* rtx, hawk_val_t* v, hawk_oow_t* len, hawk_cmgr_t* cmgr) { - if (HAWK_RTX_GETVALTYPE(rtx, v) == HAWK_VAL_STR) + switch (HAWK_RTX_GETVALTYPE(rtx, v)) { - if (len) *len = ((hawk_val_str_t*)v)->val.len; - return ((hawk_val_str_t*)v)->val.ptr; - } - else - { - return hawk_rtx_valtooocstrdupwithcmgr(rtx, v, len, cmgr); + case HAWK_VAL_STR: +#if 0 + plain_str: +#endif + if (len) *len = ((hawk_val_str_t*)v)->val.len; + return ((hawk_val_str_t*)v)->val.ptr; + +#if 0 +/* i'm commenting out this part because hawk_rtx_setrefval() changes v->adr + * and leads hawk_rtx_freevaloocstr() to check a wrong value obejct. + * if you know that a value is a reference, you can get the referenced value + * with hawk_rtx_getrefval() and call this function over it */ + case HAWK_VAL_REF: + v = hawk_rtx_getrefval(rtx, (hawk_val_ref_t*)v); + if (HAWK_RTX_GETVALTYPE(rtx, v) == HAWK_VAL_STR) goto plain_str; + /* fall through */ +#endif + + default: + return hawk_rtx_valtooocstrdupwithcmgr(rtx, v, len, cmgr); } } -void hawk_rtx_freevaloocstr (hawk_rtx_t* rtx, const hawk_val_t* v, hawk_ooch_t* str) +void hawk_rtx_freevaloocstr (hawk_rtx_t* rtx, hawk_val_t* v, hawk_ooch_t* str) { - if (HAWK_RTX_GETVALTYPE(rtx, v) != HAWK_VAL_STR || - str != ((hawk_val_str_t*)v)->val.ptr) + switch (HAWK_RTX_GETVALTYPE(rtx, v)) { - hawk_rtx_freemem (rtx, str); + case HAWK_VAL_STR: +#if 0 + plain_str: +#endif + if (str != ((hawk_val_str_t*)v)->val.ptr) hawk_rtx_freemem (rtx, str); + break; + +#if 0 + case HAWK_VAL_REF: + v = hawk_rtx_getrefval(rtx, (hawk_val_ref_t*)v); + if (HAWK_RTX_GETVALTYPE(rtx, v) == HAWK_VAL_STR) goto plain_str; + /* fall through */ +#endif + + default: + hawk_rtx_freemem (rtx, str); + break; } } -hawk_bch_t* hawk_rtx_getvalbcstrwithcmgr (hawk_rtx_t* rtx, const hawk_val_t* v, hawk_oow_t* len, hawk_cmgr_t* cmgr) +hawk_bch_t* hawk_rtx_getvalbcstrwithcmgr (hawk_rtx_t* rtx, hawk_val_t* v, hawk_oow_t* len, hawk_cmgr_t* cmgr) { - if (HAWK_RTX_GETVALTYPE(rtx, v) == HAWK_VAL_MBS) + switch (HAWK_RTX_GETVALTYPE(rtx, v)) { - if (len) *len = ((hawk_val_mbs_t*)v)->val.len; - return ((hawk_val_mbs_t*)v)->val.ptr; - } - else - { - return hawk_rtx_valtobcstrdupwithcmgr(rtx, v, len, cmgr); + case HAWK_VAL_MBS: +#if 0 + plain_mbs: +#endif + if (len) *len = ((hawk_val_mbs_t*)v)->val.len; + return ((hawk_val_mbs_t*)v)->val.ptr; + +#if 0 +/* i'm commenting out this part because hawk_rtx_setrefval() changes v->adr + * and leads hawk_rtx_freevalbcstr() to check a wrong value obejct. + * if you know that a value is a reference, you can get the referenced value + * with hawk_rtx_getrefval() and call this function over it */ + case HAWK_VAL_REF: + v = hawk_rtx_getrefval(rtx, (hawk_val_ref_t*)v); + if (HAWK_RTX_GETVALTYPE(rtx, v) == HAWK_VAL_MBS) goto plain_mbs; + /* fall through */ +#endif + + default: + return hawk_rtx_valtobcstrdupwithcmgr(rtx, v, len, cmgr); } } -void hawk_rtx_freevalbcstr (hawk_rtx_t* rtx, const hawk_val_t* v, hawk_bch_t* str) +void hawk_rtx_freevalbcstr (hawk_rtx_t* rtx, hawk_val_t* v, hawk_bch_t* str) { - if (HAWK_RTX_GETVALTYPE(rtx, v) != HAWK_VAL_MBS || - str != ((hawk_val_mbs_t*)v)->val.ptr) + switch (HAWK_RTX_GETVALTYPE(rtx, v)) { - hawk_rtx_freemem (rtx, str); + case HAWK_VAL_MBS: +#if 0 + plain_mbs: +#endif + if (str != ((hawk_val_mbs_t*)v)->val.ptr) hawk_rtx_freemem (rtx, str); + break; + +#if 0 + case HAWK_VAL_REF: + v = hawk_rtx_getrefval(rtx, (hawk_val_ref_t*)v); + if (HAWK_RTX_GETVALTYPE(rtx, v) == HAWK_VAL_MBS) goto plain_mbs; + /* fall through */ +#endif + + default: + hawk_rtx_freemem (rtx, str); + break; } } static int val_ref_to_num (hawk_rtx_t* rtx, const hawk_val_ref_t* ref, hawk_int_t* l, hawk_flt_t* r) { - hawk_t* hawk = hawk_rtx_gethawk(rtx); - switch (ref->id) { case HAWK_VAL_REF_POS: @@ -1698,7 +1748,6 @@ static int val_ref_to_num (hawk_rtx_t* rtx, const hawk_val_ref_t* ref, hawk_int_ int hawk_rtx_valtonum (hawk_rtx_t* rtx, const hawk_val_t* v, hawk_int_t* l, hawk_flt_t* r) { hawk_val_type_t vtype = HAWK_RTX_GETVALTYPE(rtx, v); - hawk_t* hawk = hawk_rtx_gethawk(rtx); switch (vtype) { @@ -1854,6 +1903,7 @@ hawk_val_type_t hawk_rtx_getrefvaltype (hawk_rtx_t* rtx, hawk_val_ref_t* ref) { return HAWK_VAL_STR; } + case HAWK_VAL_REF_GBL: { hawk_oow_t idx; @@ -1902,7 +1952,7 @@ hawk_val_t* hawk_rtx_getrefval (hawk_rtx_t* rtx, hawk_val_ref_t* ref) /* A reference value is not able to point to another * refernce value for the way values are represented * in HAWKAWK */ - HAWK_ASSERT (HAWK_RTX_GETVALTYPE (rtx, *xref)!= HAWK_VAL_REF); + HAWK_ASSERT (HAWK_RTX_GETVALTYPE(rtx, *xref) != HAWK_VAL_REF); return *xref; } }