libsheepy

C lib for handling text files, strings and json like data structure with an object oriented system
git clone https://spartatek.se/git/libsheepy.git
Log | Files | Refs | README | LICENSE

commit e3de7efbd5abd8fbbe712483e7a59c9841e0c029
parent 54aaa111222db60bab0efb5bb809e09a19759bbc
Author: Remy Noulin <loader2x@gmail.com>
Date:   Mon,  6 Jan 2020 17:43:01 +0100

fix bugs in iterator, find first non NULL element, return in iterStart when no element is found

src/json/libsheepyCSmallArray.c | 61 +++++++++++++++++++++++------
src/json/libsheepyCSmallDict.c  | 14 ++++++-
src/json/libsheepyCSmallJson.c  | 87 ++++++++++++++++++++++++++++++++---------
3 files changed, 130 insertions(+), 32 deletions(-)

Diffstat:
Msrc/json/libsheepyCSmallArray.c | 61++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Msrc/json/libsheepyCSmallDict.c | 14++++++++++++--
Msrc/json/libsheepyCSmallJson.c | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
3 files changed, 130 insertions(+), 32 deletions(-)

diff --git a/src/json/libsheepyCSmallArray.c b/src/json/libsheepyCSmallArray.c @@ -9908,21 +9908,30 @@ internal void enumerateSmallArrayF(smallArrayt *self, void *closure, enumerateEl internal baset* iterStartSmallArray(smallArrayt *self) { + // reset iterIndex to a known value + self->iterIndex = -1; if (isEmptySmallArray(self)) { // no iteration on empty arrays - self->iterIndex = -1; return(NULL); } // get first element - self->iterIndex = 0; + // find first non NULL element, deleted element can be at index 0 + range(i, self->a->count) + if (sArrayGetTiny(self->a, i)) { + self->iterIndex = i; + break; + } + if (self->iterIndex == -1) { + // no element was found in the dictionary + return(NULL); + } self->iterStep = 1; if (self->iterElementDataType != SH_DT_BASET) { // free already created base object finishO(self->iterElement); } - // TODO find first non NULL element, deleted element can be at index 0 - smallt *o = sArrayGetTiny(self->a, 0); + smallt *o = sArrayGetTiny(self->a, self->iterIndex); if ((o->type == CONTAINER) && (((sContainert*)o)->dataType == SH_DT_BASET)) { self->iterElementDataType = SH_DT_BASET; } @@ -9935,14 +9944,24 @@ internal baset* iterStartSmallArray(smallArrayt *self) { internal baset* iterStartLastSmallArray(smallArrayt *self) { + // reset iterIndex to a known value + self->iterIndex = -1; if (isEmptySmallArray(self)) { // no iteration on empty arrays - self->iterIndex = -1; return(NULL); } // get first element - self->iterIndex = (ssize_t)lenSmallArray(self) - 1; + // find first non NULL element, deleted element can be at index 0 + rangeDown(i, self->a->count) + if (sArrayGetTiny(self->a, i)) { + self->iterIndex = i; + break; + } + if (self->iterIndex == -1) { + // no element was found in the dictionary + return(NULL); + } self->iterStep = -1; if (self->iterElementDataType != SH_DT_BASET) { // free already created base object @@ -9961,9 +9980,10 @@ internal baset* iterStartLastSmallArray(smallArrayt *self) { internal baset* iterStartFromSmallArray(smallArrayt *self, intmax_t index) { + // reset iterIndex to a known value + self->iterIndex = -1; if (isEmptySmallArray(self)) { // no iteration on empty arrays - self->iterIndex = -1; return(NULL); } @@ -9978,13 +9998,22 @@ internal baset* iterStartFromSmallArray(smallArrayt *self, intmax_t index) { } // get first element - self->iterIndex = index; + // find first non NULL element, deleted element can be at index 0 + rangeFrom(i, index, self->a->count) + if (sArrayGetTiny(self->a, i)) { + self->iterIndex = i; + break; + } + if (self->iterIndex == -1) { + // no element was found in the dictionary + return(NULL); + } self->iterStep = 1; if (self->iterElementDataType != SH_DT_BASET) { // free already created base object finishO(self->iterElement); } - smallt *o = sArrayGetTiny(self->a, 0); + smallt *o = sArrayGetTiny(self->a, self->iterIndex); if ((o->type == CONTAINER) && (((sContainert*)o)->dataType == SH_DT_BASET)) { self->iterElementDataType = SH_DT_BASET; } @@ -9997,9 +10026,10 @@ internal baset* iterStartFromSmallArray(smallArrayt *self, intmax_t index) { internal baset* iterStartFromStepSmallArray(smallArrayt *self, intmax_t index, intmax_t step) { + // reset iterIndex to a known value + self->iterIndex = -1; if (isEmptySmallArray(self)) { // no iteration on empty arrays - self->iterIndex = -1; return(NULL); } @@ -10019,7 +10049,16 @@ internal baset* iterStartFromStepSmallArray(smallArrayt *self, intmax_t index, i } // get first element - self->iterIndex = index; + // find first non NULL element, deleted element can be at index 0 + rangeFromStep(i, index, self->a->count, step) + if (sArrayGetTiny(self->a, i)) { + self->iterIndex = i; + break; + } + if (self->iterIndex == -1) { + // no element was found in the dictionary + return(NULL); + } self->iterStep = step; if (self->iterElementDataType != SH_DT_BASET) { // free already created base object diff --git a/src/json/libsheepyCSmallDict.c b/src/json/libsheepyCSmallDict.c @@ -4420,9 +4420,10 @@ internal void enumerateSmallDictF(smallDictt *self, void *closure, enumerateElem internal baset* iterStartSmallDict(smallDictt *self) { + // reset iterIndex to a known value + self->iterIndex = -1; if (isEmptySmallDict(self)) { // no iteration on empty arrays - self->iterIndex = -1; return(NULL); } @@ -4432,6 +4433,10 @@ internal baset* iterStartSmallDict(smallDictt *self) { self->iterIndex = i; break; } + if (self->iterIndex == -1) { + // no element was found in the dictionary + return(NULL); + } if (self->iterElementDataType != SH_DT_BASET) { // free already created base object finishO(self->iterElement); @@ -4450,9 +4455,10 @@ internal baset* iterStartSmallDict(smallDictt *self) { internal const char* iterStartKeySmallDict(smallDictt *self) { + // reset iterIndex to a known value + self->iterIndex = -1; if (isEmptySmallDict(self)) { // no iteration on empty arrays - self->iterIndex = -1; return(NULL); } @@ -4462,6 +4468,10 @@ internal const char* iterStartKeySmallDict(smallDictt *self) { self->iterIndex = i; break; } + if (self->iterIndex == -1) { + // no element was found in the dictionary + return(NULL); + } if (self->iterElementDataType != SH_DT_BASET) { // free already created base object finishO(self->iterElement); diff --git a/src/json/libsheepyCSmallJson.c b/src/json/libsheepyCSmallJson.c @@ -18086,9 +18086,10 @@ smallJsont* zipArrayJsonSmallJson(smallJsont *self, char** array1, smallJsont *a internal baset* iterStartSmallJson(smallJsont *self) { smallt *o = NULL; + // reset iterIndex to a known value + self->iterIndex = -1; if (isEmptySmallJson(self)) { // no iteration on empty arrays - self->iterIndex = -1; return(NULL); } @@ -18100,12 +18101,16 @@ internal baset* iterStartSmallJson(smallJsont *self) { self->iterIndex = i; break; } + if (self->iterIndex == -1) { + // no element was found in the dictionary + return(NULL); + } if (self->iterElementDataType != SH_DT_BASET) { // free already created base object finishO(self->iterElement); } - self->iterKey = (&((self->top)->elements) + self->iterIndex)->key; - o = (&((self->top)->elements) + self->iterIndex)->data; + self->iterKey = (&((self->top)->elements) + self->iterIndex)->key; + o = (&((self->top)->elements) + self->iterIndex)->data; if ((o->type == CONTAINER) && (((sContainert*)o)->dataType == SH_DT_BASET)) { self->iterElementDataType = SH_DT_BASET; } @@ -18116,14 +18121,23 @@ internal baset* iterStartSmallJson(smallJsont *self) { break; case TOP_IS_ARRAY: // get first element - self->iterIndex = 0; - self->iterStep = 1; + // find first non NULL element, deleted element can be at index 0 + range(i, self->topA->count) + if (sArrayGetTiny(self->topA, i)) { + self->iterIndex = i; + break; + } + if (self->iterIndex == -1) { + // no element was found in the dictionary + return(NULL); + } + self->iterStep = 1; if (self->iterElementDataType != SH_DT_BASET) { // free already created base object finishO(self->iterElement); } // TODO find first non NULL element, deleted element can be at index 0 - o = sArrayGetTiny(self->topA, 0); + o = sArrayGetTiny(self->topA, self->iterIndex); if ((o->type == CONTAINER) && (((sContainert*)o)->dataType == SH_DT_BASET)) { self->iterElementDataType = SH_DT_BASET; } @@ -18142,9 +18156,10 @@ internal const char* iterStartKeySmallJson(smallJsont *self) { return(NULL); } + // reset iterIndex to a known value + self->iterIndex = -1; if (isEmptySmallJson(self)) { // no iteration on empty arrays - self->iterIndex = -1; return(NULL); } @@ -18154,6 +18169,10 @@ internal const char* iterStartKeySmallJson(smallJsont *self) { self->iterIndex = i; break; } + if (self->iterIndex == -1) { + // no element was found in the dictionary + return(NULL); + } if (self->iterElementDataType != SH_DT_BASET) { // free already created base object finishO(self->iterElement); @@ -18177,20 +18196,30 @@ internal baset* iterStartLastSmallJson(smallJsont *self) { return(NULL); } + // reset iterIndex to a known value + self->iterIndex = -1; if (isEmptySmallJson(self)) { // no iteration on empty arrays - self->iterIndex = -1; return(NULL); } // get first element - self->iterIndex = (ssize_t)lenSmallJson(self) - 1; - self->iterStep = -1; + // find first non NULL element, deleted element can be at index 0 + rangeDown(i, self->topA->count) + if (sArrayGetTiny(self->topA, i)) { + self->iterIndex = i; + break; + } + if (self->iterIndex == -1) { + // no element was found in the dictionary + return(NULL); + } + self->iterStep = -1; if (self->iterElementDataType != SH_DT_BASET) { // free already created base object finishO(self->iterElement); } - smallt *o = sArrayGetTiny(self->topA, self->iterIndex); + smallt *o = sArrayGetTiny(self->topA, self->iterIndex); if ((o->type == CONTAINER) && (((sContainert*)o)->dataType == SH_DT_BASET)) { self->iterElementDataType = SH_DT_BASET; } @@ -18207,9 +18236,10 @@ internal baset* iterStartFromSmallJson(smallJsont *self, intmax_t index) { return(NULL); } + // reset iterIndex to a known value + self->iterIndex = -1; if (isEmptySmallJson(self)) { // no iteration on empty arrays - self->iterIndex = -1; return(NULL); } @@ -18224,13 +18254,22 @@ internal baset* iterStartFromSmallJson(smallJsont *self, intmax_t index) { } // get first element - self->iterIndex = index; - self->iterStep = 1; + // find first non NULL element, deleted element can be at index 0 + rangeFrom(i, index, self->topA->count) + if (sArrayGetTiny(self->topA, i)) { + self->iterIndex = i; + break; + } + if (self->iterIndex == -1) { + // no element was found in the dictionary + return(NULL); + } + self->iterStep = 1; if (self->iterElementDataType != SH_DT_BASET) { // free already created base object finishO(self->iterElement); } - smallt *o = sArrayGetTiny(self->topA, 0); + smallt *o = sArrayGetTiny(self->topA, self->iterIndex); if ((o->type == CONTAINER) && (((sContainert*)o)->dataType == SH_DT_BASET)) { self->iterElementDataType = SH_DT_BASET; } @@ -18247,9 +18286,10 @@ internal baset* iterStartFromStepSmallJson(smallJsont *self, intmax_t index, int return(NULL); } + // reset iterIndex to a known value + self->iterIndex = -1; if (isEmptySmallJson(self)) { // no iteration on empty arrays - self->iterIndex = -1; return(NULL); } @@ -18269,13 +18309,22 @@ internal baset* iterStartFromStepSmallJson(smallJsont *self, intmax_t index, int } // get first element - self->iterIndex = index; - self->iterStep = step; + // find first non NULL element, deleted element can be at index 0 + rangeFromStep(i, index, self->topA->count, step) + if (sArrayGetTiny(self->topA, i)) { + self->iterIndex = i; + break; + } + if (self->iterIndex == -1) { + // no element was found in the dictionary + return(NULL); + } + self->iterStep = step; if (self->iterElementDataType != SH_DT_BASET) { // free already created base object finishO(self->iterElement); } - smallt *o = sArrayGetTiny(self->topA, self->iterIndex); + smallt *o = sArrayGetTiny(self->topA, self->iterIndex); if ((o->type == CONTAINER) && (((sContainert*)o)->dataType == SH_DT_BASET)) { self->iterElementDataType = SH_DT_BASET; }