add object_array_remove_value

This commit is contained in:
lixianjing 2022-07-29 12:03:23 +08:00
parent 5f0c217ae6
commit d119ace50b
3 changed files with 57 additions and 0 deletions

View File

@ -554,6 +554,21 @@ tk_object_t* object_array_dup(tk_object_t* obj, uint32_t start, uint32_t end) {
return dup;
}
ret_t object_array_remove_value(tk_object_t* obj, value_t* v) {
uint32_t i = 0;
object_array_t* o = OBJECT_ARRAY(obj);
return_value_if_fail(obj != NULL && v != NULL, RET_BAD_PARAMS);
for (i = 0; i < o->size; i++) {
value_t* iter = o->props + i;
if (value_equal(iter, v)) {
return object_array_remove(obj, i);
}
}
return RET_NOT_FOUND;
}
static int value_cmp_as_int(const void* a, const void* b) {
return value_int((const value_t*)a) - value_int((const value_t*)b);
}

View File

@ -191,6 +191,20 @@ int32_t object_array_last_index_of(tk_object_t* obj, const value_t* v);
*/
ret_t object_array_remove(tk_object_t* obj, uint32_t index);
/**
* @method object_array_remove_value
*
*
*
* @annotation ["scriptable"]
* @param {tk_object_t*} obj
* @param {value_t*} v
*
* @return {ret_t} RET_OK表示成功
*
*/
ret_t object_array_remove_value(tk_object_t* obj, value_t* v);
/**
* @method object_array_get_and_remove
*

View File

@ -739,3 +739,31 @@ TEST(ObjectArray, reverse) {
str_reset(&s);
TK_OBJECT_UNREF(obj);
}
TEST(ObjectArray, push_and_remove) {
value_t v;
tk_object_t* obj = object_array_create();
value_set_int(&v, 10);
object_array_push(obj, &v);
value_set_int(&v, 20);
object_array_push(obj, &v);
value_set_int(&v, 30);
object_array_push(obj, &v);
value_set_int(&v, 40);
object_array_push(obj, &v);
value_set_int(&v, 10);
ASSERT_EQ(object_array_remove_value(obj, &v), RET_OK);
ASSERT_EQ(OBJECT_ARRAY(obj)->size, 3);
ASSERT_EQ(object_array_get(obj, 0, &v), RET_OK);
ASSERT_EQ(value_int(&v), 20);
value_set_int(&v, 30);
ASSERT_EQ(object_array_remove_value(obj, &v), RET_OK);
ASSERT_EQ(OBJECT_ARRAY(obj)->size, 2);
ASSERT_EQ(object_array_get(obj, 1, &v), RET_OK);
ASSERT_EQ(value_int(&v), 40);
TK_OBJECT_UNREF(obj);
}