From 7007d9add05ac924795bcfac3efefd6d264c6941 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 29 Jun 2024 16:19:25 +0900 Subject: [PATCH] adding some primitive functions --- mod/arr.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++---- mod/str.c | 14 ++++++++------ 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/mod/arr.c b/mod/arr.c index c093b36..a054dfd 100644 --- a/mod/arr.c +++ b/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 } } }; /* ------------------------------------------------------------------------ */ diff --git a/mod/str.c b/mod/str.c index bfe4a40..28cc51e 100644 --- a/mod/str.c +++ b/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 } } }; /* ------------------------------------------------------------------------ */