ibireme / yyjson

The fastest JSON library in C
https://ibireme.github.io/yyjson/doc/doxygen/html/
MIT License
3.13k stars 268 forks source link

A problem using iterators to traverse yyjson_mut_arr #188

Closed daipingh closed 2 hours ago

daipingh commented 3 hours ago

Describe the bug When I iterate over yyjson_mut_arr through yyjson_mut_arr_iter, the number of data actually processed is 1 less than expected.

Then I looked at the code of yyjson_mut_arr_iter_next() and found that it seemed inconsistent with the implementation of yyjson_arr_iter_next().

yyjson_arr_iter_next() returns the element before the iteration, while yyjson_mut_arr_iter_next() seems to return the element after the iteration.

yyjson_api_inline yyjson_val *yyjson_arr_iter_next(yyjson_arr_iter *iter) {
    yyjson_val *val;
    if (iter && iter->idx < iter->max) {
        val = iter->cur;
        iter->cur = unsafe_yyjson_get_next(val);
        iter->idx++;
        return val;
    }
    return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_next(
    yyjson_mut_arr_iter *iter) {
    if (iter && iter->idx < iter->max) {
        yyjson_mut_val *val = iter->cur;
        iter->pre = val;
        iter->cur = val->next;
        iter->idx++;
        return iter->cur;
    }
    return NULL;
}

I'm not sure if this is the cause of my problem.

Your environment

Additional context Add any other context about the problem here.

daipingh commented 2 hours ago

Sorry, there is no problem with yyjson. This problem is caused by other code problems. This issue will be closed