@@ -355,7 +355,8 @@ ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags) | |||
lua_pop (L, 1); | |||
} | |||
} | |||
else if (is_array) { | |||
if (is_array) { | |||
#if LUA_VERSION_NUM >= 502 | |||
max = lua_rawlen (L, idx); | |||
#else |
@@ -462,6 +462,13 @@ UCL_EXTERN ucl_object_t* ucl_object_pop_key (ucl_object_t *top, const char *key) | |||
UCL_EXTERN bool ucl_object_insert_key_merged (ucl_object_t *top, ucl_object_t *elt, | |||
const char *key, size_t keylen, bool copy_key); | |||
/** | |||
* Reserve space in ucl array or object for `elt` elements | |||
* @param obj object to reserve | |||
* @param reserved size to reserve in an object | |||
*/ | |||
UCL_EXTERN void ucl_object_reserve (ucl_object_t *obj, size_t reserved); | |||
/** | |||
* Append an element to the end of array object | |||
* @param top destination object (must NOT be NULL) |
@@ -395,3 +395,25 @@ ucl_hash_delete (ucl_hash_t* hashlin, const ucl_object_t *obj) | |||
} | |||
} | |||
} | |||
void ucl_hash_reserve (ucl_hash_t *hashlin, size_t sz) | |||
{ | |||
if (hashlin == NULL) { | |||
return; | |||
} | |||
if (sz > hashlin->ar.m) { | |||
kv_resize (const ucl_object_t *, hashlin->ar, sz); | |||
if (hashlin->caseless) { | |||
khash_t(ucl_hash_caseless_node) *h = (khash_t( | |||
ucl_hash_caseless_node) *) | |||
hashlin->hash; | |||
kh_resize (ucl_hash_caseless_node, h, sz); | |||
} else { | |||
khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) | |||
hashlin->hash; | |||
kh_resize (ucl_hash_node, h, sz); | |||
} | |||
} | |||
} |
@@ -90,4 +90,10 @@ const void* ucl_hash_iterate (ucl_hash_t *hashlin, ucl_hash_iter_t *iter); | |||
*/ | |||
bool ucl_hash_iter_has_next (ucl_hash_t *hashlin, ucl_hash_iter_t iter); | |||
/** | |||
* Reserves space in hash | |||
* @param hashlin | |||
*/ | |||
void ucl_hash_reserve (ucl_hash_t *hashlin, size_t sz); | |||
#endif |
@@ -2755,6 +2755,21 @@ ucl_object_new_full (ucl_type_t type, unsigned priority) | |||
return new; | |||
} | |||
void ucl_object_reserve (ucl_object_t *obj, size_t reserved) | |||
{ | |||
if (obj->type == UCL_ARRAY) { | |||
UCL_ARRAY_GET (vec, obj); | |||
if (vec->m < reserved) { | |||
/* Preallocate some space for arrays */ | |||
kv_resize (ucl_object_t *, *vec, reserved); | |||
} | |||
} | |||
else if (obj->type == UCL_OBJECT) { | |||
ucl_hash_reserve (obj->value.ov, reserved); | |||
} | |||
} | |||
ucl_object_t* | |||
ucl_object_new_userdata (ucl_userdata_dtor dtor, | |||
ucl_userdata_emitter emitter, |