added hawk_arr_itr_t, hawk_val_arr_itr_t and functions for them
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-11-03 01:35:04 +09:00
parent 7640fbe805
commit ac39ef2bc4
8 changed files with 283 additions and 168 deletions

14
hawk.go
View File

@ -647,6 +647,20 @@ func (val *Val) ArrayField(index int) (*Val, error) {
return val.rtx.make_val(func() *C.hawk_val_t { return v })
}
/*
func (val *Val) ArrayFirstField() *Val {
var v *C.hawk_val_t
var itr C.hawk_val_arr_itr_t
v = C.hawk_rtx_getfirstarrvalitr(val.rtx.c, val.c, &itr)
if v == nil { return nil, val.rtx.make_errinfo() }
return val.rtx.make_val(func() *C.hawk_val_t { return v })
}
func (val *Val) ArrayNextField(itr ValArrItr) *Val {
}
*/
func (val *Val) MapField(key string) (*Val, error) {
var v *C.hawk_val_t
var uc []C.hawk_uch_t

View File

@ -193,11 +193,17 @@ return x;
if f.Type() != hawk.VAL_STR {
t.Errorf("the value at the hello field must be a string. but it was %s", f.Type().String())
} else {
var i int
var sv string
sv = hawk.Must(f.ToStr())
if sv != "hawk flieshawk flies" {
t.Errorf("the value for the hello field must be 'hawk flieshawk flies'. but it was %s", sv)
}
for i = 1; i <= sz; i++ {
fmt.Printf("%d %v\n", i, hawk.Must(v.ArrayField(i)))
}
}
}
}

View File

@ -33,7 +33,6 @@
#define keeper_t hawk_arr_keeper_t
#define walker_t hawk_arr_walker_t
#define TOB(arr,len) ((len)*(arr)->scale)
#define DPTR(slot) ((slot)->val.ptr)
#define DLEN(slot) ((slot)->val.len)
@ -482,6 +481,38 @@ void hawk_arr_clear (hawk_arr_t* arr)
arr->tally = 0;
}
hawk_ptl_t* hawk_arr_getfirstelem (hawk_arr_t* arr, hawk_arr_itr_t* itr)
{
hawk_oow_t i;
for (i = 0; i < arr->size; i++)
{
if (arr->slot[i])
{
itr->idx = i;
return &arr->slot[i]->val;
}
}
return HAWK_NULL;
}
hawk_ptl_t* hawk_arr_getnextelem (hawk_arr_t* arr, hawk_arr_itr_t* itr)
{
hawk_oow_t i;
for (i = itr->idx + 1; i < arr->size; i++)
{
if (arr->slot[i])
{
itr->idx = i;
return &arr->slot[i]->val;
}
}
return HAWK_NULL;
}
hawk_oow_t hawk_arr_walk (hawk_arr_t* arr, walker_t walker, void* ctx)
{
hawk_arr_walk_t w = HAWK_ARR_WALK_FORWARD;

View File

@ -134,7 +134,6 @@ typedef hawk_oow_t (*hawk_arr_sizer_t) (
hawk_oow_t hint /**< sizing hint */
);
typedef struct hawk_arr_style_t hawk_arr_style_t;
struct hawk_arr_style_t
@ -160,6 +159,12 @@ enum hawk_arr_style_kind_t
typedef enum hawk_arr_style_kind_t hawk_arr_style_kind_t;
struct hawk_arr_itr_t
{
hawk_oow_t idx;
};
typedef struct hawk_arr_itr_t hawk_arr_itr_t;
typedef hawk_arr_walk_t (*hawk_arr_walker_t) (
hawk_arr_t* arr /* array */,
@ -346,6 +351,23 @@ HAWK_EXPORT void hawk_arr_clear (
hawk_arr_t* arr
);
/**
* The hawk_arr_getfirstelem() function returns the pointer to the first element holder
* in an array.
*/
HAWK_EXPORT hawk_ptl_t* hawk_arr_getfirstelem (
hawk_arr_t* arr, /**< array */
hawk_arr_itr_t* itr /**< iterator*/
);
/**
* The hawk_arr_getnextelem() function returns the pointer to the next element holder.
*/
HAWK_EXPORT hawk_ptl_t* hawk_arr_getnextelem (
hawk_arr_t* arr, /**< array */
hawk_arr_itr_t* itr /**< iterator*/
);
/**
* The hawk_arr_walk() function calls the \a walker function for each
* element in the array beginning from the first. The \a walker function
@ -356,7 +378,7 @@ HAWK_EXPORT void hawk_arr_clear (
HAWK_EXPORT hawk_oow_t hawk_arr_walk (
hawk_arr_t* arr,
hawk_arr_walker_t walker,
void* ctx
void* ctx
);
/**

View File

@ -371,7 +371,6 @@ typedef hawk_map_itr_t hawk_val_map_itr_t;
*/
#define HAWK_VAL_MAP_ITR_VAL(itr) ((const hawk_val_t*)HAWK_MAP_VPTR((itr)->pair))
/**
* The hawk_val_map_data_type_t type defines the type of
* map value data for the #hawk_val_map_data_t structure.
@ -405,6 +404,16 @@ struct hawk_val_map_data_t
typedef struct hawk_val_map_data_t hawk_val_map_data_t;
/**
* The hawk_val_arr_itr_t type defines the iterator to array value fields.
*/
typedef struct hawk_val_arr_itr_t hawk_val_arr_itr_t;
struct hawk_val_arr_itr_t
{
hawk_arr_itr_t itr;
hawk_ptl_t elem;
};
/* ------------------------------------------------------------------------ */
@ -3378,6 +3387,18 @@ HAWK_EXPORT hawk_ooi_t hawk_rtx_getarrvaltally (
hawk_val_t* arr
);
HAWK_EXPORT hawk_val_arr_itr_t* hawk_rtx_getfirstarrvalitr (
hawk_rtx_t* rtx,
hawk_val_t* arr,
hawk_val_arr_itr_t* itr
);
HAWK_EXPORT hawk_val_arr_itr_t* hawk_rtx_getnextarrvalitr (
hawk_rtx_t* rtx,
hawk_val_t* arr,
hawk_val_arr_itr_t* itr
);
/**
* The hawk_rtx_makerefval() function creates a reference value.
* \return value on success, #HAWK_NULL on failure

View File

@ -361,6 +361,7 @@ static int fnc_array (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
tmp = hawk_rtx_makearrval(rtx, ((nargs > 0)? nargs: -1));
if (HAWK_UNLIKELY(!tmp)) return -1; /* hard failure */
/* 1-based. leave the first slot unassigned */
for (i = 0; i < nargs; i++)
{
if (HAWK_UNLIKELY(hawk_rtx_setarrvalfld(rtx, tmp, i + 1, hawk_rtx_getarg(rtx, i)) == HAWK_NULL))

View File

@ -516,7 +516,7 @@ int hawk_rtx_readio (hawk_rtx_t* rtx, hawk_in_type_t in_type, const hawk_ooch_t*
* for example, `hawk -v RS=separator -f script f1.txt f2.txt`
* the console handler is invoked over all the specified files.
* if `separator` is not found when the end of `f1.txt` is reached,
* `f2.txt` is opened and read. When the handler returns the
* `f2.txt` is opened and read. When the handler returns the
* first part of the second file, it must indicate this special
* condition. if it's not indicated, it goes on without breaking
* the record at the end of the first file.

346
lib/val.c

File diff suppressed because it is too large Load Diff