more code clean-up
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-08-31 17:58:12 +09:00
parent 8f1b8ffc03
commit 6aca30c893
3 changed files with 194 additions and 181 deletions

View File

@ -861,21 +861,19 @@ static int fnc_split (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi, int use_array)
hawk_oocs_t tok; hawk_oocs_t tok;
hawk_int_t nflds; hawk_int_t nflds;
int is_byte_str; int is_byte_str;
int x, do_fld = 0; int x;
int switch_fs_to_bchr = 0; int do_fld;
int switch_fs_to_bchr;
str.ptr = HAWK_NULL;
str.len = 0;
nargs = hawk_rtx_getnargs(rtx); nargs = hawk_rtx_getnargs(rtx);
HAWK_ASSERT (nargs >= 2 && nargs <= 3);
a0 = hawk_rtx_getarg(rtx, 0); a0 = hawk_rtx_getarg(rtx, 0);
a2 = (nargs >= 3)? hawk_rtx_getarg(rtx, 2): HAWK_NULL; a2 = (nargs >= 3)? hawk_rtx_getarg(rtx, 2): HAWK_NULL;
do_fld = 0;
switch_fs_to_bchr = 0;
is_byte_str = 0;
str.ptr = HAWK_NULL; str.ptr = HAWK_NULL;
str.len = 0; str.len = 0;
is_byte_str = 0;
/* field seperator */ /* field seperator */
t0 = a2? a2: hawk_rtx_getgbl(rtx, HAWK_GBL_FS); /* if a2 is not available, get the value from FS */ t0 = a2? a2: hawk_rtx_getgbl(rtx, HAWK_GBL_FS); /* if a2 is not available, get the value from FS */
@ -977,7 +975,8 @@ static int fnc_split (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi, int use_array)
{ {
if (fs_rex) if (fs_rex)
{ {
p = is_byte_str? (hawk_ooch_t*)hawk_rtx_tokbcharsbyrex(rtx, (hawk_bch_t*)str.ptr, org_len, (hawk_bch_t*)p, str.len, fs_rex, (hawk_bcs_t*)&tok): p = is_byte_str?
(hawk_ooch_t*)hawk_rtx_tokbcharsbyrex(rtx, (hawk_bch_t*)str.ptr, org_len, (hawk_bch_t*)p, str.len, fs_rex, (hawk_bcs_t*)&tok):
hawk_rtx_tokoocharsbyrex(rtx, str.ptr, org_len, p, str.len, fs_rex, &tok); hawk_rtx_tokoocharsbyrex(rtx, str.ptr, org_len, p, str.len, fs_rex, &tok);
if (p && hawk_rtx_geterrnum(rtx) != HAWK_ENOERR) goto oops; if (p && hawk_rtx_geterrnum(rtx) != HAWK_ENOERR) goto oops;
} }
@ -985,12 +984,15 @@ static int fnc_split (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi, int use_array)
{ {
/* [NOTE] even if is_byte_str is true, the field seperator is of the ooch type. /* [NOTE] even if is_byte_str is true, the field seperator is of the ooch type.
* there may be some data truncation and related issues */ * there may be some data truncation and related issues */
p = is_byte_str? (hawk_ooch_t*)hawk_rtx_fldbchars(rtx, (hawk_bch_t*)p, str.len, fs.ptr[1], fs.ptr[2], fs.ptr[3], fs.ptr[4], (hawk_bcs_t*)&tok): /* ? sep esc lq rq */
p = is_byte_str?
(hawk_ooch_t*)hawk_rtx_fldbchars(rtx, (hawk_bch_t*)p, str.len, (hawk_bchu_t)fs.ptr[1], (hawk_bchu_t)fs.ptr[2], (hawk_bchu_t)fs.ptr[3], (hawk_bchu_t)fs.ptr[4], (hawk_bcs_t*)&tok):
hawk_rtx_fldoochars(rtx, p, str.len, fs.ptr[1], fs.ptr[2], fs.ptr[3], fs.ptr[4], &tok); hawk_rtx_fldoochars(rtx, p, str.len, fs.ptr[1], fs.ptr[2], fs.ptr[3], fs.ptr[4], &tok);
} }
else else
{ {
p = is_byte_str? (hawk_ooch_t*)hawk_rtx_tokbcharswithbchars(rtx, (hawk_bch_t*)p, str.len, (hawk_bch_t*)fs.ptr, fs.len, (hawk_bcs_t*)&tok): p = is_byte_str?
(hawk_ooch_t*)hawk_rtx_tokbcharswithbchars(rtx, (hawk_bch_t*)p, str.len, (hawk_bch_t*)fs.ptr, fs.len, (hawk_bcs_t*)&tok):
hawk_rtx_tokoocharswithoochars(rtx, p, str.len, fs.ptr, fs.len, &tok); hawk_rtx_tokoocharswithoochars(rtx, p, str.len, fs.ptr, fs.len, &tok);
} }
@ -1311,7 +1313,6 @@ static int __substitute_oocs (hawk_rtx_t* rtx, hawk_oow_t* max_count, hawk_tre_t
{ {
m = hawk_ooecs_ccat(new, s1->ptr[i]); m = hawk_ooecs_ccat(new, s1->ptr[i]);
} }
if (HAWK_UNLIKELY(m == (hawk_oow_t)-1)) goto oops; if (HAWK_UNLIKELY(m == (hawk_oow_t)-1)) goto oops;
} }
@ -1347,13 +1348,14 @@ static int __substitute_bcs (hawk_rtx_t* rtx, hawk_oow_t* max_count, hawk_tre_t*
{ {
hawk_bcs_t mat, pmat, cur; hawk_bcs_t mat, pmat, cur;
hawk_bcs_t submat[9]; hawk_bcs_t submat[9];
hawk_oow_t sub_count, match_limit; hawk_oow_t sub_count, op_count, match_limit;
hawk_bch_t* s2_end; hawk_bch_t* s2_end;
s2_end = s2->ptr + s2->len; s2_end = s2->ptr + s2->len;
cur.ptr = s2->ptr; cur.ptr = s2->ptr;
cur.len = s2->len; cur.len = s2->len;
sub_count = 0; sub_count = 0;
op_count = 0;
match_limit = *max_count; match_limit = *max_count;
pmat.ptr = HAWK_NULL; pmat.ptr = HAWK_NULL;
@ -1389,6 +1391,13 @@ static int __substitute_bcs (hawk_rtx_t* rtx, hawk_oow_t* max_count, hawk_tre_t*
if (hawk_becs_ncat(new, cur.ptr, mat.ptr - cur.ptr) == (hawk_oow_t)-1) goto oops; if (hawk_becs_ncat(new, cur.ptr, mat.ptr - cur.ptr) == (hawk_oow_t)-1) goto oops;
if (extended && op_pos > 0 && (op_pos - 1) != op_count)
{
/* don't perform substitution. just copy the original string to the output */
if (hawk_becs_ncat(new, mat.ptr, mat.len) == (hawk_oow_t)-1) goto oops;
}
else
{
for (i = 0; i < s1->len; i++) for (i = 0; i < s1->len; i++)
{ {
if (s1->ptr[i] == '\\' && (i + 1) < s1->len) if (s1->ptr[i] == '\\' && (i + 1) < s1->len)
@ -1422,11 +1431,11 @@ static int __substitute_bcs (hawk_rtx_t* rtx, hawk_oow_t* max_count, hawk_tre_t*
{ {
m = hawk_becs_ccat(new, s1->ptr[i]); m = hawk_becs_ccat(new, s1->ptr[i]);
} }
if (HAWK_UNLIKELY(m == (hawk_oow_t)-1)) goto oops; if (HAWK_UNLIKELY(m == (hawk_oow_t)-1)) goto oops;
} }
sub_count++; sub_count++;
}
op_count++;
cur.len = cur.len - ((mat.ptr - cur.ptr) + mat.len); cur.len = cur.len - ((mat.ptr - cur.ptr) + mat.len);
cur.ptr = mat.ptr + mat.len; cur.ptr = mat.ptr + mat.len;

View File

@ -6045,9 +6045,9 @@ static hawk_val_t* eval_binop_div (hawk_rtx_t* rtx, hawk_val_t* left, hawk_val_t
return HAWK_NULL; return HAWK_NULL;
} }
if (((hawk_int_t)l1 % (hawk_int_t)l2) == 0) if ((l1 % l2) == 0)
{ {
res = hawk_rtx_makeintval(rtx, (hawk_int_t)l1 / (hawk_int_t)l2); res = hawk_rtx_makeintval(rtx, l1 / l2);
} }
else else
{ {
@ -6056,15 +6056,15 @@ static hawk_val_t* eval_binop_div (hawk_rtx_t* rtx, hawk_val_t* left, hawk_val_t
break; break;
case 1: case 1:
res = hawk_rtx_makefltval(rtx, (hawk_flt_t)r1 / (hawk_flt_t)l2); res = hawk_rtx_makefltval(rtx, r1 / (hawk_flt_t)l2);
break; break;
case 2: case 2:
res = hawk_rtx_makefltval(rtx, (hawk_flt_t)l1 / (hawk_flt_t)r2); res = hawk_rtx_makefltval(rtx, (hawk_flt_t)l1 / r2);
break; break;
case 3: case 3:
res = hawk_rtx_makefltval(rtx, (hawk_flt_t)r1 / (hawk_flt_t)r2); res = hawk_rtx_makefltval(rtx, r1 / r2);
break; break;
} }
@ -6096,21 +6096,21 @@ static hawk_val_t* eval_binop_idiv (hawk_rtx_t* rtx, hawk_val_t* left, hawk_val_
hawk_rtx_seterrnum(rtx, HAWK_NULL, HAWK_EDIVBY0); hawk_rtx_seterrnum(rtx, HAWK_NULL, HAWK_EDIVBY0);
return HAWK_NULL; return HAWK_NULL;
} }
res = hawk_rtx_makeintval(rtx, (hawk_int_t)l1 / (hawk_int_t)l2); res = hawk_rtx_makeintval(rtx, l1 / l2);
break; break;
case 1: case 1:
quo = (hawk_flt_t)r1 / (hawk_flt_t)l2; quo = r1 / (hawk_flt_t)l2;
res = hawk_rtx_makeintval(rtx, (hawk_int_t)quo); res = hawk_rtx_makeintval(rtx, (hawk_int_t)quo);
break; break;
case 2: case 2:
quo = (hawk_flt_t)l1 / (hawk_flt_t)r2; quo = (hawk_flt_t)l1 / r2;
res = hawk_rtx_makeintval(rtx, (hawk_int_t)quo); res = hawk_rtx_makeintval(rtx, (hawk_int_t)quo);
break; break;
case 3: case 3:
quo = (hawk_flt_t)r1 / (hawk_flt_t)r2; quo = r1 / r2;
res = hawk_rtx_makeintval(rtx, (hawk_int_t)quo); res = hawk_rtx_makeintval(rtx, (hawk_int_t)quo);
break; break;
} }
@ -6146,15 +6146,15 @@ static hawk_val_t* eval_binop_mod (hawk_rtx_t* rtx, hawk_val_t* left, hawk_val_t
hawk_rtx_seterrnum(rtx, HAWK_NULL, HAWK_EDIVBY0); hawk_rtx_seterrnum(rtx, HAWK_NULL, HAWK_EDIVBY0);
return HAWK_NULL; return HAWK_NULL;
} }
res = hawk_rtx_makeintval(rtx, (hawk_int_t)l1 % (hawk_int_t)l2); res = hawk_rtx_makeintval(rtx, l1 % l2);
break; break;
case 1: case 1:
res = hawk_rtx_makefltval(rtx, rtx->hawk->prm.math.mod(hawk_rtx_gethawk(rtx), (hawk_flt_t)r1, (hawk_flt_t)l2)); res = hawk_rtx_makefltval(rtx, rtx->hawk->prm.math.mod(hawk_rtx_gethawk(rtx), r1, (hawk_flt_t)l2));
break; break;
case 2: case 2:
res = hawk_rtx_makefltval(rtx, rtx->hawk->prm.math.mod(hawk_rtx_gethawk(rtx), (hawk_flt_t)l1, (hawk_flt_t)r2)); res = hawk_rtx_makefltval(rtx, rtx->hawk->prm.math.mod(hawk_rtx_gethawk(rtx), (hawk_flt_t)l1, r2));
break; break;
case 3: case 3:
@ -6232,7 +6232,7 @@ static hawk_val_t* eval_binop_exp (hawk_rtx_t* rtx, hawk_val_t* left, hawk_val_t
/* left - int, right - real */ /* left - int, right - real */
res = hawk_rtx_makefltval( res = hawk_rtx_makefltval(
rtx, rtx,
rtx->hawk->prm.math.pow(hawk_rtx_gethawk(rtx), (hawk_flt_t)l1, (hawk_flt_t)r2) rtx->hawk->prm.math.pow(hawk_rtx_gethawk(rtx), (hawk_flt_t)l1, r2)
); );
break; break;
@ -6240,7 +6240,7 @@ static hawk_val_t* eval_binop_exp (hawk_rtx_t* rtx, hawk_val_t* left, hawk_val_t
/* left - real, right - real */ /* left - real, right - real */
res = hawk_rtx_makefltval( res = hawk_rtx_makefltval(
rtx, rtx,
rtx->hawk->prm.math.pow(hawk_rtx_gethawk(rtx), (hawk_flt_t)r1,(hawk_flt_t)r2) rtx->hawk->prm.math.pow(hawk_rtx_gethawk(rtx), r1, r2)
); );
break; break;
} }

View File

@ -210,6 +210,8 @@ function main()
tap_ensure (x, "the tiger-tiger pounces on the dog", @SCRIPTNAME, @SCRIPTLINE); tap_ensure (x, "the tiger-tiger pounces on the dog", @SCRIPTNAME, @SCRIPTLINE);
x = gensub(/(tiger|dog)/, "\\1-\\1", 2, "the tiger pounces on the dog"); x = gensub(/(tiger|dog)/, "\\1-\\1", 2, "the tiger pounces on the dog");
tap_ensure (x, "the tiger pounces on the dog-dog", @SCRIPTNAME, @SCRIPTLINE); tap_ensure (x, "the tiger pounces on the dog-dog", @SCRIPTNAME, @SCRIPTLINE);
x = gensub(/(tiger|dog)/, "\\1-\\1", 2, @b"the tiger pounces on the dog");
tap_ensure (x, @b"the tiger pounces on the dog-dog", @SCRIPTNAME, @SCRIPTLINE);
## 0 as the third argument is same as not passing "g"/"G" or a positive occurrence number. ## 0 as the third argument is same as not passing "g"/"G" or a positive occurrence number.
x = gensub(/(tiger|dog)/, "\\1-\\1", 0, "the tiger pounces on the dog"); x = gensub(/(tiger|dog)/, "\\1-\\1", 0, "the tiger pounces on the dog");
@ -220,6 +222,8 @@ function main()
tap_ensure (x, "the tiger pounces on the dog", @SCRIPTNAME, @SCRIPTLINE); tap_ensure (x, "the tiger pounces on the dog", @SCRIPTNAME, @SCRIPTLINE);
x = gensub(/(tiger|(dog))/, "\\1-\\2", 'g', "the tiger pounces on the dog"); x = gensub(/(tiger|(dog))/, "\\1-\\2", 'g', "the tiger pounces on the dog");
tap_ensure (x, "the tiger- pounces on the dog-dog", @SCRIPTNAME, @SCRIPTLINE); tap_ensure (x, "the tiger- pounces on the dog-dog", @SCRIPTNAME, @SCRIPTLINE);
x = gensub(/(tiger|(dog))/, "\\1-\\2", 'g', @b"the tiger pounces on the dog");
tap_ensure (x, @b"the tiger- pounces on the dog-dog", @SCRIPTNAME, @SCRIPTLINE);
x = gensub(/(tiger|(dog))/, "[&]", 'g', "the tiger pounces on the dog"); x = gensub(/(tiger|(dog))/, "[&]", 'g', "the tiger pounces on the dog");
tap_ensure (x, "the [tiger] pounces on the [dog]", @SCRIPTNAME, @SCRIPTLINE); tap_ensure (x, "the [tiger] pounces on the [dog]", @SCRIPTNAME, @SCRIPTLINE);
x = gensub(/(tiger|(dog))/, "[\\0]", 'g', "the tiger pounces on the dog"); x = gensub(/(tiger|(dog))/, "[\\0]", 'g', "the tiger pounces on the dog");