adding some primitive functions
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
f4456dde2e
commit
7007d9add0
57
mod/arr.c
57
mod/arr.c
@ -118,12 +118,61 @@ static hcl_pfrc_t pf_arr_size (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
|
||||
return HCL_PF_SUCCESS;
|
||||
}
|
||||
|
||||
static hcl_pfrc_t pf_arr_slice (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
|
||||
{
|
||||
hcl_oop_t str, slice, a1, a2;
|
||||
hcl_ooi_t size;
|
||||
hcl_ooi_t pos;
|
||||
hcl_ooi_t len;
|
||||
hcl_ooi_t i;
|
||||
|
||||
str = HCL_STACK_GETARG(hcl, nargs, 0);
|
||||
a1 = HCL_STACK_GETARG(hcl, nargs, 1);
|
||||
a2 = HCL_STACK_GETARG(hcl, nargs, 2);
|
||||
|
||||
if (!HCL_IS_ARRAY(hcl, str))
|
||||
{
|
||||
hcl_seterrbfmt (hcl, HCL_EINVAL, "parameter not array - %O", str);
|
||||
return HCL_PF_FAILURE;
|
||||
}
|
||||
if (!HCL_OOP_IS_SMOOI(a1))
|
||||
{
|
||||
hcl_seterrbfmt (hcl, HCL_EINVAL, "position not numeric - %O", a1);
|
||||
return HCL_PF_FAILURE;
|
||||
}
|
||||
if (!HCL_OOP_IS_SMOOI(a2))
|
||||
{
|
||||
hcl_seterrbfmt (hcl, HCL_EINVAL, "length not string - %O", a2);
|
||||
return HCL_PF_FAILURE;
|
||||
}
|
||||
|
||||
size = HCL_OBJ_GET_SIZE(str);
|
||||
pos = HCL_OOP_TO_SMOOI(a1);
|
||||
len = HCL_OOP_TO_SMOOI(a2);
|
||||
|
||||
if (pos < 0) pos = 0;
|
||||
else if (pos >= size) pos = size;
|
||||
if (len >= size - pos) len = size - pos;
|
||||
slice = hcl_makearray(hcl, len, 0);
|
||||
if (HCL_UNLIKELY(!slice)) return HCL_PF_FAILURE;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
HCL_OBJ_GET_OOP_VAL(slice, i) = HCL_OBJ_GET_OOP_VAL(str, pos + i);
|
||||
}
|
||||
|
||||
HCL_STACK_SETRET (hcl, nargs, slice);
|
||||
return HCL_PF_SUCCESS;
|
||||
}
|
||||
|
||||
static hcl_pfinfo_t pfinfos[] =
|
||||
{
|
||||
{ { 'g','e','t','\0' }, { HCL_PFBASE_FUNC, pf_arr_get, 2, 2 } },
|
||||
{ { 'n','e','w','\0' }, { HCL_PFBASE_FUNC, pf_arr_new, 1, 1 } },
|
||||
{ { 'p','u','t','\0' }, { HCL_PFBASE_FUNC, pf_arr_put, 3, 3 } },
|
||||
{ { 's','i','z','e','\0' }, { HCL_PFBASE_FUNC, pf_arr_size, 1, 1 } },
|
||||
{ { 'g','e','t','\0' }, { HCL_PFBASE_FUNC, pf_arr_get, 2, 2 } },
|
||||
{ { 'l','e','n','g','t','h','\0' }, { HCL_PFBASE_FUNC, pf_arr_size, 1, 1 } },
|
||||
{ { 'n','e','w','\0' }, { HCL_PFBASE_FUNC, pf_arr_new, 1, 1 } },
|
||||
{ { 'p','u','t','\0' }, { HCL_PFBASE_FUNC, pf_arr_put, 3, 3 } },
|
||||
{ { 's','i','z','e','\0' }, { HCL_PFBASE_FUNC, pf_arr_size, 1, 1 } },
|
||||
{ { 's','l','i','c','e','\0' }, { HCL_PFBASE_FUNC, pf_arr_slice, 3, 3 } }
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
14
mod/str.c
14
mod/str.c
@ -122,9 +122,9 @@ static hcl_pfrc_t pf_str_length (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
|
||||
return HCL_PF_SUCCESS;
|
||||
}
|
||||
|
||||
static hcl_pfrc_t pf_str_substr (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
|
||||
static hcl_pfrc_t pf_str_slice (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
|
||||
{
|
||||
hcl_oop_t str, substr, a1, a2;
|
||||
hcl_oop_t str, slice, a1, a2;
|
||||
hcl_ooi_t size;
|
||||
hcl_ooi_t pos;
|
||||
hcl_ooi_t len;
|
||||
@ -156,10 +156,10 @@ static hcl_pfrc_t pf_str_substr (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs)
|
||||
if (pos < 0) pos = 0;
|
||||
else if (pos >= size) pos = size;
|
||||
if (len >= size - pos) len = size - pos;
|
||||
substr = hcl_makestring(hcl, HCL_OBJ_GET_CHAR_PTR(str, pos), len, 0);
|
||||
if (HCL_UNLIKELY(!substr)) return HCL_PF_FAILURE;
|
||||
slice = hcl_makestring(hcl, HCL_OBJ_GET_CHAR_PTR(str, pos), len, 0);
|
||||
if (HCL_UNLIKELY(!slice)) return HCL_PF_FAILURE;
|
||||
|
||||
HCL_STACK_SETRET (hcl, nargs, substr);
|
||||
HCL_STACK_SETRET (hcl, nargs, slice);
|
||||
return HCL_PF_SUCCESS;
|
||||
}
|
||||
|
||||
@ -169,7 +169,9 @@ static hcl_pfinfo_t pfinfos[] =
|
||||
{ { 'a','t','\0' }, { HCL_PFBASE_FUNC, pf_str_at, 2, 2 } },
|
||||
{ { 'a','t','P','u','t','\0' }, { HCL_PFBASE_FUNC, pf_str_at_put, 3, 3 } },
|
||||
{ { 'l','e','n','g','t','h','\0' }, { HCL_PFBASE_FUNC, pf_str_length, 1, 1 } },
|
||||
{ { 's','u','b','s','t','r'}, { HCL_PFBASE_FUNC, pf_str_substr, 3, 3 } }
|
||||
{ { 's','i','z','e'}, { HCL_PFBASE_FUNC, pf_str_length, 1, 1 } },
|
||||
{ { 's','l','i','c','e'}, { HCL_PFBASE_FUNC, pf_str_slice, 3, 3 } },
|
||||
{ { 's','u','b','s','t','r'}, { HCL_PFBASE_FUNC, pf_str_slice, 3, 3 } }
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
Loading…
Reference in New Issue
Block a user