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 39ea3271b3817a6810a0df6ee2f6ab94e0895601
parent 44f76476197dca89f351126b24b8d2419542ab13
Author: Remy Noulin <loader2x@gmail.com>
Date:   Sun, 23 May 2021 14:05:45 +0200

Fix bugs found with the libhsheepy tests

release/json/libsheepyCSmallArray.h         |   16 +-
release/json/libsheepyCSmallJson.h          |   36 +-
release/json/libsheepyCSmallStringCuTest.c  |    3 +
release/json/libsheepyCSmallStringTest.c    |    3 +
release/json/libsheepyCSmallStringTestMem.c |    3 +
release/libsheepy.c                         |   15 +-
release/libsheepy.h                         |    4 +-
release/libsheepyObject.h                   |   36 +-
src/json/libsheepyCSmallArray.c             | 1010 ++++++---
src/json/libsheepyCSmallArray.h             |   16 +-
src/json/libsheepyCSmallBool.c              |    1 +
src/json/libsheepyCSmallBytes.c             |   15 +-
src/json/libsheepyCSmallDict.c              |  339 ++--
src/json/libsheepyCSmallDouble.c            |    6 +-
src/json/libsheepyCSmallInt.c               |    2 +-
src/json/libsheepyCSmallJson.c              | 2931 ++++++++++++++++++++-------
src/json/libsheepyCSmallJson.h              |   36 +-
src/json/libsheepyCSmallJsonInternal.h      |   30 +-
src/json/libsheepyCSmallString.c            |  860 +++++++-
src/json/libsheepyObject.c                  |  161 +-
src/json/libsheepyObjectCuTest.c            |    1 +
src/json/libsheepyObjectTest.c              |    1 +
src/libsheepy.c                             |   15 +-
src/libsheepy.h                             |    4 +-
src/libsheepyCuTest.c                       |    4 +
src/libsheepyObject.h                       |   36 +-
src/libsheepyTest.c                         |    4 +
27 files changed, 4224 insertions(+), 1364 deletions(-)

Diffstat:
Mrelease/json/libsheepyCSmallArray.h | 16+++++++++++++++-
Mrelease/json/libsheepyCSmallJson.h | 36+++++++++++++++++++++++++-----------
Mrelease/json/libsheepyCSmallStringCuTest.c | 3+++
Mrelease/json/libsheepyCSmallStringTest.c | 3+++
Mrelease/json/libsheepyCSmallStringTestMem.c | 3+++
Mrelease/libsheepy.c | 15+++++++++------
Mrelease/libsheepy.h | 4++--
Mrelease/libsheepyObject.h | 36+++++++++++++++++++++++++-----------
Msrc/json/libsheepyCSmallArray.c | 1010+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
Msrc/json/libsheepyCSmallArray.h | 16+++++++++++++++-
Msrc/json/libsheepyCSmallBool.c | 1+
Msrc/json/libsheepyCSmallBytes.c | 15++++++++++++++-
Msrc/json/libsheepyCSmallDict.c | 339++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
Msrc/json/libsheepyCSmallDouble.c | 6+++---
Msrc/json/libsheepyCSmallInt.c | 2+-
Msrc/json/libsheepyCSmallJson.c | 2931++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
Msrc/json/libsheepyCSmallJson.h | 36+++++++++++++++++++++++++-----------
Msrc/json/libsheepyCSmallJsonInternal.h | 30++++++++++++++++++++++++------
Msrc/json/libsheepyCSmallString.c | 860++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Msrc/json/libsheepyObject.c | 161+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
Msrc/json/libsheepyObjectCuTest.c | 1+
Msrc/json/libsheepyObjectTest.c | 1+
Msrc/libsheepy.c | 15+++++++++------
Msrc/libsheepy.h | 4++--
Msrc/libsheepyCuTest.c | 4++++
Msrc/libsheepyObject.h | 36+++++++++++++++++++++++++-----------
Msrc/libsheepyTest.c | 4++++
27 files changed, 4224 insertions(+), 1364 deletions(-)

diff --git a/release/json/libsheepyCSmallArray.h b/release/json/libsheepyCSmallArray.h @@ -1228,10 +1228,17 @@ typedef smallArrayt* (*zipSmallJsonSmallArrayFt) (smallArrayt *self, s typedef smallArrayt* (*zipSmallJsonSmallArraySmallArrayFt)(smallArrayt *self, smallJsont *array1, smallArrayt *array2); typedef smallArrayt* (*zipSmallJsonSmallJsonSmallArrayFt) (smallArrayt *self, smallJsont *array1, smallJsont *array2); typedef smallArrayt* (*zipSmallJsonCharSmallArrayFt) (smallArrayt *self, smallJsont *array1, char **array2); +typedef smallArrayt* (*zipSmallJsonCCharSmallArrayFt) (smallArrayt *self, smallJsont *array1, const char **array2); typedef smallArrayt* (*zipArraySmallArrayFt) (smallArrayt *self, char** array1, smallArrayt *array2); +typedef smallArrayt* (*zipCArraySmallArrayFt) (smallArrayt *self, const char** array1, smallArrayt *array2); typedef smallArrayt* (*zipArraySmallJsonSmallArrayFt) (smallArrayt *self, char** array1, smallJsont *array2); +typedef smallArrayt* (*zipCArraySmallJsonSmallArrayFt) (smallArrayt *self, const char** array1, smallJsont *array2); typedef smallArrayt* (*zipCharSmallArrayFt) (smallArrayt *self, smallArrayt *array1, char** array2); +typedef smallArrayt* (*zipCCharSmallArrayFt) (smallArrayt *self, smallArrayt *array1, const char** array2); typedef smallArrayt* (*zipArrayCharSmallArrayFt) (smallArrayt *self, char** array1, char** array2); +typedef smallArrayt* (*zipCArrayCharSmallArrayFt) (smallArrayt *self, const char** array1, char** array2); +typedef smallArrayt* (*zipArrayCCharSmallArrayFt) (smallArrayt *self, char** array1, const char** array2); +typedef smallArrayt* (*zipCArrayCCharSmallArrayFt) (smallArrayt *self, const char** array1, const char** array2); /** * print list elements, one line per element @@ -1782,10 +1789,17 @@ typedef bool (*areAllEBytesSmallArrayFt) (smallArrayt *self); zipSmallJsonSmallArraySmallArrayFt zipSmallJsonSmallArray;\ zipSmallJsonSmallJsonSmallArrayFt zipSmallJsonSmallJson;\ zipSmallJsonCharSmallArrayFt zipSmallJsonChar;\ + zipSmallJsonCCharSmallArrayFt zipSmallJsonCChar;\ zipArraySmallArrayFt zipArray;\ + zipCArraySmallArrayFt zipCArray;\ zipArraySmallJsonSmallArrayFt zipArraySmallJson;\ + zipCArraySmallJsonSmallArrayFt zipCArraySmallJson;\ zipCharSmallArrayFt zipChar;\ + zipCCharSmallArrayFt zipCChar;\ zipArrayCharSmallArrayFt zipArrayChar;\ + zipCArrayCharSmallArrayFt zipCArrayChar;\ + zipArrayCCharSmallArrayFt zipArrayCChar;\ + zipCArrayCCharSmallArrayFt zipCArrayCChar;\ logSmallArrayFt log;\ readTextSmallArrayFt readText;\ readTextSmallJsonSmallArrayFt readTextSmallJson;\ @@ -2215,7 +2229,7 @@ smallArrayt* injectSmallJsonSmallArrayG (smallArrayt *self, intmax_t index, smallArrayt* injectSmallStringSmallArrayG (smallArrayt *self, intmax_t index, smallStringt *string); smallArrayt* injectSmallContainerSmallArrayG(smallArrayt *self, intmax_t index, smallContainert *container); smallArrayt* injectNFreeSmallArrayG (smallArrayt *self, intmax_t index, baset *value); -smallArrayt* injectNFreeUndefinedSmallArrayG(smallArrayt *self, intmax_t index, void *value); +smallArrayt* injectNFreeUndefinedSmallArrayG(smallArrayt *self, intmax_t index, undefinedt *value); smallArrayt* injectNFreeSSmallArrayG (smallArrayt *self, intmax_t index, char *string); smallArrayt* injectNFreeDictSmallArrayG (smallArrayt *self, intmax_t index, smallDictt *dict); smallArrayt* injectNFreeArraySmallArrayG (smallArrayt *self, intmax_t index, smallArrayt *array); diff --git a/release/json/libsheepyCSmallJson.h b/release/json/libsheepyCSmallJson.h @@ -1064,7 +1064,7 @@ typedef smallJsont* (*appendNSmashArraySmallJsonFt)(smallJsont *self, c typedef smallJsont* (*shiftSmallJsonFt) (smallJsont *self, smallArrayt *array); typedef smallJsont* (*shiftNSmashSmallJsonFt) (smallJsont *self, smallArrayt *array); typedef smallJsont* (*shiftSmallJsonSmallJsonFt) (smallJsont *self, smallJsont *array); -typedef smallJsont* (*shiftSmallJsonNSmashSmallJsonFt)(smallJsont *self, smallJsont *array); +typedef smallJsont* (*shiftNSmashSmallJsonSmallJsonFt)(smallJsont *self, smallJsont *array); /** * add self and list in a new list @@ -1222,8 +1222,8 @@ typedef smallJsont* (*copySmallJsonFt) (smallJsont *self, intmax_t star */ typedef smallJsont* (*insertSmallJsonFt) (smallJsont *self, intmax_t index, smallArrayt *toInsert); typedef smallJsont* (*insertNSmashSmallJsonFt) (smallJsont *self, intmax_t index, smallArrayt *toInsert); -typedef smallJsont* (*insertJsonSmallJsonFt) (smallJsont *self, intmax_t index, smallJsont *toInsert); -typedef smallJsont* (*insertJsonNSmashSmallJsonFt)(smallJsont *self, intmax_t index, smallJsont *toInsert); +typedef smallJsont* (*insertSmallJsonSmallJsonFt) (smallJsont *self, intmax_t index, smallJsont *toInsert); +typedef smallJsont* (*insertNSmashSmallJsonSmallJsonFt)(smallJsont *self, intmax_t index, smallJsont *toInsert); /** * insert smallString in self at index @@ -2254,13 +2254,20 @@ typedef char* (*colordSmallJsonFt) (smallJsont *self, const char *color); */ typedef smallJsont* (*zipSmallJsonFt) (smallJsont *self, smallArrayt *array1, smallArrayt *array2); typedef smallJsont* (*zipArraySmallJsonFt) (smallJsont *self, char** array1, smallArrayt *array2); +typedef smallJsont* (*zipCArraySmallJsonFt) (smallJsont *self, const char** array1, smallArrayt *array2); typedef smallJsont* (*zipCharSmallJsonFt) (smallJsont *self, smallArrayt *array1, char** array2); +typedef smallJsont* (*zipCCharSmallJsonFt) (smallJsont *self, smallArrayt *array1, const char** array2); typedef smallJsont* (*zipArrayCharSmallJsonFt) (smallJsont *self, char** array1, char** array2); +typedef smallJsont* (*zipCArrayCharSmallJsonFt) (smallJsont *self, const char** array1, char** array2); +typedef smallJsont* (*zipArrayCCharSmallJsonFt) (smallJsont *self, char** array1, const char** array2); +typedef smallJsont* (*zipCArrayCCharSmallJsonFt)(smallJsont *self, const char** array1, const char** array2); typedef smallJsont* (*zipJsonSmallJsonFt) (smallJsont *self, smallJsont *array1, smallJsont *array2); typedef smallJsont* (*zipJsonSmallArraySmallJsonFt)(smallJsont *self, smallJsont *array1, smallArrayt *array2); typedef smallJsont* (*zipJsonArraySmallJsonFt) (smallJsont *self, smallJsont *array1, char** array2); +typedef smallJsont* (*zipJsonCArraySmallJsonFt) (smallJsont *self, smallJsont *array1, const char** array2); typedef smallJsont* (*zipSmallArrayJsonSmallJsonFt)(smallJsont *self, smallArrayt *array1, smallJsont *array2); typedef smallJsont* (*zipArrayJsonSmallJsonFt) (smallJsont *self, char** array1, smallJsont *array2); +typedef smallJsont* (*zipCArrayJsonSmallJsonFt) (smallJsont *self, const char** array1, smallJsont *array2); /** * iterator @@ -2950,7 +2957,7 @@ typedef bool (*areAllEBytesSmallJsonFt) (smallJsont *self); shiftSmallJsonFt shift;\ shiftNSmashSmallJsonFt shiftNSmash;\ shiftSmallJsonSmallJsonFt shiftSmallJson;\ - shiftSmallJsonNSmashSmallJsonFt shiftSmallJsonNSmash;\ + shiftNSmashSmallJsonSmallJsonFt shiftNSmashSmallJson;\ addSmallJsonFt add;\ addJsonSmallJsonFt addJson;\ sliceSmallJsonFt slice;\ @@ -2999,8 +3006,8 @@ typedef bool (*areAllEBytesSmallJsonFt) (smallJsont *self); copySmallJsonFt copy;\ insertSmallJsonFt insert;\ insertNSmashSmallJsonFt insertNSmash;\ - insertJsonSmallJsonFt insertJson;\ - insertJsonNSmashSmallJsonFt insertJsonNSmash;\ + insertSmallJsonSmallJsonFt insertSmallJson;\ + insertNSmashSmallJsonSmallJsonFt insertNSmashSmallJson;\ insertStringSmallJsonFt insertString;\ insertSSmallJsonFt insertS;\ insertNFreeStringSmallJsonFt insertNFreeString;\ @@ -3316,13 +3323,20 @@ typedef bool (*areAllEBytesSmallJsonFt) (smallJsont *self); colordSmallJsonFt colord;\ zipSmallJsonFt zip;\ zipArraySmallJsonFt zipArray;\ + zipCArraySmallJsonFt zipCArray;\ zipCharSmallJsonFt zipChar;\ + zipCCharSmallJsonFt zipCChar;\ zipArrayCharSmallJsonFt zipArrayChar;\ + zipCArrayCharSmallJsonFt zipCArrayChar;\ + zipArrayCCharSmallJsonFt zipArrayCChar;\ + zipCArrayCCharSmallJsonFt zipCArrayCChar;\ zipJsonSmallJsonFt zipJson;\ zipJsonSmallArraySmallJsonFt zipJsonSmallArray;\ zipJsonArraySmallJsonFt zipJsonArray;\ + zipJsonCArraySmallJsonFt zipJsonCArray;\ zipSmallArrayJsonSmallJsonFt zipSmallArrayJson;\ zipArrayJsonSmallJsonFt zipArrayJson;\ + zipCArrayJsonSmallJsonFt zipCArrayJson;\ iterStartSmallJsonFt iterStart;\ iterStartKeySmallJsonFt iterStartKey;\ iterStartLastSmallJsonFt iterStartLast;\ @@ -3917,7 +3931,7 @@ smallJsont* appendCArraySmallJsonG (smallJsont *self, const char **array); smallJsont* shiftSmallJsonG (smallJsont *self, smallArrayt *array); smallJsont* shiftNSmashSmallJsonG (smallJsont *self, smallArrayt *array); smallJsont* shiftSmallJsonSmallJsonG (smallJsont *self, smallJsont *array); -smallJsont* shiftSmallJsonNSmashSmallJsonG(smallJsont *self, smallJsont *array); +smallJsont* shiftNSmashSmallJsonSmallJsonG(smallJsont *self, smallJsont *array); smallJsont* addSmallJsonG (smallJsont *self, smallArrayt *array); smallJsont* addJsonSmallJsonG(smallJsont *self, smallJsont *array); @@ -3967,12 +3981,12 @@ smallContainert* cropElemKeySmallContainerSmallJsonG(smallJsont *self, const cha smallJsont* copySmallJsonG (smallJsont *self, intmax_t start, intmax_t end); smallJsont* insertSmallJsonG(smallJsont *self, intmax_t index, smallArrayt *toInsert); smallJsont* insertNSmashSmallJsonG(smallJsont *self, intmax_t index, smallArrayt *toInsert); -smallJsont* insertJsonSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert); -smallJsont* insertJsonNSmashSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert); +smallJsont* insertSmallJsonSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert); +smallJsont* insertNSmashSmallJsonSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert); smallJsont* insertStringSmallJsonG (smallJsont *self, intmax_t index, smallStringt *toInsert); smallJsont* insertSSmallJsonG (smallJsont *self, intmax_t index, const char *toInsert); -smallJsont* insertNFreeStringSmallJsonG(smallJsont *self, intmax_t index, smallStringt *toInsert); -smallJsont* insertSNFreeSmallJsonG (smallJsont *self, intmax_t index, char *toInsert); +smallJsont* insertNFreeStringSmallJsonG(smallJsont *self, intmax_t index, smallStringt *toInsert); // not used in generics +smallJsont* insertNFreeSSmallJsonG (smallJsont *self, intmax_t index, char *toInsert); // not used in generics smallJsont* injectSmallJsonG (smallJsont *self, intmax_t index, baset *value); smallJsont* injectUndefinedSmallJsonG(smallJsont *self, intmax_t index, void *value); diff --git a/release/json/libsheepyCSmallStringCuTest.c b/release/json/libsheepyCSmallStringCuTest.c @@ -11808,6 +11808,7 @@ void readFileSmallStringT(CuTest *tc UNUSED) { free(s); fileChmod("../writeOnlyText.null", S_IWUSR | S_IWGRP | S_IWOTH); ck_assert_ptr_eq(readFileO(self, "../writeOnlyText.null"), NULL); + fileChmod("../writeOnlyText.null", S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); // blank path ck_assert_ptr_eq(readFileO(self, ""), NULL); // NULL path @@ -11846,6 +11847,7 @@ void readFileSmallJsonSmallStringT(CuTest *tc UNUSED) { fileChmod("../writeOnlyText.null", S_IWUSR | S_IWGRP | S_IWOTH); setTopSO(filePath, "../writeOnlyText.null"); ck_assert_ptr_eq(self->f->readFileSmallJson(self, filePath), NULL); + fileChmod("../writeOnlyText.null", S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); // blank path freeO(filePath); setTopSO(filePath, ""); @@ -11895,6 +11897,7 @@ void readFileSmallStringSmallStringT(CuTest *tc UNUSED) { fileChmod("../writeOnlyText.null", S_IWUSR | S_IWGRP | S_IWOTH); setValO(filePath, "../writeOnlyText.null"); ck_assert_ptr_eq(self->f->readFileSmallString(self, filePath), NULL); + fileChmod("../writeOnlyText.null", S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); // blank path setValO(filePath, ""); ck_assert_ptr_eq(self->f->readFileSmallString(self, filePath), NULL); diff --git a/release/json/libsheepyCSmallStringTest.c b/release/json/libsheepyCSmallStringTest.c @@ -11797,6 +11797,7 @@ START_TEST(readFileSmallStringT) free(s); fileChmod("../writeOnlyText.null", S_IWUSR | S_IWGRP | S_IWOTH); ck_assert_ptr_eq(readFileO(self, "../writeOnlyText.null"), NULL); + fileChmod("../writeOnlyText.null", S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); // blank path ck_assert_ptr_eq(readFileO(self, ""), NULL); // NULL path @@ -11835,6 +11836,7 @@ START_TEST(readFileSmallJsonSmallStringT) fileChmod("../writeOnlyText.null", S_IWUSR | S_IWGRP | S_IWOTH); setTopSO(filePath, "../writeOnlyText.null"); ck_assert_ptr_eq(self->f->readFileSmallJson(self, filePath), NULL); + fileChmod("../writeOnlyText.null", S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); // blank path freeO(filePath); setTopSO(filePath, ""); @@ -11884,6 +11886,7 @@ START_TEST(readFileSmallStringSmallStringT) fileChmod("../writeOnlyText.null", S_IWUSR | S_IWGRP | S_IWOTH); setValO(filePath, "../writeOnlyText.null"); ck_assert_ptr_eq(self->f->readFileSmallString(self, filePath), NULL); + fileChmod("../writeOnlyText.null", S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); // blank path setValO(filePath, ""); ck_assert_ptr_eq(self->f->readFileSmallString(self, filePath), NULL); diff --git a/release/json/libsheepyCSmallStringTestMem.c b/release/json/libsheepyCSmallStringTestMem.c @@ -11806,6 +11806,7 @@ void readFileSmallStringT(void) { free(s); fileChmod("../writeOnlyText.null", S_IWUSR | S_IWGRP | S_IWOTH); ck_assert_ptr_eq(readFileO(self, "../writeOnlyText.null"), NULL); + fileChmod("../writeOnlyText.null", S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); // blank path ck_assert_ptr_eq(readFileO(self, ""), NULL); // NULL path @@ -11844,6 +11845,7 @@ void readFileSmallJsonSmallStringT(void) { fileChmod("../writeOnlyText.null", S_IWUSR | S_IWGRP | S_IWOTH); setTopSO(filePath, "../writeOnlyText.null"); ck_assert_ptr_eq(self->f->readFileSmallJson(self, filePath), NULL); + fileChmod("../writeOnlyText.null", S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); // blank path freeO(filePath); setTopSO(filePath, ""); @@ -11893,6 +11895,7 @@ void readFileSmallStringSmallStringT(void) { fileChmod("../writeOnlyText.null", S_IWUSR | S_IWGRP | S_IWOTH); setValO(filePath, "../writeOnlyText.null"); ck_assert_ptr_eq(self->f->readFileSmallString(self, filePath), NULL); + fileChmod("../writeOnlyText.null", S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); // blank path setValO(filePath, ""); ck_assert_ptr_eq(self->f->readFileSmallString(self, filePath), NULL); diff --git a/release/libsheepy.c b/release/libsheepy.c @@ -233,7 +233,7 @@ char *randomAlphaNumS(uint64_t length); char *bRandomAlphaNumS(char *dst, size_t dstSize); char *readS(void); char *bLReadS(char *dst, size_t dstSize); -local char *shGetpass(void); +UNUSED local char *shGetpass(void); char *readPasswordS(void); bool zeroS(char *string); bool zeroBuf(void *buf, size_t len); @@ -6194,7 +6194,7 @@ char *bLReadS(char *dst, size_t dstSize) { * * currently used in Termux because getpass is missging */ -local char *shGetpass(void) { +UNUSED local char *shGetpass(void) { char *r = NULL; #ifndef unitTest @@ -9134,7 +9134,7 @@ bool endsWithCharS(const char *string1, char c) { ssize_t countS(const char *s, const char *needle) { // sanity checks - if (!s || !needle) { + if (!s || !needle || !needle[0]) { return(-1); } @@ -9350,7 +9350,7 @@ bool icEndsWithCharS(const char *string1, char c) { ssize_t icCountS(const char *s, const char *needle) { // sanity checks - if (!s || !needle) { + if (!s || !needle || !needle[0]) { return(-1); } @@ -13242,7 +13242,7 @@ char cropElemS(char *string, intmax_t index) { string[len-1] = 0; } else { - strLCpy(string+index, len, string+index+1); + strLCpy(string+index, len - index, string+index+1); } return(r); @@ -46396,9 +46396,11 @@ internal size_t runeFullyDecompose(rune ch, rune *result, size_t resultLen) { } internal int output_marks(const char **p_inout, char *dst, bool remove_dot) { - const char *p = *p_inout = NULL;; + const char *p = NULL; int len = 0;; + p = *p_inout; + while (*p) { rune c = code2RuneUTF8(p);; @@ -52553,6 +52555,7 @@ int systemf(const char *fmt, ...) { } va_start(pl, fmt); if (vasprintf(&cmd, fmt, pl) == -1) { + va_end(pl); return(-1); } va_end(pl); diff --git a/release/libsheepy.h b/release/libsheepy.h @@ -2872,7 +2872,7 @@ int listPrintS(char **list); */ #define forEachS(list, element) \ ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ - for (char *element = list[0]; list[UNIQVAR(libsheepyInternalIndex)]!= NULL ; UNIQVAR(libsheepyInternalIndex)++, element = list[UNIQVAR(libsheepyInternalIndex)]) + for (char *element = (list)[0]; (list)[UNIQVAR(libsheepyInternalIndex)]!= NULL ; UNIQVAR(libsheepyInternalIndex)++, element = (list)[UNIQVAR(libsheepyInternalIndex)]) /** * forEach - loop macro on list indexes @@ -2898,7 +2898,7 @@ int listPrintS(char **list); */ #define enumerateS(list, element, index) \ ;size_t index = 0; \ - for (char *element=list[0]; element != NULL ; index++, element = list[index]) + for (char *element=(list)[0]; element != NULL ; index++, element = (list)[index]) /** * enumerateCharP list diff --git a/release/libsheepyObject.h b/release/libsheepyObject.h @@ -2838,7 +2838,7 @@ void finishManyOF(void *paramType, ...); smallJsont*: _Generic(obj, \ smallArrayt *: shiftNSmashSmallJsonG, \ smallDictt *: mergeDictNSmashSmallJsonG, \ - smallJsont *: shiftSmallJsonNSmashSmallJsonG, \ + smallJsont *: shiftNSmashSmallJsonSmallJsonG, \ default: mergeNSmashSmallJsonG), \ smallStringt*: _Generic((obj), \ smallStringt*: prependNSmashSmallStringG, \ @@ -5172,7 +5172,7 @@ void finishManyOF(void *paramType, ...); smallStringt *: rTrimSmallStringG \ )(self) -#define uniqO(self) (self)->f->uniq(self) +#define uniqO(self, c) (self)->f->uniq(self, c) #define uniqG(self, c) _Generic((self), \ char *: uniqS, \ const char *: uniqS, \ @@ -5183,7 +5183,7 @@ void finishManyOF(void *paramType, ...); smallStringt *: uniqSmallStringG \ )(self, c) -#define icUniqO(self) (self)->f->icUniq(self) +#define icUniqO(self, c) (self)->f->icUniq(self, c) #define icUniqG(self, c) _Generic((self), \ char *: icUniqS, \ const char *: icUniqS, \ @@ -5591,7 +5591,7 @@ void finishManyOF(void *paramType, ...); smallBytest*: injectSmallBytesSmallJsonG, \ smallDoublet*: injectSmallDoubleSmallJsonG, \ smallIntt*: injectSmallIntSmallJsonG, \ - smallJsont*: insertJsonSmallJsonG, \ + smallJsont*: insertSmallJsonSmallJsonG, \ smallStringt*: injectSmallStringSmallJsonG, \ smallContainert*: injectSmallContainerSmallJsonG, \ undefinedt*: injectUndefinedSmallJsonG, \ @@ -5677,7 +5677,7 @@ void finishManyOF(void *paramType, ...); smallBytest*: injectNFreeSmallBytesSmallJsonG, \ smallDoublet*: injectNFreeSmallDoubleSmallJsonG, \ smallIntt*: injectNFreeSmallIntSmallJsonG, \ - smallJsont*: insertJsonNSmashSmallJsonG, \ + smallJsont*: insertNSmashSmallJsonSmallJsonG, \ smallStringt*: injectNFreeSmallStringSmallJsonG, \ smallContainert*: injectNFreeSmallContainerSmallJsonG, \ undefinedt*: injectNFreeUndefinedSmallJsonG, \ @@ -7403,6 +7403,8 @@ void finishManyOF(void *paramType, ...); #define pushBufferG pushBufferO +#define setTopSO setTopStringO +#define setTopSG setTopStringO // generated with utils/oGMacros @@ -7970,15 +7972,33 @@ void finishManyOF(void *paramType, ...); #define zipSmallJsonCharO(self, array1, array2) (self)->f->zipSmallJsonChar(self, array1, array2) #define zipSmallJsonCharG zipSmallJsonCharO +#define zipSmallJsonCCharO(self, array1, array2) (self)->f->zipSmallJsonCChar(self, array1, array2) +#define zipSmallJsonCCharG zipSmallJsonCCharO + #define zipArrayO(self, array1, array2) (self)->f->zipArray(self, array1, array2) #define zipArrayG zipArrayO +#define zipCArrayO(self, a1, array2) (self)->f->zipCArray(self, a1, array2) +#define zipCArrayG zipCArrayO + #define zipCharO(self, array1, array2) (self)->f->zipChar(self, array1, array2) #define zipCharG zipCharO +#define zipCCharO(self, array1, a2) (self)->f->zipCChar(self, array1, a2) +#define zipCCharG zipCCharO + #define zipArrayCharO(self, array1, array2) (self)->f->zipArrayChar(self, array1, array2) #define zipArrayCharG zipArrayCharO +#define zipCArrayCharO(self, a1, array2) (self)->f->zipCArrayChar(self, a1, array2) +#define zipCArrayCharG zipCArrayCharO + +#define zipArrayCCharO(self, array1, a2) (self)->f->zipArrayCChar(self, array1, a2) +#define zipArrayCCharG zipArrayCCharO + +#define zipCArrayCCharO(self, a1, a2) (self)->f->zipCArrayCChar(self, a1, a2) +#define zipCArrayCCharG zipCArrayCCharO + #define readTextSmallStringO(self, filePath) (self)->f->readTextSmallString(self, filePath) #define readTextSmallStringG readTextSmallStringO @@ -8840,12 +8860,6 @@ void finishManyOF(void *paramType, ...); #define cropElemKeySmallContainerO(self, key) (self)->f->cropElemKeySmallContainer(self, key) #define cropElemKeySmallContainerG cropElemKeySmallContainerO -#define insertJsonO(self, index, toInsert) (self)->f->insertJson(self, index, toInsert) -#define insertJsonG insertJsonO - -#define insertJsonNSmashO(self, index, toInsert) (self)->f->insertJsonNSmash(self, index, toInsert) -#define insertJsonNSmashG insertJsonNSmashO - #define insertStringO(self, index, toInsert) (self)->f->insertString(self, index, toInsert) #define insertStringG insertStringO diff --git a/src/json/libsheepyCSmallArray.c b/src/json/libsheepyCSmallArray.c @@ -386,7 +386,7 @@ internal ssize_t indexOfSmallJsonSmallArray(smallArrayt *self, smallJsont *strin internal ssize_t indexOfSmallStringSmallArray(smallArrayt *self, smallStringt *string); internal ssize_t indexOfSmallContainerSmallArray(smallArrayt *self UNUSED, smallContainert *container UNUSED); internal ssize_t binarySearchSmallArray(smallArrayt *self, baset *value); -internal ssize_t binarySearchUndefinedSmallArray(smallArrayt *self, undefinedt *undefined); +internal ssize_t binarySearchUndefinedSmallArray(smallArrayt *self UNUSED, undefinedt *undefined UNUSED); internal ssize_t binarySearchBoolSmallArray(smallArrayt *self, bool value); internal ssize_t binarySearchDoubleSmallArray(smallArrayt *self, double value); internal ssize_t binarySearchIntSmallArray(smallArrayt *self, int64_t value); @@ -455,10 +455,17 @@ internal smallArrayt* zipSmallJsonSmallArray(smallArrayt *self, smallArrayt *ar internal smallArrayt* zipSmallJsonSmallArraySmallArray(smallArrayt *self, smallJsont *array1, smallArrayt *array2); internal smallArrayt* zipSmallJsonSmallJsonSmallArray(smallArrayt *self, smallJsont *array1, smallJsont *array2); internal smallArrayt* zipSmallJsonCharSmallArray(smallArrayt *self, smallJsont *array1, char **array2); +internal smallArrayt* zipSmallJsonCCharSmallArray(smallArrayt *self, smallJsont *array1, const char **array2); internal smallArrayt* zipArraySmallArray(smallArrayt *self, char** array1, smallArrayt *array2); +internal smallArrayt* zipCArraySmallArray(smallArrayt *self, const char** a1, smallArrayt *array2); internal smallArrayt* zipArraySmallJsonSmallArray(smallArrayt *self, char** array1, smallJsont *array2); +internal smallArrayt* zipCArraySmallJsonSmallArray(smallArrayt *self, const char** array1, smallJsont *array2); internal smallArrayt* zipCharSmallArray(smallArrayt *self, smallArrayt *array1, char** array2); +internal smallArrayt* zipCCharSmallArray(smallArrayt *self, smallArrayt *array1, const char** a2); internal smallArrayt* zipArrayCharSmallArray(smallArrayt *self, char** array1, char** array2); +internal smallArrayt* zipCArrayCharSmallArray(smallArrayt *self, const char** a1, char** array2); +internal smallArrayt* zipArrayCCharSmallArray(smallArrayt *self, char** array1, const char** a2); +internal smallArrayt* zipCArrayCCharSmallArray(smallArrayt *self, const char** a1, const char** a2); internal void logSmallArray(smallArrayt *self); internal smallArrayt* readTextSmallArray(smallArrayt *self, const char *filePath); internal smallArrayt* readTextSmallJsonSmallArray(smallArrayt *self, smallJsont *filePath); @@ -651,7 +658,7 @@ smallStringt* getAtNDupSmallStringSmallArrayG (smallArrayt *self, smallStri void* getAtNDupVoidSmallArrayG (smallArrayt *self, void* retType UNUSED, intmax_t index); smallContainert* getAtNDupSmallContainerSmallArrayG(smallArrayt *self, smallContainert* retType UNUSED, intmax_t index); smallArrayt* setAtSmallArrayG (smallArrayt *self, intmax_t index, baset *value); -smallArrayt* setAtUndefinedSmallArrayG(smallArrayt *self, intmax_t index, void *value); +smallArrayt* setAtUndefinedSmallArrayG(smallArrayt *self, intmax_t index, void *value UNUSED); smallArrayt* setAtBoolSmallArrayG (smallArrayt *self, intmax_t index, bool value); smallArrayt* setAtDoubleSmallArrayG (smallArrayt *self, intmax_t index, double value); smallArrayt* setAtIntSmallArrayG (smallArrayt *self, intmax_t index, int64_t value); @@ -731,7 +738,7 @@ smallArrayt* insertSmallJsonSmallArrayG (smallArrayt *self, intmax_t index, smallArrayt* insertNSmashSmallArrayG(smallArrayt *self, intmax_t index, smallArrayt *toInsert); smallArrayt* insertNSmashSmallJsonSmallArrayG(smallArrayt *self, intmax_t index, smallJsont *toInsert); smallArrayt* injectSmallArrayG (smallArrayt *self, intmax_t index, baset *value); -smallArrayt* injectUndefinedSmallArrayG(smallArrayt *self, intmax_t index, void *value); +smallArrayt* injectUndefinedSmallArrayG(smallArrayt *self, intmax_t index, void *value UNUSED); smallArrayt* injectBoolSmallArrayG (smallArrayt *self, intmax_t index, bool value); smallArrayt* injectDoubleSmallArrayG (smallArrayt *self, intmax_t index, double value); smallArrayt* injectIntSmallArrayG (smallArrayt *self, intmax_t index, int64_t value); @@ -750,7 +757,7 @@ smallArrayt* injectSmallJsonSmallArrayG (smallArrayt *self, intmax_t index, smallArrayt* injectSmallStringSmallArrayG (smallArrayt *self, intmax_t index, smallStringt *string); smallArrayt* injectSmallContainerSmallArrayG(smallArrayt *self, intmax_t index, smallContainert *container); smallArrayt* injectNFreeSmallArrayG (smallArrayt *self, intmax_t index, baset *value); -smallArrayt* injectNFreeUndefinedSmallArrayG(smallArrayt *self, intmax_t index, void *value); +smallArrayt* injectNFreeUndefinedSmallArrayG(smallArrayt *self, intmax_t index, undefinedt *value); smallArrayt* injectNFreeSSmallArrayG (smallArrayt *self, intmax_t index, char *string); smallArrayt* injectNFreeDictSmallArrayG (smallArrayt *self, intmax_t index, smallDictt *dict); smallArrayt* injectNFreeArraySmallArrayG (smallArrayt *self, intmax_t index, smallArrayt *array); @@ -1323,10 +1330,17 @@ void registerMethodsSmallArray(smallArrayFunctionst *f) { f->zipSmallJsonSmallArray = zipSmallJsonSmallArraySmallArray; f->zipSmallJsonSmallJson = zipSmallJsonSmallJsonSmallArray; f->zipSmallJsonChar = zipSmallJsonCharSmallArray; + f->zipSmallJsonCChar = zipSmallJsonCCharSmallArray; f->zipArray = zipArraySmallArray; + f->zipCArray = zipCArraySmallArray; f->zipArraySmallJson = zipArraySmallJsonSmallArray; + f->zipCArraySmallJson = zipCArraySmallJsonSmallArray; f->zipChar = zipCharSmallArray; + f->zipCChar = zipCCharSmallArray; f->zipArrayChar = zipArrayCharSmallArray; + f->zipCArrayChar = zipCArrayCharSmallArray; + f->zipArrayCChar = zipArrayCCharSmallArray; + f->zipCArrayCChar = zipCArrayCCharSmallArray; f->log = logSmallArray; f->readText = readTextSmallArray; f->readTextSmallJson = readTextSmallJsonSmallArray; @@ -1611,10 +1625,11 @@ internal smallArrayt* mirrorSmallArray(smallArrayt *self) { return(mirror); } - mirror->a = self->a; + mirror->a = self->a; - mirror->iterIndex = self->iterIndex; - mirror->iterStep = self->iterStep; + mirror->iterElementDataType = self->iterElementDataType; + mirror->iterIndex = self->iterIndex; + mirror->iterStep = self->iterStep; if (mirror->iterIndex != -1) { mirror->iterElement = toBaset(sArrayGetTiny(mirror->a, mirror->iterIndex)); } @@ -1820,8 +1835,10 @@ internal smallArrayt* pushSmallBoolSmallArray(smallArrayt *self, smallBoolt *val return(NULL); } - smallt *o = (smallt *) value->value; - sArrayPushTiny(&(self->a), o); + if (!value->value) { + isError(value->value, allocSBool(false)) return(NULL); + } + sArrayPushTiny(&(self->a), (smallt *) value->value); return(self); } @@ -1835,8 +1852,10 @@ internal smallArrayt* pushSmallBytesSmallArray(smallArrayt *self, smallBytest *v return(NULL); } - smallt *o = (smallt *) value->B; - sArrayPushTiny(&(self->a), o); + if (!value->B) { + isError(value->B, allocSBytes()) return(NULL); + } + sArrayPushTiny(&(self->a), (smallt*) value->B); return(self); } @@ -1850,8 +1869,10 @@ internal smallArrayt* pushSmallDoubleSmallArray(smallArrayt *self, smallDoublet return(NULL); } - smallt *o = (smallt *) value->value; - sArrayPushTiny(&(self->a), o); + if (!value->value) { + isError(value->value, allocSDouble(0)) return(NULL); + } + sArrayPushTiny(&(self->a), (smallt *) value->value); return(self); } @@ -1865,8 +1886,10 @@ internal smallArrayt* pushSmallIntSmallArray(smallArrayt *self, smallIntt *value return(NULL); } - smallt *o = (smallt *) value->value; - sArrayPushTiny(&(self->a), o); + if (!value->value) { + isError(value->value, allocSInt(0)) return(NULL); + } + sArrayPushTiny(&(self->a), (smallt *) value->value); return(self); } @@ -1881,6 +1904,12 @@ internal smallArrayt* pushSmallJsonSmallArray(smallArrayt *self, smallJsont *val } smallt *o = getsoO(value); + if (!o) { + // smallJson is empty, create an empty dict + isError(o, (smallt*)allocSDict()) return(NULL); + setsoO(value, o); + } + sArrayPushTiny(&(self->a), o); return(self); } @@ -1895,19 +1924,14 @@ internal smallArrayt* pushSmallStringSmallArray(smallArrayt *self, smallStringt return(NULL); } - smallt *o; if (!string->data) { - isError(o, (smallt *) allocSStringTiny("")) return(NULL); + isError(string->data, allocSStringTiny("")) return(NULL); } - else { - o = (smallt *) string->data; - } - sArrayPushTiny(&(self->a), o); + sArrayPushTiny(&(self->a), (smallt*) string->data); return(self); } internal smallArrayt* pushSmallContainerSmallArray(smallArrayt *self, smallContainert *container) { - smallt *o = NULL; if (checkObjectTypes && container && !isOSmallContainer(container)) { return(NULL); @@ -1918,12 +1942,9 @@ internal smallArrayt* pushSmallContainerSmallArray(smallArrayt *self, smallConta } if (!container->data) { - isError(o, (smallt *) allocSContainer(NULL)) return(NULL); + isError(container->data, allocSContainer(NULL)) return(NULL); } - else { - o = (smallt *) container->data; - } - sArrayPushTiny(&(self->a), o); + sArrayPushTiny(&(self->a), (smallt *) container->data); return(self); } @@ -1949,7 +1970,7 @@ internal smallArrayt* pushNFreeUndefinedSmallArray(smallArrayt *self, undefinedt if (r) { terminateO(undefined); } - return(self); + return(r); } internal smallArrayt* pushNFreeSSmallArray(smallArrayt *self, char *string) { @@ -1958,7 +1979,7 @@ internal smallArrayt* pushNFreeSSmallArray(smallArrayt *self, char *string) { if (r) { free(string); } - return(self); + return(r); } internal smallArrayt* pushNFreeDictSmallArray(smallArrayt *self, smallDictt *dict) { @@ -1985,7 +2006,7 @@ internal smallArrayt* pushNFreeArraycSmallArray(smallArrayt *self, char **array) if (r) { listFreeS(array); } - return(self); + return(r); } internal smallArrayt* pushNFreeSmallBoolSmallArray(smallArrayt *self, smallBoolt *value) { @@ -2191,6 +2212,7 @@ internal undefinedt* popUndefinedSmallArray(smallArrayt *self) { } // finish poping + free(o); self->a->count--; return(allocUndefined()); } @@ -2534,7 +2556,7 @@ internal void* popVoidSmallArray(smallArrayt *self) { e = popSmallContainerSmallArray(self); if (e) { r = e->f->get(e); - finishO(e); + smashO(e); } return(r); } @@ -2755,8 +2777,10 @@ internal smallArrayt* prependSmallBoolSmallArray(smallArrayt *self, smallBoolt * return(NULL); } - smallt *o = (smallt *) value->value; - sArrayPrependTiny(&(self->a), o); + if (!value->value) { + isError(value->value, allocSBool(false)) return(NULL); + } + sArrayPrependTiny(&(self->a), (smallt *) value->value); return(self); } @@ -2770,8 +2794,10 @@ internal smallArrayt* prependSmallBytesSmallArray(smallArrayt *self, smallBytest return(NULL); } - smallt *o = (smallt *) value->B; - sArrayPrependTiny(&(self->a), o); + if (!value->B) { + isError(value->B, allocSBytes()) return(NULL); + } + sArrayPrependTiny(&(self->a), (smallt *) value->B); return(self); } @@ -2785,8 +2811,10 @@ internal smallArrayt* prependSmallDoubleSmallArray(smallArrayt *self, smallDoubl return(NULL); } - smallt *o = (smallt *) value->value; - sArrayPrependTiny(&(self->a), o); + if (!value->value) { + isError(value->value, allocSDouble(0)) return(NULL); + } + sArrayPrependTiny(&(self->a), (smallt *) value->value); return(self); } @@ -2800,8 +2828,10 @@ internal smallArrayt* prependSmallIntSmallArray(smallArrayt *self, smallIntt *va return(NULL); } - smallt *o = (smallt *) value->value; - sArrayPrependTiny(&(self->a), o); + if (!value->value) { + isError(value->value, allocSInt(0)) return(NULL); + } + sArrayPrependTiny(&(self->a), (smallt *) value->value); return(self); } @@ -2816,6 +2846,12 @@ internal smallArrayt* prependSmallJsonSmallArray(smallArrayt *self, smallJsont * } smallt *o = getsoO(json); + if (!o) { + // smallJson is empty, create an empty dict + isError(o, (smallt*)allocSDict()) return(NULL); + setsoO(json, o); + } + sArrayPrependTiny(&(self->a), o); return(self); } @@ -2830,19 +2866,14 @@ internal smallArrayt* prependSmallStringSmallArray(smallArrayt *self, smallStrin return(NULL); } - smallt *o; if (!string->data) { - isError(o, (smallt *) allocSStringTiny("")) return(NULL); - } - else { - o = (smallt *) string->data; + isError(string->data, allocSStringTiny("")) return(NULL); } - sArrayPrependTiny(&(self->a), o); + sArrayPrependTiny(&(self->a), (smallt *) string->data); return(self); } internal smallArrayt* prependSmallContainerSmallArray(smallArrayt *self, smallContainert *container) { - smallt *o = NULL; if (checkObjectTypes && container && !isOSmallContainer(container)) { return(NULL); @@ -2853,12 +2884,9 @@ internal smallArrayt* prependSmallContainerSmallArray(smallArrayt *self, smallCo } if (!container->data) { - isError(o, (smallt *) allocSContainer(NULL)) return(NULL); - } - else { - o = (smallt *) container->data; + isError(container->data, allocSContainer(NULL)) return(NULL); } - sArrayPrependTiny(&(self->a), o); + sArrayPrependTiny(&(self->a), (smallt *) container->data); return(self); } @@ -2894,7 +2922,7 @@ internal smallArrayt* prependNFreeSSmallArray(smallArrayt *self, char *string) { if (r) { free(string); } - return(self); + return(r); } internal smallArrayt* prependNFreeDictSmallArray(smallArrayt *self, smallDictt *dict) { @@ -2921,7 +2949,7 @@ internal smallArrayt* prependNFreeArraycSmallArray(smallArrayt *self, char **arr if (r) { listFreeS(array); } - return(self); + return(r); } internal smallArrayt* prependNFreeSmallBoolSmallArray(smallArrayt *self, smallBoolt *value) { @@ -3057,6 +3085,7 @@ internal undefinedt* dequeueUndefinedSmallArray(smallArrayt *self) { } self->a->count -= i+1;; + free(o); return(allocUndefined()); } @@ -3446,7 +3475,7 @@ internal void* dequeueVoidSmallArray(smallArrayt *self) { e = dequeueSmallContainerSmallArray(self); if (e) { r = e->f->get(e); - finishO(e); + smashO(e); } return(r); } @@ -3533,7 +3562,7 @@ internal double dequeueNumSmallArray(smallArrayt *self) { elif (isOSmallInt(e)) { r = getValO((smallIntt*)e); } - finishO(e); + terminateO(e); return(r); } @@ -3733,10 +3762,8 @@ internal smallArrayt* addSmallArray(smallArrayt *self, smallArrayt *array) { return(NULL); } - r = duplicateSmallArray(self); - if (!array->f->len(array)) { - return(r); + return(duplicateSmallArray(self)); } if (self->a == array->a) { @@ -3745,6 +3772,8 @@ internal smallArrayt* addSmallArray(smallArrayt *self, smallArrayt *array) { return(NULL); } + r = duplicateSmallArray(self); + // copy array forEachSArray(array->a, o) { sArrayPushTiny(&(r->a), o); @@ -3975,7 +4004,7 @@ internal bool cropElemBoolSmallArray(smallArrayt *self, intmax_t index) { return(false); } r = e->f->get(e); - finishO(e); + terminateO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->a->data); @@ -4010,11 +4039,14 @@ internal double cropElemDoubleSmallArray(smallArrayt *self, intmax_t index) { smallDoublet *e = (smallDoublet*)toBaset(o); if (e) { if (checkObjectTypes && !isOSmallDouble(e)) { + if ((o->type == CONTAINER) && (((sContainert*)o)->dataType == SH_DT_BASET)) { + return(0); + } finishO(e); return(0); } r = e->f->get(e); - finishO(e); + terminateO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->a->data); @@ -4049,11 +4081,14 @@ internal int64_t cropElemIntSmallArray(smallArrayt *self, intmax_t index) { smallIntt *e = (smallIntt*)toBaset(o); if (e) { if (checkObjectTypes && !isOSmallInt(e)) { + if ((o->type == CONTAINER) && (((sContainert*)o)->dataType == SH_DT_BASET)) { + return(0); + } finishO(e); return(0); } r = e->f->get(e); - finishO(e); + terminateO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->a->data); @@ -4089,11 +4124,14 @@ internal int32_t cropElemInt32SmallArray(smallArrayt *self, intmax_t index) { smallIntt *e = (smallIntt*)toBaset(o); if (e) { if (checkObjectTypes && !isOSmallInt(e)) { + if ((o->type == CONTAINER) && (((sContainert*)o)->dataType == SH_DT_BASET)) { + return(0); + } finishO(e); return(0); } r = e->f->get(e); - finishO(e); + terminateO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->a->data); @@ -4128,11 +4166,14 @@ internal uint64_t cropElemUintSmallArray(smallArrayt *self, intmax_t index) { smallIntt *e = (smallIntt*)toBaset(o); if (e) { if (checkObjectTypes && !isOSmallInt(e)) { + if ((o->type == CONTAINER) && (((sContainert*)o)->dataType == SH_DT_BASET)) { + return(0); + } finishO(e); return(0); } r = e->f->get(e); - finishO(e); + terminateO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->a->data); @@ -4167,11 +4208,14 @@ internal uint32_t cropElemUint32SmallArray(smallArrayt *self, intmax_t index) { smallIntt *e = (smallIntt*)toBaset(o); if (e) { if (checkObjectTypes && !isOSmallInt(e)) { + if ((o->type == CONTAINER) && (((sContainert*)o)->dataType == SH_DT_BASET)) { + return(0); + } finishO(e); return(0); } r = e->f->get(e); - finishO(e); + terminateO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->a->data); @@ -4206,11 +4250,14 @@ internal char* cropElemSSmallArray(smallArrayt *self, intmax_t index) { smallStringt *e = (smallStringt*)toBaset(o); if (e) { if (checkObjectTypes && !isOSmallString(e)) { + if ((o->type == CONTAINER) && (((sContainert*)o)->dataType == SH_DT_BASET)) { + return(NULL); + } finishO(e); return(NULL); } - r = e->f->get(e); - finishO(e); + r = dupS(e->f->get(e)); + terminateO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->a->data); @@ -4551,10 +4598,10 @@ internal void* cropElemVoidSmallArray(smallArrayt *self, intmax_t index) { } if (checkObjectTypes && !isOSmallContainer(e)) { finishO(e); - return(false); + return(NULL); } r = e->f->get(e); - finishO(e); + smashO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->a->data); @@ -4673,6 +4720,10 @@ internal smallArrayt* insertSmallArray(smallArrayt *self, intmax_t index, smallA return(NULL); } + if (self->a == toInsert->a) { + return(NULL); + } + len = lenSmallArray(self); if (!len && index == -1) { @@ -4752,10 +4803,14 @@ internal smallArrayt* insertSmallJsonSmallArray(smallArrayt *self, intmax_t inde const char *type = getTopTypeO(toInsert); - if (!eqS(type,"array") || !lenO(toInsert)) { + if (!eqS(type,"array")) { return(NULL); } + if (!lenO(toInsert)) { + return(self); + } + createSmallArray(a); setsoO(&a, (sArrayt*)getsoO(toInsert)); return(insertSmallArray(self, index, &a)); @@ -5212,6 +5267,10 @@ internal smallArrayt* injectDictSmallArray(smallArrayt *self, intmax_t index, sm return(NULL); } + if (!toInject->d) { + isError(toInject->d, allocSDict()) return(NULL); + } + smallt *o = (smallt *) toInject->d; if (index == 0) { @@ -5278,6 +5337,13 @@ internal smallArrayt* injectArraySmallArray(smallArrayt *self, intmax_t index, s sArrayt *a = allocSArray(); + if (!a) { + return(NULL); + } + + if (!toInject->a) { + isError(toInject->a, allocSArray()) return(NULL); + } smallt *o = (smallt *) toInject->a; @@ -5400,6 +5466,10 @@ internal smallArrayt* injectSmallBoolSmallArray(smallArrayt *self, intmax_t inde return(NULL); } + if (!toInject->value) { + isError(toInject->value, allocSBool(false)) return(NULL); + } + return(injectSmallArray(self, index, (baset*)toInject)); } @@ -5413,6 +5483,10 @@ internal smallArrayt* injectSmallBytesSmallArray(smallArrayt *self, intmax_t ind return(NULL); } + if (!toInject->B) { + isError(toInject->B, allocSBytes()) return(NULL); + } + return(injectSmallArray(self, index, (baset*)toInject)); } @@ -5426,6 +5500,10 @@ internal smallArrayt* injectSmallDoubleSmallArray(smallArrayt *self, intmax_t in return(NULL); } + if (!toInject->value) { + isError(toInject->value, allocSDouble(0)) return(NULL); + } + return(injectSmallArray(self, index, (baset*)toInject)); } @@ -5439,6 +5517,10 @@ internal smallArrayt* injectSmallIntSmallArray(smallArrayt *self, intmax_t index return(NULL); } + if (!toInject->value) { + isError(toInject->value, allocSInt(0)) return(NULL); + } + return(injectSmallArray(self, index, (baset*)toInject)); } @@ -5452,11 +5534,15 @@ internal smallArrayt* injectSmallJsonSmallArray(smallArrayt *self, intmax_t inde return(NULL); } - baset *O = NULL; - if (toInject) { - O = getTopO(toInject); + smallt *o = getsoO(toInject); + if (!o) { + // smallJson is empty, create an empty dict + isError(o, (smallt*)allocSDict()) return(NULL); + setsoO(toInject, o); } + baset *O = getTopO(toInject); + smallArrayt *r = injectSmallArray(self, index, O); finishO(O); return(r); @@ -5472,10 +5558,10 @@ internal smallArrayt* injectSmallStringSmallArray(smallArrayt *self, intmax_t in return(NULL); } - if (!toInject || !toInject->data) { - // inject empty string when toInject is NULL - return(injectSSmallArray(self, index, NULL)); + if (!toInject->data) { + isError(toInject->data, allocSStringTiny("")) return(NULL); } + return(injectSmallArray(self, index, (baset*)toInject)); } @@ -5489,6 +5575,10 @@ internal smallArrayt* injectSmallContainerSmallArray(smallArrayt *self, intmax_t return(NULL); } + if (!toInject->data) { + isError(toInject->data, allocSContainer(NULL)) return(NULL); + } + return(injectSmallArray(self, index, (baset*)toInject)); } @@ -5569,7 +5659,7 @@ internal smallArrayt* injectNFreeUndefinedSmallArray(smallArrayt *self, intmax_t if (r) { terminateO(undefined); } - return(self); + return(r); } internal smallArrayt* injectNFreeSSmallArray(smallArrayt *self, intmax_t index, char *toInject) { @@ -5578,7 +5668,7 @@ internal smallArrayt* injectNFreeSSmallArray(smallArrayt *self, intmax_t index, if (r) { free(toInject); } - return(self); + return(r); } internal smallArrayt* injectNFreeDictSmallArray(smallArrayt *self, intmax_t index, smallDictt *toInject) { @@ -5605,7 +5695,7 @@ internal smallArrayt* injectNFreeArraycSmallArray(smallArrayt *self, intmax_t in if (r) { listFreeS(toInject); } - return(self); + return(r); } internal smallArrayt* injectNFreeSmallBoolSmallArray(smallArrayt *self, intmax_t index, smallBoolt *toInject) { @@ -5798,17 +5888,17 @@ internal smallArrayt* removeElemSmallArray(smallArrayt *self, intmax_t index) { internal int sortSCmp(const void * a, const void * b) { // sanity checks - if (!a && !b) { + smallt *A = (*((smallt **)a)); + smallt *B = (*((smallt **)b)); + if (!A && !B) { return(0); } - if (!a && b) { + if (!A && B) { return(-1); } - if (a && !b) { + if (A && !B) { return(1); } - smallt *A = (*((smallt **)a)); - smallt *B = (*((smallt **)b)); char *As, *Bs; int r; if (A->type != B->type) { @@ -5819,7 +5909,7 @@ internal int sortSCmp(const void * a, const void * b) { case UNDEFINED: return(0); case BOOL: - return(((sBoolt*)A)->value < ((sBoolt*)B)->value); + return((((sBoolt*)A)->value < ((sBoolt*)B)->value) * -1); case CONTAINER: return(0); case DICT: @@ -5917,8 +6007,12 @@ internal int sortFCmp(void *func, const void *a, const void *b) { cmp = (shCmpt) func; int r = cmp(aO, bO); - finishO(aO); - finishO(bO); + if ((A->type != CONTAINER) || (A->type == CONTAINER && ((sContainert*)A)->dataType != SH_DT_BASET)) { + finishO(aO); + } + if ((A->type != CONTAINER) || (A->type == CONTAINER && ((sContainert*)A)->dataType != SH_DT_BASET)) { + finishO(bO); + } return(r); } #else @@ -5934,8 +6028,12 @@ internal int sortFCmp(const void *a, const void *b, void *func) { cmp = (shCmpt) func; int r = cmp(aO, bO); - finishO(aO); - finishO(bO); + if ((A->type != CONTAINER) || (A->type == CONTAINER && ((sContainert*)A)->dataType != SH_DT_BASET)) { + finishO(aO); + } + if ((A->type != CONTAINER) || (A->type == CONTAINER && ((sContainert*)A)->dataType != SH_DT_BASET)) { + finishO(bO); + } return(r); } #endif @@ -5943,17 +6041,17 @@ internal int sortFCmp(const void *a, const void *b, void *func) { internal int icSortSCmp(const void * a, const void * b) { // sanity checks - if (!a && !b) { + smallt *A = (*((smallt **)a)); + smallt *B = (*((smallt **)b)); + if (!A && !B) { return(0); } - if (!a && b) { + if (!A && B) { return(-1); } - if (a && !b) { + if (A && !B) { return(1); } - smallt *A = (*((smallt **)a)); - smallt *B = (*((smallt **)b)); char *As, *Bs; int r; if (A->type != B->type) { @@ -5964,7 +6062,7 @@ internal int icSortSCmp(const void * a, const void * b) { case UNDEFINED: return(0); case BOOL: - return(((sBoolt*)A)->value < ((sBoolt*)B)->value); + return((((sBoolt*)A)->value < ((sBoolt*)B)->value) * -1); case CONTAINER: return(0); case DICT: @@ -6251,6 +6349,10 @@ internal bool equalSmallArrayBase(smallArrayt* self, baset* p2) { internal bool icEqualSmallArray(smallArrayt *self, smallArrayt *array) { // sanity checks + if (!array) { + return(false); + } + if (checkObjectTypes && array && !isOSmallArray(array)) { return(false); } @@ -6878,6 +6980,11 @@ baset* getAtNDupSmallArray(smallArrayt *self, intmax_t index) { if (!o) { return(NULL); } + + if (o->type == UNDEFINED) { + return((baset*) allocUndefined()); + } + if (o->type == CONTAINER && (((sContainert*)o)->dataType == SH_DT_BASET)) { baset *b = ((sContainert*)o)->data; return(duplicateO(b)); @@ -7183,7 +7290,7 @@ internal void* getAtNDupVoidSmallArray(smallArrayt *self, intmax_t index) { e = getAtNDupSmallContainerSmallArray(self, index); if (e) { r = e->f->get(e); - finishO(e); + smashO(e); } return(r); } @@ -7318,6 +7425,10 @@ internal smallArrayt* setAtIntSmallArray(smallArrayt *self, intmax_t index, int6 internal smallArrayt* setAtSSmallArray(smallArrayt *self, intmax_t index, const char *string) { + if (!string) { + return(NULL); + } + if (index >= (intmax_t)lenSmallArray(self)) { return(NULL); } @@ -7329,13 +7440,7 @@ internal smallArrayt* setAtSSmallArray(smallArrayt *self, intmax_t index, const } smallt *o; - if (!string) { - // create an empty sStringt instead of NULL - isError(o, (smallt *) allocSStringTiny("")) return(NULL); - } - else { - isError(o, (smallt *) allocSStringTiny(string)) return(NULL); - } + isError(o, (smallt *) allocSStringTiny(string)) return(NULL); sArraySetTiny(self->a, index, o); return(self); } @@ -7460,8 +7565,10 @@ internal smallArrayt* setAtSmallBoolSmallArray(smallArrayt *self, intmax_t index index = lenSmallArray(self) + index; } - smallt *o = (smallt *) value->value; - sArraySetTiny(self->a, index, o); + if (!value->value) { + isError(value->value, allocSBool(false)) return(NULL); + } + sArraySetTiny(self->a, index, (smallt *) value->value); return(self); } @@ -7485,8 +7592,10 @@ internal smallArrayt* setAtSmallBytesSmallArray(smallArrayt *self, intmax_t inde index = lenSmallArray(self) + index; } - smallt *o = (smallt *) value->B; - sArraySetTiny(self->a, index, o); + if (!value->B) { + isError(value->B, allocSBytes()) return(NULL); + } + sArraySetTiny(self->a, index, (smallt*) value->B); return(self); } @@ -7510,8 +7619,10 @@ internal smallArrayt* setAtSmallDoubleSmallArray(smallArrayt *self, intmax_t ind index = lenSmallArray(self) + index; } - smallt *o = (smallt *) value->value; - sArraySetTiny(self->a, index, o); + if (!value->value) { + isError(value->value, allocSDouble(0)) return(NULL); + } + sArraySetTiny(self->a, index, (smallt*) value->value); return(self); } @@ -7535,8 +7646,10 @@ internal smallArrayt* setAtSmallIntSmallArray(smallArrayt *self, intmax_t index, index = lenSmallArray(self) + index; } - smallt *o = (smallt *) value->value; - sArraySetTiny(self->a, index, o); + if (!value->value) { + isError(value->value, allocSInt(0)) return(NULL); + } + sArraySetTiny(self->a, index, (smallt*) value->value); return(self); } @@ -7561,6 +7674,12 @@ internal smallArrayt* setAtSmallJsonSmallArray(smallArrayt *self, intmax_t index } smallt *o = getsoO(value); + if (!o) { + // smallJson is empty, create an empty dict + isError(o, (smallt*) allocSDict()) return(NULL); + setsoO(value, o); + } + sArraySetTiny(self->a, index, o); return(self); } @@ -7585,19 +7704,14 @@ internal smallArrayt* setAtSmallStringSmallArray(smallArrayt *self, intmax_t ind index = lenSmallArray(self) + index; } - smallt *o; if (!string->data) { - isError(o, (smallt *) allocSStringTiny("")) return(NULL); + isError(string->data, allocSStringTiny("")) return(NULL); } - else { - o = (smallt *) string->data; - } - sArraySetTiny(self->a, index, o); + sArraySetTiny(self->a, index, (smallt *)string->data); return(self); } internal smallArrayt* setAtSmallContainerSmallArray(smallArrayt *self, intmax_t index, smallContainert *container) { - smallt *o = NULL; if (!container) { return(NULL); @@ -7618,12 +7732,9 @@ internal smallArrayt* setAtSmallContainerSmallArray(smallArrayt *self, intmax_t } if (!container->data) { - isError(o, (smallt *) allocSContainer(NULL)) return(NULL); + isError(container->data, allocSContainer(NULL)) return(NULL); } - else { - o = (smallt *) container->data; - } - sArraySetTiny(self->a, index, o); + sArraySetTiny(self->a, index, (smallt *) container->data); return(self); } @@ -7658,7 +7769,7 @@ internal smallArrayt* setAtNFreeUndefinedSmallArray(smallArrayt *self, intmax_t if (r) { terminateO(undefined); } - return(self); + return(r); } internal smallArrayt* setAtNFreeSSmallArray(smallArrayt *self, intmax_t index, char *string) { @@ -7667,7 +7778,7 @@ internal smallArrayt* setAtNFreeSSmallArray(smallArrayt *self, intmax_t index, c if (r) { free(string); } - return(self); + return(r); } internal smallArrayt* setAtNFreeDictSmallArray(smallArrayt *self, intmax_t index, smallDictt *dict) { @@ -7694,7 +7805,7 @@ internal smallArrayt* setAtNFreeArraycSmallArray(smallArrayt *self, intmax_t ind if (r) { listFreeS(array); } - return(self); + return(r); } internal smallArrayt* setAtNFreeSmallBoolSmallArray(smallArrayt *self, intmax_t index, smallBoolt *value) { @@ -7782,7 +7893,8 @@ internal smallArrayt* setPAtDictSmallArray(smallArrayt *self, intmax_t index, sm } if (!dict->d) { - isError(dict->d, allocSDict()) return(NULL); + // realloc error + return(NULL); } sArraySetP(self->a, index, (smallt *)dict->d); @@ -7810,8 +7922,8 @@ internal smallArrayt* setPAtArraySmallArray(smallArrayt *self, intmax_t index, s } if (!array->a) { - // allocate empty array - isError(array->a, allocSArray()) return(NULL); + // realloc error + return(NULL); } sArraySetP(self->a, index, (smallt *)array->a); @@ -7841,7 +7953,8 @@ internal smallArrayt* setPAtSmallJsonSmallArray(smallArrayt *self, intmax_t inde smallt *o; o = getsoO(json); if (!o) { - isError(o, (smallt *) allocSUndefined()) return(NULL); + // realloc error + return(NULL); } sArraySetP(self->a, index, o); return(self); @@ -7867,13 +7980,12 @@ internal smallArrayt* setPAtSmallStringSmallArray(smallArrayt *self, intmax_t in index = lenSmallArray(self) + index; } - smallt *o; if (!string->data) { - isError(o, (smallt *) allocSStringTiny("")) return(NULL); - } - else { - o = (smallt *) string->data; + // realloc error + return(NULL); } + + smallt *o = (smallt *) string->data; sArraySetP(self->a, index, o); return(self); } @@ -7899,7 +8011,8 @@ internal smallArrayt* setPAtNFreeDictSmallArray(smallArrayt *self, intmax_t inde } if (!dict->d) { - isError(dict->d, allocSDict()) return(NULL); + // realloc error + return(NULL); } sArraySetP(self->a, index, (smallt *)dict->d); @@ -7929,8 +8042,8 @@ internal smallArrayt* setPAtNFreeArraySmallArray(smallArrayt *self, intmax_t ind } if (!array->a) { - // allocate empty array - isError(array->a, allocSArray()) return(NULL); + // realloc error + return(NULL); } sArraySetP(self->a, index, (smallt *)array->a); @@ -7961,7 +8074,8 @@ internal smallArrayt* setPAtNFreeSmallJsonSmallArray(smallArrayt *self, intmax_t smallt *o; o = getsoO(json);; if (!o) { - isError(o, (smallt *) allocSUndefined()) return(NULL); + // realloc error + return(NULL); } finishO(json); sArraySetP(self->a, index, o); @@ -7988,14 +8102,13 @@ internal smallArrayt* setPAtNFreeSmallStringSmallArray(smallArrayt *self, intmax index = lenSmallArray(self) + index; } - smallt *o; if (!string->data) { - isError(o, (smallt *) allocSStringTiny("")) return(NULL); - } - else { - o = (smallt *) string->data; - finishO(string); + // realloc error + return(NULL); } + + smallt *o = (smallt *) string->data; + finishO(string); sArraySetP(self->a, index, o); return(self); } @@ -8484,8 +8597,6 @@ internal ssize_t indexOfArraycSmallArray(smallArrayt *self, char **array) { // search object in elements char *s = toStringListSGF(array); - iPrependCharS(&s, '['); - iAppendCharS(&s, ']'); char *es = NULL;; enumerateSArray(self->a, e, i) { @@ -8696,6 +8807,7 @@ internal ssize_t indexOfSmallStringSmallArray(smallArrayt *self, smallStringt *s internal ssize_t indexOfSmallContainerSmallArray(smallArrayt *self UNUSED, smallContainert *container UNUSED) { + // TODO return(-1); } @@ -8714,6 +8826,11 @@ internal ssize_t binarySearchSmallArray(smallArrayt *self, baset *value) { while (first <= last) { middle = (first+last)/2; m = sToString(sArrayGetTiny(self->a, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (strcmp(m, s) < 0) { first = middle + 1; } @@ -8732,37 +8849,40 @@ internal ssize_t binarySearchSmallArray(smallArrayt *self, baset *value) { return(r); } -internal ssize_t binarySearchUndefinedSmallArray(smallArrayt *self, undefinedt *undefined) { - ssize_t first = 0, middle, last;; - - if (!lenSmallArray(self) || !undefined) { - return(-1); - } - - if (checkObjectTypes && !isOUndefined(undefined)) { - return(-1); - } +internal ssize_t binarySearchUndefinedSmallArray(smallArrayt *self UNUSED, undefinedt *undefined UNUSED) { + // ssize_t first = 0, middle, last - smallt *m = NULL; + // it doesn't make sense to search for undefined + // the array has to be sorted + // so undefined is always at postion 0 + return(-1); - ssize_t r = -1; - last = lenSmallArray(self) - 1; - while (first <= last) { - middle = (first+last)/2; - m = sArrayGetTiny(self->a, middle); - if (m->type < UNDEFINED) { - first = middle + 1; - } - else if (m->type == UNDEFINED) { - r = middle; - goto finish; - } - else { - last = middle -1; - } - } - finish: - return(r); + // if !lenSmallArray(self) || !undefined + // return -1 + + // if checkObjectTypes && !isOUndefined(undefined) + // return -1 + + // smallt *m = NULL + + // ssize_t r = -1 + // last = lenSmallArray(self) - 1 + // while first <= last + // middle = (first+last)/2 + // m = sArrayGetTiny(self->a, middle) + // if !m + // // there are null element in the array + // // the array needs to be trimmed or compacted + // return -1 + // if m->type < UNDEFINED + // first = middle + 1 + // else if m->type = UNDEFINED + // r = middle + // goto finish; + // else + // last = middle -1 + // finish: + // return r } internal ssize_t binarySearchBoolSmallArray(smallArrayt *self, bool value) { @@ -8779,6 +8899,11 @@ internal ssize_t binarySearchBoolSmallArray(smallArrayt *self, bool value) { while (first <= last) { middle = (first+last)/2; m = (sBoolt*)(sArrayGetTiny(self->a, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + return(-1); + } if (m->type < BOOL || ((m->type == BOOL) && (m->value < value))) { first = middle + 1; } @@ -8809,6 +8934,11 @@ internal ssize_t binarySearchDoubleSmallArray(smallArrayt *self, double value) { while (first <= last) { middle = (first+last)/2; m = (sDoublet*)(sArrayGetTiny(self->a, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + return(-1); + } if (m->type < DOUBLE || ((m->type == DOUBLE) && (m->value < value))) { first = middle + 1; } @@ -8838,6 +8968,11 @@ internal ssize_t binarySearchIntSmallArray(smallArrayt *self, int64_t value) { while (first <= last) { middle = (first+last)/2; m = (sIntt*)(sArrayGetTiny(self->a, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + return(-1); + } if (m->type < INT || ((m->type == INT) && (m->value < value))) { first = middle + 1; } @@ -8866,6 +9001,11 @@ internal ssize_t binarySearchSSmallArray(smallArrayt *self, const char *string) while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->a, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + return(-1); + } char *m = sStringGetTiny((sStringt*)(e)); if (e->type < STRING || ((e->type == STRING) && (strcmp(m, string) < 0))) { first = middle + 1; @@ -8906,6 +9046,11 @@ internal ssize_t binarySearchDictSmallArray(smallArrayt *self, smallDictt *dict) while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->a, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } // cg_c bug, can't use comma operator, extra code if (e->type == DICT) { m = sToString(e); @@ -8952,6 +9097,11 @@ internal ssize_t binarySearchArraySmallArray(smallArrayt *self, smallArrayt *arr while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->a, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } // cg_c bug, can't use comma operator, extra code if (e->type == ARRAY) { m = sToString(e); @@ -8986,8 +9136,6 @@ internal ssize_t binarySearchArraycSmallArray(smallArrayt *self, char **array) { } char *s = toStringListSGF(array); - iPrependCharS(&s, '['); - iAppendCharS(&s, ']'); smallt *e = NULL; char *m = NULL; @@ -8996,6 +9144,11 @@ internal ssize_t binarySearchArraycSmallArray(smallArrayt *self, char **array) { while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->a, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } // cg_c bug, can't use comma operator, extra code if (e->type == ARRAY) { m = sToString(e); @@ -9041,6 +9194,11 @@ internal ssize_t binarySearchSmallBoolSmallArray(smallArrayt *self, smallBoolt * while (first <= last) { middle = (first+last)/2; m = (sBoolt*)(sArrayGetTiny(self->a, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (m->type < BOOL || ((m->type == BOOL) && (m->value < b))) { first = middle + 1; } @@ -9076,6 +9234,11 @@ internal ssize_t binarySearchSmallBytesSmallArray(smallArrayt *self, smallBytest while (first <= last) { middle = (first+last)/2; m = (sBytest*)(sArrayGetTiny(self->a, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (m->type < BYTES || ((m->type == BYTES) && ((m->count < count) || (memcmp(&m->data, b, count) < 0)))) { first = middle + 1; } @@ -9110,6 +9273,11 @@ internal ssize_t binarySearchSmallDoubleSmallArray(smallArrayt *self, smallDoubl while (first <= last) { middle = (first+last)/2; m = (sDoublet*)(sArrayGetTiny(self->a, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (m->type < DOUBLE || ((m->type == DOUBLE) && (m->value < v))) { first = middle + 1; } @@ -9144,6 +9312,11 @@ internal ssize_t binarySearchSmallIntSmallArray(smallArrayt *self, smallIntt *va while (first <= last) { middle = (first+last)/2; m = (sIntt*)(sArrayGetTiny(self->a, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (m->type < INT || ((m->type == INT) && (m->value < v))) { first = middle + 1; } @@ -9179,9 +9352,10 @@ internal ssize_t binarySearchSmallJsonSmallArray(smallArrayt *self, smallJsont * baset *O = NULL; ssize_t r = -1; if (eqS(type, "undefined")) { - O = getTopO(string); - r = binarySearchSmallArray(self, O); - finishO(O); + // it doesn't make sense to search for undefined + // the array has to be sorted + // so undefined is always at postion 0 + return(-1); } else if (eqS(type, "bool")) { O = getTopO(string); @@ -9234,6 +9408,11 @@ internal ssize_t binarySearchSmallStringSmallArray(smallArrayt *self, smallStrin while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->a, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + return(-1); + } char *m = sStringGetTiny((sStringt*)(e)); if (e->type < STRING || ((e->type == STRING) && (strcmp(m, s) < 0))) { first = middle + 1; @@ -9250,6 +9429,7 @@ internal ssize_t binarySearchSmallStringSmallArray(smallArrayt *self, smallStrin internal ssize_t binarySearchSmallContainerSmallArray(smallArrayt *self UNUSED, smallContainert *container UNUSED) { + // TODO return(-1); } @@ -9292,19 +9472,27 @@ internal smallArrayt* uniqSmallArray(smallArrayt *self) { } // push element to new list if it is not already in new list - bool pushE = false; + bool pushE = false; + bool foundUndefined = false; forEachSArray(self->a, e) { if (e) { switch (e->type) { case UNDEFINED: - if (r->f->indexOfUndefined(r, (undefinedt*)e) == -1) { - pushE = true; + if (!foundUndefined) { + pushE = true; + foundUndefined = true; + } + else { + free(e); } break; case BOOL: if (r->f->indexOfBool(r, ((sBoolt*)e)->value) == -1) { pushE = true; } + else { + free(e); + } break; // TODO case CONTAINER: case DICT: @@ -9312,33 +9500,51 @@ internal smallArrayt* uniqSmallArray(smallArrayt *self) { if (r->f->indexOfDict(r, d) == -1) { pushE = true; } + else { + freeO(d); + } break; case DOUBLE: if (r->f->indexOfDouble(r, ((sDoublet*)e)->value) == -1) { pushE = true; } + else { + free(e); + } break; case INT: if (r->f->indexOfInt(r, ((sIntt*)e)->value) == -1) { pushE = true; } + else { + free(e); + } break; case STRING: if (r->f->indexOfS(r, sStringGetTiny((sStringt*)e)) == -1) { pushE = true; } + else { + free(e); + } break; case ARRAY: a->a = (sArrayt*)e; if (r->f->indexOfArray(r, a) == -1) { pushE = true; } + else { + freeO(a); + } break; case BYTES: B->B = (sBytest*)e; if (r->f->indexOfSmallBytes(r, B) == -1) { pushE = true; } + else { + freeO(B); + } break; } } @@ -9583,8 +9789,6 @@ internal ssize_t icIndexOfArraycSmallArray(smallArrayt *self, char **array) { // search object in elements char *s = toStringListSGF(array); - iPrependCharS(&s, '['); - iAppendCharS(&s, ']'); char *es = NULL;; enumerateSArray(self->a, e, i) { @@ -9706,10 +9910,15 @@ internal ssize_t icBinarySearchSmallArray(smallArrayt *self, baset *value) { while (first <= last) { middle = (first+last)/2; m = sToString(sArrayGetTiny(self->a, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (strcasecmp(m, s) < 0) { first = middle + 1; } - else if (strcmp(m, s) == 0) { + else if (strcasecmp(m, s) == 0) { r = middle; free(m); goto finish; @@ -9737,6 +9946,11 @@ internal ssize_t icBinarySearchSSmallArray(smallArrayt *self, const char *string while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->a, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + return(-1); + } char *m = sStringGetTiny((sStringt*)(e)); if (e->type < STRING || ((e->type == STRING) && (strcasecmp(m, string) < 0))) { first = middle + 1; @@ -9777,6 +9991,11 @@ internal ssize_t icBinarySearchDictSmallArray(smallArrayt *self, smallDictt *dic while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->a, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } // cg_c bug, can't use comma operator, extra code if (e->type == DICT) { m = sToString(e); @@ -9823,6 +10042,11 @@ internal ssize_t icBinarySearchArraySmallArray(smallArrayt *self, smallArrayt *a while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->a, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } // cg_c bug, can't use comma operator, extra code if (e->type == ARRAY) { m = sToString(e); @@ -9857,8 +10081,6 @@ internal ssize_t icBinarySearchArraycSmallArray(smallArrayt *self, char **array) } char *s = toStringListSGF(array); - iPrependCharS(&s, '['); - iAppendCharS(&s, ']'); smallt *e = NULL; char *m = NULL; @@ -9867,6 +10089,11 @@ internal ssize_t icBinarySearchArraycSmallArray(smallArrayt *self, char **array) while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->a, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } // cg_c bug, can't use comma operator, extra code if (e->type == ARRAY) { m = sToString(e); @@ -9913,9 +10140,10 @@ internal ssize_t icBinarySearchSmallJsonSmallArray(smallArrayt *self, smallJsont baset *O = NULL; ssize_t r = -1; if (eqS(type, "undefined")) { - O = getTopO(string); - r = binarySearchSmallArray(self, O); - finishO(O); + // it doesn't make sense to search for undefined + // the array has to be sorted + // so undefined is always at postion 0 + return(-1); } else if (eqS(type, "bool")) { O = getTopO(string); @@ -9968,6 +10196,11 @@ internal ssize_t icBinarySearchSmallStringSmallArray(smallArrayt *self, smallStr while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->a, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + return(-1); + } char *m = sStringGetTiny((sStringt*)(e)); if (e->type < STRING || ((e->type == STRING) && (strcasecmp(m, s) < 0))) { first = middle + 1; @@ -10021,19 +10254,27 @@ internal smallArrayt* icUniqSmallArray(smallArrayt *self) { } // push element to new list if it is not already in new list - bool pushE = false; + bool pushE = false; + bool foundUndefined = false; forEachSArray(self->a, e) { if (e) { switch (e->type) { case UNDEFINED: - if (r->f->indexOfUndefined(r, (undefinedt*)e) == -1) { - pushE = true; + if (!foundUndefined) { + pushE = true; + foundUndefined = true; + } + else { + free(e); } break; case BOOL: if (r->f->indexOfBool(r, ((sBoolt*)e)->value) == -1) { pushE = true; } + else { + free(e); + } break; // TODO case CONTAINER: case DICT: @@ -10041,33 +10282,51 @@ internal smallArrayt* icUniqSmallArray(smallArrayt *self) { if (r->f->icIndexOfDict(r, d) == -1) { pushE = true; } + else { + freeO(d); + } break; case DOUBLE: if (r->f->indexOfDouble(r, ((sDoublet*)e)->value) == -1) { pushE = true; } + else { + free(e); + } break; case INT: if (r->f->indexOfInt(r, ((sIntt*)e)->value) == -1) { pushE = true; } + else { + free(e); + } break; case STRING: if (r->f->icIndexOfS(r, sStringGetTiny((sStringt*)e)) == -1) { pushE = true; } + else { + free(e); + } break; case ARRAY: a->a = (sArrayt*)e; if (r->f->icIndexOfArray(r, a) == -1) { pushE = true; } + else { + freeO(a); + } break; case BYTES: B->B = (sBytest*)e; if (r->f->indexOfSmallBytes(r, B) == -1) { pushE = true; } + else { + freeO(B); + } break; } } @@ -10104,7 +10363,9 @@ internal smallArrayt* compactSmallArray(smallArrayt *self) { forEachSArray(self->a, e) { if (e) { switch (e->type) { - // skip undefined - case UNDEFINED: + case UNDEFINED: + free(e); + break; case BOOL: sArrayPushTiny(&(r->a), e); break; @@ -10115,6 +10376,9 @@ internal smallArrayt* compactSmallArray(smallArrayt *self) { if (((sDictt*)e)->count > 0) { sArrayPushTiny(&(r->a), e); } + else { + sFree(e); + } break; case DOUBLE: sArrayPushTiny(&(r->a), e); @@ -10127,17 +10391,26 @@ internal smallArrayt* compactSmallArray(smallArrayt *self) { if (*trim != 0) { sArrayPushTiny(&(r->a), e); } + else { + free(e); + } free(trim); break; case ARRAY: if (((sArrayt*)e)->count > 0) { sArrayPushTiny(&(r->a), e); } + else { + sFree(e); + } break; case BYTES: if (((sBytest*)e)->count > 0) { sArrayPushTiny(&(r->a), e); } + else { + sFree(e); + } break; } } @@ -10221,27 +10494,54 @@ internal bool isBlankSmallArray(smallArrayt *self) { internal void forEachSmallArrayF(smallArrayt *self, void *closure, forEachElementSmallArrayFt funcElem) { - forEachSmallArray(self, e) { + // sanity checks + if (!lenSmallArray(self)) { + return; + } + + forEachSArray(self->a, e) { if (not e) { continue; } - if (!funcElem(closure, e)) { - finishO(e); + baset *E = toBaset(e); + if (!funcElem(closure, E)) { + if (e->type == CONTAINER && ((sContainert*)e)->dataType == SH_DT_BASET) { + break; + } + finishO(E); break; } - finishO(e); + if (e->type == CONTAINER && ((sContainert*)e)->dataType == SH_DT_BASET) { + continue; + } + finishO(E); } return; } internal void enumerateSmallArrayF(smallArrayt *self, void *closure, enumerateElementSmallArrayFt funcElem) { - enumerateSmallArray(self, e, i) { - if (!funcElem(closure, i, e)) { - finishO(e); + // sanity checks + if (!lenSmallArray(self)) { + return; + } + + enumerateSArray(self->a, e, i) { + if (not e) { + continue; + } + baset *E = toBaset(e); + if (!funcElem(closure, i, E)) { + if (e->type == CONTAINER && ((sContainert*)e)->dataType == SH_DT_BASET) { + break; + } + finishO(E); break; } - finishO(e); + if (e->type == CONTAINER && ((sContainert*)e)->dataType == SH_DT_BASET) { + continue; + } + finishO(E); } return; } @@ -10262,10 +10562,6 @@ internal baset* iterStartSmallArray(smallArrayt *self) { 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 @@ -10298,10 +10594,6 @@ internal baset* iterStartLastSmallArray(smallArrayt *self) { 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 @@ -10425,6 +10717,9 @@ internal baset* iterNextSmallArray(smallArrayt *self) { if (self->iterElementDataType != SH_DT_BASET) { finishO(self->iterElement); } + else { + self->iterElement = NULL; + } // get next element until we get a valid item or the end is reached while (!self->iterElement) { // get next element @@ -10487,7 +10782,7 @@ internal smallStringt* joinSmallArray(smallArrayt *self, const char* delim) { } break; default: - s = sToString(e);; + s = sToString(e); if (!r) { r = allocSmallString(s); } @@ -10623,7 +10918,7 @@ internal smallArrayt* zipSmallArray(smallArrayt *self, smallArrayt *array1, smal return(self); } - len = MIN(array1->f->len(array1), array2->f->len(array2));; + len = MIN(array1->f->len(array1), array2->f->len(array2)); count = 0; @@ -10749,6 +11044,27 @@ internal smallArrayt* zipSmallJsonCharSmallArray(smallArrayt *self, smallJsont return(zipCharSmallArray(self, &a, array2)); } +internal smallArrayt* zipSmallJsonCCharSmallArray(smallArrayt *self, smallJsont *array1, const char **array2) { + + if (!array1 || !array2) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallJson(array1)) { + return(NULL); + } + + const char *type = getTopTypeO(array1); + + if (!eqS(type,"array")) { + return(NULL); + } + + createSmallArray(a); + setsoO(&a, (sArrayt*)getsoO(array1)); + return(zipCCharSmallArray(self, &a, array2)); +} + internal smallArrayt* zipArraySmallArray(smallArrayt *self, char** array1, smallArrayt *array2) { size_t len; size_t count; @@ -10786,6 +11102,45 @@ internal smallArrayt* zipArraySmallArray(smallArrayt *self, char** array1, small return(self); } +internal smallArrayt* zipCArraySmallArray(smallArrayt *self, const char** a1, smallArrayt *array2) { + size_t len; + size_t count; + + if (!a1 || !array2) { + return(NULL); + } + + char** array1 = (char**)a1; + + if (checkObjectTypes && !isOSmallArray(array2)) { + return(NULL); + } + + if (!listLengthS(array1) || !array2->f->len(array2)) { + return(self); + } + + len = MIN(listLengthS(array1), array2->f->len(array2));; + + count = 0; + + enumerateS(array1, E1, i) { + createAllocateSmallArray(a); + if (!a) { + return(NULL); + } + a->f->pushS(a, E1); + pushNFreeO(a, getO(array2, i)); + pushNFreeArraySmallArray(self, a); + count++; + if (count == len) { + break; + } + } + + return(self); +} + internal smallArrayt* zipArraySmallJsonSmallArray(smallArrayt *self, char** array1, smallJsont *array2) { if (!array1 || !array2) { @@ -10807,6 +11162,27 @@ internal smallArrayt* zipArraySmallJsonSmallArray(smallArrayt *self, char** arr return(zipArraySmallArray(self, array1, &a)); } +internal smallArrayt* zipCArraySmallJsonSmallArray(smallArrayt *self, const char** array1, smallJsont *array2) { + + if (!array1 || !array2) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallJson(array2)) { + return(NULL); + } + + const char *type = getTopTypeO(array2); + + if (!eqS(type,"array")) { + return(NULL); + } + + createSmallArray(a); + setsoO(&a, (sArrayt*)getsoO(array2)); + return(zipCArraySmallArray(self, array1, &a)); +} + internal smallArrayt* zipCharSmallArray(smallArrayt *self, smallArrayt *array1, char** array2) { size_t len; size_t count; @@ -10845,6 +11221,46 @@ internal smallArrayt* zipCharSmallArray(smallArrayt *self, smallArrayt *array1, return(self); } +internal smallArrayt* zipCCharSmallArray(smallArrayt *self, smallArrayt *array1, const char** a2) { + size_t len; + size_t count; + + if (!array1 || !a2) { + return(NULL); + } + + char** array2 = (char**)a2; + + if (checkObjectTypes && !isOSmallArray(array1)) { + return(NULL); + } + + if (!array1->f->len(array1) || !listLengthS(array2)) { + return(self); + } + + len = MIN(array1->f->len(array1), listLengthS(array2));; + + count = 0; + + enumerateSmallArray(array1, E1, i) { + createAllocateSmallArray(a); + if (!a) { + finishO(E1); + return(NULL); + } + pushNFreeO(a, E1); + a->f->pushS(a, array2[i]); + pushNFreeArraySmallArray(self, a); + count++; + if (count == len) { + break; + } + } + + return(self); +} + internal smallArrayt* zipArrayCharSmallArray(smallArrayt *self, char** array1, char** array2) { size_t len; size_t count; @@ -10878,6 +11294,112 @@ internal smallArrayt* zipArrayCharSmallArray(smallArrayt *self, char** array1, c return(self); } +internal smallArrayt* zipCArrayCharSmallArray(smallArrayt *self, const char** a1, char** array2) { + size_t len; + size_t count; + + if (!a1 || !array2) { + return(NULL); + } + + char** array1 = (char**)a1; + + if (!listLengthS(array1) || !listLengthS(array2)) { + return(self); + } + + len = MIN(listLengthS(array1), listLengthS(array2));; + + count = 0; + + enumerateS(array1, E1, i) { + createAllocateSmallArray(a); + if (!a) { + return(NULL); + } + a->f->pushS(a, E1); + a->f->pushNFreeS(a, array2[i]); + pushNFreeArraySmallArray(self, a); + count++; + if (count == len) { + break; + } + } + + return(self); +} + +internal smallArrayt* zipArrayCCharSmallArray(smallArrayt *self, char** array1, const char** a2) { + size_t len; + size_t count; + + if (!array1 || !a2) { + return(NULL); + } + + char** array2 = (char**)a2; + + if (!listLengthS(array1) || !listLengthS(array2)) { + return(self); + } + + len = MIN(listLengthS(array1), listLengthS(array2));; + + count = 0; + + enumerateS(array1, E1, i) { + createAllocateSmallArray(a); + if (!a) { + return(NULL); + } + a->f->pushNFreeS(a, E1); + a->f->pushS(a, array2[i]); + pushNFreeArraySmallArray(self, a); + count++; + if (count == len) { + break; + } + } + + return(self); +} + +internal smallArrayt* zipCArrayCCharSmallArray(smallArrayt *self, const char** a1, const char** a2) { + size_t len; + size_t count; + + if (!a1 || !a2) { + return(NULL); + } + + char** array1 = (char**)a1; + char** array2 = (char**)a2; + + if (!listLengthS(array1) || !listLengthS(array2)) { + return(self); + } + + len = MIN(listLengthS(array1), listLengthS(array2));; + + count = 0; + + enumerateS(array1, E1, i) { + createAllocateSmallArray(a); + if (!a) { + return(NULL); + } + a->f->pushS(a, E1); + a->f->pushS(a, array2[i]); + pushNFreeArraySmallArray(self, a); + count++; + if (count == len) { + break; + } + } + + return(self); +} + internal void logSmallArray(smallArrayt *self) { if (!lenSmallArray(self)) { @@ -10995,7 +11517,8 @@ internal bool writeTextSmallArray(smallArrayt *self, const char *filePath) { fp = fopen(filePath, "w"); if (!fp) { - pStrError("writeText error"); + pFuncError + shEPrintfS("The path was: \"%s\"\n", filePath); return(false); } if (!lenSmallArray(self)) { @@ -11152,7 +11675,8 @@ internal smallArrayt* typeSmallStringsSmallArray(smallArrayt *self) { if (!a) { return(NULL); } - fromArrayNFreeSmallArray(a, r, 0); + fromArraySmallArray(a, r, 0); + free(r); return(a); } @@ -11400,11 +11924,9 @@ smallArrayt* pushVoidSmallArrayG (smallArrayt *self, void *value) { if (!c) { return(NULL); } - self->f->pushNFreeSmallContainer(self, c); - return(self); + return(self->f->pushNFreeSmallContainer(self, c)); } - self->f->pushUndefined(self); - return(self); + return(self->f->pushUndefined(self)); } smallArrayt* pushSmallBoolSmallArrayG (smallArrayt *self, smallBoolt *value) { @@ -11668,11 +12190,9 @@ smallArrayt* prependVoidSmallArrayG (smallArrayt *self, void *value) { if (!c) { return(NULL); } - self->f->prependNFreeSmallContainer(self, c); - return(self); + return(self->f->prependNFreeSmallContainer(self, c)); } - self->f->prependUndefined(self); - return(self); + return(self->f->prependUndefined(self)); } smallArrayt* prependSmallBoolSmallArrayG (smallArrayt *self, smallBoolt *value) { @@ -12102,13 +12622,9 @@ smallArrayt* setAtSmallArrayG (smallArrayt *self, intmax_t index, baset return(self->f->setAt(self, index, value)); } -smallArrayt* setAtUndefinedSmallArrayG(smallArrayt *self, intmax_t index, void *value) { +smallArrayt* setAtUndefinedSmallArrayG(smallArrayt *self, intmax_t index, void *value UNUSED) { - if (value) { - free(value); - } - self->f->setAtUndefined(self,index); - return(self); + return(self->f->setAtUndefined(self,index)); } smallArrayt* setAtBoolSmallArrayG (smallArrayt *self, intmax_t index, bool value) { @@ -12163,11 +12679,9 @@ smallArrayt* setAtVoidSmallArrayG (smallArrayt *self, intmax_t index, void * if (!c) { return(NULL); } - self->f->setAtNFreeSmallContainer(self, index, c); - return(self); + return(self->f->setAtNFreeSmallContainer(self, index, c)); } - self->f->setAtUndefined(self,index); - return(self); + return(self->f->setAtUndefined(self,index)); } smallArrayt* setAtSmallBoolSmallArrayG (smallArrayt *self, intmax_t index, smallBoolt *value) { @@ -12525,13 +13039,9 @@ smallArrayt* injectSmallArrayG (smallArrayt *self, intmax_t index, baset return(self->f->inject(self, index, value)); } -smallArrayt* injectUndefinedSmallArrayG(smallArrayt *self, intmax_t index, void *value) { +smallArrayt* injectUndefinedSmallArrayG(smallArrayt *self, intmax_t index, void *value UNUSED) { - if (value) { - free(value); - } - self->f->injectUndefined(self,index); - return(self); + return(self->f->injectUndefined(self,index)); } smallArrayt* injectBoolSmallArrayG (smallArrayt *self, intmax_t index, bool value) { @@ -12586,11 +13096,9 @@ smallArrayt* injectVoidSmallArrayG (smallArrayt *self, intmax_t index, void if (!c) { return(NULL); } - self->f->injectNFreeSmallContainer(self, index, c); - return(self); + return(self->f->injectNFreeSmallContainer(self, index, c)); } - self->f->injectUndefined(self,index); - return(self); + return(self->f->injectUndefined(self,index)); } smallArrayt* injectSmallBoolSmallArrayG (smallArrayt *self, intmax_t index, smallBoolt *value) { @@ -12634,7 +13142,7 @@ smallArrayt* injectNFreeSmallArrayG (smallArrayt *self, intmax_t index, baset return(self->f->injectNFree(self,index,value)); } -smallArrayt* injectNFreeUndefinedSmallArrayG(smallArrayt *self, intmax_t index, void *value) { +smallArrayt* injectNFreeUndefinedSmallArrayG(smallArrayt *self, intmax_t index, undefinedt *value) { return(self->f->injectNFreeUndefined(self,index,value)); } @@ -13291,7 +13799,7 @@ smallArrayt* zipSmallJsonCharSmallArrayG(smallArrayt *self, smallJsont *array1, smallArrayt* zipSmallJsonCCharSmallArrayG(smallArrayt *self, smallJsont *array1, const char **array2) { - return(self->f->zipSmallJsonChar(self,array1,(char**)array2)); + return(self->f->zipSmallJsonCChar(self,array1,array2)); } smallArrayt* zipArraySmallArrayG(smallArrayt *self, char** array1, smallArrayt *array2) { @@ -13301,7 +13809,7 @@ smallArrayt* zipArraySmallArrayG(smallArrayt *self, char** array1, smallArrayt * smallArrayt* zipCArraySmallArrayG(smallArrayt *self, const char** array1, smallArrayt *array2) { - return(self->f->zipArray(self,(char**)array1,array2)); + return(self->f->zipCArray(self,array1,array2)); } smallArrayt* zipArraySmallJsonSmallArrayG(smallArrayt *self, char** array1, smallJsont *array2) { @@ -13311,7 +13819,7 @@ smallArrayt* zipArraySmallJsonSmallArrayG(smallArrayt *self, char** array1, smal smallArrayt* zipCArraySmallJsonSmallArrayG(smallArrayt *self, const char** array1, smallJsont *array2) { - return(self->f->zipArraySmallJson(self,(char**)array1,array2)); + return(self->f->zipCArraySmallJson(self,array1,array2)); } smallArrayt* zipCharSmallArrayG(smallArrayt *self, smallArrayt *array1, char** array2) { @@ -13321,7 +13829,7 @@ smallArrayt* zipCharSmallArrayG(smallArrayt *self, smallArrayt *array1, char** a smallArrayt* zipCCharSmallArrayG(smallArrayt *self, smallArrayt *array1, const char** array2) { - return(self->f->zipChar(self,array1,(char**)array2)); + return(self->f->zipCChar(self,array1,array2)); } smallArrayt* zipArrayCharSmallArrayG(smallArrayt *self, char** array1, char** array2) { @@ -13331,17 +13839,17 @@ smallArrayt* zipArrayCharSmallArrayG(smallArrayt *self, char** array1, char** ar smallArrayt* zipArrayCCharSmallArrayG(smallArrayt *self, char** array1, const char** array2) { - return(self->f->zipArrayChar(self,array1,(char**)array2)); + return(self->f->zipArrayCChar(self,array1,array2)); } smallArrayt* zipCArrayCharSmallArrayG(smallArrayt *self, const char** array1, char** array2) { - return(self->f->zipArrayChar(self,(char**)array1,array2)); + return(self->f->zipCArrayChar(self,array1,array2)); } smallArrayt* zipCArrayCCharSmallArrayG(smallArrayt *self, const char** array1, const char** array2) { - return(self->f->zipArrayChar(self,(char**)array1,(char**)array2)); + return(self->f->zipCArrayCChar(self,array1,array2)); } void logSmallArrayG(smallArrayt *self) { diff --git a/src/json/libsheepyCSmallArray.h b/src/json/libsheepyCSmallArray.h @@ -1228,10 +1228,17 @@ typedef smallArrayt* (*zipSmallJsonSmallArrayFt) (smallArrayt *self, s typedef smallArrayt* (*zipSmallJsonSmallArraySmallArrayFt)(smallArrayt *self, smallJsont *array1, smallArrayt *array2); typedef smallArrayt* (*zipSmallJsonSmallJsonSmallArrayFt) (smallArrayt *self, smallJsont *array1, smallJsont *array2); typedef smallArrayt* (*zipSmallJsonCharSmallArrayFt) (smallArrayt *self, smallJsont *array1, char **array2); +typedef smallArrayt* (*zipSmallJsonCCharSmallArrayFt) (smallArrayt *self, smallJsont *array1, const char **array2); typedef smallArrayt* (*zipArraySmallArrayFt) (smallArrayt *self, char** array1, smallArrayt *array2); +typedef smallArrayt* (*zipCArraySmallArrayFt) (smallArrayt *self, const char** array1, smallArrayt *array2); typedef smallArrayt* (*zipArraySmallJsonSmallArrayFt) (smallArrayt *self, char** array1, smallJsont *array2); +typedef smallArrayt* (*zipCArraySmallJsonSmallArrayFt) (smallArrayt *self, const char** array1, smallJsont *array2); typedef smallArrayt* (*zipCharSmallArrayFt) (smallArrayt *self, smallArrayt *array1, char** array2); +typedef smallArrayt* (*zipCCharSmallArrayFt) (smallArrayt *self, smallArrayt *array1, const char** array2); typedef smallArrayt* (*zipArrayCharSmallArrayFt) (smallArrayt *self, char** array1, char** array2); +typedef smallArrayt* (*zipCArrayCharSmallArrayFt) (smallArrayt *self, const char** array1, char** array2); +typedef smallArrayt* (*zipArrayCCharSmallArrayFt) (smallArrayt *self, char** array1, const char** array2); +typedef smallArrayt* (*zipCArrayCCharSmallArrayFt) (smallArrayt *self, const char** array1, const char** array2); /** * print list elements, one line per element @@ -1782,10 +1789,17 @@ typedef bool (*areAllEBytesSmallArrayFt) (smallArrayt *self); zipSmallJsonSmallArraySmallArrayFt zipSmallJsonSmallArray;\ zipSmallJsonSmallJsonSmallArrayFt zipSmallJsonSmallJson;\ zipSmallJsonCharSmallArrayFt zipSmallJsonChar;\ + zipSmallJsonCCharSmallArrayFt zipSmallJsonCChar;\ zipArraySmallArrayFt zipArray;\ + zipCArraySmallArrayFt zipCArray;\ zipArraySmallJsonSmallArrayFt zipArraySmallJson;\ + zipCArraySmallJsonSmallArrayFt zipCArraySmallJson;\ zipCharSmallArrayFt zipChar;\ + zipCCharSmallArrayFt zipCChar;\ zipArrayCharSmallArrayFt zipArrayChar;\ + zipCArrayCharSmallArrayFt zipCArrayChar;\ + zipArrayCCharSmallArrayFt zipArrayCChar;\ + zipCArrayCCharSmallArrayFt zipCArrayCChar;\ logSmallArrayFt log;\ readTextSmallArrayFt readText;\ readTextSmallJsonSmallArrayFt readTextSmallJson;\ @@ -2215,7 +2229,7 @@ smallArrayt* injectSmallJsonSmallArrayG (smallArrayt *self, intmax_t index, smallArrayt* injectSmallStringSmallArrayG (smallArrayt *self, intmax_t index, smallStringt *string); smallArrayt* injectSmallContainerSmallArrayG(smallArrayt *self, intmax_t index, smallContainert *container); smallArrayt* injectNFreeSmallArrayG (smallArrayt *self, intmax_t index, baset *value); -smallArrayt* injectNFreeUndefinedSmallArrayG(smallArrayt *self, intmax_t index, void *value); +smallArrayt* injectNFreeUndefinedSmallArrayG(smallArrayt *self, intmax_t index, undefinedt *value); smallArrayt* injectNFreeSSmallArrayG (smallArrayt *self, intmax_t index, char *string); smallArrayt* injectNFreeDictSmallArrayG (smallArrayt *self, intmax_t index, smallDictt *dict); smallArrayt* injectNFreeArraySmallArrayG (smallArrayt *self, intmax_t index, smallArrayt *array); diff --git a/src/json/libsheepyCSmallBool.c b/src/json/libsheepyCSmallBool.c @@ -937,6 +937,7 @@ internal int appendFileSmallStringSmallBool(smallBoolt *self, smallStringt *file if (!filePath) { return(0); } + // TODO check if filePath is really a smallString return(appendFileSmallBool(self, ssGet(filePath))); } diff --git a/src/json/libsheepyCSmallBytes.c b/src/json/libsheepyCSmallBytes.c @@ -234,7 +234,7 @@ void cleanUpSmallBytesFinishG(smallBytest **val) { internal void freeSmallBytes(smallBytest *self) { sFree((smallt *)self->B); - self->B = NULL;; + self->B = NULL; return; } @@ -460,6 +460,9 @@ internal smallBytest* readStreamSmallBytes(smallBytest *self, FILE *fp) { // readStatus == len when ok pushBufferSmallBytes(self, buffer, readStatus); + // pushBuffer copies the buffer, buffer is not used anymore, free it + free(buffer); + if (readStatus != (size_t)len) { pFuncError return(NULL); @@ -481,6 +484,11 @@ internal int writeFileSmallBytes(smallBytest *self, const char *filePath) { len = self->B->count; f = fopen(filePath, "w"); + if (!f) { + pFuncError + shEPrintfS("The path was: \"%s\"\n", filePath); + return(0); + } writeStatus = fwrite(sBytesGet(self->B), 1, len , f); fclose(f); @@ -559,6 +567,11 @@ internal int appendFileSmallBytes(smallBytest *self, const char *filePath) { len = self->B->count; f = fopen(filePath, "a"); + if (!f) { + pFuncError + shEPrintfS("The path was: \"%s\"\n", filePath); + return(0); + } writeStatus = fwrite(sBytesGet(self->B), 1, len , f); fclose(f); diff --git a/src/json/libsheepyCSmallDict.c b/src/json/libsheepyCSmallDict.c @@ -634,6 +634,7 @@ void initiateSmallDict(smallDictt *self) { self->d = NULL; self->iterIndex = -1; + self->iterKey = NULL; self->iterElement = NULL; self->iterElementDataType = 0; } @@ -878,12 +879,12 @@ void registerMethodsSmallDict(smallDictFunctionst *f) { f->mergeNSmashSmallJson = mergeNSmashSmallJsonSmallDict; f->append = appendSmallDict; f->appendNSmash = appendNSmashSmallDict; - f->equalBase = equalSmallDictBase;; - f->equalSmallJson = equalSmallDictSmallJson;; - f->equal = equalSmallDict;; - f->icEqualBase = icEqualSmallDictBase;; - f->icEqualSmallJson = icEqualSmallDictSmallJson;; - f->icEqual = icEqualSmallDict;; + f->equalBase = equalSmallDictBase; + f->equalSmallJson = equalSmallDictSmallJson; + f->equal = equalSmallDict; + f->icEqualBase = icEqualSmallDictBase; + f->icEqualSmallJson = icEqualSmallDictSmallJson; + f->icEqual = icEqualSmallDict; f->len = lenSmallDict; f->empty = emptySmallDict; f->isEmpty = isEmptySmallDict; @@ -1136,6 +1137,7 @@ internal void resetSmallDict(smallDictt *self) { if (self->iterElementDataType != SH_DT_BASET) { finishO(self->iterElement); } + self->iterKey = NULL; self->iterElement = NULL; self->iterIndex = -1; } @@ -1165,9 +1167,10 @@ internal smallDictt* mirrorSmallDict(smallDictt *self) { return(mirror); } - mirror->d = self->d; + mirror->d = self->d; - mirror->iterIndex = self->iterIndex; + mirror->iterElementDataType = self->iterElementDataType; + mirror->iterIndex = self->iterIndex; if (mirror->iterIndex != -1) { mirror->iterKey = (&((mirror->d)->elements) + mirror->iterIndex)->key; } @@ -1341,8 +1344,10 @@ internal smallDictt* setSmallBoolSmallDict(smallDictt *self, const char *key, sm return(NULL); } - smallt *o = (smallt *) value->value; - sDictSetTiny(&(self->d), key, o); + if (!value->value) { + isError(value->value, allocSBool(false)) return(NULL); + } + sDictSetTiny(&(self->d), key, (smallt *) value->value); return(self); } @@ -1357,12 +1362,9 @@ internal smallDictt* setSmallBytesSmallDict(smallDictt *self, const char *key, s } if (!value->B) { - sDictSetTiny(&(self->d), key, (smallt *)allocSUndefined()); - } - else { - smallt *o = (smallt *) value->B; - sDictSetTiny(&(self->d), key, o); + isError(value->B, allocSBytes()) return(NULL); } + sDictSetTiny(&(self->d), key, (smallt *) value->B); return(self); } @@ -1376,8 +1378,10 @@ internal smallDictt* setSmallDoubleSmallDict(smallDictt *self, const char *key, return(NULL); } - smallt *o = (smallt *) value->value; - sDictSetTiny(&(self->d), key, o); + if (!value->value) { + isError(value->value, allocSDouble(0)) return(NULL); + } + sDictSetTiny(&(self->d), key, (smallt *) value->value); return(self); } @@ -1391,8 +1395,10 @@ internal smallDictt* setSmallIntSmallDict(smallDictt *self, const char *key, sma return(NULL); } - smallt *o = (smallt *) value->value; - sDictSetTiny(&(self->d), key, o); + if (!value->value) { + isError(value->value, allocSInt(0)) return(NULL); + } + sDictSetTiny(&(self->d), key, (smallt *) value->value); return(self); } @@ -1407,6 +1413,12 @@ internal smallDictt* setSmallJsonSmallDict(smallDictt *self, const char *key, sm } smallt *o = getsoO(value); + if (!o) { + // smallJson is empty, create an empty dict + isError(o, (smallt*)allocSDict()) return(NULL); + setsoO(value, o); + } + sDictSetTiny(&(self->d), key, o); return(self); } @@ -1421,23 +1433,15 @@ internal smallDictt* setSmallStringSmallDict(smallDictt *self, const char *key, return(NULL); } - smallt *o; if (!string->data) { // set empty string when NULL - o = (smallt *) allocSStringTiny(""); - if (!o) { - return(NULL); - } - } - else { - o = (smallt *) string->data; + isError(string->data, allocSStringTiny("")) return(NULL); } - sDictSetTiny(&(self->d), key, o); + sDictSetTiny(&(self->d), key, (smallt *) string->data); return(self); } internal smallDictt* setSmallContainerSmallDict(smallDictt *self, const char *key, smallContainert *container) { - smallt *o = NULL; if (!key || !container) { return(NULL); @@ -1448,15 +1452,9 @@ internal smallDictt* setSmallContainerSmallDict(smallDictt *self, const char *ke } if (!container->data) { - o = (smallt *) allocSContainer(NULL); - if (!o) { - return(NULL); + isError(container->data, allocSContainer(NULL)) return(NULL); } - } - else { - o = (smallt *) container->data; - } - sDictSetTiny(&(self->d), key, o); + sDictSetTiny(&(self->d), key, (smallt *) container->data); return(self); } @@ -1584,6 +1582,9 @@ internal smallDictt* setNFreeSmallDict(smallDictt *self, const char *key, baset internal smallDictt* setNFreeUndefinedSmallDict(smallDictt *self, const char *key, undefinedt *undefined) { + if (!undefined) { + return(NULL); + } smallDictt *r = setUndefinedSmallDict(self, key); if (r) { terminateO(undefined); @@ -1593,11 +1594,11 @@ internal smallDictt* setNFreeUndefinedSmallDict(smallDictt *self, const char *ke internal smallDictt* setNFreeSSmallDict(smallDictt *self, const char *key, char *string) { - smallDictt *r = setSSmallDict(self, key, string);; + smallDictt *r = setSSmallDict(self, key, string); if (r) { free(string); } - return(self); + return(r); } internal smallDictt* setNFreeDictSmallDict(smallDictt *self, const char *key, smallDictt *dict) { @@ -1620,11 +1621,11 @@ internal smallDictt* setNFreeArraySmallDict(smallDictt *self, const char *key, s internal smallDictt* setNFreeArraycSmallDict(smallDictt *self, const char *key, char **array) { - smallDictt *r = setArraycSmallDict(self, key, array);; + smallDictt *r = setArraycSmallDict(self, key, array); if (r) { listFreeS(array); } - return(self); + return(r); } internal smallDictt* setNFreeSmallBoolSmallDict(smallDictt *self, const char *key, smallBoolt *value) { @@ -1780,7 +1781,7 @@ internal smallDictt* setPDictSmallDict(smallDictt *self, const char *key, smallD } if (!dict->d) { - isError(dict->d, allocSDict()) return(NULL); + return(NULL); } sDictSetP(&(self->d), key, (smallt *)dict->d); @@ -1798,8 +1799,7 @@ internal smallDictt* setPArraySmallDict(smallDictt *self, const char *key, small } if (!array->a) { - // allocate empty array - isError(array->a, allocSArray()) return(NULL); + return(NULL); } sDictSetP(&(self->d), key, (smallt *)array->a); @@ -1812,19 +1812,15 @@ internal smallDictt* setPSmallJsonSmallDict(smallDictt *self, const char *key, s return(NULL); } - if (checkObjectTypes && json && !isOSmallString(json)) { + if (checkObjectTypes && json && !isOSmallJson(json)) { return(NULL); } smallt *o; o = getsoO(json); if (!o) { - // set empty string when NULL - o = (smallt *) allocSUndefined(); - if (!o) { - return(NULL); + return(NULL); } - } sDictSetP(&(self->d), key, (smallt *)o); return(self); } @@ -1839,18 +1835,10 @@ internal smallDictt* setPSmallStringSmallDict(smallDictt *self, const char *key, return(NULL); } - smallt *o; if (!string->data) { - // set empty string when NULL - o = (smallt *) allocSStringTiny(""); - if (!o) { - return(NULL); - } - } - else { - o = (smallt *) string->data; + return(NULL); } - sDictSetP(&(self->d), key, (smallt *)o); + sDictSetP(&(self->d), key, (smallt *)string->data); return(self); } @@ -1865,7 +1853,7 @@ internal smallDictt* setNFreePDictSmallDict(smallDictt *self, const char *key, s } if (!dict->d) { - isError(dict->d, allocSDict()) return(NULL); + return(NULL); } sDictSetP(&(self->d), key, (smallt *)dict->d); @@ -1884,8 +1872,7 @@ internal smallDictt* setNFreePArraySmallDict(smallDictt *self, const char *key, } if (!array->a) { - // allocate empty array - isError(array->a, allocSArray()) return(NULL); + return(NULL); } sDictSetP(&(self->d), key, (smallt *)array->a); @@ -1899,13 +1886,13 @@ internal smallDictt* setNFreePSmallJsonSmallDict(smallDictt *self, const char *k return(NULL); } - if (checkObjectTypes && json && !isOSmallString(json)) { + if (checkObjectTypes && json && !isOSmallJson(json)) { return(NULL); } smallt *o = getsoO(json); if (!o) { - isError(o, (smallt *)allocSUndefined()) return(NULL); + return(NULL); } sDictSetP(&(self->d), key, o); @@ -1924,7 +1911,7 @@ internal smallDictt* setNFreePSmallStringSmallDict(smallDictt *self, const char } if (!string->data) { - isError(string->data, allocSStringTiny("")) return(NULL); + return(NULL); } sDictSetP(&(self->d), key, (smallt *)string->data); @@ -2513,9 +2500,6 @@ internal baset* getNDupSmallDict(smallDictt *self, const char *key) { return(NULL); } - if (!o) { - return(NULL); - } if (o->type == CONTAINER && (((sContainert*)o)->dataType == SH_DT_BASET)) { baset *b = ((sContainert*)o)->data; return(duplicateO(b)); @@ -2783,7 +2767,7 @@ internal void* getNDupVoidSmallDict(smallDictt *self, const char *key) { e = getNDupSmallContainerSmallDict(self, key); if (e) { r = e->f->get(e); - finishO(e); + smashO(e); } return(r); } @@ -3005,13 +2989,14 @@ internal undefinedt* cropElemUndefinedSmallDict(smallDictt *self, char* key) { forEachSDict(self->d, e) { if (e->key && strEq(key, e->key)) { sm = e->data; - r = (undefinedt*)toBaset(sm); - if (checkObjectTypes && r && !isOUndefined(r)) { - finishO(r); + if (sm->type != UNDEFINED) { return(NULL); } + r = (undefinedt*)toBaset(sm); free(e->key); - e->key = NULL; + free(e->data); + e->key = NULL; + e->data = NULL; break; } } @@ -3035,6 +3020,9 @@ internal bool cropElemBoolSmallDict(smallDictt *self, char* key) { return(false); } if (checkObjectTypes && o && !isOSmallBool(o)) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(false); + } finishO(o); return(false); } @@ -3042,7 +3030,7 @@ internal bool cropElemBoolSmallDict(smallDictt *self, char* key) { e->key = NULL; bool r = o->f->get(o); - finishO(o); + terminateO(o); return(r); } } @@ -3066,6 +3054,9 @@ internal double cropElemDoubleSmallDict(smallDictt *self, char* key) { return(0); } if (checkObjectTypes && o && !isOSmallDouble(o)) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(0); + } finishO(o); return(0); } @@ -3073,7 +3064,7 @@ internal double cropElemDoubleSmallDict(smallDictt *self, char* key) { e->key = NULL; double r = o->f->get(o); - finishO(o); + terminateO(o); return(r); } } @@ -3097,6 +3088,9 @@ internal int64_t cropElemIntSmallDict(smallDictt *self, char* key) { return(0); } if (checkObjectTypes && o && !isOSmallInt(o)) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(0); + } finishO(o); return(0); } @@ -3104,7 +3098,7 @@ internal int64_t cropElemIntSmallDict(smallDictt *self, char* key) { e->key = NULL; int64_t r = o->f->get(o); - finishO(o); + terminateO(o); return(r); } } @@ -3128,6 +3122,9 @@ internal int32_t cropElemInt32SmallDict(smallDictt *self, char* key) { return(0); } if (checkObjectTypes && o && !isOSmallInt(o)) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(0); + } finishO(o); return(0); } @@ -3135,7 +3132,7 @@ internal int32_t cropElemInt32SmallDict(smallDictt *self, char* key) { e->key = NULL; int32_t r = o->f->get(o); - finishO(o); + terminateO(o); return(r); } } @@ -3159,6 +3156,9 @@ internal uint64_t cropElemUintSmallDict(smallDictt *self, char* key) { return(0); } if (checkObjectTypes && o && !isOSmallInt(o)) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(0); + } finishO(o); return(0); } @@ -3166,7 +3166,7 @@ internal uint64_t cropElemUintSmallDict(smallDictt *self, char* key) { e->key = NULL; uint64_t r = o->f->get(o); - finishO(o); + terminateO(o); return(r); } } @@ -3190,6 +3190,9 @@ internal uint32_t cropElemUint32SmallDict(smallDictt *self, char* key) { return(0); } if (checkObjectTypes && o && !isOSmallInt(o)) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(0); + } finishO(o); return(0); } @@ -3197,7 +3200,7 @@ internal uint32_t cropElemUint32SmallDict(smallDictt *self, char* key) { e->key = NULL; uint32_t r = o->f->get(o); - finishO(o); + terminateO(o); return(r); } } @@ -3221,14 +3224,17 @@ internal char* cropElemSSmallDict(smallDictt *self, char* key) { return(NULL); } if (checkObjectTypes && o && !isOSmallString(o)) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(NULL); + } finishO(o); return(NULL); } free(e->key); e->key = NULL; - char *r = o->f->get(o); - finishO(o); + char *r = dupS(o->f->get(o)); + terminateO(o); return(r); } } @@ -3249,6 +3255,9 @@ internal smallDictt* cropElemDictSmallDict(smallDictt *self, char* key) { sm = e->data; r = (smallDictt*)toBaset(sm); if (checkObjectTypes && r && !isOSmallDict(r)) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(NULL); + } finishO(r); return(NULL); } @@ -3274,6 +3283,9 @@ internal smallArrayt* cropElemArraySmallDict(smallDictt *self, char* key) { sm = e->data; r = (smallArrayt*)toBaset(sm); if (checkObjectTypes && r && !isOSmallArray(r)) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(NULL); + } finishO(r); return(NULL); } @@ -3299,6 +3311,9 @@ internal smallBoolt* cropElemSmallBoolSmallDict(smallDictt *self, char* key) { sm = e->data; r = (smallBoolt*)toBaset(sm); if (checkObjectTypes && r && !isOSmallBool(r)) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(NULL); + } finishO(r); return(NULL); } @@ -3324,6 +3339,9 @@ internal smallBytest* cropElemSmallBytesSmallDict(smallDictt *self, char* key) { sm = e->data; r = (smallBytest*)toBaset(sm); if (checkObjectTypes && r && !isOSmallBytes(r)) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(NULL); + } finishO(r); return(NULL); } @@ -3349,6 +3367,9 @@ internal smallDoublet* cropElemSmallDoubleSmallDict(smallDictt *self, char* key) sm = e->data; r = (smallDoublet*)toBaset(sm); if (checkObjectTypes && r && !isOSmallDouble(r)) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(NULL); + } finishO(r); return(NULL); } @@ -3374,6 +3395,9 @@ internal smallIntt* cropElemSmallIntSmallDict(smallDictt *self, char* key) { sm = e->data; r = (smallIntt*)toBaset(sm); if (checkObjectTypes && r && !isOSmallInt(r)) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(NULL); + } finishO(r); return(NULL); } @@ -3396,6 +3420,9 @@ internal smallJsont* cropElemSmallJsonSmallDict(smallDictt *self, char* key) { forEachSDict(self->d, e) { if (e->key && strEq(key, e->key)) { sm = e->data; + if ((sm->type == BYTES) || (sm->type == CONTAINER)) { + return(NULL); + } baset *o = toBaset(sm); if (!o) { return(NULL); @@ -3428,6 +3455,9 @@ internal smallStringt* cropElemSmallStringSmallDict(smallDictt *self, char* key) sm = e->data; r = (smallStringt*)toBaset(sm); if (checkObjectTypes && r && !isOSmallString(r)) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(NULL); + } finishO(r); return(NULL); } @@ -3451,6 +3481,9 @@ internal void* cropElemVoidSmallDict(smallDictt *self, char* key) { forEachSDict(self->d, e) { if (e->key && strEq(key, e->key)) { sm = e->data; + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(NULL); + } o = (smallContainert*)toBaset(sm); if (!o) { return(NULL); @@ -3463,7 +3496,7 @@ internal void* cropElemVoidSmallDict(smallDictt *self, char* key) { e->key = NULL; void *r = o->f->get(o); - finishO(o); + terminateO(o); return(r); } } @@ -3482,6 +3515,9 @@ internal smallContainert* cropElemSmallContainerSmallDict(smallDictt *self, char forEachSDict(self->d, e) { if (e->key && strEq(key, e->key)) { sm = e->data; + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + return(NULL); + } r = (smallContainert*)toBaset(sm); if (checkObjectTypes && r && !isOSmallContainer(r)) { finishO(r); @@ -3499,9 +3535,11 @@ internal smallContainert* cropElemSmallContainerSmallDict(smallDictt *self, char internal smallDictt* delSmallDict(smallDictt *self, const char *key) { if (key && self->d) { + // TODO report error (null) when key is not found sDictDelTiny(self->d, key); + return(self); } - return(self); + return(NULL); } internal smallDictt* delKCharSmallDict(smallDictt *self, char key) { @@ -3515,12 +3553,14 @@ internal smallDictt* removeSmallDict(smallDictt *self, const char *key) { if (key && self->d) { forEachSDict(self->d, e) { if (e->key && eqS(key, e->key)) { + free(e->key); e->key = NULL; break; - } } } - return(self); + return(self); + } + return(NULL); } internal smallDictt* removeKCharSmallDict(smallDictt *self, char key) { @@ -3539,7 +3579,7 @@ internal bool hasSmallDict(smallDictt *self, const char *key) { return(false); } - smallt *o = sDictGetTiny(self->d, key);; + smallt *o = sDictGetTiny(self->d, key); if (!o) { return(false); @@ -3682,7 +3722,7 @@ internal char* keyByDictSmallDict(smallDictt *self, smallDictt *dict) { // search object in elements char *s = toStringO(dict); - char *es = NULL;; + char *es = NULL; forEachSDict(self->d, e) { if (e->key && e->data->type == DICT) { @@ -3712,7 +3752,7 @@ internal char* keyByArraySmallDict(smallDictt *self, smallArrayt *array) { // search object in elements char *s = toStringO(array); - char *es = NULL;; + char *es = NULL; forEachSDict(self->d, e) { if (e->key && e->data->type == ARRAY) { @@ -3738,9 +3778,7 @@ internal char* keyByArraycSmallDict(smallDictt *self, char **array) { // search object in elements char *s = toStringListSGF(array); - iPrependCharS(&s, '['); - iAppendCharS(&s, ']'); - char *es = NULL;; + char *es = NULL; forEachSDict(self->d, e) { if (e->key && e->data->type == ARRAY) { @@ -3932,7 +3970,7 @@ internal char* keyBySmallStringSmallDict(smallDictt *self, smallStringt *string) internal char* keyBySmallContainerSmallDict(smallDictt *self, smallContainert *container) { - if (!self || !container) { + if (!lenSmallDict(self) || !container) { return(NULL); } @@ -4003,7 +4041,7 @@ internal char* icKeyByDictSmallDict(smallDictt *self, smallDictt *dict) { // search object in elements char *s = toStringO(dict); - char *es = NULL;; + char *es = NULL; forEachSDict(self->d, e) { if (e->key && e->data->type == DICT) { @@ -4033,7 +4071,7 @@ internal char* icKeyByArraySmallDict(smallDictt *self, smallArrayt *array) { // search object in elements char *s = toStringO(array); - char *es = NULL;; + char *es = NULL; forEachSDict(self->d, e) { if (e->key && e->data->type == ARRAY) { @@ -4059,9 +4097,7 @@ internal char* icKeyByArraycSmallDict(smallDictt *self, char **array) { // search object in elements char *s = toStringListSGF(array); - iPrependCharS(&s, '['); - iAppendCharS(&s, ']'); - char *es = NULL;; + char *es = NULL; forEachSDict(self->d, e) { if (e->key && e->data->type == ARRAY) { @@ -4175,6 +4211,7 @@ internal smallDictt* trimSmallDict(smallDictt *self) { forEachSDict(self->d, e) { if (e->key) { sDictSetTiny(&d, e->key, e->data); + free(e->key); } } @@ -4376,7 +4413,7 @@ internal bool equalSmallDictSmallJson(smallDictt* self, smallJsont* p2) { internal bool equalSmallDict(smallDictt* self, smallDictt* p2) { - if (!self->d || !p2 || !p2->d) { + if (!self->d || !p2) { return(false); } @@ -4384,6 +4421,16 @@ internal bool equalSmallDict(smallDictt* self, smallDictt* p2) { return(false); } + if (!p2->d) { + return(false); + } + + if (lenSmallDict(self) != lenSmallDict(p2)) { + // different key counts in self and p2 + return(false); + } + + //TODO - sort keys, use binary search forEachSDict(self->d, e) { if (e->key) { @@ -4445,7 +4492,7 @@ internal bool icEqualSmallDictSmallJson(smallDictt* self, smallJsont* p2) { internal bool icEqualSmallDict(smallDictt* self, smallDictt* p2) { - if (!self->d || !p2 || !p2->d) { + if (!self->d || !p2) { return(false); } @@ -4453,6 +4500,16 @@ internal bool icEqualSmallDict(smallDictt* self, smallDictt* p2) { return(false); } + if (!p2->d) { + return(false); + } + + if (lenSmallDict(self) != lenSmallDict(p2)) { + // different key counts in self and p2 + return(false); + } + + //TODO - sort keys, use binary search forEachSDict(self->d, e) { if (e->key) { @@ -4519,11 +4576,16 @@ internal bool isEmptySmallDict(smallDictt *self) { } } + // should not be reached, make sure a value is returned return(true); } internal void enumerateSmallDictF(smallDictt *self, void *closure, enumerateElementSmallDictFt funcElem) { + if (!lenSmallDict(self)) { + return; + } + forEachSDict(self->d, e) { if (e->key) { baset *E = toBaset(e->data); @@ -4553,14 +4615,10 @@ internal baset* iterStartSmallDict(smallDictt *self) { // get first element range(i, self->d->count) - if (&((self->d)->elements) + i) { + if ((&((self->d)->elements) + i)->key) { 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); @@ -4588,14 +4646,10 @@ internal const char* iterStartKeySmallDict(smallDictt *self) { // get first element range(i, self->d->count) - if (&((self->d)->elements) + i) { + if ((&((self->d)->elements) + i)->key) { 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); @@ -4625,11 +4679,11 @@ internal baset* iterNextSmallDict(smallDictt *self) { finishO(self->iterElement); } rangeFrom(i, self->iterIndex, self->d->count) { - if (&((self->d)->elements) + i) { + if ((&((self->d)->elements) + i)->key) { self->iterIndex = i; break; } - if (!(&((self->d)->elements) + i) && (i == (self->d->count -1))) { + if (!(&((self->d)->elements) + i)->key && (i == (self->d->count -1))) { // last element is null, finish iteration self->iterIndex = self->d->count; } @@ -4664,11 +4718,11 @@ internal const char* iterNextKeySmallDict(smallDictt *self) { finishO(self->iterElement); } rangeFrom(i, self->iterIndex, self->d->count) { - if (&((self->d)->elements) + i) { + if ((&((self->d)->elements) + i)->key) { self->iterIndex = i; break; } - if (!(&((self->d)->elements) + i) && (i == (self->d->count -1))) { + if (!(&((self->d)->elements) + i)->key && (i == (self->d->count -1))) { // last element is null, finish iteration self->iterIndex = self->d->count; } @@ -4716,14 +4770,11 @@ internal smallDictt* zipSmallDict(smallDictt *self, smallArrayt *keys, smallArra return(self); } - len = MIN(keys->f->len(keys), values->f->len(values));; + len = MIN(keys->f->len(keys), values->f->len(values)); count = 0; forEachSmallArray(keys, K) { - if (not K) { - return(NULL); - } if (!isOSmallString(K)) { finishO(K); return(NULL); @@ -4863,7 +4914,7 @@ internal smallDictt* zipArraySmallDict(smallDictt *self, char** keys, smallArray return(self); } - len = MIN(listLengthS(keys), values->f->len(values));; + len = MIN(listLengthS(keys), values->f->len(values)); count = 0; @@ -4961,9 +5012,6 @@ internal smallDictt* zipVArraySmallDict(smallDictt *self, smallArrayt *keys, cha smallt *e; forEachSmallArray(keys, K) { - if (not K) { - return(NULL); - } if (!isOSmallString(K)) { finishO(K); return(NULL); @@ -4997,8 +5045,8 @@ internal smallDictt* fromArraySmallDict(smallDictt *self, smallArrayt *items) { return(NULL); } - forEachSmallArray(items, E) { - if (not E or !isOType(E, "smallArray")) { + enumerateSmallArray(items, E, i) { + if (!isOType(E, "smallArray")) { goto cont; } cast(smallArrayt*, a, E); @@ -5012,10 +5060,12 @@ internal smallDictt* fromArraySmallDict(smallDictt *self, smallArrayt *items) { } setNFreeSmallDict(self, ssGet(s), a->f->get(a, 1)); finishO(s); + removeElemO(a, 1); + delElemO(items, i); cont: finishO(E); } - return(NULL); + return(self); } internal smallArrayt* toArraySmallDict(smallDictt *self) { @@ -5159,7 +5209,11 @@ internal const char* typeStringSmallDict(smallDictt *self, const char *key) { internal smallStringt* typeSmallStringSmallDict(smallDictt *self, const char *key) { - return(allocSmallString(typeStringSmallDict(self, key))); + const char *r = typeStringSmallDict(self, key); + if (!r) { + return(NULL); + } + return(allocSmallString(r)); } internal const char* typeStringKCharSmallDict(smallDictt *self, char key) { @@ -5181,7 +5235,7 @@ internal char typeSmallDict(smallDictt *self, const char *key) { return(0); } - smallt *o = sDictGet(self->d, key);; + smallt *o = sDictGet(self->d, key); if (!o) { return(0); @@ -5233,7 +5287,7 @@ internal bool isETypeSmallDict(smallDictt *self, const char *key, const char *ty return(false); } - smallt *o = sDictGet(self->d, key);; + smallt *o = sDictGet(self->d, key); if (!o) { return(false); @@ -5252,7 +5306,7 @@ internal bool isEUndefinedSmallDict(smallDictt *self, const char *key) { return(false); } - smallt *o = sDictGet(self->d, key);; + smallt *o = sDictGet(self->d, key); if (!o) { return(false); @@ -5271,7 +5325,7 @@ internal bool isEBoolSmallDict(smallDictt *self, const char *key) { return(false); } - smallt *o = sDictGet(self->d, key);; + smallt *o = sDictGet(self->d, key); if (!o) { return(false); @@ -5290,7 +5344,7 @@ internal bool isEContainerSmallDict(smallDictt *self, const char *key) { return(false); } - smallt *o = sDictGet(self->d, key);; + smallt *o = sDictGet(self->d, key); if (!o) { return(false); @@ -5309,7 +5363,7 @@ internal bool isEDictSmallDict(smallDictt *self, const char *key) { return(false); } - smallt *o = sDictGet(self->d, key);; + smallt *o = sDictGet(self->d, key); if (!o) { return(false); @@ -5328,7 +5382,7 @@ internal bool isEDoubleSmallDict(smallDictt *self, const char *key) { return(false); } - smallt *o = sDictGet(self->d, key);; + smallt *o = sDictGet(self->d, key); if (!o) { return(false); @@ -5347,7 +5401,7 @@ internal bool isEIntSmallDict(smallDictt *self, const char *key) { return(false); } - smallt *o = sDictGet(self->d, key);; + smallt *o = sDictGet(self->d, key); if (!o) { return(false); @@ -5366,7 +5420,7 @@ internal bool isEStringSmallDict(smallDictt *self, const char *key) { return(false); } - smallt *o = sDictGet(self->d, key);; + smallt *o = sDictGet(self->d, key); if (!o) { return(false); @@ -5385,7 +5439,7 @@ internal bool isEFaststringSmallDict(smallDictt *self, const char *key) { return(false); } - smallt *o = sDictGet(self->d, key);; + smallt *o = sDictGet(self->d, key); if (!o) { return(false); @@ -5404,7 +5458,7 @@ internal bool isEArraySmallDict(smallDictt *self, const char *key) { return(false); } - smallt *o = sDictGet(self->d, key);; + smallt *o = sDictGet(self->d, key); if (!o) { return(false); @@ -5423,7 +5477,7 @@ internal bool isEBytesSmallDict(smallDictt *self, const char *key) { return(false); } - smallt *o = sDictGet(self->d, key);; + smallt *o = sDictGet(self->d, key); if (!o) { return(false); @@ -6042,17 +6096,18 @@ smallDictt* setCArraycSmallDictG (smallDictt *self, const char *key, const char } smallDictt* setVoidSmallDictG (smallDictt *self, const char *key, void *value) { + smallDictt *r = NULL; if (key && value) { smallContainert *c = allocSmallContainer(value); if (!c) { return(NULL); } - self->f->setNFreeSmallContainer(self, key, c); - return(self); + r = self->f->setNFreeSmallContainer(self, key, c);; + return(r); } - self->f->setUndefined(self,key); - return(self); + r = self->f->setUndefined(self,key);; + return(r); } smallDictt* setSmallBoolSmallDictG (smallDictt *self, const char *key, smallBoolt *value) { diff --git a/src/json/libsheepyCSmallDouble.c b/src/json/libsheepyCSmallDouble.c @@ -514,10 +514,10 @@ internal smallDoublet* setSmallJsonSmallDouble(smallDoublet* self, smallJsont* p else if (eqS(type, "string")) { return(setSSmallDouble(self, getTopSO(p2))); } - - return(self); + return(NULL); } + internal smallDoublet* setSmallStringSmallDouble(smallDoublet* self, smallStringt* p2) { if (!p2 || !isNumberO(p2)) { @@ -682,7 +682,7 @@ internal bool equalSmallDoubleSmallBytes(smallDoublet* self, smallBytest* p2) { return(false); } - if (!isInt(s)) { + if (isInt(s) || !isNumber(s)) { return(false); } diff --git a/src/json/libsheepyCSmallInt.c b/src/json/libsheepyCSmallInt.c @@ -521,7 +521,7 @@ internal smallIntt* setSmallJsonSmallInt(smallIntt* self, smallJsont* p2) { return(setSSmallInt(self, getTopSO(p2))); } - return(self); + return(NULL); } internal smallIntt* setSmallStringSmallInt(smallIntt* self, smallStringt* p2) { diff --git a/src/json/libsheepyCSmallJson.c b/src/json/libsheepyCSmallJson.c @@ -331,7 +331,7 @@ internal smallJsont* appendNSmashArraySmallJson(smallJsont *self, char **array); internal smallJsont* shiftSmallJson(smallJsont *self, smallArrayt *array); internal smallJsont* shiftNSmashSmallJson(smallJsont *self, smallArrayt *array); internal smallJsont* shiftSmallJsonSmallJson(smallJsont *self, smallJsont *array); -internal smallJsont* shiftSmallJsonNSmashSmallJson(smallJsont *self, smallJsont *array); +internal smallJsont* shiftNSmashSmallJsonSmallJson(smallJsont *self, smallJsont *array); internal smallJsont* addSmallJson(smallJsont *self, smallArrayt *array); internal smallJsont* addJsonSmallJson(smallJsont *self, smallJsont *array); internal smallJsont* sliceSmallJson(smallJsont *self, intmax_t start, intmax_t end); @@ -380,8 +380,8 @@ internal smallContainert* cropElemKeySmallContainerSmallJson(smallJsont *self, c internal smallJsont* copySmallJson(smallJsont *self, intmax_t start, intmax_t end); internal smallJsont* insertSmallJson(smallJsont *self, intmax_t index, smallArrayt *toInsert); internal smallJsont* insertNSmashSmallJson(smallJsont *self, intmax_t index, smallArrayt *toInsert); -internal smallJsont* insertJsonSmallJson(smallJsont *self, intmax_t index, smallJsont *toInsert); -internal smallJsont* insertJsonNSmashSmallJson(smallJsont *self, intmax_t index, smallJsont *toInsert); +internal smallJsont* insertSmallJsonSmallJson(smallJsont *self, intmax_t index, smallJsont *toInsert); +internal smallJsont* insertNSmashSmallJsonSmallJson(smallJsont *self, intmax_t index, smallJsont *toInsert); internal smallJsont* insertStringSmallJson(smallJsont *self, intmax_t index, smallStringt *toInsert); internal smallJsont* insertSSmallJson(smallJsont *self, intmax_t index, const char *toInsert); internal smallJsont* insertNFreeStringSmallJson(smallJsont *self, intmax_t index, smallStringt *toInsert); @@ -473,7 +473,7 @@ internal ssize_t indexOfSmallJsonSmallJson(smallJsont *self, smallJsont *value); internal ssize_t indexOfSmallStringSmallJson(smallJsont *self, smallStringt *string); internal ssize_t indexOfSmallContainerSmallJson(smallJsont *self UNUSED, smallContainert *container UNUSED); internal ssize_t binarySearchSmallJson(smallJsont *self, baset *value); -internal ssize_t binarySearchUndefinedSmallJson(smallJsont *self, undefinedt *undefined); +internal ssize_t binarySearchUndefinedSmallJson(smallJsont *self UNUSED, undefinedt *undefined UNUSED); internal ssize_t binarySearchBoolSmallJson(smallJsont *self, bool value); internal ssize_t binarySearchDoubleSmallJson(smallJsont *self, double value); internal ssize_t binarySearchIntSmallJson(smallJsont *self, int64_t value); @@ -708,13 +708,20 @@ internal smallJsont* colorSmallJson(smallJsont *self, const char *colr); internal char* colordSmallJson(smallJsont *self, const char *color); internal smallJsont* zipSmallJson(smallJsont *self, smallArrayt *array1, smallArrayt *array2); internal smallJsont* zipArraySmallJson(smallJsont *self, char** array1, smallArrayt *array2); +internal smallJsont* zipCArraySmallJson(smallJsont *self, const char** array1, smallArrayt *array2); internal smallJsont* zipCharSmallJson(smallJsont *self, smallArrayt *array1, char** array2); +internal smallJsont* zipCCharSmallJson(smallJsont *self, smallArrayt *array1, const char** array2); internal smallJsont* zipArrayCharSmallJson(smallJsont *self, char** array1, char** array2); +internal smallJsont* zipCArrayCharSmallJson(smallJsont *self, const char** array1, char** array2); +internal smallJsont* zipArrayCCharSmallJson(smallJsont *self, char** array1, const char** array2); +internal smallJsont* zipCArrayCCharSmallJson(smallJsont *self, const char** array1, const char** array2); smallJsont* zipJsonSmallJson(smallJsont *self, smallJsont *array1, smallJsont *array2); smallJsont* zipJsonSmallArraySmallJson(smallJsont *self, smallJsont *array1, smallArrayt *array2); smallJsont* zipJsonArraySmallJson(smallJsont *self, smallJsont *array1, char** array2); +smallJsont* zipJsonCArraySmallJson(smallJsont *self, smallJsont *array1, const char** array2); smallJsont* zipSmallArrayJsonSmallJson(smallJsont *self, smallArrayt *array1, smallJsont *array2); smallJsont* zipArrayJsonSmallJson(smallJsont *self, char** array1, smallJsont *array2); +smallJsont* zipCArrayJsonSmallJson(smallJsont *self, const char** array1, smallJsont *array2); internal baset* iterStartSmallJson(smallJsont *self); internal const char* iterStartKeySmallJson(smallJsont *self); internal baset* iterStartLastSmallJson(smallJsont *self); @@ -1025,7 +1032,7 @@ smallStringt* popSmallStringSmallJsonG (smallJsont *self, smallStringt* ret void* popVoidSmallJsonG (smallJsont *self, void* retType UNUSED); smallContainert* popSmallContainerSmallJsonG(smallJsont *self, smallContainert* retType UNUSED); smallJsont* setSmallJsonG (smallJsont *self, const char *key, baset *value); -smallJsont* setUndefinedSmallJsonG(smallJsont *self, const char *key, void *value); +smallJsont* setUndefinedSmallJsonG(smallJsont *self, const char *key, void *value UNUSED); smallJsont* setBoolSmallJsonG (smallJsont *self, const char *key, bool value); smallJsont* setDoubleSmallJsonG (smallJsont *self, const char *key, double value); smallJsont* setIntSmallJsonG (smallJsont *self, const char *key, int64_t value); @@ -1262,7 +1269,7 @@ smallJsont* appendCArraySmallJsonG (smallJsont *self, const char **array) smallJsont* shiftSmallJsonG (smallJsont *self, smallArrayt *array); smallJsont* shiftNSmashSmallJsonG (smallJsont *self, smallArrayt *array); smallJsont* shiftSmallJsonSmallJsonG(smallJsont *self, smallJsont *array); -smallJsont* shiftSmallJsonNSmashSmallJsonG(smallJsont *self, smallJsont *array); +smallJsont* shiftNSmashSmallJsonSmallJsonG(smallJsont *self, smallJsont *array); smallJsont* addSmallJsonG (smallJsont *self, smallArrayt *array); smallJsont* addJsonSmallJsonG (smallJsont *self, smallJsont *array); smallJsont* sliceSmallJsonG(smallJsont *self, intmax_t start, intmax_t end); @@ -1311,14 +1318,14 @@ smallContainert* cropElemKeySmallContainerSmallJsonG(smallJsont *self, const cha smallJsont* copySmallJsonG (smallJsont *self, intmax_t start, intmax_t end); smallJsont* insertSmallJsonG(smallJsont *self, intmax_t index, smallArrayt *toInsert); smallJsont* insertNSmashSmallJsonG(smallJsont *self, intmax_t index, smallArrayt *toInsert); -smallJsont* insertJsonSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert); -smallJsont* insertJsonNSmashSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert); +smallJsont* insertSmallJsonSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert); +smallJsont* insertNSmashSmallJsonSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert); smallJsont* insertStringSmallJsonG (smallJsont *self, intmax_t index, smallStringt *toInsert); smallJsont* insertSSmallJsonG (smallJsont *self, intmax_t index, const char *toInsert); -smallJsont* insertNFreeSmallJsonG(smallJsont *self, intmax_t index, smallStringt *toInsert); -smallJsont* insertSNFreeStringSmallJsonG(smallJsont *self, intmax_t index, char *toInsert); +smallJsont* insertNFreeStringSmallJsonG(smallJsont *self, intmax_t index, smallStringt *toInsert); +smallJsont* insertNFreeSSmallJsonG(smallJsont *self, intmax_t index, char *toInsert); smallJsont* injectSmallJsonG (smallJsont *self, intmax_t index, baset *value); -smallJsont* injectUndefinedSmallJsonG(smallJsont *self, intmax_t index, void *value); +smallJsont* injectUndefinedSmallJsonG(smallJsont *self, intmax_t index, void *value UNUSED); smallJsont* injectBoolSmallJsonG (smallJsont *self, intmax_t index, bool value); smallJsont* injectDoubleSmallJsonG (smallJsont *self, intmax_t index, double value); smallJsont* injectIntSmallJsonG (smallJsont *self, intmax_t index, int64_t value); @@ -1511,6 +1518,7 @@ bool equalSmallJsonInt32G(smallJsont* self, int32_t p2); bool equalSmallJsonUint32G(smallJsont* self, uint32_t p2); bool equalSmallJsonUint64G(smallJsont* self, uint64_t p2); bool equalSmallJsonSmallBoolG(smallJsont* self, smallBoolt* p2); +bool equalSmallJsonSmallBytesG(smallJsont* self, smallBytest* p2); bool equalSmallJsonSmallDoubleG(smallJsont* self, smallDoublet* p2); bool equalSmallJsonSmallIntG(smallJsont* self, smallIntt* p2); bool equalSmallJsonSmallJsonG(smallJsont* self, smallJsont* p2); @@ -2079,7 +2087,7 @@ void registerMethodsSmallJson(smallJsonFunctionst *f) { f->shift = shiftSmallJson; { f->shiftNSmash = shiftNSmashSmallJson; { f->shiftSmallJson = shiftSmallJsonSmallJson; { - f->shiftSmallJsonNSmash = shiftSmallJsonNSmashSmallJson; { + f->shiftNSmashSmallJson = shiftNSmashSmallJsonSmallJson; { f->add = addSmallJson; f->addJson = addJsonSmallJson; f->slice = sliceSmallJson; @@ -2128,8 +2136,8 @@ void registerMethodsSmallJson(smallJsonFunctionst *f) { f->copy = copySmallJson; f->insert = insertSmallJson; f->insertNSmash = insertNSmashSmallJson; - f->insertJson = insertJsonSmallJson; - f->insertJsonNSmash = insertJsonNSmashSmallJson; + f->insertSmallJson = insertSmallJsonSmallJson; + f->insertNSmashSmallJson = insertNSmashSmallJsonSmallJson; f->insertString = insertStringSmallJson; f->insertS = insertSSmallJson; f->insertNFreeString = insertNFreeStringSmallJson; @@ -2446,13 +2454,20 @@ void registerMethodsSmallJson(smallJsonFunctionst *f) { f->colord = colordSmallJson; f->zip = zipSmallJson; f->zipArray = zipArraySmallJson; + f->zipCArray = zipCArraySmallJson; f->zipChar = zipCharSmallJson; + f->zipCChar = zipCCharSmallJson; f->zipArrayChar = zipArrayCharSmallJson; + f->zipCArrayChar = zipCArrayCharSmallJson; + f->zipArrayCChar = zipArrayCCharSmallJson; + f->zipCArrayCChar = zipCArrayCCharSmallJson; f->zipJson = zipJsonSmallJson; f->zipJsonSmallArray = zipJsonSmallArraySmallJson; f->zipJsonArray = zipJsonArraySmallJson; + f->zipJsonCArray = zipJsonCArraySmallJson; f->zipSmallArrayJson = zipSmallArrayJsonSmallJson; f->zipArrayJson = zipArrayJsonSmallJson; + f->zipCArrayJson = zipCArrayJsonSmallJson; f->iterStart = iterStartSmallJson; f->iterStartKey = iterStartKeySmallJson; f->iterStartLast = iterStartLastSmallJson; @@ -2941,7 +2956,8 @@ internal smallJsont* mirrorSmallJson(smallJsont *self) { mirror->topIsA = self->topIsA; mirror->top = self->top; if (self->top) { - mirror->iterIndex = self->iterIndex; + mirror->iterElementDataType = self->iterElementDataType; + mirror->iterIndex = self->iterIndex; if (mirror->iterIndex != -1) { mirror->iterKey = (&((mirror->top)->elements) + mirror->iterIndex)->key; } @@ -2959,9 +2975,10 @@ internal smallJsont* mirrorSmallJson(smallJsont *self) { mirror->topIsA = self->topIsA; mirror->topA = self->topA; if (self->topA) { - mirror->iterIndex = self->iterIndex; - mirror->iterKey = NULL; - mirror->iterStep = self->iterStep; + mirror->iterElementDataType = self->iterElementDataType; + mirror->iterIndex = self->iterIndex; + mirror->iterKey = NULL; + mirror->iterStep = self->iterStep; if (mirror->iterIndex != -1) { mirror->iterElement = toBaset(sArrayGetTiny(mirror->topA, mirror->iterIndex)); } @@ -2995,6 +3012,7 @@ internal const char* getTopTypeSmallJson(smallJsont *self) { internal smallJsont* setTypeUndefinedSmallJson(smallJsont *self) { + freeSmallJson(self); isError(self->topU , allocSUndefined()) return(NULL); self->topIsA = TOP_IS_UNDEFINED; return(self); @@ -3002,6 +3020,7 @@ internal smallJsont* setTypeUndefinedSmallJson(smallJsont *self) { internal smallJsont* setTypeBoolSmallJson(smallJsont *self) { + freeSmallJson(self); isError(self->topB , allocSBool(false)) return(NULL); self->topIsA = TOP_IS_BOOL; return(self); @@ -3009,6 +3028,7 @@ internal smallJsont* setTypeBoolSmallJson(smallJsont *self) { internal smallJsont* setTypeDoubleSmallJson(smallJsont *self) { + freeSmallJson(self); isError(self->topD , allocSDouble(0)) return(NULL); self->topIsA = TOP_IS_DOUBLE; return(self); @@ -3016,6 +3036,7 @@ internal smallJsont* setTypeDoubleSmallJson(smallJsont *self) { internal smallJsont* setTypeIntSmallJson(smallJsont *self) { + freeSmallJson(self); isError(self->topI , allocSInt(0)) return(NULL); self->topIsA = TOP_IS_INT; return(self); @@ -3023,6 +3044,7 @@ internal smallJsont* setTypeIntSmallJson(smallJsont *self) { internal smallJsont* setTypeStringSmallJson(smallJsont *self) { + freeSmallJson(self); isError(self->topS , allocSStringTiny("")) return(NULL); self->topIsA = TOP_IS_STRING; return(self); @@ -3030,6 +3052,7 @@ internal smallJsont* setTypeStringSmallJson(smallJsont *self) { internal smallJsont* setTypeDictSmallJson(smallJsont *self) { + freeSmallJson(self); isError(self->top , allocSDict()) return(NULL); self->topIsA = TOP_IS_DICT; return(self); @@ -3037,6 +3060,7 @@ internal smallJsont* setTypeDictSmallJson(smallJsont *self) { internal smallJsont* setTypeArraySmallJson(smallJsont *self) { + freeSmallJson(self); isError(self->topA , allocSArray()) return(NULL); self->topIsA = TOP_IS_ARRAY; return(self); @@ -3212,8 +3236,8 @@ internal smallJsont* setTopDictSmallJson(smallJsont *self, smallDictt *value) { return(NULL); } - setTopSmallJson(self, (baset*) value); - return(self); + smallJsont *r = setTopSmallJson(self, (baset*) value);; + return(r); } internal smallJsont* setTopArraySmallJson(smallJsont *self, smallArrayt *value) { @@ -3222,8 +3246,8 @@ internal smallJsont* setTopArraySmallJson(smallJsont *self, smallArrayt *value) return(NULL); } - setTopSmallJson(self, (baset*) value); - return(self); + smallJsont *r = setTopSmallJson(self, (baset*) value);; + return(r); } internal smallJsont* setTopArraycSmallJson(smallJsont *self, char **value) { @@ -3232,7 +3256,7 @@ internal smallJsont* setTopArraycSmallJson(smallJsont *self, char **value) { return(NULL); } - smallArrayt *a = allocArraySmallArray(value);; + smallArrayt *a = allocArraySmallArray(value); if (!a) { return(NULL); } @@ -3249,8 +3273,8 @@ internal smallJsont* setTopSmallBoolSmallJson(smallJsont *self, smallBoolt *valu return(NULL); } - setTopSmallJson(self, (baset*) value); - return(self); + smallJsont *r = setTopSmallJson(self, (baset*) value);; + return(r); } internal smallJsont* setTopSmallDoubleSmallJson(smallJsont *self, smallDoublet *value) { @@ -3259,8 +3283,8 @@ internal smallJsont* setTopSmallDoubleSmallJson(smallJsont *self, smallDoublet * return(NULL); } - setTopSmallJson(self, (baset*) value); - return(self); + smallJsont *r = setTopSmallJson(self, (baset*) value);; + return(r); } internal smallJsont* setTopSmallIntSmallJson(smallJsont *self, smallIntt *value) { @@ -3269,8 +3293,8 @@ internal smallJsont* setTopSmallIntSmallJson(smallJsont *self, smallIntt *value) return(NULL); } - setTopSmallJson(self, (baset*) value); - return(self); + smallJsont *r =setTopSmallJson(self, (baset*) value);; + return(r); } internal smallJsont* setTopSmallJsonSmallJson(smallJsont *self, smallJsont *value) { @@ -3279,8 +3303,8 @@ internal smallJsont* setTopSmallJsonSmallJson(smallJsont *self, smallJsont *valu return(NULL); } - setTopSmallJson(self, (baset*) value); - return(self); + smallJsont *r =setTopSmallJson(self, (baset*) value);; + return(r); } internal smallJsont* setTopSmallStringSmallJson(smallJsont *self, smallStringt *value) { @@ -3289,8 +3313,8 @@ internal smallJsont* setTopSmallStringSmallJson(smallJsont *self, smallStringt * return(NULL); } - setTopSmallJson(self, (baset*) value); - return(self); + smallJsont *r =setTopSmallJson(self, (baset*) value);; + return(r); } internal smallJsont* setTopNFreeSmallJson(smallJsont *self, baset *value) { @@ -3299,7 +3323,7 @@ internal smallJsont* setTopNFreeSmallJson(smallJsont *self, baset *value) { if (r) { finishO(value); } - return(self); + return(r); } internal smallJsont* setTopNFreeBoolSmallJson(smallJsont *self, bool value) { @@ -3312,7 +3336,7 @@ internal smallJsont* setTopNFreeBoolSmallJson(smallJsont *self, bool value) { if (!r) { terminateO(v); } - return(self); + return(r); } internal smallJsont* setTopNFreeDoubleSmallJson(smallJsont *self, double value) { @@ -3325,7 +3349,7 @@ internal smallJsont* setTopNFreeDoubleSmallJson(smallJsont *self, double value) if (!r) { terminateO(v); } - return(self); + return(r); } internal smallJsont* setTopNFreeIntSmallJson(smallJsont *self, int64_t value) { @@ -3338,7 +3362,7 @@ internal smallJsont* setTopNFreeIntSmallJson(smallJsont *self, int64_t value) { if (!r) { terminateO(v); } - return(self); + return(r); } internal smallJsont* setTopNFreeStringSmallJson(smallJsont *self, char *value) { @@ -3353,12 +3377,11 @@ internal smallJsont* setTopNFreeStringSmallJson(smallJsont *self, char *value) { smallJsont *r = setTopNFreeSmallJson(self, (baset*) v);; if (r) { free(value); - finishO(v); } else { terminateO(v); } - return(self); + return(r); } internal smallJsont* setTopNFreeDictSmallJson(smallJsont *self, smallDictt *value) { @@ -3456,12 +3479,10 @@ internal smallJsont* fromArraySmallJson(smallJsont *self, char **array, size_t s return(NULL); } - if (self->topIsA == SMALLJSON_IS_EMPTY) { - self->topIsA = TOP_IS_ARRAY; - } - else { + if (self->topIsA != SMALLJSON_IS_EMPTY) { freeSmallJson(self); } + self->topIsA = TOP_IS_ARRAY; if (!size) { forEachCharP(array, e) { @@ -3524,8 +3545,12 @@ internal smallJsont* fromArrayDictSmallJson(smallJsont *self, smallArrayt *items return(NULL); } + if (self->topIsA == SMALLJSON_IS_EMPTY) { + setTypeDictSmallJson(self); + } + forEachSmallArray(items, E) { - if (!E || !isOType(E, "smallArray")) { + if (!isOType(E, "smallArray")) { goto cont; } cast(smallArrayt*, a, E); @@ -3537,12 +3562,12 @@ internal smallJsont* fromArrayDictSmallJson(smallJsont *self, smallArrayt *items finishO(s); goto cont; } - setNFreeSmallJson(self, ssGet(s), a->f->get(a, 1)); + setNFreeSmallJson(self, ssGet(s), a->f->getNDup(a, 1)); finishO(s); cont: finishO(E); } - return(NULL); + return(self); } internal smallArrayt* toArrayDictSmallJson(smallJsont *self) { @@ -3568,7 +3593,7 @@ internal smallArrayt* toArrayDictSmallJson(smallJsont *self) { return(NULL); } a->f->pushS(a, e->key); - pushNFreeO(a, toBaset(e->data)); + pushNFreeO(a, toBaset(sDuplicate(e->data))); r->f->pushNFreeArray(r, a); } } @@ -4011,28 +4036,16 @@ internal char* bLMakeKeySmallJson(smallJsont *self UNUSED, char *dest, size_t s size_t di = 0; dest[di++] = '"'; - if (di >= size) { - return(NULL); - } for (size_t i = 0 ; key[i] ; i++) { if (key[i]=='"' || key[i]=='\\') { dest[di++] = '\\'; - if (di >= size) { - return(NULL); } - } dest[di++] = key[i];; - if (di >= size) { - return(NULL); } - } // close quote dest[di++] = '"'; - if (di >= size) { - return(NULL); - } dest[di] = 0; return(dest); } @@ -4082,38 +4095,37 @@ internal void unescapeKey(char *dest, char *key, size_t length) { internal smallJsont* setSmallJson(smallJsont *self, const char *key, baset *value) { - // TODO remove memory leak when !value is true and setJsonPath fails - mainSetJsonPath(if (!value) return NULL/*initValue*/, isError(o,toSmallt(value)) return NULL/*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!value) return NULL/*initValue*/, isError(o,toSmallt(value)) return NULL; if (!isOSmallContainer(value) && o->type == CONTAINER) isNew = true/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setUndefinedSmallJson(smallJsont *self, const char *key) { - mainSetJsonPath(/*initValue*/, isError(o,(smallt*)allocSUndefined()) return NULL/*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(/*initValue*/, isError(o,(smallt*)allocSUndefined()) return NULL; isNew = true/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setBoolSmallJson(smallJsont *self, const char *key, bool value) { - mainSetJsonPath(/*initValue*/, isError(o,(smallt*)allocSBool(value)) return NULL/*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(/*initValue*/, isError(o,(smallt*)allocSBool(value)) return NULL; isNew = true/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setDoubleSmallJson(smallJsont *self, const char *key, double value) { - mainSetJsonPath(/*initValue*/, isError(o,(smallt*)allocSDouble(value)) return NULL/*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(/*initValue*/, isError(o,(smallt*)allocSDouble(value)) return NULL; isNew = true/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setIntSmallJson(smallJsont *self, const char *key, int64_t value) { - mainSetJsonPath(/*initValue*/, isError(o,(smallt*)allocSInt(value)) return NULL/*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(/*initValue*/, isError(o,(smallt*)allocSInt(value)) return NULL; isNew = true/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setSSmallJson(smallJsont *self, const char *key, const char *string) { - mainSetJsonPath(if (!string) return NULL; isError(o,(smallt*)allocSStringTiny(string)) return NULL/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!string) return NULL/*initValue*/, isError(o,(smallt*)allocSStringTiny(string)) return NULL; isNew = true/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } @@ -4125,68 +4137,67 @@ internal smallJsont* setCharSmallJson(smallJsont *self, const char *key, char c) internal smallJsont* setDictSmallJson(smallJsont *self, const char *key, smallDictt *dict) { - mainSetJsonPath(if (!dict) return NULL; if (checkObjectTypes && dict && !isOSmallDict(dict)) return NULL;if (!dict->d) isError(dict->d, allocSDict()) return NULL;o = (smallt *)dict->d;/*initValue*/,/*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!dict) return NULL; if (checkObjectTypes && dict && !isOSmallDict(dict)) return NULL/*initValue*/, if (!dict->d) {isError(dict->d, allocSDict()) return NULL;}o = (smallt *)dict->d/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setArraySmallJson(smallJsont *self, const char *key, smallArrayt *array) { - mainSetJsonPath(if (!array) return NULL; if (checkObjectTypes && array && !isOSmallArray(array)) return NULL; if (!array->a) /*allocate empty array*/ isError(array->a, allocSArray()) return NULL; o = (smallt *)array->a;/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!array) return NULL; if (checkObjectTypes && array && !isOSmallArray(array)) return NULL/*initValue*/, if (!array->a) {/*allocate empty array*/ isError(array->a, allocSArray()) return NULL;} o = (smallt *)array->a/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setArraycSmallJson(smallJsont *self, const char *key, char **array) { - mainSetJsonPath(if (!array) return NULL; sArrayt *a = allocSArray(); if (!a) return NULL;forEachCharP(array, e){sStringt *s = allocSStringTiny(*e);if (!s) {if (a) sArrayFreeTiny(a); return NULL;};sArrayPushTiny(&a, (smallt *) s);}o = (smallt *)a;/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!array) return NULL/*initValue*/, sArrayt *A = allocSArray(); if (!A) return NULL;forEachCharP(array, e){sStringt *s = allocSStringTiny(*e);if (!s) {if (A) sArrayFreeTiny(A); return NULL;};sArrayPushTiny(&A, (smallt *) s);}o = (smallt *)A; isNew = true/*allocValue*/, setJsonPathsFree/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setSmallBoolSmallJson(smallJsont *self, const char *key, smallBoolt *value) { - mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallBool(value)) return NULL; o = (smallt*)value->value/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallBool(value)) return NULL/*initValue*/, if (!value->value) {isError(value->value, allocSBool(false)) return NULL;} o = (smallt*)value->value/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setSmallBytesSmallJson(smallJsont *self, const char *key, smallBytest *value) { - mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallBytes(value)) return NULL; o = (!value->B)?(smallt*)allocSUndefined():(smallt*)value->B; if (!o) return NULL/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallBytes(value)) return NULL/*initValue*/, if (!value->B) {isError(value->B, allocSBytes()) return NULL;} o = (smallt*)value->B/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setSmallDoubleSmallJson(smallJsont *self, const char *key, smallDoublet *value) { - mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallDouble(value)) return NULL; o = (smallt*)value->value/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallDouble(value)) return NULL/*initValue*/, if (!value->value) {isError(value->value, allocSDouble(0)) return NULL;} o = (smallt*)value->value/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setSmallIntSmallJson(smallJsont *self, const char *key, smallIntt *value) { - mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallInt(value)) return NULL; o = (smallt*)value->value/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallInt(value)) return NULL/*initValue*/, if (!value->value) {isError(value->value, allocSInt(0)) return NULL;} o = (smallt*)value->value/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setSmallJsonSmallJson(smallJsont *self, const char *key, smallJsont *value) { - mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallJson(value)) return NULL; switch(value->topIsA){case SMALLJSON_IS_EMPTY:isError(o, (smallt *) allocSUndefined()) return NULL;break;case TOP_IS_UNDEFINED:o = (smallt *) value->topU;break;case TOP_IS_BOOL:o = (smallt *) value->topB;break;case TOP_IS_DOUBLE:o = (smallt *) value->topD;break;case TOP_IS_INT:o = (smallt *) value->topI;break;case TOP_IS_STRING:o = (smallt *) value->topS;break;case TOP_IS_DICT:o = (smallt *) value->top;break;case TOP_IS_ARRAY:o = (smallt *) value->topA;break;}/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallJson(value)) return NULL/*initValue*/, o = getsoO(value); if (!o) {isError(o, (smallt*)allocSDict()) return NULL; setsoO(value, o);}/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setSmallStringSmallJson(smallJsont *self, const char *key, smallStringt *string) { - mainSetJsonPath(if (!string) return NULL; if (checkObjectTypes && string && !isOSmallString(string)) return NULL; o = (!string->data)?(smallt*)allocSStringTiny(""):(smallt*)string->data; if (!o) return NULL/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!string) return NULL; if (checkObjectTypes && string && !isOSmallString(string)) return NULL/*initValue*/, if (!string->data) {isError(string->data, allocSStringTiny("")) return NULL;} o = (smallt*)string->data/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setSmallContainerSmallJson(smallJsont *self, const char *key, smallContainert *container) { - mainSetJsonPath(if (!container) return NULL; if (checkObjectTypes && container && !isOSmallContainer(container)) return NULL; if (!container->data) {isError(o, (smallt *) allocSContainer(NULL)) return NULL;} else o = (smallt *) container->data/*initValue*/, /*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPath(if (!container) return NULL; if (checkObjectTypes && container && !isOSmallContainer(container)) return NULL/*initValue*/, if (!container->data) {isError(container->data, allocSContainer(0)) return NULL;} o = (smallt*)container->data/*allocValue*/, setJsonPath/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setNFreeSmallJson(smallJsont *self, const char *key, baset *value) { - // TODO remove memory leak when !value is true and setJsonPath fails - mainSetJsonPath(if (!value) return NULL/*initValue*/, isError(o,toSmallt(value)) return NULL/*allocValue*/, setJsonPath/*subSetJsonPath*/); + mainSetJsonPathWithoutReturn(if (!value) return NULL/*initValue*/, isError(o,toSmallt(value)) return NULL; if (!isOSmallContainer(value) && o->type == CONTAINER) isNew = true/*allocValue*/, setJsonPathWithoutReturn/*subSetJsonPath*/); // cg_c bug if (!(o->type == CONTAINER && (((sContainert*)o)->dataType == SH_DT_BASET))) { @@ -4198,22 +4209,30 @@ internal smallJsont* setNFreeSmallJson(smallJsont *self, const char *key, baset internal smallJsont* setNFreeUndefinedSmallJson(smallJsont *self, const char *key, undefinedt *undefined) { + if (!undefined) { + return(NULL); + } + smallJsont *r = setUndefinedSmallJson(self, key); if (r) { /* free parameter only when successfully stored in self */ terminateO(undefined); } - return(self); + return(r); } internal smallJsont* setNFreeSSmallJson(smallJsont *self, const char *key, char *string) { + if (!string) { + return(NULL); + } + smallJsont *r = setSSmallJson(self, key, string); if (r) { /* free parameter only when successfully stored in self */ free(string); } - return(self); + return(r); } internal smallJsont* setNFreeDictSmallJson(smallJsont *self, const char *key, smallDictt *dict) { @@ -4238,11 +4257,15 @@ internal smallJsont* setNFreeArraySmallJson(smallJsont *self, const char *key, s internal smallJsont* setNFreeArraycSmallJson(smallJsont *self, const char *key, char **array) { + if (!array) { + return(NULL); + } + smallJsont *r = setArraycSmallJson(self, key, array); if (r) { listFreeS(array); } - return(self); + return(r); } internal smallJsont* setNFreeSmallBoolSmallJson(smallJsont *self, const char *key, smallBoolt *value) { @@ -4310,49 +4333,49 @@ internal smallJsont* setNFreeSmallContainerSmallJson(smallJsont *self, const cha internal smallJsont* setPDictSmallJson(smallJsont *self, const char *key, smallDictt *dict) { - mainSetJsonPath(if (!dict) return NULL; if (checkObjectTypes && dict && !isOSmallDict(dict)) return NULL; o = (smallt *)dict->d/*initValue*/,/*allocValue*/, setPJsonPath(o)/*subSetJsonPath*/); + mainSetJsonPath(if (!dict) return NULL; if (checkObjectTypes && dict && !isOSmallDict(dict)) return NULL; if (!dict->d) return NULL; o = (smallt *)dict->d/*initValue*/,/*allocValue*/, setPJsonPath(o)/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setPArraySmallJson(smallJsont *self, const char *key, smallArrayt *array) { - mainSetJsonPath(if (!array) return NULL; if (checkObjectTypes && array && !isOSmallArray(array)) return NULL; o = (smallt *)array->a/*initValue*/, /*allocValue*/, setPJsonPath(o)/*subSetJsonPath*/); + mainSetJsonPath(if (!array) return NULL; if (checkObjectTypes && array && !isOSmallArray(array)) return NULL; if (!array->a) return NULL; o = (smallt *)array->a/*initValue*/, /*allocValue*/, setPJsonPath(o)/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setPSmallJsonSmallJson(smallJsont *self, const char *key, smallJsont *value) { - mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallJson(value)) return NULL; switch(value->topIsA){case SMALLJSON_IS_EMPTY:isError(o, (smallt *) allocSUndefined()) return NULL;break;case TOP_IS_UNDEFINED:o = (smallt *) value->topU;break;case TOP_IS_BOOL:o = (smallt *) value->topB;break;case TOP_IS_DOUBLE:o = (smallt *) value->topD;break;case TOP_IS_INT:o = (smallt *) value->topI;break;case TOP_IS_STRING:o = (smallt *) value->topS;break;case TOP_IS_DICT:o = (smallt *) value->top;break;case TOP_IS_ARRAY:o = (smallt *) value->topA;break;}/*initValue*/, /*allocValue*/, setPJsonPath(o)/*subSetJsonPath*/); + mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallJson(value)) return NULL; o = getsoO(value); if (!o) return NULL/*initValue*/, /*allocValue*/, setPJsonPath(o)/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setPSmallStringSmallJson(smallJsont *self, const char *key, smallStringt *string) { - mainSetJsonPath(if (!string) return NULL; if (checkObjectTypes && string && !isOSmallString(string)) return NULL; o = (!string->data)?(smallt*)allocSUndefined():(smallt*)string->data; if (!o) return NULL/*initValue*/, /*allocValue*/, setPJsonPath(o)/*subSetJsonPath*/); + mainSetJsonPath(if (!string) return NULL; if (checkObjectTypes && string && !isOSmallString(string)) return NULL; if (!string->data) return NULL; o = (smallt*)string->data;/*initValue*/, /*allocValue*/, setPJsonPath(o)/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setNFreePDictSmallJson(smallJsont *self, const char *key, smallDictt *dict) { - mainSetJsonPath(if (!dict) return NULL; if (checkObjectTypes && dict && !isOSmallDict(dict)) return NULL; o = (smallt *)dict->d/*initValue*/,/*allocValue*/, setPNFreeJsonPath(o,finishO(dict));finishO(dict)/*subSetJsonPath*/); + mainSetJsonPath(if (!dict) return NULL; if (checkObjectTypes && dict && !isOSmallDict(dict)) return NULL; if (!dict->d) return NULL; o = (smallt *)dict->d/*initValue*/,/*allocValue*/, setPNFreeJsonPath(o,finishO(dict));finishO(dict)/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setNFreePArraySmallJson(smallJsont *self, const char *key, smallArrayt *array) { - mainSetJsonPath(if (!array) return NULL; if (checkObjectTypes && array && !isOSmallArray(array)) return NULL; o = (smallt *)array->a/*initValue*/, /*allocValue*/, setPNFreeJsonPath(o,finishO(array));finishO(array)/*subSetJsonPath*/); + mainSetJsonPath(if (!array) return NULL; if (checkObjectTypes && array && !isOSmallArray(array)) return NULL; if (!array->a) return NULL; o = (smallt *)array->a/*initValue*/, /*allocValue*/, setPNFreeJsonPath(o,finishO(array));finishO(array)/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setNFreePSmallJsonSmallJson(smallJsont *self, const char *key, smallJsont *value) { - mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallJson(value)) return NULL; switch(value->topIsA){case SMALLJSON_IS_EMPTY:isError(o, (smallt *) allocSUndefined()) return NULL;break;case TOP_IS_UNDEFINED:o = (smallt *) value->topU;break;case TOP_IS_BOOL:o = (smallt *) value->topB;break;case TOP_IS_DOUBLE:o = (smallt *) value->topD;break;case TOP_IS_INT:o = (smallt *) value->topI;break;case TOP_IS_STRING:o = (smallt *) value->topS;break;case TOP_IS_DICT:o = (smallt *) value->top;break;case TOP_IS_ARRAY:o = (smallt *) value->topA;break;}/*initValue*/, /*allocValue*/, setPNFreeJsonPath(o,finishO(value));finishO(value)/*subSetJsonPath*/); + mainSetJsonPath(if (!value) return NULL; if (checkObjectTypes && value && !isOSmallJson(value)) return NULL; o = getsoO(value); if (!o) return NULL;/*initValue*/, /*allocValue*/, setPNFreeJsonPath(o,finishO(value));finishO(value)/*subSetJsonPath*/); // cg_c bug } internal smallJsont* setNFreePSmallStringSmallJson(smallJsont *self, const char *key, smallStringt *string) { - mainSetJsonPath(if (!string) return NULL; if (checkObjectTypes && string && !isOSmallString(string)) return NULL; o = (!string->data)?(smallt*)allocSUndefined():(smallt*)string->data; if (!o) return NULL/*initValue*/, /*allocValue*/, setPNFreeJsonPath(o,finishO(string));finishO(string)/*subSetJsonPath*/); + mainSetJsonPath(if (!string) return NULL; if (checkObjectTypes && string && !isOSmallString(string)) return NULL; if (!string->data) return NULL; o = (smallt*)string->data;/*initValue*/, /*allocValue*/, setPNFreeJsonPath(o,finishO(string));finishO(string)/*subSetJsonPath*/); // cg_c bug } @@ -4621,8 +4644,10 @@ internal smallJsont* setAtSmallBoolSmallJson(smallJsont *self, intmax_t index, s index = lenSmallJson(self) + index; } - smallt *o = (smallt *) value->value; - sArraySetTiny(self->topA, index, o); + if (!value->value) { + isError(value->value, allocSBool(false)) return(NULL); + } + sArraySetTiny(self->topA, index, (smallt *) value->value); return(self); } @@ -4646,8 +4671,10 @@ internal smallJsont* setAtSmallBytesSmallJson(smallJsont *self, intmax_t index, index = lenSmallJson(self) + index; } - smallt *o = (smallt *) value->B; - sArraySetTiny(self->topA, index, o); + if (!value->B) { + isError(value->B, allocSBytes()) return(NULL); + } + sArraySetTiny(self->topA, index, (smallt*) value->B); return(self); } @@ -4671,8 +4698,10 @@ internal smallJsont* setAtSmallDoubleSmallJson(smallJsont *self, intmax_t index, index = lenSmallJson(self) + index; } - smallt *o = (smallt *) value->value; - sArraySetTiny(self->topA, index, o); + if (!value->value) { + isError(value->value, allocSDouble(0)) return(NULL); + } + sArraySetTiny(self->topA, index, (smallt*) value->value); return(self); } @@ -4696,8 +4725,10 @@ internal smallJsont* setAtSmallIntSmallJson(smallJsont *self, intmax_t index, sm index = lenSmallJson(self) + index; } - smallt *o = (smallt *) value->value; - sArraySetTiny(self->topA, index, o); + if (!value->value) { + isError(value->value, allocSInt(0)) return(NULL); + } + sArraySetTiny(self->topA, index, (smallt*) value->value); return(self); } @@ -4721,35 +4752,13 @@ internal smallJsont* setAtSmallJsonSmallJson(smallJsont *self, intmax_t index, s index = lenSmallJson(self) + index; } - smallt *o; - switch(value->topIsA){ - case SMALLJSON_IS_EMPTY: - isError(o, (smallt *) allocSUndefined()) return(NULL); - break; - case TOP_IS_UNDEFINED: - o = (smallt *) value->topU; - break; - case TOP_IS_BOOL: - o = (smallt *) value->topB; - break; - case TOP_IS_DOUBLE: - o = (smallt *) value->topD; - break; - case TOP_IS_INT: - o = (smallt *) value->topI; - break; - case TOP_IS_STRING: - o = (smallt *) value->topS; - break; - case TOP_IS_DICT: - o = (smallt *) value->top; - break; - case TOP_IS_ARRAY: - o = (smallt *) value->topA; - break; - default: - return(NULL); + smallt *o = getsoO(value); + if (!o) { + // smallJson is empty, create an empty dict + isError(o, (smallt*) allocSDict()) return(NULL); + setsoO(value, o); } + sArraySetTiny(self->topA, index, o); return(self); } @@ -4774,19 +4783,14 @@ internal smallJsont* setAtSmallStringSmallJson(smallJsont *self, intmax_t index, index = lenSmallJson(self) + index; } - smallt *o; if (!string->data) { - isError(o, (smallt *) allocSStringTiny("")) return(NULL); - } - else { - o = (smallt *) string->data; + isError(string->data, allocSStringTiny("")) return(NULL); } - sArraySetTiny(self->topA, index, o); + sArraySetTiny(self->topA, index, (smallt *)string->data); return(self); } internal smallJsont* setAtSmallContainerSmallJson(smallJsont *self, intmax_t index, smallContainert *container) { - smallt *o = NULL; if (self->topIsA != TOP_IS_ARRAY || !container) { return(NULL); @@ -4808,12 +4812,9 @@ internal smallJsont* setAtSmallContainerSmallJson(smallJsont *self, intmax_t ind } if (!container->data) { - isError(o, (smallt *) allocSContainer(NULL)) return(NULL); - } - else { - o = (smallt *) container->data; + isError(container->data, allocSContainer(NULL)) return(NULL); } - sArraySetTiny(self->topA, index, o); + sArraySetTiny(self->topA, index, (smallt *) container->data); return(self); } @@ -5020,7 +5021,8 @@ internal smallJsont* setPAtDictSmallJson(smallJsont *self, intmax_t index, small } if (!dict->d) { - isError(dict->d, allocSDict()) return(NULL); + // realloc error + return(NULL); } sArraySetP(self->topA, index, (smallt *)dict->d); @@ -5048,8 +5050,8 @@ internal smallJsont* setPAtArraySmallJson(smallJsont *self, intmax_t index, smal } if (!array->a) { - // allocate empty array - isError(array->a, allocSArray()) return(NULL); + // realloc error + return(NULL); } sArraySetP(self->topA, index, (smallt *)array->a); @@ -5077,33 +5079,10 @@ internal smallJsont* setPAtSmallJsonSmallJson(smallJsont *self, intmax_t index, } smallt *o; - switch(value->topIsA){ - case SMALLJSON_IS_EMPTY: - isError(o, (smallt *) allocSUndefined()) return(NULL); - break; - case TOP_IS_UNDEFINED: - o = (smallt *) value->topU; - break; - case TOP_IS_BOOL: - o = (smallt *) value->topB; - break; - case TOP_IS_DOUBLE: - o = (smallt *) value->topD; - break; - case TOP_IS_INT: - o = (smallt *) value->topI; - break; - case TOP_IS_STRING: - o = (smallt *) value->topS; - break; - case TOP_IS_DICT: - o = (smallt *) value->top; - break; - case TOP_IS_ARRAY: - o = (smallt *) value->topA; - break; - default: - return(NULL); + o = getsoO(value); + if (!o) { + // realloc error + return(NULL); } sArraySetP(self->topA, index, o); return(self); @@ -5129,13 +5108,12 @@ internal smallJsont* setPAtSmallStringSmallJson(smallJsont *self, intmax_t index index = lenSmallJson(self) + index; } - smallt *o; if (!string->data) { - isError(o, (smallt *) allocSStringTiny("")) return(NULL); - } - else { - o = (smallt *) string->data; + // realloc error + return(NULL); } + + smallt *o = (smallt *) string->data; sArraySetP(self->topA, index, o); return(self); } @@ -5161,7 +5139,8 @@ internal smallJsont* setPAtNFreeDictSmallJson(smallJsont *self, intmax_t index, } if (!dict->d) { - isError(dict->d, allocSDict()) return(NULL); + // realloc error + return(NULL); } sArraySetP(self->topA, index, (smallt *)dict->d); @@ -5191,8 +5170,8 @@ internal smallJsont* setPAtNFreeArraySmallJson(smallJsont *self, intmax_t index, } if (!array->a) { - // allocate empty array - isError(array->a, allocSArray()) return(NULL); + // realloc error + return(NULL); } sArraySetP(self->topA, index, (smallt *)array->a); @@ -5221,35 +5200,13 @@ internal smallJsont* setPAtNFreeSmallJsonSmallJson(smallJsont *self, intmax_t in } smallt *o; - switch(value->topIsA){ - case SMALLJSON_IS_EMPTY: - isError(o, (smallt *) allocSUndefined()) return(NULL); - break; - case TOP_IS_UNDEFINED: - o = (smallt *) value->topU; - break; - case TOP_IS_BOOL: - o = (smallt *) value->topB; - break; - case TOP_IS_DOUBLE: - o = (smallt *) value->topD; - break; - case TOP_IS_INT: - o = (smallt *) value->topI; - break; - case TOP_IS_STRING: - o = (smallt *) value->topS; - break; - case TOP_IS_DICT: - o = (smallt *) value->top; - break; - case TOP_IS_ARRAY: - o = (smallt *) value->topA; - break; - default: - return(NULL); + o = getsoO(value);; + if (!o) { + // realloc error + return(NULL); } sArraySetP(self->topA, index, o); + finishO(value); return(self); } @@ -5273,13 +5230,12 @@ internal smallJsont* setPAtNFreeSmallStringSmallJson(smallJsont *self, intmax_t index = lenSmallJson(self) + index; } - smallt *o; if (!string->data) { - isError(o, (smallt *) allocSStringTiny("")) return(NULL); - } - else { - o = (smallt *) string->data; + // realloc error + return(NULL); } + + smallt *o = (smallt *) string->data; finishO(string); sArraySetP(self->topA, index, o); return(self); @@ -5387,11 +5343,7 @@ internal smallJsont* pushSSmallJson(smallJsont *self, const char *string) { isError(o, (smallt *) allocSStringTiny(string)) return(NULL); sArrayPushTiny(&(self->topA), o); break; - case TOP_IS_STRING: - if (!*string) { - return(self); - } - + case TOP_IS_STRING:; size_t len = strlen(string); if (!self->topS) { @@ -5518,7 +5470,6 @@ internal smallJsont* pushArraycSmallJson(smallJsont *self, char **array) { } internal smallJsont* pushSmallBoolSmallJson(smallJsont *self, smallBoolt *value) { - smallt *o = NULL; if (!value) { return(NULL); @@ -5533,8 +5484,10 @@ internal smallJsont* pushSmallBoolSmallJson(smallJsont *self, smallBoolt *value) self->topIsA = TOP_IS_ARRAY; FALLTHRU; case TOP_IS_ARRAY: - o = (smallt *) value->value; - sArrayPushTiny(&(self->topA), o); + if (!value->value) { + isError(value->value, allocSBool(false)) return(NULL); + } + sArrayPushTiny(&(self->topA), (smallt *) value->value); break; default: return(NULL); @@ -5543,7 +5496,6 @@ internal smallJsont* pushSmallBoolSmallJson(smallJsont *self, smallBoolt *value) } internal smallJsont* pushSmallBytesSmallJson(smallJsont *self, smallBytest *value) { - smallt *o = NULL; if (!value) { return(NULL); @@ -5559,12 +5511,9 @@ internal smallJsont* pushSmallBytesSmallJson(smallJsont *self, smallBytest *valu FALLTHRU; case TOP_IS_ARRAY: if (!value->B) { - isError(o, (smallt *)allocSUndefined()) return(NULL); - } - else { - o = (smallt *) value->B; + isError(value->B, allocSBytes()) return(NULL); } - sArrayPushTiny(&(self->topA), o); + sArrayPushTiny(&(self->topA), (smallt*) value->B); break; default: return(NULL); @@ -5573,7 +5522,6 @@ internal smallJsont* pushSmallBytesSmallJson(smallJsont *self, smallBytest *valu } internal smallJsont* pushSmallDoubleSmallJson(smallJsont *self, smallDoublet *value) { - smallt *o = NULL; if (!value) { return(NULL); @@ -5588,8 +5536,10 @@ internal smallJsont* pushSmallDoubleSmallJson(smallJsont *self, smallDoublet *va self->topIsA = TOP_IS_ARRAY; FALLTHRU; case TOP_IS_ARRAY: - o = (smallt *) value->value; - sArrayPushTiny(&(self->topA), o); + if (!value->value) { + isError(value->value, allocSDouble(0)) return(NULL); + } + sArrayPushTiny(&(self->topA), (smallt *) value->value); break; default: return(NULL); @@ -5598,7 +5548,6 @@ internal smallJsont* pushSmallDoubleSmallJson(smallJsont *self, smallDoublet *va } internal smallJsont* pushSmallIntSmallJson(smallJsont *self, smallIntt *value) { - smallt *o = NULL; if (!value) { return(NULL); @@ -5613,8 +5562,10 @@ internal smallJsont* pushSmallIntSmallJson(smallJsont *self, smallIntt *value) { self->topIsA = TOP_IS_ARRAY; FALLTHRU; case TOP_IS_ARRAY: - o = (smallt *) value->value; - sArrayPushTiny(&(self->topA), o); + if (!value->value) { + isError(value->value, allocSInt(0)) return(NULL); + } + sArrayPushTiny(&(self->topA), (smallt *) value->value); break; default: return(NULL); @@ -5638,32 +5589,13 @@ internal smallJsont* pushSmallJsonSmallJson(smallJsont *self, smallJsont *value) self->topIsA = TOP_IS_ARRAY; FALLTHRU; case TOP_IS_ARRAY: - switch(value->topIsA){ - case SMALLJSON_IS_EMPTY: - isError(o, (smallt *) allocSUndefined()) return(NULL); - break; - case TOP_IS_UNDEFINED: - o = (smallt *) value->topU; - break; - case TOP_IS_BOOL: - o = (smallt *) value->topB; - break; - case TOP_IS_DOUBLE: - o = (smallt *) value->topD; - break; - case TOP_IS_INT: - o = (smallt *) value->topI; - break; - case TOP_IS_STRING: - o = (smallt *) value->topS; - break; - case TOP_IS_DICT: - o = (smallt *) value->top; - break; - case TOP_IS_ARRAY: - o = (smallt *) value->topA; - break; + o = getsoO(value); + if (!o) { + // smallJson is empty, create an empty dict + isError(o, (smallt*)allocSDict()) return(NULL); + setsoO(value, o); } + sArrayPushTiny(&(self->topA), o); break; default: @@ -5673,7 +5605,6 @@ internal smallJsont* pushSmallJsonSmallJson(smallJsont *self, smallJsont *value) } internal smallJsont* pushSmallStringSmallJson(smallJsont *self, smallStringt *string) { - smallt *o = NULL; if (!string) { return(NULL); @@ -5689,12 +5620,9 @@ internal smallJsont* pushSmallStringSmallJson(smallJsont *self, smallStringt *st FALLTHRU; case TOP_IS_ARRAY: if (!string->data) { - isError(o, (smallt *) allocSStringTiny("")) return(NULL); + isError(string->data, allocSStringTiny("")) return(NULL); } - else { - o = (smallt *) string->data; - } - sArrayPushTiny(&(self->topA), o); + sArrayPushTiny(&(self->topA), (smallt*) string->data); break; case TOP_IS_STRING: if (!string->data) { @@ -5730,7 +5658,6 @@ internal smallJsont* pushSmallStringSmallJson(smallJsont *self, smallStringt *st } internal smallJsont* pushSmallContainerSmallJson(smallJsont *self, smallContainert *container) { - smallt *o = NULL; if (!container) { return(NULL); @@ -5746,12 +5673,9 @@ internal smallJsont* pushSmallContainerSmallJson(smallJsont *self, smallContaine FALLTHRU; case TOP_IS_ARRAY: if (!container->data) { - isError(o, (smallt *) allocSContainer(NULL)) return(NULL); + isError(container->data, allocSContainer(NULL)) return(NULL); } - else { - o = (smallt *) container->data; - } - sArrayPushTiny(&(self->topA), o); + sArrayPushTiny(&(self->topA), (smallt *) container->data); break; default: return(NULL); @@ -5908,7 +5832,7 @@ internal smallJsont* pushManySmallJson(smallJsont *self, ...) { va_start(pl, self); paramType = va_arg(pl, baset*); while (paramType) { - smallJsont *r = pushSmallJson(self, paramType);; + smallJsont *r = pushSmallJson(self, paramType); if (!r) { va_end(pl); return(NULL); @@ -5924,13 +5848,20 @@ internal smallJsont* pushManySmallJson(smallJsont *self, ...) { createSmallArray(l); if (self->topS) { // add self to result - l.f->pushS(&l, sStringGetTiny(self->topS)); + createSmallString(ss); + ss.data = self->topS; + l.f->pushSmallString(&l, &ss); } // add arguments to a list va_start(pls, self); paramTypes = va_arg(pls, smallStringt*); while (paramTypes) { + if (checkObjectTypes && !isOSmallString(paramTypes)) { + disposeO(&l); + va_end(pls); + return(NULL); + } l.f->pushSmallString(&l, paramTypes); paramTypes = va_arg(pls, smallStringt*); } @@ -5961,7 +5892,7 @@ internal smallJsont* pushManySSmallJson(smallJsont *self, ...) { va_start(pl, self); paramType = va_arg(pl, char*); while (paramType) { - smallJsont *r = pushSSmallJson(self, paramType);; + smallJsont *r = pushSSmallJson(self, paramType); if (!r) { va_end(pl); return(NULL); @@ -5977,7 +5908,9 @@ internal smallJsont* pushManySSmallJson(smallJsont *self, ...) { createSmallArray(l); if (self->topS) { // add self to result - l.f->pushS(&l, sStringGetTiny(self->topS)); + createSmallString(ss); + ss.data = self->topS; + l.f->pushSmallString(&l, &ss); } // add arguments to a list @@ -6013,7 +5946,7 @@ internal smallJsont* pushNFreeManySmallJson(smallJsont *self, ...) { va_start(pl, self); paramType = va_arg(pl, baset*); while (paramType) { - smallJsont *r = pushNFreeSmallJson(self, paramType);; + smallJsont *r = pushNFreeSmallJson(self, paramType); if (!r) { va_end(pl); return(NULL); @@ -6029,24 +5962,31 @@ internal smallJsont* pushNFreeManySmallJson(smallJsont *self, ...) { createSmallArray(l); if (self->topS) { // add self to result - l.f->pushS(&l, sStringGetTiny(self->topS)); + createSmallString(ss); + ss.data = self->topS; + l.f->pushSmallString(&l, &ss); } + // TODO free parameters after all have been added to the l array without error // add arguments to a list va_start(pls, self); paramTypes = va_arg(pls, smallStringt*); while (paramTypes) { + if (checkObjectTypes && !isOSmallString(paramTypes)) { + delO(&l, 1, 0); + disposeO(&l); + return(NULL); + } l.f->pushNFreeSmallString(&l, paramTypes); paramTypes = va_arg(pls, smallStringt*); } va_end(pls); // join list with empty delimiter smallStringt *r = l.f->join(&l, ""); - free(self->topS); + freeO(&l); self->topS = r->data; //TODO self->_len = r->_len finishO(r); - disposeO(&l); break; } return(self); @@ -6066,7 +6006,7 @@ internal smallJsont* pushNFreeManySSmallJson(smallJsont *self, ...) { va_start(pl, self); paramType = va_arg(pl, char*); while (paramType) { - smallJsont *r = pushNFreeSSmallJson(self, paramType);; + smallJsont *r = pushNFreeSSmallJson(self, paramType); if (!r) { va_end(pl); return(NULL); @@ -6082,7 +6022,9 @@ internal smallJsont* pushNFreeManySSmallJson(smallJsont *self, ...) { createSmallArray(l); if (self->topS) { // add self to result - l.f->pushS(&l, sStringGetTiny(self->topS)); + createSmallString(ss); + ss.data = self->topS; + l.f->pushSmallString(&l, &ss); } // add arguments to a list @@ -6175,6 +6117,7 @@ internal undefinedt* popUndefinedSmallJson(smallJsont *self) { } // finish poping + free(o); self->topA->count--; return(allocUndefined()); } @@ -6551,7 +6494,7 @@ internal void* popVoidSmallJson(smallJsont *self) { e = popSmallContainerSmallJson(self); if (e) { r = e->f->get(e); - finishO(e); + smashO(e); } return(r); } @@ -6661,16 +6604,19 @@ internal smallJsont* prependSmallJson(smallJsont *self, baset *value) { } internal smallJsont* prependUndefinedSmallJson(smallJsont *self) { + smallt *o = NULL; - if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_ARRAY) { - return(NULL); - } - - smallt *o = (smallt *) allocSUndefined(); - if (!o) { - return(NULL); + switch(self->topIsA) { + case SMALLJSON_IS_EMPTY: + self->topIsA = TOP_IS_ARRAY; + FALLTHRU; + case TOP_IS_ARRAY: + isError(o, (smallt *) allocSUndefined()) return(NULL); + sArrayPrependTiny(&(self->topA), o); + break; + default: + return(NULL); } - sArrayPrependTiny(&(self->topA), o); return(self); } @@ -6738,12 +6684,12 @@ internal smallJsont* prependSSmallJson(smallJsont *self, const char *string) { return(NULL); } - switch(self->topIsA) { - case TOP_IS_ARRAY: - if (self->topIsA == SMALLJSON_IS_EMPTY) { - self->topIsA = TOP_IS_ARRAY; - } + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + switch(self->topIsA) { + case TOP_IS_ARRAY:; smallt *o; isError(o, (smallt *) allocSStringTiny(string)) return(NULL); sArrayPrependTiny(&(self->topA), o); @@ -6879,8 +6825,10 @@ internal smallJsont* prependSmallBoolSmallJson(smallJsont *self, smallBoolt *val self->topIsA = TOP_IS_ARRAY; } - smallt *o = (smallt *) value->value; - sArrayPrependTiny(&(self->topA), o); + if (!value->value) { + isError(value->value, allocSBool(false)) return(NULL); + } + sArrayPrependTiny(&(self->topA), (smallt *) value->value); return(self); } @@ -6902,14 +6850,10 @@ internal smallJsont* prependSmallBytesSmallJson(smallJsont *self, smallBytest *v self->topIsA = TOP_IS_ARRAY; } - smallt *o; if (!value->B) { - isError(o, (smallt *)allocSUndefined()) return(NULL); - } - else { - o = (smallt *) value->B; + isError(value->B, allocSBytes()) return(NULL); } - sArrayPrependTiny(&(self->topA), o); + sArrayPrependTiny(&(self->topA), (smallt *) value->B); return(self); } @@ -6931,8 +6875,10 @@ internal smallJsont* prependSmallDoubleSmallJson(smallJsont *self, smallDoublet self->topIsA = TOP_IS_ARRAY; } - smallt *o = (smallt *) value->value; - sArrayPrependTiny(&(self->topA), o); + if (!value->value) { + isError(value->value, allocSDouble(0)) return(NULL); + } + sArrayPrependTiny(&(self->topA), (smallt *) value->value); return(self); } @@ -6954,8 +6900,10 @@ internal smallJsont* prependSmallIntSmallJson(smallJsont *self, smallIntt *value self->topIsA = TOP_IS_ARRAY; } - smallt *o = (smallt *) value->value; - sArrayPrependTiny(&(self->topA), o); + if (!value->value) { + isError(value->value, allocSInt(0)) return(NULL); + } + sArrayPrependTiny(&(self->topA), (smallt *) value->value); return(self); } @@ -6977,33 +6925,13 @@ internal smallJsont* prependSmallJsonSmallJson(smallJsont *self, smallJsont *val self->topIsA = TOP_IS_ARRAY; } - smallt *o = NULL; - switch(value->topIsA){ - case SMALLJSON_IS_EMPTY: - isError(o, (smallt *) allocSUndefined()) return(NULL); - break; - case TOP_IS_UNDEFINED: - o = (smallt *) value->topU; - break; - case TOP_IS_BOOL: - o = (smallt *) value->topB; - break; - case TOP_IS_DOUBLE: - o = (smallt *) value->topD; - break; - case TOP_IS_INT: - o = (smallt *) value->topI; - break; - case TOP_IS_STRING: - o = (smallt *) value->topS; - break; - case TOP_IS_DICT: - o = (smallt *) value->top; - break; - case TOP_IS_ARRAY: - o = (smallt *) value->topA; - break; + smallt *o = getsoO(value); + if (!o) { + // smallJson is empty, create an empty dict + isError(o, (smallt*)allocSDict()) return(NULL); + setsoO(value, o); } + sArrayPrependTiny(&(self->topA), o); return(self); } @@ -7022,20 +6950,16 @@ internal smallJsont* prependSmallStringSmallJson(smallJsont *self, smallStringt return(NULL); } + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + switch(self->topIsA) { case TOP_IS_ARRAY: - if (self->topIsA == SMALLJSON_IS_EMPTY) { - self->topIsA = TOP_IS_ARRAY; - } - - smallt *o; if (!string->data) { - isError(o, (smallt *) allocSStringTiny("")) return(NULL); - } - else { - o = (smallt *) string->data; + isError(string->data, allocSStringTiny("")) return(NULL); } - sArrayPrependTiny(&(self->topA), o); + sArrayPrependTiny(&(self->topA), (smallt *) string->data); break; case TOP_IS_STRING: if (!string->data) { @@ -7060,7 +6984,6 @@ internal smallJsont* prependSmallStringSmallJson(smallJsont *self, smallStringt } internal smallJsont* prependSmallContainerSmallJson(smallJsont *self, smallContainert *container) { - smallt *o = NULL; if (!container) { return(NULL); @@ -7079,12 +7002,9 @@ internal smallJsont* prependSmallContainerSmallJson(smallJsont *self, smallConta } if (!container->data) { - isError(o, (smallt *) allocSContainer(NULL)) return(NULL); - } - else { - o = (smallt *) container->data; + isError(container->data, allocSContainer(NULL)) return(NULL); } - sArrayPrependTiny(&(self->topA), o); + sArrayPrependTiny(&(self->topA), (smallt *) container->data); return(self); } @@ -7299,6 +7219,7 @@ internal undefinedt* dequeueUndefinedSmallJson(smallJsont *self) { } self->topA->count -= i+1;; + free(o); return(allocUndefined()); } @@ -7721,7 +7642,7 @@ internal void* dequeueVoidSmallJson(smallJsont *self) { e = dequeueSmallContainerSmallJson(self); if (e) { r = e->f->get(e); - finishO(e); + smashO(e); } return(r); } @@ -7816,7 +7737,7 @@ internal double dequeueNumSmallJson(smallJsont *self) { elif (isOSmallInt(e)) { r = getValO((smallIntt*)e); } - finishO(e); + terminateO(e); return(r); } @@ -7849,7 +7770,7 @@ internal smallJsont* catSmallJson(smallJsont *self, ...) { va_start(pl, self); paramType = va_arg(pl, smallArrayt*); while (paramType) { - smallJsont *r = appendSmallJson(self, paramType);; + smallJsont *r = appendSmallJson(self, paramType); if (!r) { va_end(pl); return(NULL); @@ -7865,13 +7786,20 @@ internal smallJsont* catSmallJson(smallJsont *self, ...) { createSmallArray(l); if (self->topS) { // add self to result - l.f->pushS(&l, sStringGetTiny(self->topS)); + createSmallString(ss); + ss.data = self->topS; + l.f->pushSmallString(&l, &ss); } // add arguments to a list va_start(pls, self); paramTypes = va_arg(pls, smallStringt*); while (paramTypes) { + if (checkObjectTypes && !isOSmallString(paramTypes)) { + disposeO(&l); + va_end(pl); + return(NULL); + } l.f->pushSmallString(&l, paramTypes); paramTypes = va_arg(pls, smallStringt*); } @@ -7914,6 +7842,12 @@ internal smallJsont* mergeDictSmallJson(smallJsont *self, smallDictt *smallDict) self->topIsA = TOP_IS_DICT; } + if (self->top == smallDict->d) { + // self and array identical, cancel this operations + // to avoid double free undefined behavior in free + return(NULL); + } + forEachSDict(smallDict->d, e) { if (e->key) { sDictSetTiny(&(self->top), e->key, e->data); @@ -7964,6 +7898,11 @@ internal smallJsont* mergeSmallJson(smallJsont *self, smallJsont *smallJson) { self->topIsA = TOP_IS_DICT; FALLTHRU; case TOP_IS_DICT:; + if (self->top == smallJson->top) { + // self and array identical, cancel this operations + // to avoid double free undefined behavior in free + return(NULL); + } forEachSDict(smallJson->top, e) { if (e->key) { sDictSetTiny(&(self->top), e->key, e->data); @@ -7972,6 +7911,11 @@ internal smallJsont* mergeSmallJson(smallJsont *self, smallJsont *smallJson) { break; case TOP_IS_ARRAY: appendToArray:; + if (self->topA == smallJson->topA) { + // self and array identical, cancel this operations + // to avoid double free undefined behavior in free + return(NULL); + } forEachSArray(smallJson->topA, o) { sArrayPushTiny(&(self->topA), o); } @@ -8151,12 +8095,12 @@ internal smallJsont* shiftNSmashSmallJson(smallJsont *self, smallArrayt *array) internal smallJsont* shiftSmallJsonSmallJson(smallJsont *self, smallJsont *array) { - return(insertJsonSmallJson(self, 0, array)); + return(insertSmallJsonSmallJson(self, 0, array)); } -internal smallJsont* shiftSmallJsonNSmashSmallJson(smallJsont *self, smallJsont *array) { +internal smallJsont* shiftNSmashSmallJsonSmallJson(smallJsont *self, smallJsont *array) { - return(insertJsonNSmashSmallJson(self, 0, array)); + return(insertNSmashSmallJsonSmallJson(self, 0, array)); } internal smallJsont* addSmallJson(smallJsont *self, smallArrayt *array) { @@ -8168,17 +8112,15 @@ internal smallJsont* addSmallJson(smallJsont *self, smallArrayt *array) { } if (!array) { - return(duplicateSmallJson(self)); + return(NULL); } if (checkObjectTypes && !isOSmallArray(array)) { return(NULL); } - r = duplicateSmallJson(self); - if (!array->f->len(array)) { - return(r); + return(duplicateSmallJson(self)); } if (self->topA == array->a) { @@ -8187,6 +8129,8 @@ internal smallJsont* addSmallJson(smallJsont *self, smallArrayt *array) { return(NULL); } + r = duplicateSmallJson(self); + // copy array forEachSArray(array->a, o) { sArrayPushTiny(&(r->topA), o); @@ -8204,17 +8148,15 @@ internal smallJsont* addJsonSmallJson(smallJsont *self, smallJsont *array) { } if (!array) { - return(duplicateSmallJson(self)); + return(NULL); } if (checkObjectTypes && !isOSmallJson(array)) { return(NULL); } - r = duplicateSmallJson(self); - - if (array->topIsA != TOP_IS_ARRAY || !array->f->len(array)) { - return(r); + if (array->topIsA != TOP_IS_ARRAY) { + return(NULL); } if (self->topA == array->topA) { @@ -8223,6 +8165,12 @@ internal smallJsont* addJsonSmallJson(smallJsont *self, smallJsont *array) { return(NULL); } + r = duplicateSmallJson(self); + + if (!array->f->len(array)) { + return(r); + } + // copy array forEachSArray(array->topA, o) { sArrayPushTiny(&(r->topA), o); @@ -8275,7 +8223,7 @@ internal smallJsont* sliceSmallJson(smallJsont *self, intmax_t start, intmax_t e case TOP_IS_ARRAY: if (start == end) { // empty list - freeSmallJson(self); + emptySmallJson(self); return(self); } else { @@ -8301,10 +8249,8 @@ internal smallJsont* sliceSmallJson(smallJsont *self, intmax_t start, intmax_t e s[n] = 0; } - if (!setTopNFreeStringSmallJson(self, s)) { - free(s); - return(NULL); - } + freeSmallJson(self); + setTopNFreeStringSmallJson(self, s); break; } return(self); @@ -8349,18 +8295,24 @@ internal smallJsont* cropSmallJson(smallJsont *self, intmax_t start, intmax_t en return(NULL); } - smallJsont *r = NULL; + createAllocateSmallJson(r); + if (!r) { + return(NULL); + } + switch(self->topIsA) { + case TOP_IS_ARRAY: + setTypeArraySmallJson(r); + break; + case TOP_IS_STRING: + setTypeStringSmallJson(r); + break; + } if (start < end) { - initiateAllocateSmallJson(&r); - if (!r) { - return(NULL); - } n = end - start; switch(self->topIsA) { case TOP_IS_ARRAY: // start < end < len - setTypeArraySmallJson(r); for (intmax_t i=0;i < n;i++) { o = sArrayGetTiny(self->topA, start+i); sArrayPushTiny(&(r->topA), o); @@ -8386,7 +8338,8 @@ internal smallJsont* cropSmallJson(smallJsont *self, intmax_t start, intmax_t en strLCpy(string+start, len-end+1, string+end); // TODO self->_len -= n - setTopStringO(r, l); + sFree((smallt*)r->topS); + r->topS = allocSStringTiny(l);; free(l); break; } @@ -8444,6 +8397,10 @@ internal char* cropSSmallJson(smallJsont *self, intmax_t start, intmax_t end) { strLCpy(string+start, len-end+1, string+end); // TODO self->_len -= n } + else { + // start = end, return empty string + l = strdup(""); + } return(l); } @@ -8592,7 +8549,7 @@ internal bool cropElemAtBoolSmallJson(smallJsont *self, intmax_t index) { return(false); } r = e->f->get(e); - finishO(e); + terminateO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->topA->data); @@ -8639,7 +8596,7 @@ internal double cropElemAtDoubleSmallJson(smallJsont *self, intmax_t index) { return(0); } r = e->f->get(e); - finishO(e); + terminateO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->topA->data); @@ -8686,7 +8643,7 @@ internal int64_t cropElemAtIntSmallJson(smallJsont *self, intmax_t index) { return(0); } r = e->f->get(e); - finishO(e); + terminateO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->topA->data); @@ -8733,7 +8690,7 @@ internal int32_t cropElemAtInt32SmallJson(smallJsont *self, intmax_t index) { return(0); } r = e->f->get(e); - finishO(e); + terminateO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->topA->data); @@ -8780,7 +8737,7 @@ internal uint64_t cropElemAtUintSmallJson(smallJsont *self, intmax_t index) { return(0); } r = e->f->get(e); - finishO(e); + terminateO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->topA->data); @@ -8827,7 +8784,7 @@ internal uint32_t cropElemAtUint32SmallJson(smallJsont *self, intmax_t index) { return(0); } r = e->f->get(e); - finishO(e); + terminateO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->topA->data); @@ -8873,8 +8830,8 @@ internal char* cropElemAtSSmallJson(smallJsont *self, intmax_t index) { finishO(e); return(NULL); } - r = e->f->get(e); - finishO(e); + r = dupS(e->f->get(e)); + terminateO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->topA->data); @@ -8921,7 +8878,7 @@ internal char cropElemAtCharSmallJson(smallJsont *self, intmax_t index) { string[len-1] = 0; } else { - strLCpy(string+index, len-1, string+index+1); + strLCpy(string+index, len - index, string+index+1); } return(r); @@ -9298,10 +9255,10 @@ internal void* cropElemAtVoidSmallJson(smallJsont *self, intmax_t index) { } if (checkObjectTypes && !isOSmallContainer(e)) { finishO(e); - return(false); + return(NULL); } r = e->f->get(e); - finishO(e); + smashO(e); // copy pointers from range index+1, array->count to index smallt **arr = &(self->topA->data); @@ -9466,7 +9423,7 @@ internal bool cropElemKeyBoolSmallJson(smallJsont *self, char* key) { e->key = NULL; bool r = o->f->get(o); - finishO(o); + terminateO(o); return(r); } } @@ -9508,7 +9465,7 @@ internal double cropElemKeyDoubleSmallJson(smallJsont *self, char* key) { e->key = NULL; double r = o->f->get(o); - finishO(o); + terminateO(o); return(r); } } @@ -9550,7 +9507,7 @@ internal int64_t cropElemKeyIntSmallJson(smallJsont *self, char* key) { e->key = NULL; int64_t r = o->f->get(o); - finishO(o); + terminateO(o); return(r); } } @@ -9592,7 +9549,7 @@ internal int32_t cropElemKeyInt32SmallJson(smallJsont *self, char* key) { e->key = NULL; int32_t r = o->f->get(o); - finishO(o); + terminateO(o); return(r); } } @@ -9634,7 +9591,7 @@ internal uint64_t cropElemKeyUintSmallJson(smallJsont *self, char* key) { e->key = NULL; uint64_t r = o->f->get(o); - finishO(o); + terminateO(o); return(r); } } @@ -9676,7 +9633,7 @@ internal uint32_t cropElemKeyUint32SmallJson(smallJsont *self, char* key) { e->key = NULL; uint32_t r = o->f->get(o); - finishO(o); + terminateO(o); return(r); } } @@ -9717,8 +9674,8 @@ internal char* cropElemKeySSmallJson(smallJsont *self, char* key) { free(e->key); e->key = NULL; - char *r = o->f->get(o); - finishO(o); + char *r = dupS(o->f->get(o)); + terminateO(o); return(r); } } @@ -10038,11 +9995,11 @@ internal void* cropElemKeyVoidSmallJson(smallJsont *self, char* key) { if (!sm) { return(NULL); } - o = (smallContainert*)toBaset(sm); - if (!o) { + if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { return(NULL); } - if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { + o = (smallContainert*)toBaset(sm); + if (!o) { return(NULL); } if (checkObjectTypes && o && !isOSmallContainer(o)) { @@ -10053,7 +10010,7 @@ internal void* cropElemKeyVoidSmallJson(smallJsont *self, char* key) { e->key = NULL; void *r = o->f->get(o); - finishO(o); + terminateO(o); return(r); } } @@ -10080,10 +10037,10 @@ internal smallContainert* cropElemKeySmallContainerSmallJson(smallJsont *self, c if (!sm) { return(NULL); } - r = (smallContainert*)toBaset(sm); if ((sm->type == CONTAINER) && (((sContainert*)sm)->dataType == SH_DT_BASET)) { return(NULL); } + r = (smallContainert*)toBaset(sm); if (checkObjectTypes && r && !isOSmallContainer(r)) { finishO(r); return(NULL); @@ -10159,8 +10116,7 @@ internal smallJsont* copySmallJson(smallJsont *self, intmax_t start, intmax_t en } } break; - case TOP_IS_STRING: - r->f->setTypeString(r); + case TOP_IS_STRING:; intmax_t n; if (start == end) { @@ -10189,7 +10145,11 @@ internal smallJsont* insertSmallJson(smallJsont *self, intmax_t index, smallArra size_t len;; //sanity checks - if (self->topIsA != TOP_IS_ARRAY || !toInsert) { + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_ARRAY) { + return(NULL); + } + + if (!toInsert) { return(NULL); } @@ -10197,6 +10157,14 @@ internal smallJsont* insertSmallJson(smallJsont *self, intmax_t index, smallArra return(NULL); } + if (self->topA == toInsert->a) { + return(NULL); + } + + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + len = lenSmallJson(self); if (!len && index == -1) { @@ -10274,7 +10242,7 @@ internal smallJsont* insertNSmashSmallJson(smallJsont *self, intmax_t index, sma return(r); } -internal smallJsont* insertJsonSmallJson(smallJsont *self, intmax_t index, smallJsont *toInsert) { +internal smallJsont* insertSmallJsonSmallJson(smallJsont *self, intmax_t index, smallJsont *toInsert) { size_t len;; //sanity checks @@ -10286,6 +10254,10 @@ internal smallJsont* insertJsonSmallJson(smallJsont *self, intmax_t index, small return(NULL); } + if (self->topA == toInsert->topA) { + return(NULL); + } + len = lenSmallJson(self); if (!len && index == -1) { @@ -10310,7 +10282,11 @@ internal smallJsont* insertJsonSmallJson(smallJsont *self, intmax_t index, small index = len + index; } - if (toInsert->topIsA != TOP_IS_ARRAY || !toInsert->f->len(toInsert)) { + if (toInsert->topIsA != TOP_IS_ARRAY) { + return(NULL); + } + + if (!toInsert->f->len(toInsert)) { return(self); } @@ -10353,10 +10329,10 @@ internal smallJsont* insertJsonSmallJson(smallJsont *self, intmax_t index, small return(self); } -internal smallJsont* insertJsonNSmashSmallJson(smallJsont *self, intmax_t index, smallJsont *toInsert) { +internal smallJsont* insertNSmashSmallJsonSmallJson(smallJsont *self, intmax_t index, smallJsont *toInsert) { smallJsont *r = NULL; - r = insertJsonSmallJson(self, index, toInsert); + r = insertSmallJsonSmallJson(self, index, toInsert); if (r) { smashO(toInsert); } @@ -10367,9 +10343,24 @@ internal smallJsont* insertStringSmallJson(smallJsont *self, intmax_t index, sma char *r = NULL; // sanity checks - if (self->topIsA != TOP_IS_STRING || !toInsert) { + if (checkObjectTypes && toInsert && !isOSmallString(toInsert)) { + return(NULL); + } + if (!toInsert) { + return(NULL); + } + if (self->topIsA == SMALLJSON_IS_EMPTY && !index) { + if (toInsert->data) { + self->topS = (sStringt*)sDuplicateTiny((smallt*)toInsert->data); + self->topIsA = TOP_IS_STRING; + } + // TODO self->_len = toInsert->_len + return(self); + } + else if (self->topIsA != TOP_IS_STRING) { return(NULL); } + if (!self->topS) { if (index) { return(NULL); @@ -10429,7 +10420,17 @@ internal smallJsont* insertSSmallJson(smallJsont *self, intmax_t index, const ch char *r = NULL; // sanity checks - if (self->topIsA != TOP_IS_STRING || !toInsert) { + if (!toInsert) { + return(self); + } + if (self->topIsA == SMALLJSON_IS_EMPTY && !index) { + char header[2] = {STRING ,0}; + self->topS = (sStringt*) catS(header, toInsert); + self->topIsA = TOP_IS_STRING; + // TODO self->_len = toInsert->_len + return(self); + } + if (self->topIsA != TOP_IS_STRING) { return(NULL); } if (!self->topS) { @@ -10508,10 +10509,18 @@ internal smallJsont* insertSNFreeSmallJson(smallJsont *self, intmax_t index, cha internal smallJsont* injectSmallJson(smallJsont *self, intmax_t index, baset *toInject) { size_t len; - if (self->topIsA != TOP_IS_ARRAY || !toInject) { + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_ARRAY) { + return(NULL); + } + + if (!toInject) { return(NULL); } + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + len = lenSmallJson(self); if (!len && index == -1) { @@ -10578,10 +10587,14 @@ internal smallJsont* injectSmallJson(smallJsont *self, intmax_t index, baset *to internal smallJsont* injectUndefinedSmallJson(smallJsont *self, intmax_t index) { size_t len; - if (self->topIsA != TOP_IS_ARRAY) { + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_ARRAY) { return(NULL); } + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + len = lenSmallJson(self); if (!len && index == -1) { @@ -10648,10 +10661,14 @@ internal smallJsont* injectUndefinedSmallJson(smallJsont *self, intmax_t index) internal smallJsont* injectBoolSmallJson(smallJsont *self, intmax_t index, bool toInject) { size_t len; - if (self->topIsA != TOP_IS_ARRAY) { + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_ARRAY) { return(NULL); } + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + len = lenSmallJson(self); if (!len && index == -1) { @@ -10719,10 +10736,14 @@ internal smallJsont* injectBoolSmallJson(smallJsont *self, intmax_t index, bool internal smallJsont* injectDoubleSmallJson(smallJsont *self, intmax_t index, double toInject) { size_t len; - if (self->topIsA != TOP_IS_ARRAY) { + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_ARRAY) { return(NULL); } + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + len = lenSmallJson(self); if (!len && index == -1) { @@ -10790,10 +10811,14 @@ internal smallJsont* injectDoubleSmallJson(smallJsont *self, intmax_t index, dou internal smallJsont* injectIntSmallJson(smallJsont *self, intmax_t index, int64_t toInject) { size_t len; - if (self->topIsA != TOP_IS_ARRAY) { + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_ARRAY) { return(NULL); } + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + len = lenSmallJson(self); if (!len && index == -1) { @@ -10869,6 +10894,10 @@ internal smallJsont* injectSSmallJson(smallJsont *self, intmax_t index, const ch return(insertSSmallJson(self, index, toInject)); } + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + if (self->topIsA != TOP_IS_ARRAY) { return(NULL); } @@ -10949,7 +10978,7 @@ internal smallJsont* injectDictSmallJson(smallJsont *self, intmax_t index, small return(NULL); } - if (self->topIsA != TOP_IS_ARRAY || !toInject) { + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_ARRAY) { return(NULL); } @@ -10957,6 +10986,10 @@ internal smallJsont* injectDictSmallJson(smallJsont *self, intmax_t index, small return(NULL); } + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + len = lenSmallJson(self); if (!len && index == -1) { @@ -10987,6 +11020,10 @@ internal smallJsont* injectDictSmallJson(smallJsont *self, intmax_t index, small return(NULL); } + if (!toInject->d) { + isError(toInject->d, allocSDict()) return(NULL); + } + smallt *o = (smallt *) toInject->d; if (index == 0) { @@ -11023,7 +11060,7 @@ internal smallJsont* injectArraySmallJson(smallJsont *self, intmax_t index, smal return(NULL); } - if (self->topIsA != TOP_IS_ARRAY) { + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_ARRAY) { return(NULL); } @@ -11031,6 +11068,10 @@ internal smallJsont* injectArraySmallJson(smallJsont *self, intmax_t index, smal return(NULL); } + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + len = lenSmallJson(self); if (!len && index == -1) { @@ -11061,6 +11102,10 @@ internal smallJsont* injectArraySmallJson(smallJsont *self, intmax_t index, smal return(NULL); } + if (!toInject->a) { + isError(toInject->a, allocSArray()) return(NULL); + } + smallt *o = (smallt *) toInject->a; if (index == 0) { @@ -11097,10 +11142,14 @@ internal smallJsont* injectArraycSmallJson(smallJsont *self, intmax_t index, cha return(NULL); } - if (self->topIsA != TOP_IS_ARRAY) { + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_ARRAY) { return(NULL); } + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + len = lenSmallJson(self); if (!len && index == -1) { @@ -11188,6 +11237,10 @@ internal smallJsont* injectSmallBoolSmallJson(smallJsont *self, intmax_t index, return(NULL); } + if (!toInject->value) { + isError(toInject->value, allocSBool(false)) return(NULL); + } + return(injectSmallJson(self, index, (baset*)toInject)); } @@ -11201,6 +11254,10 @@ internal smallJsont* injectSmallBytesSmallJson(smallJsont *self, intmax_t index, return(NULL); } + if (!toInject->B) { + isError(toInject->B, allocSBytes()) return(NULL); + } + return(injectSmallJson(self, index, (baset*)toInject)); } @@ -11214,6 +11271,10 @@ internal smallJsont* injectSmallDoubleSmallJson(smallJsont *self, intmax_t index return(NULL); } + if (!toInject->value) { + isError(toInject->value, allocSDouble(0)) return(NULL); + } + return(injectSmallJson(self, index, (baset*)toInject)); } @@ -11227,6 +11288,10 @@ internal smallJsont* injectSmallIntSmallJson(smallJsont *self, intmax_t index, s return(NULL); } + if (!toInject->value) { + isError(toInject->value, allocSInt(0)) return(NULL); + } + return(injectSmallJson(self, index, (baset*)toInject)); } @@ -11240,11 +11305,15 @@ internal smallJsont* injectSmallJsonSmallJson(smallJsont *self, intmax_t index, return(NULL); } - baset *O = NULL; - if (toInject) { - O = getTopSmallJson(toInject); + smallt *o = getsoO(toInject); + if (!o) { + // smallJson is empty, create an empty dict + isError(o, (smallt*)allocSDict()) return(NULL); + setsoO(toInject, o); } + baset *O = getTopO(toInject); + smallJsont *r = injectSmallJson(self, index, O); finishO(O); return(r); @@ -11264,10 +11333,10 @@ internal smallJsont* injectSmallStringSmallJson(smallJsont *self, intmax_t index return(insertStringSmallJson(self, index, toInject)); } - if (!toInject || !toInject->data) { - // inject empty string when toInject is NULL - return(injectSSmallJson(self, index, NULL)); + if (!toInject->data) { + isError(toInject->data, allocSStringTiny("")) return(NULL); } + return(injectSmallJson(self, index, (baset*)toInject)); } @@ -11281,16 +11350,28 @@ internal smallJsont* injectSmallContainerSmallJson(smallJsont *self, intmax_t in return(NULL); } + if (!toInject->data) { + isError(toInject->data, allocSContainer(NULL)) return(NULL); + } + return(injectSmallJson(self, index, (baset*)toInject)); } internal smallJsont* injectNFreeSmallJson(smallJsont *self, intmax_t index, baset *toInject) { size_t len; - if (self->topIsA != TOP_IS_ARRAY || !toInject) { + if (!toInject) { + return(NULL); + } + + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_ARRAY) { return(NULL); } + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + len = lenSmallJson(self); if (!len && index == -1) { @@ -11416,7 +11497,7 @@ internal smallJsont* injectNFreeArraycSmallJson(smallJsont *self, intmax_t index if (r) { listFreeS(toInject); } - return(self); + return(r); } internal smallJsont* injectNFreeSmallBoolSmallJson(smallJsont *self, intmax_t index, smallBoolt *toInject) { @@ -11551,19 +11632,27 @@ internal smallJsont* uniqSmallJson(smallJsont *self) { // push element to new list if it is not already in new list - bool pushE = false; + bool pushE = false; + bool foundUndefined = false; forEachSArray(self->topA, e) { if (e) { switch (e->type) { case UNDEFINED: - if (r->f->indexOfUndefined(r, (undefinedt*)e) == -1) { + if (!foundUndefined) { pushE = true; + foundUndefined = true; + } + else { + free(e); } break; case BOOL: if (r->f->indexOfBool(r, ((sBoolt*)e)->value) == -1) { pushE = true; } + else { + free(e); + } break; // TODO case CONTAINER: case DICT: @@ -11571,33 +11660,51 @@ internal smallJsont* uniqSmallJson(smallJsont *self) { if (r->f->indexOfDict(r, d) == -1) { pushE = true; } + else { + freeO(d); + } break; case DOUBLE: if (r->f->indexOfDouble(r, ((sDoublet*)e)->value) == -1) { pushE = true; } + else { + free(e); + } break; case INT: if (r->f->indexOfInt(r, ((sIntt*)e)->value) == -1) { pushE = true; } + else { + free(e); + } break; case STRING: if (r->f->indexOfS(r, sStringGetTiny((sStringt*)e)) == -1) { pushE = true; } + else { + free(e); + } break; case ARRAY: a->a = (sArrayt*)e; if (r->f->indexOfArray(r, a) == -1) { pushE = true; } + else { + freeO(a); + } break; case BYTES: B->B = (sBytest*)e; if (r->f->indexOfSmallBytes(r, B) == -1) { pushE = true; } + else { + freeO(B); + } break; } } @@ -11618,17 +11725,17 @@ internal smallJsont* uniqSmallJson(smallJsont *self) { internal int sortSCmp(const void * a, const void * b) { // sanity checks - if (!a && !b) { + smallt *A = (*((smallt **)a)); + smallt *B = (*((smallt **)b)); + if (!A && !B) { return(0); } - if (!a && b) { + if (!A && B) { return(-1); } - if (a && !b) { + if (A && !B) { return(1); } - smallt *A = (*((smallt **)a)); - smallt *B = (*((smallt **)b)); char *As, *Bs; int r; if (A->type != B->type) { @@ -11639,7 +11746,7 @@ internal int sortSCmp(const void * a, const void * b) { case UNDEFINED: return(0); case BOOL: - return(((sBoolt*)A)->value < ((sBoolt*)B)->value); + return(((sBoolt*)A)->value < ((sBoolt*)B)->value * -1); case CONTAINER: return(0); case DICT: @@ -11780,17 +11887,17 @@ internal int sortFCmp(const void *a, const void *b, void *func) { internal int icSortSCmp(const void * a, const void * b) { // sanity checks - if (!a && !b) { + smallt *A = (*((smallt **)a)); + smallt *B = (*((smallt **)b)); + if (!A && !B) { return(0); } - if (!a && b) { + if (!A && B) { return(-1); } - if (a && !b) { + if (A && !B) { return(1); } - smallt *A = (*((smallt **)a)); - smallt *B = (*((smallt **)b)); char *As, *Bs; int r; if (A->type != B->type) { @@ -11801,7 +11908,7 @@ internal int icSortSCmp(const void * a, const void * b) { case UNDEFINED: return(0); case BOOL: - return(((sBoolt*)A)->value < ((sBoolt*)B)->value); + return(((sBoolt*)A)->value < ((sBoolt*)B)->value * -1); case CONTAINER: return(0); case DICT: @@ -11980,19 +12087,27 @@ internal smallJsont* icUniqSmallJson(smallJsont *self) { // push element to new list if it is not already in new list - bool pushE = false; + bool pushE = false; + bool foundUndefined = false; forEachSArray(self->topA, e) { if (e) { switch (e->type) { case UNDEFINED: - if (r->f->indexOfUndefined(r, (undefinedt*)e) == -1) { + if (!foundUndefined) { pushE = true; + foundUndefined = true; + } + else { + free(e); } break; case BOOL: if (r->f->indexOfBool(r, ((sBoolt*)e)->value) == -1) { pushE = true; } + else { + free(e); + } break; // TODO case CONTAINER: case DICT: @@ -12000,33 +12115,51 @@ internal smallJsont* icUniqSmallJson(smallJsont *self) { if (r->f->icIndexOfDict(r, d) == -1) { pushE = true; } + else { + freeO(d); + } break; case DOUBLE: if (r->f->indexOfDouble(r, ((sDoublet*)e)->value) == -1) { pushE = true; } + else { + free(e); + } break; case INT: if (r->f->indexOfInt(r, ((sIntt*)e)->value) == -1) { pushE = true; } + else { + free(e); + } break; case STRING: if (r->f->icIndexOfS(r, sStringGetTiny((sStringt*)e)) == -1) { pushE = true; } + else { + free(e); + } break; case ARRAY: a->a = (sArrayt*)e; if (r->f->icIndexOfArray(r, a) == -1) { pushE = true; } + else { + freeO(a); + } break; case BYTES: B->B = (sBytest*)e; if (r->f->indexOfSmallBytes(r, B) == -1) { pushE = true; } + else { + freeO(B); + } break; } } @@ -12090,7 +12223,7 @@ internal smallJsont* icUniqCharSmallJson(smallJsont *self, char c) { // index in r size_t j = 1;; rangeFrom(i, 1, lenSmallJson(self)) - if (string[i-1] == c && string[i] == c) { + if (tolower(string[i-1]) == c && tolower(string[i]) == c) { // skip continue; } @@ -12376,8 +12509,21 @@ internal smallJsont* findSmallJson(smallJsont *self, const char *needle) { internal smallJsont* findCharSmallJson(smallJsont *self, char c) { - charToS(s, c); - return(findSmallJson(self, s)); + // sanity checks + if (self->topIsA != TOP_IS_STRING || !self->topS || !lenSmallJson(self)) { + return(NULL); + } + + char *s = strchr(sStringGetTiny(self->topS), c); + if (!s) { + return(NULL); + } + createAllocateSmallJson(r); + if (!r) { + return(NULL); + } + r->f->setTopString(r, s); + return(r); } internal smallJsont* findSmallStringSmallJson(smallJsont *self, smallStringt *needle) { @@ -12385,6 +12531,10 @@ internal smallJsont* findSmallStringSmallJson(smallJsont *self, smallStringt *ne if (!needle) { return(NULL); } + if (checkObjectTypes && !isOSmallString(needle)) { + return(NULL); + } + return(findSmallJson(self, ssGet(needle))); } @@ -12393,6 +12543,10 @@ internal smallJsont* findJsonSmallJson(smallJsont *self, smallJsont *needle) { if (!needle) { return(NULL); } + if (checkObjectTypes && !isOSmallJson(needle)) { + return(NULL); + } + return(findSmallJson(self, sjGet(needle))); } @@ -12648,8 +12802,6 @@ internal ssize_t indexOfArraycSmallJson(smallJsont *self, char **array) { // search object in elements char *s = toStringListSGF(array); - iPrependCharS(&s, '['); - iAppendCharS(&s, ']'); char *es = NULL;; enumerateSArray(self->topA, e, i) { @@ -12853,7 +13005,7 @@ internal ssize_t indexOfSmallStringSmallJson(smallJsont *self, smallStringt *str return(-1); } - if (!string || !string->data) { + if (!string) { return(-1); } @@ -12861,6 +13013,10 @@ internal ssize_t indexOfSmallStringSmallJson(smallJsont *self, smallStringt *str return(-1); } + if (!string->data) { + return(-1); + } + // search string in elements char *s = sStringGetTiny(string->data); @@ -12909,6 +13065,11 @@ internal ssize_t binarySearchSmallJson(smallJsont *self, baset *value) { while (first <= last) { middle = (first+last)/2; m = sToString(sArrayGetTiny(self->topA, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (strcmp(m, s) < 0) { first = middle + 1; } @@ -12927,43 +13088,41 @@ internal ssize_t binarySearchSmallJson(smallJsont *self, baset *value) { return(r); } -internal ssize_t binarySearchUndefinedSmallJson(smallJsont *self, undefinedt *undefined) { - ssize_t first = 0, middle, last;; - - if (self->topIsA != TOP_IS_ARRAY) { - return(-1); - } - - if (!lenSmallJson(self) || !undefined) { - return(-1); - } - - if (checkObjectTypes && !isOUndefined(undefined)) { - return(-1); - } - - smallt *m = NULL; +internal ssize_t binarySearchUndefinedSmallJson(smallJsont *self UNUSED, undefinedt *undefined UNUSED) { + // ssize_t first = 0, middle, last - ssize_t r = -1; - last = lenSmallJson(self) - 1; - while (first <= last) { - middle = (first+last)/2; - m = sArrayGetTiny(self->topA, middle); - if (m->type < UNDEFINED) { - first = middle + 1; - } - else if (m->type == UNDEFINED) { - r = middle; - goto finish; - } - else { - last = middle -1; - } - } - finish: - return(r); + // it doesn't make sense to search for undefined + // the array has to be sorted + // so undefined is always at postion 0 + return(-1); } + /* if self->topIsA != TOP_IS_ARRAY */ + /* return -1 */ + /* */ + /* if !lenSmallJson(self) || !undefined */ + /* return -1 */ + /* */ + /* if checkObjectTypes && !isOUndefined(undefined) */ + /* return -1 */ + /* */ + /* smallt *m = NULL */ + /* */ + /* ssize_t r = -1 */ + /* last = lenSmallJson(self) - 1 */ + /* while first <= last */ + /* middle = (first+last)/2 */ + /* m = sArrayGetTiny(self->topA, middle) */ + /* if m->type < UNDEFINED */ + /* first = middle + 1 */ + /* else if m->type = UNDEFINED */ + /* r = middle */ + /* goto finish; */ + /* else */ + /* last = middle -1 */ + /* finish: */ + /* return r */ + internal ssize_t binarySearchBoolSmallJson(smallJsont *self, bool value) { ssize_t first = 0, middle, last;; @@ -12982,6 +13141,11 @@ internal ssize_t binarySearchBoolSmallJson(smallJsont *self, bool value) { while (first <= last) { middle = (first+last)/2; m = (sBoolt*)(sArrayGetTiny(self->topA, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (m->type < BOOL || ((m->type == BOOL) && (m->value < value))) { first = middle + 1; } @@ -13016,6 +13180,11 @@ internal ssize_t binarySearchDoubleSmallJson(smallJsont *self, double value) { while (first <= last) { middle = (first+last)/2; m = (sDoublet*)(sArrayGetTiny(self->topA, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (m->type < DOUBLE || ((m->type == DOUBLE) && (m->value < value))) { first = middle + 1; } @@ -13049,6 +13218,11 @@ internal ssize_t binarySearchIntSmallJson(smallJsont *self, int64_t value) { while (first <= last) { middle = (first+last)/2; m = (sIntt*)(sArrayGetTiny(self->topA, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (m->type < INT || ((m->type == INT) && (m->value < value))) { first = middle + 1; } @@ -13081,6 +13255,11 @@ internal ssize_t binarySearchSSmallJson(smallJsont *self, const char *string) { while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->topA, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + return(-1); + } char *m = sStringGetTiny((sStringt*)(e)); if (e->type < STRING || ((e->type == STRING) && (strcmp(m, string) < 0))) { first = middle + 1; @@ -13125,6 +13304,11 @@ internal ssize_t binarySearchDictSmallJson(smallJsont *self, smallDictt *dict) { while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->topA, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } // cg_c bug, can't use comma operator, extra code if (e->type == DICT) { m = sToString(e); @@ -13175,6 +13359,11 @@ internal ssize_t binarySearchArraySmallJson(smallJsont *self, smallArrayt *array while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->topA, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } // cg_c bug, can't use comma operator, extra code if (e->type == ARRAY) { m = sToString(e); @@ -13213,8 +13402,6 @@ internal ssize_t binarySearchArraycSmallJson(smallJsont *self, char **array) { } char *s = toStringListSGF(array); - iPrependCharS(&s, '['); - iAppendCharS(&s, ']'); smallt *e = NULL; char *m = NULL; @@ -13223,6 +13410,11 @@ internal ssize_t binarySearchArraycSmallJson(smallJsont *self, char **array) { while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->topA, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } // cg_c bug, can't use comma operator, extra code if (e->type == ARRAY) { m = sToString(e); @@ -13272,6 +13464,11 @@ internal ssize_t binarySearchSmallBoolSmallJson(smallJsont *self, smallBoolt *va while (first <= last) { middle = (first+last)/2; m = (sBoolt*)(sArrayGetTiny(self->topA, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (m->type < BOOL || ((m->type == BOOL) && (m->value < b))) { first = middle + 1; } @@ -13311,6 +13508,11 @@ internal ssize_t binarySearchSmallBytesSmallJson(smallJsont *self, smallBytest * while (first <= last) { middle = (first+last)/2; m = (sBytest*)(sArrayGetTiny(self->topA, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (m->type < BYTES || ((m->type == BYTES) && ((m->count < count) || (memcmp(&m->data, b, count) < 0)))) { first = middle + 1; } @@ -13349,6 +13551,11 @@ internal ssize_t binarySearchSmallDoubleSmallJson(smallJsont *self, smallDoublet while (first <= last) { middle = (first+last)/2; m = (sDoublet*)(sArrayGetTiny(self->topA, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (m->type < DOUBLE || ((m->type == DOUBLE) && (m->value < v))) { first = middle + 1; } @@ -13387,6 +13594,11 @@ internal ssize_t binarySearchSmallIntSmallJson(smallJsont *self, smallIntt *valu while (first <= last) { middle = (first+last)/2; m = (sIntt*)(sArrayGetTiny(self->topA, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (m->type < INT || ((m->type == INT) && (m->value < v))) { first = middle + 1; } @@ -13422,10 +13634,10 @@ internal ssize_t binarySearchSmallJsonSmallJson(smallJsont *self, smallJsont *va case SMALLJSON_IS_EMPTY: return(-1); case TOP_IS_UNDEFINED: - O = getTopSmallJson(value); - r = binarySearchUndefinedSmallJson(self, (undefinedt*)O);; - finishO(O); - break; + // it doesn't make sense to search for undefined + // the array has to be sorted + // so undefined is always at postion 0 + return(-1); case TOP_IS_BOOL: O = getTopSmallJson(value); r = binarySearchSmallBoolSmallJson(self, (smallBoolt*)O);; @@ -13467,21 +13679,30 @@ internal ssize_t binarySearchSmallStringSmallJson(smallJsont *self, smallStringt return(-1); } - if (!lenSmallJson(self) || !string || !string->data) { + if (!lenSmallJson(self) || !string) { return(-1); } - char *s = sStringGetTiny(string->data); - smallt *e = NULL; - if (checkObjectTypes && !isOSmallString(string)) { return(-1); } + if (!string->data) { + return(-1); + } + + char *s = sStringGetTiny(string->data); + smallt *e = NULL; + last = lenSmallJson(self) - 1; while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->topA, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + return(-1); + } char *m = sStringGetTiny((sStringt*)(e)); if (e->type < STRING || ((e->type == STRING) && (strcmp(m, s) < 0))) { first = middle + 1; @@ -13619,6 +13840,27 @@ internal smallJsont* icFindSmallJson(smallJsont *self, const char *needle) { internal smallJsont* icFindCharSmallJson(smallJsont *self, char c) { + if (c == 0) { + // include terminating '\0' in comparison + // like findCharS + // sanity checks + if (self->topIsA != TOP_IS_STRING || !self->topS || !lenSmallJson(self)) { + return(NULL); + } + char *s = strchr(sStringGetTiny(self->topS), c); + if (!s) { + // this should not happen because strings end with 0 + // if there is no 0 at the end of the string, strchr will stop when it finds a 0 or crash the program + // anyway, it is wrong + return(NULL); + } + createAllocateSmallJson(r); + if (!r) { + return(NULL); + } + r->f->setTopString(r, s); + return(r); + } charToS(s, c); return(icFindSmallJson(self, s)); } @@ -13628,6 +13870,10 @@ internal smallJsont* icFindSmallStringSmallJson(smallJsont *self, smallStringt * if (!needle) { return(NULL); } + if (checkObjectTypes && !isOSmallString(needle)) { + return(NULL); + } + return(icFindSmallJson(self, ssGet(needle))); } @@ -13636,6 +13882,10 @@ internal smallJsont* icFindJsonSmallJson(smallJsont *self, smallJsont *needle) { if (!needle) { return(NULL); } + if (checkObjectTypes && !isOSmallJson(needle)) { + return(NULL); + } + return(icFindSmallJson(self, sjGet(needle))); } @@ -13795,8 +14045,6 @@ internal ssize_t icIndexOfArraycSmallJson(smallJsont *self, char **array) { // search object in elements char *s = toStringListSGF(array); - iPrependCharS(&s, '['); - iAppendCharS(&s, ']'); char *es = NULL;; enumerateSArray(self->topA, e, i) { @@ -13824,7 +14072,7 @@ internal ssize_t icIndexOfSmallStringSmallJson(smallJsont *self, smallStringt *s return(-1); } - if (!string || !string->data) { + if (!string) { return(-1); } @@ -13832,6 +14080,10 @@ internal ssize_t icIndexOfSmallStringSmallJson(smallJsont *self, smallStringt *s return(-1); } + if (!string->data) { + return(-1); + } + // search string in elements char *s = sStringGetTiny(string->data); @@ -13875,10 +14127,15 @@ internal ssize_t icBinarySearchSmallJson(smallJsont *self, baset *value) { while (first <= last) { middle = (first+last)/2; m = sToString(sArrayGetTiny(self->topA, middle)); + if (!m) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } if (strcasecmp(m, s) < 0) { first = middle + 1; } - else if (strcmp(m, s) == 0) { + else if (strcasecmp(m, s) == 0) { r = middle; free(m); goto finish; @@ -13910,6 +14167,11 @@ internal ssize_t icBinarySearchSSmallJson(smallJsont *self, const char *string) while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->topA, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + return(-1); + } char *m = sStringGetTiny((sStringt*)(e)); if (e->type < STRING || ((e->type == STRING) && (strcasecmp(m, string) < 0))) { first = middle + 1; @@ -13954,6 +14216,11 @@ internal ssize_t icBinarySearchDictSmallJson(smallJsont *self, smallDictt *dict) while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->topA, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } // cg_c bug, can't use comma operator, extra code if (e->type == DICT) { m = sToString(e); @@ -14004,6 +14271,11 @@ internal ssize_t icBinarySearchArraySmallJson(smallJsont *self, smallArrayt *arr while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->topA, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } // cg_c bug, can't use comma operator, extra code if (e->type == ARRAY) { m = sToString(e); @@ -14042,8 +14314,6 @@ internal ssize_t icBinarySearchArraycSmallJson(smallJsont *self, char **array) { } char *s = toStringListSGF(array); - iPrependCharS(&s, '['); - iAppendCharS(&s, ']'); smallt *e = NULL; char *m = NULL; @@ -14052,6 +14322,11 @@ internal ssize_t icBinarySearchArraycSmallJson(smallJsont *self, char **array) { while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->topA, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + goto finish; + } // cg_c bug, can't use comma operator, extra code if (e->type == ARRAY) { m = sToString(e); @@ -14085,7 +14360,7 @@ internal ssize_t icBinarySearchSmallStringSmallJson(smallJsont *self, smallStrin return(-1); } - if (!lenSmallJson(self) || !string || !string->data) { + if (!lenSmallJson(self) || !string) { return(-1); } @@ -14093,6 +14368,10 @@ internal ssize_t icBinarySearchSmallStringSmallJson(smallJsont *self, smallStrin return(-1); } + if (!string->data) { + return(-1); + } + char *s = sStringGetTiny(string->data); smallt *e = NULL; @@ -14100,6 +14379,11 @@ internal ssize_t icBinarySearchSmallStringSmallJson(smallJsont *self, smallStrin while (first <= last) { middle = (first+last)/2; e = sArrayGetTiny(self->topA, middle); + if (!e) { + // there are null element in the array + // the array needs to be trimmed or compacted + return(-1); + } char *m = sStringGetTiny((sStringt*)(e)); if (e->type < STRING || ((e->type == STRING) && (strcasecmp(m, s) < 0))) { first = middle + 1; @@ -14334,8 +14618,6 @@ internal char* keyByArraycSmallJson(smallJsont *self, char **array) { // search object in elements char *s = toStringListSGF(array); - iPrependCharS(&s, '['); - iAppendCharS(&s, ']'); char *es = NULL;; forEachSDict(self->top, e) { @@ -14528,7 +14810,7 @@ internal char* keyBySmallStringSmallJson(smallJsont *self, smallStringt *string) return(NULL); } - if (!string || !string->data) { + if (!string) { return(NULL); } @@ -14536,6 +14818,10 @@ internal char* keyBySmallStringSmallJson(smallJsont *self, smallStringt *string) return(NULL); } + if (!string->data) { + return(NULL); + } + // search string in elements char *s = sStringGetTiny(string->data); @@ -14700,8 +14986,6 @@ internal char* icKeyByArraycSmallJson(smallJsont *self, char **array) { // search object in elements char *s = toStringListSGF(array); - iPrependCharS(&s, '['); - iAppendCharS(&s, ']'); char *es = NULL;; forEachSDict(self->top, e) { @@ -14730,7 +15014,7 @@ internal char* icKeyBySmallStringSmallJson(smallJsont *self, smallStringt *strin return(NULL); } - if (!string || !string->data) { + if (!string) { return(NULL); } @@ -14738,6 +15022,10 @@ internal char* icKeyBySmallStringSmallJson(smallJsont *self, smallStringt *strin return(NULL); } + if (!string->data) { + return(NULL); + } + // search string in elements char *s = sStringGetTiny(string->data); @@ -14776,7 +15064,7 @@ internal smallJsont* replaceSmallJson(smallJsont *self, const char *olds, const } if (!lenSmallJson(self)) { // s is empty, do nothing - return(NULL); + return(self); } if (!olds) { return(NULL); @@ -14869,100 +15157,313 @@ internal smallJsont* replaceCharCharSmallJson(smallJsont *self, char olds, char internal smallJsont* replaceSmallStringSmallStringSmallJson(smallJsont *self, smallStringt *olds, smallStringt *news, size_t max) { - if (!olds || !news) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + + if (!olds) { + return(NULL); + } + if (checkObjectTypes && !isOSmallString(olds)) { + return(NULL); + } + + if (!news) { + return(replaceSmallStringSSmallJson(self, olds, "", max)); + } + + if (checkObjectTypes && !isOSmallString(news)) { return(NULL); } + return(replaceSmallJson(self, ssGet(olds), ssGet(news), max)); } internal smallJsont* replaceSmallStringSSmallJson(smallJsont *self, smallStringt *olds, const char *news, size_t max) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + if (!olds) { return(NULL); } + if (checkObjectTypes && !isOSmallString(olds)) { + return(NULL); + } + return(replaceSmallJson(self, ssGet(olds), news, max)); } internal smallJsont* replaceSmallStringCharSmallJson(smallJsont *self, smallStringt *olds, char news, size_t max) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + if (!olds) { return(NULL); } + if (checkObjectTypes && !isOSmallString(olds)) { + return(NULL); + } + charToS(s, news); return(replaceSmallJson(self, ssGet(olds), s, max)); } internal smallJsont* replaceSSmallStringSmallJson(smallJsont *self, const char *olds, smallStringt *news, size_t max) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + if (!news) { + return(replaceSmallJson(self, olds, "", max)); + } + + if (checkObjectTypes && !isOSmallString(news)) { return(NULL); } + return(replaceSmallJson(self, olds, ssGet(news), max)); } internal smallJsont* replaceCharSmallStringSmallJson(smallJsont *self, char olds, smallStringt *news, size_t max) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + if (!news) { + charToS(s, olds); + return(replaceSmallJson(self, s, "", max)); + } + if (checkObjectTypes && !isOSmallString(news)) { return(NULL); } + charToS(s, olds); return(replaceSmallJson(self, s, ssGet(news), max)); } internal smallJsont* replaceJsonJsonSmallJson(smallJsont *self, smallJsont *olds, smallJsont *news, size_t max) { - if (!olds || !news) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { return(NULL); } - return(replaceSmallJson(self, sjGet(olds), sjGet(news), max)); -} + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } -internal smallJsont* replaceJsonSmallStringSmallJson(smallJsont *self, smallJsont *olds, smallStringt *news, size_t max) { + if (!olds) { + return(NULL); + } - if (!olds || !news) { + if (checkObjectTypes && !isOSmallJson(olds)) { return(NULL); } - return(replaceSmallJson(self, sjGet(olds), ssGet(news), max)); -} + + if (!news) { + return(replaceJsonSSmallJson(self, olds, "", max)); + } + + if (checkObjectTypes && !isOSmallJson(news)) { + return(NULL); + } + + if (olds->topIsA != TOP_IS_STRING || news->topIsA != TOP_IS_STRING) { + return(NULL); + } + + return(replaceSmallJson(self, sjGet(olds), sjGet(news), max)); +} + +internal smallJsont* replaceJsonSmallStringSmallJson(smallJsont *self, smallJsont *olds, smallStringt *news, size_t max) { + + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + + if (!olds) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallJson(olds)) { + return(NULL); + } + + if (!news) { + return(replaceJsonSSmallJson(self, olds, "", max)); + } + + if (checkObjectTypes && !isOSmallString(news)) { + return(NULL); + } + + if (olds->topIsA != TOP_IS_STRING) { + return(NULL); + } + + return(replaceSmallJson(self, sjGet(olds), ssGet(news), max)); +} internal smallJsont* replaceJsonSSmallJson(smallJsont *self, smallJsont *olds, const char *news, size_t max) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + if (!olds) { return(NULL); } + if (checkObjectTypes && !isOSmallJson(olds)) { + return(NULL); + } + + if (olds->topIsA != TOP_IS_STRING) { + return(NULL); + } + return(replaceSmallJson(self, sjGet(olds), news, max)); } internal smallJsont* replaceJsonCharSmallJson(smallJsont *self, smallJsont *olds, char news, size_t max) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + if (!olds) { return(NULL); } + + if (checkObjectTypes && !isOSmallJson(olds)) { + return(NULL); + } + + if (olds->topIsA != TOP_IS_STRING) { + return(NULL); + } + charToS(s, news); return(replaceSmallJson(self, sjGet(olds), s, max)); } internal smallJsont* replaceSmallStringJsonSmallJson(smallJsont *self, smallStringt *olds, smallJsont *news, size_t max) { - if (!olds || !news) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + + if (!olds) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallString(olds)) { + return(NULL); + } + + if (!news) { + return(replaceSmallStringSSmallJson(self, olds, "", max)); + } + + if (checkObjectTypes && !isOSmallJson(news)) { return(NULL); } + + if (news->topIsA != TOP_IS_STRING) { + return(NULL); + } + return(replaceSmallJson(self, ssGet(olds), sjGet(news), max)); } internal smallJsont* replaceSJsonSmallJson(smallJsont *self, const char *olds, smallJsont *news, size_t max) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + if (!news) { + return(replaceSmallJson(self, olds, "", max)); + } + + if (checkObjectTypes && !isOSmallJson(news)) { + return(NULL); + } + + if (news->topIsA != TOP_IS_STRING) { return(NULL); } + return(replaceSmallJson(self, olds, sjGet(news), max)); } internal smallJsont* replaceCharJsonSmallJson(smallJsont *self, char olds, smallJsont *news, size_t max) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + if (!news) { + charToS(s, olds); + return(replaceSmallJson(self, s, "", max)); + } + + if (checkObjectTypes && !isOSmallJson(news)) { return(NULL); } + + if (news->topIsA != TOP_IS_STRING) { + return(NULL); + } + charToS(s, olds); return(replaceSmallJson(self, s, sjGet(news), max)); } @@ -14979,7 +15480,7 @@ internal smallJsont* replaceManySmallJson(smallJsont *self, const char *paramTyp } if (!lenSmallJson(self)) { - return(NULL); + return(self); } olds = (char *) paramType;; @@ -14987,13 +15488,23 @@ internal smallJsont* replaceManySmallJson(smallJsont *self, const char *paramTyp // add arguments to a list va_start(pl, paramType); paramType = va_arg(pl, char *); + if (!paramType) { + // missing news + va_end(pl); + return(NULL); + } while (paramType) { if (!olds) { olds = (char *) paramType; } else if (!news) { news = (char *) paramType; - replaceSmallJson(self, olds, news, 0); + smallJsont *r = replaceSmallJson(self, olds, news, 0); + if (!r) { + // replace error + va_end(pl); + return(NULL); + } olds = NULL; news = NULL; } @@ -15031,7 +15542,7 @@ internal smallJsont* icReplaceSmallJson(smallJsont *self, const char *olds, cons } if (!lenSmallJson(self)) { // s is empty, do nothing - return(NULL); + return(self); } if (!olds) { return(NULL); @@ -15124,127 +15635,358 @@ internal smallJsont* icReplaceCharCharSmallJson(smallJsont *self, char olds, cha internal smallJsont* icReplaceSmallStringSmallStringSmallJson(smallJsont *self, smallStringt *olds, smallStringt *news, size_t max) { - if (!olds || !news) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + + if (!olds) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallString(olds)) { + return(NULL); + } + + if (!news) { + return(icReplaceSmallStringSSmallJson(self, olds, "", max)); + } + + if (checkObjectTypes && !isOSmallString(news)) { return(NULL); } + return(icReplaceSmallJson(self, ssGet(olds), ssGet(news), max)); } internal smallJsont* icReplaceSmallStringSSmallJson(smallJsont *self, smallStringt *olds, const char *news, size_t max) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + if (!olds) { return(NULL); } + + if (checkObjectTypes && !isOSmallString(olds)) { + return(NULL); + } + return(icReplaceSmallJson(self, ssGet(olds), news, max)); } internal smallJsont* icReplaceSmallStringCharSmallJson(smallJsont *self, smallStringt *olds, char news, size_t max) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + if (!olds) { return(NULL); } + + if (checkObjectTypes && !isOSmallString(olds)) { + return(NULL); + } + charToS(s, news); return(icReplaceSmallJson(self, ssGet(olds), s, max)); } internal smallJsont* icReplaceSSmallStringSmallJson(smallJsont *self, const char *olds, smallStringt *news, size_t max) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + if (!news) { + return(icReplaceSmallJson(self, olds, "", max)); + } + + if (checkObjectTypes && !isOSmallString(news)) { return(NULL); } + return(icReplaceSmallJson(self, olds, ssGet(news), max)); } internal smallJsont* icReplaceCharSmallStringSmallJson(smallJsont *self, char olds, smallStringt *news, size_t max) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + + if (!news) { + charToS(s, olds); + return(icReplaceSmallJson(self, s, "", max)); + } + + if (checkObjectTypes && !isOSmallString(news)) { + return(NULL); + } + charToS(s, olds); return(icReplaceSmallJson(self, s, ssGet(news), max)); } internal smallJsont* icReplaceJsonJsonSmallJson(smallJsont *self, smallJsont *olds, smallJsont *news, size_t max) { - if (!olds || !news) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { return(NULL); } - return(icReplaceSmallJson(self, sjGet(olds), sjGet(news), max)); -} - -internal smallJsont* icReplaceJsonSmallStringSmallJson(smallJsont *self, smallJsont *olds, smallStringt *news, size_t max) { + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } - if (!olds || !news) { + if (!olds) { return(NULL); } - return(icReplaceSmallJson(self, sjGet(olds), ssGet(news), max)); -} - -internal smallJsont* icReplaceJsonSSmallJson(smallJsont *self, smallJsont *olds, const char *news, size_t max) { - if (!olds) { + if (checkObjectTypes && !isOSmallJson(olds)) { return(NULL); } - return(icReplaceSmallJson(self, sjGet(olds), news, max)); -} -internal smallJsont* icReplaceJsonCharSmallJson(smallJsont *self, smallJsont *olds, char news, size_t max) { + if (!news) { + return(icReplaceJsonSSmallJson(self, olds, "", max)); + } - if (!olds) { + if (checkObjectTypes && !isOSmallJson(news)) { return(NULL); } - charToS(s, news); - return(icReplaceSmallJson(self, sjGet(olds), s, max)); -} - -internal smallJsont* icReplaceSmallStringJsonSmallJson(smallJsont *self, smallStringt *olds, smallJsont *news, size_t max) { - if (!olds || !news) { + if (olds->topIsA != TOP_IS_STRING || news->topIsA != TOP_IS_STRING) { return(NULL); } - return(icReplaceSmallJson(self, ssGet(olds), sjGet(news), max)); + + return(icReplaceSmallJson(self, sjGet(olds), sjGet(news), max)); } -internal smallJsont* icReplaceSJsonSmallJson(smallJsont *self, const char *olds, smallJsont *news, size_t max) { +internal smallJsont* icReplaceJsonSmallStringSmallJson(smallJsont *self, smallJsont *olds, smallStringt *news, size_t max) { - if (!news) { + if (self->topIsA != TOP_IS_STRING || !self->topS) { return(NULL); } - return(icReplaceSmallJson(self, olds, sjGet(news), max)); -} + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } -internal smallJsont* icReplaceCharJsonSmallJson(smallJsont *self, char olds, smallJsont *news, size_t max) { + if (!olds) { + return(NULL); + } - if (!news) { + if (checkObjectTypes && !isOSmallJson(olds)) { return(NULL); } - charToS(s, olds); - return(icReplaceSmallJson(self, s, sjGet(news), max)); -} -internal smallJsont* icReplaceManySmallJson(smallJsont *self, const char *paramType, ...) { - va_list pl; - char *olds = NULL; - char *news = NULL; + if (!news) { + return(icReplaceJsonSSmallJson(self, olds, "", max)); + } - // sanity checks - if (self->topIsA != TOP_IS_STRING) { + if (checkObjectTypes && !isOSmallString(news)) { return(NULL); } - if (!lenSmallJson(self)) { + if (olds->topIsA != TOP_IS_STRING) { return(NULL); } - olds = (char *) paramType;; + return(icReplaceSmallJson(self, sjGet(olds), ssGet(news), max)); +} + +internal smallJsont* icReplaceJsonSSmallJson(smallJsont *self, smallJsont *olds, const char *news, size_t max) { + + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + + if (!olds) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallJson(olds)) { + return(NULL); + } + + if (olds->topIsA != TOP_IS_STRING) { + return(NULL); + } + + return(icReplaceSmallJson(self, sjGet(olds), news, max)); +} + +internal smallJsont* icReplaceJsonCharSmallJson(smallJsont *self, smallJsont *olds, char news, size_t max) { + + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + + if (!olds) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallJson(olds)) { + return(NULL); + } + + if (olds->topIsA != TOP_IS_STRING) { + return(NULL); + } + + charToS(s, news); + return(icReplaceSmallJson(self, sjGet(olds), s, max)); +} + +internal smallJsont* icReplaceSmallStringJsonSmallJson(smallJsont *self, smallStringt *olds, smallJsont *news, size_t max) { + + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + + if (!olds) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallString(olds)) { + return(NULL); + } + + if (!news) { + return(icReplaceSmallStringSSmallJson(self, olds, "", max)); + } + + if (checkObjectTypes && !isOSmallJson(news)) { + return(NULL); + } + + if (news->topIsA != TOP_IS_STRING) { + return(NULL); + } + + return(icReplaceSmallJson(self, ssGet(olds), sjGet(news), max)); +} + +internal smallJsont* icReplaceSJsonSmallJson(smallJsont *self, const char *olds, smallJsont *news, size_t max) { + + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + + if (!news) { + return(icReplaceSmallJson(self, olds, "", max)); + } + + if (checkObjectTypes && !isOSmallJson(news)) { + return(NULL); + } + + if (news->topIsA != TOP_IS_STRING) { + return(NULL); + } + + return(icReplaceSmallJson(self, olds, sjGet(news), max)); +} + +internal smallJsont* icReplaceCharJsonSmallJson(smallJsont *self, char olds, smallJsont *news, size_t max) { + + if (self->topIsA != TOP_IS_STRING || !self->topS) { + return(NULL); + } + if (!lenSmallJson(self)) { + // s is empty, do nothing + return(self); + } + + if (!news) { + charToS(s, olds); + return(icReplaceSmallJson(self, s, "", max)); + } + + if (checkObjectTypes && !isOSmallJson(news)) { + return(NULL); + } + + if (news->topIsA != TOP_IS_STRING) { + return(NULL); + } + + charToS(s, olds); + return(icReplaceSmallJson(self, s, sjGet(news), max)); +} + +internal smallJsont* icReplaceManySmallJson(smallJsont *self, const char *paramType, ...) { + va_list pl; + char *olds = NULL; + char *news = NULL; + + // sanity checks + if (self->topIsA != TOP_IS_STRING) { + return(NULL); + } + + if (!lenSmallJson(self)) { + return(self); + } + + olds = (char *) paramType;; // add arguments to a list va_start(pl, paramType); paramType = va_arg(pl, char *); + if (!paramType) { + // missing news + va_end(pl); + return(NULL); + } while (paramType) { if (!olds) { olds = (char *) paramType; } else if (!news) { news = (char *) paramType; - icReplaceSmallJson(self, olds, news, 0); + smallJsont *r = icReplaceSmallJson(self, olds, news, 0); + if (!r) { + // replace error + va_end(pl); + return(NULL); + } olds = NULL; news = NULL; } @@ -15267,6 +16009,9 @@ internal bool equalSmallJsonSmallArray(smallJsont *self, smallArrayt *array) { return(false); } + if (!lenSmallJson(self) && !lenO(array)) { + return(true); + } if (!lenSmallJson(self) || !lenO(array)) { return(false); } @@ -15279,8 +16024,13 @@ internal bool equalSmallJsonSmallArray(smallJsont *self, smallArrayt *array) { char *as = NULL; enumerateSArray(self->topA, e, i) { if (not e) { - return(false); + if (not sArrayGetTiny(array->a, i)) { + continue; + } + else { + return(false); } + } if ((e->type != sArrayGetTiny(array->a, i)->type)) { return(false); } @@ -15348,6 +16098,10 @@ internal bool equalSmallJsonSmallArray(smallJsont *self, smallArrayt *array) { bool equalSmallJsonArray(smallJsont *self, char ** p2) { // sanity checks + if (!lenSmallJson(self) && !listLengthS(p2)) { + // both arrays are empty + return(true); + } if (self->topIsA != TOP_IS_ARRAY) { return(false); } @@ -15464,13 +16218,6 @@ internal bool equalSmallJsonCha(smallJsont* self, char p2) { charToS(s, p2); switch(self->topIsA) { - case TOP_IS_DOUBLE: - if (isInt(s) || !isNumber(s)) { - return(false); - } - double value = parseDouble(s); - return(self->topD->value == value);; - break; case TOP_IS_INT: if (!isInt(s)) { return(false); @@ -15478,6 +16225,12 @@ internal bool equalSmallJsonCha(smallJsont* self, char p2) { int64_t ivalue = parseInt(s); return(self->topI->value == ivalue);; break; + case TOP_IS_STRING: + if (lenSmallJson(self) != 1) { + return(false); + } + return(sStringGetTiny(self->topS)[0] == p2);; + break; } return(false); } @@ -15828,11 +16581,15 @@ internal bool equalSmallJsonSmallJson(smallJsont* self, smallJsont* p2) { internal bool equalSmallJsonSmallString(smallJsont* self, smallStringt* p2) { char *s = NULL; + if (!p2) { + return(false); + } + if (checkObjectTypes && p2 && !isOSmallString(p2)) { return(false); } - if (self->topIsA == SMALLJSON_IS_EMPTY || !p2 || !p2->data) { + if (self->topIsA == SMALLJSON_IS_EMPTY || !p2->data) { return(false); } @@ -15909,7 +16666,7 @@ internal bool equalSmallJsonSmallDict(smallJsont* self, smallDictt* p2) { internal bool icEqualSmallJsonSmallArray(smallJsont *self, smallArrayt *array) { // sanity checks - if (self->topIsA != TOP_IS_ARRAY) { + if (self->topIsA != TOP_IS_ARRAY || !array) { return(false); } @@ -15917,6 +16674,9 @@ internal bool icEqualSmallJsonSmallArray(smallJsont *self, smallArrayt *array) { return(false); } + if (!lenSmallJson(self) && !lenO(array)) { + return(true); + } if (!lenSmallJson(self) || !lenO(array)) { return(false); } @@ -15929,8 +16689,13 @@ internal bool icEqualSmallJsonSmallArray(smallJsont *self, smallArrayt *array) { char *as = NULL; enumerateSArray(self->topA, e, i) { if (not e) { - return(false); + if (not sArrayGetTiny(array->a, i)) { + continue; + } + else { + return(false); } + } if ((e->type != sArrayGetTiny(array->a, i)->type)) { return(false); } @@ -15998,6 +16763,13 @@ internal bool icEqualSmallJsonSmallArray(smallJsont *self, smallArrayt *array) { bool icEqualSmallJsonArray(smallJsont *self, char ** p2) { // sanity checks + if (self->topIsA != TOP_IS_ARRAY) { + return(false); + } + if (!lenSmallJson(self) && !listLengthS(p2)) { + // both arrays are empty + return(true); + } if (!lenSmallJson(self) || !listLengthS(p2)) { return(false); } @@ -16045,8 +16817,9 @@ bool icEqualSmallJsonBase(smallJsont *self, baset* p2) { } cast(smallStringt *, so, p2); return(icEqualSmallJsonSmallString(self, so)); + default: + return(equalSmallJsonBase(self, p2)); } - return(false); } @@ -16106,7 +16879,7 @@ internal bool icEqualSmallJsonSmallJson(smallJsont *self, smallJsont *string) { internal bool icEqualSmallJsonSmallString(smallJsont *self, smallStringt *string) { - if (self->topIsA != TOP_IS_STRING || !string || !string->data) { + if (self->topIsA != TOP_IS_STRING || !string) { return(false); } @@ -16114,6 +16887,10 @@ internal bool icEqualSmallJsonSmallString(smallJsont *self, smallStringt *string return(false); } + if (!string->data) { + return(false); + } + if (lenSmallJson(self) != string->_len) { return(false); } @@ -16143,13 +16920,6 @@ internal bool icEqualCharSmallJson(smallJsont *self, char c) { charToS(s, c); switch(self->topIsA) { - case TOP_IS_DOUBLE: - if (isInt(s) || !isNumber(s)) { - return(false); - } - double value = parseDouble(s); - return(self->topD->value == value);; - break; case TOP_IS_INT: if (!isInt(s)) { return(false); @@ -16169,7 +16939,7 @@ internal bool equalISSmallJson(smallJsont *self, const char *string, intmax_t in return(false); } - if (index >= (intmax_t)lenSmallJson(self)) { + if (index >= (intmax_t)lenSmallJson(self) && lenSmallJson(self)) { return(false); } if (index < -(intmax_t)lenSmallJson(self)) { @@ -16183,6 +16953,14 @@ internal bool equalISSmallJson(smallJsont *self, const char *string, intmax_t in return(false); } + if (!string[0] && !lenSmallJson(self)) { + return(true); + } + + if (!string[0]) { + return(false); + } + return(strncmp(sStringGetTiny(self->topS) + index, string, strlen(string)) == 0);; } @@ -16233,8 +17011,11 @@ internal bool startsWithSSmallJson(smallJsont *self, const char *string) { internal bool startsWithCharSmallJson(smallJsont *self, char c) { - charToS(s, c); - return(startsWithSSmallJson(self, s)); + if (!self->topS) { + return(false); + } + + return(sStringGetTiny(self->topS)[0] == c);; } internal bool startsWithSmallStringSmallJson(smallJsont *self, smallStringt *string) { @@ -16260,6 +17041,10 @@ internal bool startsWithJsonSmallJson(smallJsont *self, smallJsont *string) { return(false); } + if (string->topIsA != TOP_IS_STRING) { + return(false); + } + return(startsWithSSmallJson(self, sjGet(string))); } @@ -16277,8 +17062,19 @@ internal bool endsWithSSmallJson(smallJsont *self, const char *string) { internal bool endsWithCharSmallJson(smallJsont *self, char c) { - charToS(s, c); - return(endsWithSSmallJson(self, s)); + if (!self->topS) { + return(false); + } + + if (!lenSmallJson(self) && !c) { + // self is empty and c is 0 + return(true); + } + if (!lenSmallJson(self)) { + return(false); + } + + return(*(sStringGetTiny(self->topS) + lenSmallJson(self)-1) == c);; } internal bool endsWithSmallStringSmallJson(smallJsont *self, smallStringt *string) { @@ -16304,12 +17100,16 @@ internal bool endsWithJsonSmallJson(smallJsont *self, smallJsont *string) { return(false); } + if (string->topIsA != TOP_IS_STRING) { + return(false); + } + return(endsWithSSmallJson(self, sjGet(string))); } internal ssize_t countSSmallJson(smallJsont *self, const char *string) { - if (self->topIsA != TOP_IS_STRING || !self->topS || !string) { + if (self->topIsA != TOP_IS_STRING || !self->topS || !string || !string[0]) { return(-1); } @@ -16360,6 +17160,10 @@ internal ssize_t countJsonSmallJson(smallJsont *self, smallJsont *string) { return(-1); } + if (string->topIsA != TOP_IS_STRING) { + return(-1); + } + return(countSSmallJson(self, sjGet(string))); } @@ -16374,8 +17178,11 @@ internal bool icStartsWithSSmallJson(smallJsont *self, const char *string) { internal bool icStartsWithCharSmallJson(smallJsont *self, char c) { - charToS(s, c); - return(icStartsWithSSmallJson(self, s)); + if (!self->topS) { + return(false); + } + + return(toupper(sStringGetTiny(self->topS)[0]) == toupper(c));; } internal bool icStartsWithSmallStringSmallJson(smallJsont *self, smallStringt *string) { @@ -16401,6 +17208,10 @@ internal bool icStartsWithJsonSmallJson(smallJsont *self, smallJsont *string) { return(false); } + if (string->topIsA != TOP_IS_STRING) { + return(false); + } + return(icStartsWithSSmallJson(self, sjGet(string))); } @@ -16418,8 +17229,19 @@ internal bool icEndsWithSSmallJson(smallJsont *self, const char *string) { internal bool icEndsWithCharSmallJson(smallJsont *self, char c) { - charToS(s, c); - return(icEndsWithSSmallJson(self, s)); + if (!self->topS) { + return(false); + } + + if (!lenSmallJson(self) && !c) { + // self is empty and c is 0 + return(true); + } + if (!lenSmallJson(self)) { + return(false); + } + + return(toupper(*(sStringGetTiny(self->topS) + lenSmallJson(self)-1)) == toupper(c));; } internal bool icEndsWithSmallStringSmallJson(smallJsont *self, smallStringt *string) { @@ -16445,12 +17267,16 @@ internal bool icEndsWithJsonSmallJson(smallJsont *self, smallJsont *string) { return(false); } + if (string->topIsA != TOP_IS_STRING) { + return(false); + } + return(icEndsWithSSmallJson(self, sjGet(string))); } internal ssize_t icCountSSmallJson(smallJsont *self, const char *string) { - if (self->topIsA != TOP_IS_STRING || !self->topS || !string) { + if (self->topIsA != TOP_IS_STRING || !self->topS || !string || !string[0]) { return(-1); } @@ -16501,6 +17327,10 @@ internal ssize_t icCountJsonSmallJson(smallJsont *self, smallJsont *string) { return(-1); } + if (string->topIsA != TOP_IS_STRING) { + return(-1); + } + return(icCountSSmallJson(self, sjGet(string))); } @@ -16609,9 +17439,6 @@ internal double parseDoubleSmallJson(smallJsont *self) { } char *string = sStringGetTiny(self->topS); - if (!isNumber(string)) { - return(0); - } while (!isdigit(*string) && *string != '-' && *string != 0) { string++; } @@ -16623,6 +17450,7 @@ internal smallJsont* intToSmallJson(smallJsont *self, intmax_t n) { char s[50]; sprintf(s, "%" PRIiMAX, n); + freeSmallJson(self); setTopStringSmallJson(self, s); return(self); } @@ -16631,6 +17459,7 @@ internal smallJsont* doubleToSmallJson(smallJsont *self, double n) { char s[256]; sprintf(s, "%e", n); + freeSmallJson(self); setTopStringSmallJson(self, s); return(self); } @@ -16731,6 +17560,7 @@ internal smallJsont* trimSmallJson(smallJsont *self) { forEachSDict(self->top, e) { if (e->key) { sDictSetTiny(&d, e->key, e->data); + free(e->key); } } @@ -16761,6 +17591,7 @@ internal smallJsont* trimSmallJson(smallJsont *self) { if (!*workingS) { // all spaces + freeSmallJson(self); setTopStringSmallJson(self, ""); return(self); } @@ -16777,6 +17608,7 @@ internal smallJsont* trimSmallJson(smallJsont *self) { r = strncpy(r, workingS, len - 1); r[len - 1] = 0; + freeSmallJson(self); if (!setTopNFreeStringSmallJson(self, r)) { free(r); return(NULL); @@ -16808,6 +17640,7 @@ internal smallJsont* lTrimSmallJson(smallJsont *self) { r = strdup(workingS);; + freeSmallJson(self); if (!setTopNFreeStringSmallJson(self, r)) { free(r); return(NULL); @@ -16921,7 +17754,9 @@ internal smallJsont* compactSmallJson(smallJsont *self) { forEachSArray(self->topA, e) { if (e) { switch (e->type) { - // skip undefined - case UNDEFINED: + case UNDEFINED: + free(e); + break; case BOOL: sArrayPushTiny(&(r->topA), e); break; @@ -16932,6 +17767,9 @@ internal smallJsont* compactSmallJson(smallJsont *self) { if (((sDictt*)e)->count > 0) { sArrayPushTiny(&(r->topA), e); } + else { + sFree(e); + } break; case DOUBLE: sArrayPushTiny(&(r->topA), e); @@ -16944,17 +17782,26 @@ internal smallJsont* compactSmallJson(smallJsont *self) { if (*trim != 0) { sArrayPushTiny(&(r->topA), e); } + else { + free(e); + } free(trim); break; case ARRAY: if (((sArrayt*)e)->count > 0) { sArrayPushTiny(&(r->topA), e); } + else { + sFree(e); + } break; case BYTES: if (((sBytest*)e)->count > 0) { sArrayPushTiny(&(r->topA), e); } + else { + sFree(e); + } break; } } @@ -16968,6 +17815,14 @@ internal smallJsont* compactSmallJson(smallJsont *self) { internal smallJsont* emptySmallJson(smallJsont *self) { + if (self->iterIndex != -1) { + if (self->iterElementDataType != SH_DT_BASET) { + finishO(self->iterElement); + } + self->iterElement = NULL; + self->iterIndex = -1; + } + switch(self->topIsA) { case TOP_IS_STRING: sFree((smallt *)self->topS); @@ -16988,9 +17843,31 @@ internal smallJsont* emptySmallJson(smallJsont *self) { internal bool isEmptySmallJson(smallJsont *self) { - return(lenSmallJson(self) == 0);; + if (lenSmallJson(self) == 0) { + return(true); + } + + // check if all elements are null + switch(self->topIsA) { + case TOP_IS_DICT: + {forEachSDict(self->top, e) + if (e->key) + return(false); + } + break; + case TOP_IS_ARRAY:; + {forEachSArray(self->topA, e) + if (e) + return(false); + } + break; + case TOP_IS_STRING: + return(false); + } + return(true); } + internal bool isBlankSmallJson(smallJsont *self) { if (self->topIsA == TOP_IS_DICT) { @@ -17050,19 +17927,26 @@ internal bool isBlankSmallJson(smallJsont *self) { internal void forEachSmallJsonF(smallJsont *self, void *closure, forEachElementSmallArrayFt funcElem) { // sanity checks - if (self->topIsA != TOP_IS_ARRAY) { + if (self->topIsA != TOP_IS_ARRAY || !lenSmallJson(self)) { return; } - forEachSmallJson(self, e) { - if (!e) { + forEachSArray(self->topA, e) { + if (not e) { continue; } - if (!funcElem(closure, e)) { - finishO(e); + baset *E = toBaset(e); + if (!funcElem(closure, E)) { + if (e->type == CONTAINER && ((sContainert*)e)->dataType == SH_DT_BASET) { + break; + } + finishO(E); break; } - finishO(e); + if (e->type == CONTAINER && ((sContainert*)e)->dataType == SH_DT_BASET) { + continue; + } + finishO(E); } return; } @@ -17070,7 +17954,7 @@ internal void forEachSmallJsonF(smallJsont *self, void *closure, forEachElementS internal void enumerateSmallJsonF(smallJsont *self, void *closure, enumerateElementSmallArrayFt funcElem) { // sanity checks - if (self->topIsA != TOP_IS_ARRAY) { + if (self->topIsA != TOP_IS_ARRAY || !lenSmallJson(self)) { return; } @@ -17096,6 +17980,10 @@ internal void enumerateSmallJsonF(smallJsont *self, void *closure, enumerateElem internal void enumerateDictSmallJson(smallJsont *self, void *closure, enumerateDictElementSmallJsonFt funcElem) { + if (self->topIsA != TOP_IS_DICT || !lenSmallJson(self)) { + return; + } + forEachSDict(self->top, e) { if (e->key) { baset *E = toBaset(e->data); @@ -17184,6 +18072,10 @@ internal smallStringt* joinSmallStringSmallJson(smallJsont *self, smallStringt* if (!delim) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim)) { + return(NULL); + } + return(joinSmallJson(self, ssGet(delim))); } @@ -17258,6 +18150,10 @@ internal char* joinSmallStringSSmallJson(smallJsont *self, smallStringt* delim) if (!delim) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim)) { + return(NULL); + } + return(joinSSmallJson(self, ssGet(delim))); } @@ -17307,6 +18203,10 @@ internal smallJsont* splitSmallStringSmallJson(smallJsont *self, smallStringt *d if (!delim) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim)) { + return(NULL); + } + return(splitSmallJson(self, ssGet(delim))); } @@ -17324,7 +18224,7 @@ internal char** splitSSmallJson(smallJsont *self, const char *delim) { if (!*delim || !*sStringGetTiny(self->topS)) { // empty delimiter or empty string - listPushS(&r, ""); + listPushS(&r, sStringGetTiny(self->topS)); return(r); } @@ -17384,6 +18284,10 @@ internal char** splitSmallStringSSmallJson(smallJsont *self, smallStringt *delim if (!delim) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim)) { + return(NULL); + } + return(splitSSmallJson(self, ssGet(delim))); } @@ -17400,7 +18304,7 @@ internal smallJsont* extractSmallJson(smallJsont *self, const char* delim1, cons return(NULL); } - tmp = splitSSmallJson(self, delim1);; + tmp = splitSSmallJson(self, delim1); if (!tmp) { return(NULL); @@ -17425,6 +18329,10 @@ internal smallJsont* extractSmallJson(smallJsont *self, const char* delim1, cons listFreeS(tmp); + if (!r) { + return(NULL); + } + createAllocateSmallJson(a); if (!a) { listFreeS(r); @@ -17484,6 +18392,10 @@ internal smallJsont* extractSmallJsonSmallStringSmallJson(smallJsont *self, smal return(NULL); } + if (checkObjectTypes && !isOSmallString(delim2)) { + return(NULL); + } + if (delim1->topIsA != TOP_IS_STRING) { return(NULL); } @@ -17532,6 +18444,10 @@ internal smallJsont* extractSmallStringSmallJsonSmallJson(smallJsont *self, smal return(NULL); } + if (checkObjectTypes && !isOSmallString(delim1)) { + return(NULL); + } + if (checkObjectTypes && !isOSmallJson(delim2)) { return(NULL); } @@ -17548,6 +18464,10 @@ internal smallJsont* extractSmallStringSmallStringSmallJson(smallJsont *self, sm if (!delim1 || !delim2) { return(NULL); } + if (checkObjectTypes && (!isOSmallString(delim1) || !isOSmallString(delim2))) { + return(NULL); + } + return(extractSmallJson(self, ssGet(delim1), ssGet(delim2))); } @@ -17556,6 +18476,10 @@ internal smallJsont* extractSmallStringSSmallJson(smallJsont *self, smallStringt if (!delim1) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim1)) { + return(NULL); + } + return(extractSmallJson(self, ssGet(delim1), delim2)); } @@ -17564,6 +18488,10 @@ internal smallJsont* extractSmallStringCharSmallJson(smallJsont *self, smallStri if (!delim1) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim1)) { + return(NULL); + } + charToS(d2, delim2); return(extractSmallJson(self, ssGet(delim1), d2)); } @@ -17590,6 +18518,10 @@ internal smallJsont* extractSSmallStringSmallJson(smallJsont *self, const char* if (!delim2) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim2)) { + return(NULL); + } + return(extractSmallJson(self, delim1, ssGet(delim2))); } @@ -17616,6 +18548,10 @@ internal smallJsont* extractCharSmallStringSmallJson(smallJsont *self, char deli if (!delim2) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim2)) { + return(NULL); + } + charToS(d1, delim1); return(extractSmallJson(self, d1, ssGet(delim2))); } @@ -17666,6 +18602,10 @@ internal smallJsont* icSplitSmallStringSmallJson(smallJsont *self, smallStringt if (!delim) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim)) { + return(NULL); + } + return(icSplitSmallJson(self, ssGet(delim))); } @@ -17683,7 +18623,7 @@ internal char** icSplitSSmallJson(smallJsont *self, const char *delim) { if (!*delim || !*sStringGetTiny(self->topS)) { // empty delimiter or empty string - listPushS(&r, ""); + listPushS(&r, sStringGetTiny(self->topS)); return(r); } @@ -17743,6 +18683,10 @@ internal char** icSplitSmallStringSSmallJson(smallJsont *self, smallStringt *del if (!delim) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim)) { + return(NULL); + } + return(icSplitSSmallJson(self, ssGet(delim))); } @@ -17759,7 +18703,7 @@ internal smallJsont* icExtractSmallJson(smallJsont *self, const char* delim1, co return(NULL); } - tmp = icSplitSSmallJson(self, delim1);; + tmp = icSplitSSmallJson(self, delim1); if (!tmp) { return(NULL); @@ -17784,6 +18728,10 @@ internal smallJsont* icExtractSmallJson(smallJsont *self, const char* delim1, co listFreeS(tmp); + if (!r) { + return(NULL); + } + createAllocateSmallJson(a); if (!a) { listFreeS(r); @@ -17843,6 +18791,10 @@ internal smallJsont* icExtractSmallJsonSmallStringSmallJson(smallJsont *self, sm return(NULL); } + if (checkObjectTypes && !isOSmallString(delim2)) { + return(NULL); + } + if (delim1->topIsA != TOP_IS_STRING) { return(NULL); } @@ -17891,6 +18843,10 @@ internal smallJsont* icExtractSmallStringSmallJsonSmallJson(smallJsont *self, sm return(NULL); } + if (checkObjectTypes && !isOSmallString(delim1)) { + return(NULL); + } + if (checkObjectTypes && !isOSmallJson(delim2)) { return(NULL); } @@ -17907,6 +18863,10 @@ internal smallJsont* icExtractSmallStringSmallStringSmallJson(smallJsont *self, if (!delim1 || !delim2) { return(NULL); } + if (checkObjectTypes && (!isOSmallString(delim1) || !isOSmallString(delim2))) { + return(NULL); + } + return(icExtractSmallJson(self, ssGet(delim1), ssGet(delim2))); } @@ -17915,6 +18875,10 @@ internal smallJsont* icExtractSmallStringSSmallJson(smallJsont *self, smallStrin if (!delim1) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim1)) { + return(NULL); + } + return(icExtractSmallJson(self, ssGet(delim1), delim2)); } @@ -17923,6 +18887,10 @@ internal smallJsont* icExtractSmallStringCharSmallJson(smallJsont *self, smallSt if (!delim1) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim1)) { + return(NULL); + } + charToS(d2, delim2); return(icExtractSmallJson(self, ssGet(delim1), d2)); } @@ -17949,6 +18917,10 @@ internal smallJsont* icExtractSSmallStringSmallJson(smallJsont *self, const char if (!delim2) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim2)) { + return(NULL); + } + return(icExtractSmallJson(self, delim1, ssGet(delim2))); } @@ -17975,17 +18947,22 @@ internal smallJsont* icExtractCharSmallStringSmallJson(smallJsont *self, char de if (!delim2) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim2)) { + return(NULL); + } + charToS(d1, delim1); return(icExtractSmallJson(self, d1, ssGet(delim2))); } internal smallJsont* colorSmallJson(smallJsont *self, const char *colr) { - if (self->topIsA != TOP_IS_STRING || !self->topS) { - return(self); + if (self->topIsA != TOP_IS_STRING || !self->topS || !colr) { + return(NULL); } char *s = catS(colr, sStringGetTiny(self->topS), RST); + freeSmallJson(self); setTopStringSmallJson(self, s); free(s); return(self); @@ -17993,13 +18970,13 @@ internal smallJsont* colorSmallJson(smallJsont *self, const char *colr) { internal char* colordSmallJson(smallJsont *self, const char *color) { - if (self->topIsA != TOP_IS_STRING || !self->topS) { + if (self->topIsA != TOP_IS_STRING || !self->topS || !color) { return(NULL); } if (isEmptySmallJson(self)) { return(emptySF()); } - return(catS(color, self->topS, RST)); + return(catS(color, sStringGetTiny(self->topS), RST)); } @@ -18052,14 +19029,270 @@ internal smallJsont* zipSmallJson(smallJsont *self, smallArrayt *array1, smallAr count = 0; forEachSmallArray(array1, K) { - if (!K) { + if (!isOSmallString(K)) { + finishO(K); return(NULL); } + sDictSetTiny(&(self->top), ssGet(K), sArrayGetTiny(array2->a, count)); + finishO(K); + count++; + if (count == len) { + break; + } + } + } + + + return(self); +} + +internal smallJsont* zipArraySmallJson(smallJsont *self, char** array1, smallArrayt *array2) { + size_t len; + size_t count; + + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_DICT && self->topIsA != TOP_IS_ARRAY) { + return(NULL); + } + + if (!array1 || !array2) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallArray(array2)) { + return(NULL); + } + + if (!listLengthS(array1) || !array2->f->len(array2)) { + return(self); + } + + len = MIN(listLengthS(array1), array2->f->len(array2));; + + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + + if (self->topIsA == TOP_IS_ARRAY) { + count = 0; + + enumerateS(array1, E1, i) { + createAllocateSmallArray(a); + if (!a) { + return(NULL); + } + a->f->pushNFreeS(a, E1); + pushNFreeO(a, getO(array2, i)); + pushNFreeArraySmallJson(self, a); + count++; + if (count == len) { + break; + } + } + } + + else if (self->topIsA == TOP_IS_DICT) { + count = 0; + + forEachS(array1, k) { + sDictSetTiny(&(self->top), k, sArrayGetTiny(array2->a, count)); + count++; + if (count == len) { + break; + } + } + } + + return(self); +} + +internal smallJsont* zipCArraySmallJson(smallJsont *self, const char** array1, smallArrayt *array2) { + size_t len; + size_t count; + + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_DICT && self->topIsA != TOP_IS_ARRAY) { + return(NULL); + } + + if (!array1 || !array2) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallArray(array2)) { + return(NULL); + } + + if (!listLengthS((char**)array1) || !array2->f->len(array2)) { + return(self); + } + + len = MIN(listLengthS((char**)array1), array2->f->len(array2));; + + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + + if (self->topIsA == TOP_IS_ARRAY) { + count = 0; + + enumerateS((char**)array1, E1, i) { + createAllocateSmallArray(a); + if (!a) { + return(NULL); + } + a->f->pushS(a, E1); + pushNFreeO(a, getO(array2, i)); + pushNFreeArraySmallJson(self, a); + count++; + if (count == len) { + break; + } + } + } + + else if (self->topIsA == TOP_IS_DICT) { + count = 0; + + forEachS((char**)array1, k) { + sDictSetTiny(&(self->top), k, sArrayGetTiny(array2->a, count)); + count++; + if (count == len) { + break; + } + } + } + + return(self); +} + +internal smallJsont* zipCharSmallJson(smallJsont *self, smallArrayt *array1, char** array2) { + size_t len; + size_t count; + + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_DICT && self->topIsA != TOP_IS_ARRAY) { + return(NULL); + } + + if (!array1 || !array2) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallArray(array1)) { + return(NULL); + } + + if (!array1->f->len(array1) || !listLengthS(array2)) { + return(self); + } + + len = MIN(array1->f->len(array1), listLengthS(array2));; + + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + + if (self->topIsA == TOP_IS_ARRAY) { + count = 0; + + enumerateSmallArray(array1, E1, i) { + createAllocateSmallArray(a); + if (!a) { + finishO(E1); + return(NULL); + } + pushNFreeO(a, E1); + a->f->pushNFreeS(a, array2[i]); + pushNFreeArraySmallJson(self, a); + count++; + if (count == len) { + break; + } + } + } + + else if (self->topIsA == TOP_IS_DICT) { + count = 0; + + smallt *e; + forEachSmallArray(array1, K) { if (!isOSmallString(K)) { finishO(K); return(NULL); } - sDictSetTiny(&(self->top), ssGet(K), sArrayGetTiny(array2->a, count)); + isError(e, (smallt *) allocSStringTiny(array2[count])) { + finishO(K); + return(NULL); + } + sDictSetTiny(&(self->top), ssGet(K), e); + finishO(K); + count++; + if (count == len) { + break; + } + } + } + + return(self); +} + +internal smallJsont* zipCCharSmallJson(smallJsont *self, smallArrayt *array1, const char** array2) { + size_t len; + size_t count; + + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_DICT && self->topIsA != TOP_IS_ARRAY) { + return(NULL); + } + + if (!array1 || !array2) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallArray(array1)) { + return(NULL); + } + + if (!array1->f->len(array1) || !listLengthS((char**)array2)) { + return(self); + } + + len = MIN(array1->f->len(array1), listLengthS((char**)array2));; + + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + + if (self->topIsA == TOP_IS_ARRAY) { + count = 0; + + enumerateSmallArray(array1, E1, i) { + createAllocateSmallArray(a); + if (!a) { + finishO(E1); + return(NULL); + } + pushNFreeO(a, E1); + a->f->pushS(a, array2[i]); + pushNFreeArraySmallJson(self, a); + count++; + if (count == len) { + break; + } + } + } + + else if (self->topIsA == TOP_IS_DICT) { + count = 0; + + smallt *e; + forEachSmallArray(array1, K) { + if (!isOSmallString(K)) { + finishO(K); + return(NULL); + } + isError(e, (smallt *) allocSStringTiny(array2[count])) { + finishO(K); + return(NULL); + } + sDictSetTiny(&(self->top), ssGet(K), e); finishO(K); count++; if (count == len) { @@ -18068,11 +19301,124 @@ internal smallJsont* zipSmallJson(smallJsont *self, smallArrayt *array1, smallAr } } - return(self); } -internal smallJsont* zipArraySmallJson(smallJsont *self, char** array1, smallArrayt *array2) { +internal smallJsont* zipArrayCharSmallJson(smallJsont *self, char** array1, char** array2) { + size_t len; + size_t count; + + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_DICT && self->topIsA != TOP_IS_ARRAY) { + return(NULL); + } + + if (!array1 || !array2) { + return(NULL); + } + + if (!listLengthS(array1) || !listLengthS(array2)) { + return(self); + } + + len = MIN(listLengthS(array1), listLengthS(array2));; + + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + + if (self->topIsA == TOP_IS_ARRAY) { + count = 0; + + enumerateS(array1, E1, i) { + createAllocateSmallArray(a); + if (!a) { + return(NULL); + } + a->f->pushNFreeS(a, E1); + a->f->pushNFreeS(a, array2[i]); + pushNFreeArraySmallJson(self, a); + count++; + if (count == len) { + break; + } + } + } + + else if (self->topIsA == TOP_IS_DICT) { + count = 0; + + smallt *e; + forEachS(array1, k) { + isError(e, (smallt *) allocSStringTiny(array2[count])) return(NULL); + sDictSetTiny(&(self->top), k, e); + count++; + if (count == len) { + break; + } + } + } + + return(self); +} + +internal smallJsont* zipCArrayCharSmallJson(smallJsont *self, const char** array1, char** array2) { + size_t len; + size_t count; + + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_DICT && self->topIsA != TOP_IS_ARRAY) { + return(NULL); + } + + if (!array1 || !array2) { + return(NULL); + } + + if (!listLengthS((char**)array1) || !listLengthS(array2)) { + return(self); + } + + len = MIN(listLengthS((char**)array1), listLengthS(array2));; + + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + + if (self->topIsA == TOP_IS_ARRAY) { + count = 0; + + enumerateS((char**)array1, E1, i) { + createAllocateSmallArray(a); + if (!a) { + return(NULL); + } + a->f->pushS(a, E1); + a->f->pushNFreeS(a, array2[i]); + pushNFreeArraySmallJson(self, a); + count++; + if (count == len) { + break; + } + } + } + + else if (self->topIsA == TOP_IS_DICT) { + count = 0; + + smallt *e; + forEachS((char**)array1, k) { + isError(e, (smallt *) allocSStringTiny(array2[count])) return(NULL); + sDictSetTiny(&(self->top), k, e); + count++; + if (count == len) { + break; + } + } + } + + return(self); +} + +internal smallJsont* zipArrayCCharSmallJson(smallJsont *self, char** array1, const char** array2) { size_t len; size_t count; @@ -18084,15 +19430,11 @@ internal smallJsont* zipArraySmallJson(smallJsont *self, char** array1, smallArr return(NULL); } - if (checkObjectTypes && !isOSmallArray(array2)) { - return(NULL); - } - - if (!listLengthS(array1) || !array2->f->len(array2)) { + if (!listLengthS(array1) || !listLengthS((char**)array2)) { return(self); } - len = MIN(listLengthS(array1), array2->f->len(array2));; + len = MIN(listLengthS(array1), listLengthS((char**)array2));; if (self->topIsA == SMALLJSON_IS_EMPTY) { self->topIsA = TOP_IS_ARRAY; @@ -18107,7 +19449,7 @@ internal smallJsont* zipArraySmallJson(smallJsont *self, char** array1, smallArr return(NULL); } a->f->pushNFreeS(a, E1); - pushNFreeO(a, getO(array2, i)); + a->f->pushS(a, array2[i]); pushNFreeArraySmallJson(self, a); count++; if (count == len) { @@ -18119,8 +19461,10 @@ internal smallJsont* zipArraySmallJson(smallJsont *self, char** array1, smallArr else if (self->topIsA == TOP_IS_DICT) { count = 0; + smallt *e; forEachS(array1, k) { - sDictSetTiny(&(self->top), k, sArrayGetTiny(array2->a, count)); + isError(e, (smallt *) allocSStringTiny(array2[count])) return(NULL); + sDictSetTiny(&(self->top), k, e); count++; if (count == len) { break; @@ -18131,7 +19475,7 @@ internal smallJsont* zipArraySmallJson(smallJsont *self, char** array1, smallArr return(self); } -internal smallJsont* zipCharSmallJson(smallJsont *self, smallArrayt *array1, char** array2) { +internal smallJsont* zipCArrayCCharSmallJson(smallJsont *self, const char** array1, const char** array2) { size_t len; size_t count; @@ -18143,15 +19487,11 @@ internal smallJsont* zipCharSmallJson(smallJsont *self, smallArrayt *array1, cha return(NULL); } - if (checkObjectTypes && !isOSmallArray(array1)) { - return(NULL); - } - - if (!array1->f->len(array1) || !listLengthS(array2)) { + if (!listLengthS((char**)array1) || !listLengthS((char**)array2)) { return(self); } - len = MIN(array1->f->len(array1), listLengthS(array2));; + len = MIN(listLengthS((char**)array1), listLengthS((char**)array2));; if (self->topIsA == SMALLJSON_IS_EMPTY) { self->topIsA = TOP_IS_ARRAY; @@ -18160,14 +19500,13 @@ internal smallJsont* zipCharSmallJson(smallJsont *self, smallArrayt *array1, cha if (self->topIsA == TOP_IS_ARRAY) { count = 0; - enumerateSmallArray(array1, E1, i) { + enumerateS((char**)array1, E1, i) { createAllocateSmallArray(a); if (!a) { - finishO(E1); return(NULL); } - pushNFreeO(a, E1); - a->f->pushNFreeS(a, array2[i]); + a->f->pushS(a, E1); + a->f->pushS(a, array2[i]); pushNFreeArraySmallJson(self, a); count++; if (count == len) { @@ -18180,20 +19519,9 @@ internal smallJsont* zipCharSmallJson(smallJsont *self, smallArrayt *array1, cha count = 0; smallt *e; - forEachSmallArray(array1, K) { - if (!K) { - return(NULL); - } - if (!isOSmallString(K)) { - finishO(K); - return(NULL); - } - isError(e, (smallt *) allocSStringTiny(array2[count])) { - finishO(K); - return(NULL); - } - sDictSetTiny(&(self->top), ssGet(K), e); - finishO(K); + forEachS((char**)array1, k) { + isError(e, (smallt *) allocSStringTiny(array2[count])) return(NULL); + sDictSetTiny(&(self->top), k, e); count++; if (count == len) { break; @@ -18204,7 +19532,7 @@ internal smallJsont* zipCharSmallJson(smallJsont *self, smallArrayt *array1, cha return(self); } -internal smallJsont* zipArrayCharSmallJson(smallJsont *self, char** array1, char** array2) { +smallJsont* zipJsonSmallJson(smallJsont *self, smallJsont *array1, smallJsont *array2) { size_t len; size_t count; @@ -18216,11 +19544,19 @@ internal smallJsont* zipArrayCharSmallJson(smallJsont *self, char** array1, char return(NULL); } - if (!listLengthS(array1) || !listLengthS(array2)) { + if (checkObjectTypes && (!isOSmallJson(array1) || !isOSmallJson(array2))) { + return(NULL); + } + + if (array1->topIsA != TOP_IS_ARRAY || array2->topIsA != TOP_IS_ARRAY) { + return(NULL); + } + + if (!array1->f->len(array1) || !array2->f->len(array2)) { return(self); } - len = MIN(listLengthS(array1), listLengthS(array2));; + len = MIN(array1->f->len(array1), array2->f->len(array2)); if (self->topIsA == SMALLJSON_IS_EMPTY) { self->topIsA = TOP_IS_ARRAY; @@ -18229,13 +19565,14 @@ internal smallJsont* zipArrayCharSmallJson(smallJsont *self, char** array1, char if (self->topIsA == TOP_IS_ARRAY) { count = 0; - enumerateS(array1, E1, i) { + enumerateSmallJson(array1, E1, i) { createAllocateSmallArray(a); if (!a) { + finishO(E1); return(NULL); } - a->f->pushNFreeS(a, E1); - a->f->pushNFreeS(a, array2[i]); + pushNFreeO(a, E1); + pushNFreeO(a, getAtO(array2, i)); pushNFreeArraySmallJson(self, a); count++; if (count == len) { @@ -18247,10 +19584,13 @@ internal smallJsont* zipArrayCharSmallJson(smallJsont *self, char** array1, char else if (self->topIsA == TOP_IS_DICT) { count = 0; - smallt *e; - forEachS(array1, k) { - isError(e, (smallt *) allocSStringTiny(array2[count])) return(NULL); - sDictSetTiny(&(self->top), k, e); + forEachSmallJson(array1, K) { + if (!isOSmallString(K)) { + finishO(K); + return(NULL); + } + sDictSetTiny(&(self->top), ssGet(K), sArrayGetTiny(array2->topA, count)); + finishO(K); count++; if (count == len) { break; @@ -18258,10 +19598,11 @@ internal smallJsont* zipArrayCharSmallJson(smallJsont *self, char** array1, char } } + return(self); } -smallJsont* zipJsonSmallJson(smallJsont *self, smallJsont *array1, smallJsont *array2) { +smallJsont* zipJsonSmallArraySmallJson(smallJsont *self, smallJsont *array1, smallArrayt *array2) { size_t len; size_t count; @@ -18273,11 +19614,11 @@ smallJsont* zipJsonSmallJson(smallJsont *self, smallJsont *array1, smallJsont *a return(NULL); } - if (checkObjectTypes && (!isOSmallJson(array1) || !isOSmallJson(array2))) { + if (checkObjectTypes && (!isOSmallJson(array1) || !isOSmallArray(array2))) { return(NULL); } - if (array1->topIsA != TOP_IS_ARRAY || array2->topIsA != TOP_IS_ARRAY) { + if (array1->topIsA != TOP_IS_ARRAY) { return(NULL); } @@ -18314,14 +19655,11 @@ smallJsont* zipJsonSmallJson(smallJsont *self, smallJsont *array1, smallJsont *a count = 0; forEachSmallJson(array1, K) { - if (!K) { - return(NULL); - } if (!isOSmallString(K)) { finishO(K); return(NULL); } - sDictSetTiny(&(self->top), ssGet(K), sArrayGetTiny(array2->topA, count)); + sDictSetTiny(&(self->top), ssGet(K), sArrayGetTiny(array2->a, count)); finishO(K); count++; if (count == len) { @@ -18334,7 +19672,7 @@ smallJsont* zipJsonSmallJson(smallJsont *self, smallJsont *array1, smallJsont *a return(self); } -smallJsont* zipJsonSmallArraySmallJson(smallJsont *self, smallJsont *array1, smallArrayt *array2) { +smallJsont* zipJsonArraySmallJson(smallJsont *self, smallJsont *array1, char** array2) { size_t len; size_t count; @@ -18346,7 +19684,7 @@ smallJsont* zipJsonSmallArraySmallJson(smallJsont *self, smallJsont *array1, sma return(NULL); } - if (checkObjectTypes && (!isOSmallJson(array1) || !isOSmallArray(array2))) { + if (checkObjectTypes && !isOSmallJson(array1)) { return(NULL); } @@ -18354,11 +19692,11 @@ smallJsont* zipJsonSmallArraySmallJson(smallJsont *self, smallJsont *array1, sma return(NULL); } - if (!array1->f->len(array1) || !array2->f->len(array2)) { + if (!array1->f->len(array1) || !listLengthS(array2)) { return(self); } - len = MIN(array1->f->len(array1), array2->f->len(array2)); + len = MIN(array1->f->len(array1), listLengthS(array2));; if (self->topIsA == SMALLJSON_IS_EMPTY) { self->topIsA = TOP_IS_ARRAY; @@ -18374,7 +19712,7 @@ smallJsont* zipJsonSmallArraySmallJson(smallJsont *self, smallJsont *array1, sma return(NULL); } pushNFreeO(a, E1); - pushNFreeO(a, getAtO(array2, i)); + a->f->pushNFreeS(a, array2[i]); pushNFreeArraySmallJson(self, a); count++; if (count == len) { @@ -18386,15 +19724,17 @@ smallJsont* zipJsonSmallArraySmallJson(smallJsont *self, smallJsont *array1, sma else if (self->topIsA == TOP_IS_DICT) { count = 0; + smallt *e; forEachSmallJson(array1, K) { - if (!K) { + if (!isOSmallString(K)) { + finishO(K); return(NULL); } - if (!isOSmallString(K)) { + isError(e, (smallt *) allocSStringTiny(array2[count])) { finishO(K); return(NULL); } - sDictSetTiny(&(self->top), ssGet(K), sArrayGetTiny(array2->a, count)); + sDictSetTiny(&(self->top), ssGet(K), e); finishO(K); count++; if (count == len) { @@ -18403,11 +19743,10 @@ smallJsont* zipJsonSmallArraySmallJson(smallJsont *self, smallJsont *array1, sma } } - return(self); } -smallJsont* zipJsonArraySmallJson(smallJsont *self, smallJsont *array1, char** array2) { +smallJsont* zipJsonCArraySmallJson(smallJsont *self, smallJsont *array1, const char** array2) { size_t len; size_t count; @@ -18427,11 +19766,11 @@ smallJsont* zipJsonArraySmallJson(smallJsont *self, smallJsont *array1, char** a return(NULL); } - if (!array1->f->len(array1) || !listLengthS(array2)) { + if (!array1->f->len(array1) || !listLengthS((char**)array2)) { return(self); } - len = MIN(array1->f->len(array1), listLengthS(array2));; + len = MIN(array1->f->len(array1), listLengthS((char**)array2));; if (self->topIsA == SMALLJSON_IS_EMPTY) { self->topIsA = TOP_IS_ARRAY; @@ -18447,7 +19786,7 @@ smallJsont* zipJsonArraySmallJson(smallJsont *self, smallJsont *array1, char** a return(NULL); } pushNFreeO(a, E1); - a->f->pushNFreeS(a, array2[i]); + a->f->pushS(a, array2[i]); pushNFreeArraySmallJson(self, a); count++; if (count == len) { @@ -18461,9 +19800,6 @@ smallJsont* zipJsonArraySmallJson(smallJsont *self, smallJsont *array1, char** a smallt *e; forEachSmallJson(array1, K) { - if (!K) { - return(NULL); - } if (!isOSmallString(K)) { finishO(K); return(NULL); @@ -18537,9 +19873,6 @@ smallJsont* zipSmallArrayJsonSmallJson(smallJsont *self, smallArrayt *array1, sm count = 0; forEachSmallArray(array1, K) { - if (!K) { - return(NULL); - } if (!isOSmallString(K)) { finishO(K); return(NULL); @@ -18620,6 +19953,69 @@ smallJsont* zipArrayJsonSmallJson(smallJsont *self, char** array1, smallJsont *a return(self); } +smallJsont* zipCArrayJsonSmallJson(smallJsont *self, const char** array1, smallJsont *array2) { + size_t len; + size_t count; + + if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_DICT && self->topIsA != TOP_IS_ARRAY) { + return(NULL); + } + + if (!array1 || !array2) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallJson(array2)) { + return(NULL); + } + + if (array2->topIsA != TOP_IS_ARRAY) { + return(NULL); + } + + if (!listLengthS((char**)array1) || !array2->f->len(array2)) { + return(self); + } + + len = MIN(listLengthS((char**)array1), array2->f->len(array2));; + + if (self->topIsA == SMALLJSON_IS_EMPTY) { + self->topIsA = TOP_IS_ARRAY; + } + + if (self->topIsA == TOP_IS_ARRAY) { + count = 0; + + enumerateS((char**)array1, E1, i) { + createAllocateSmallArray(a); + if (!a) { + return(NULL); + } + a->f->pushS(a, E1); + pushNFreeO(a, getAtO(array2, i)); + pushNFreeArraySmallJson(self, a); + count++; + if (count == len) { + break; + } + } + } + + else if (self->topIsA == TOP_IS_DICT) { + count = 0; + + forEachS((char**)array1, k) { + sDictSetTiny(&(self->top), k, sArrayGetTiny(array2->topA, count)); + count++; + if (count == len) { + break; + } + } + } + + return(self); +} + internal baset* iterStartSmallJson(smallJsont *self) { smallt *o = NULL; @@ -18635,14 +20031,10 @@ internal baset* iterStartSmallJson(smallJsont *self) { case TOP_IS_DICT: // get first element range(i, self->top->count) - if (&((self->top)->elements) + i) { + if ((&((self->top)->elements) + i)->key) { 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); @@ -18665,10 +20057,6 @@ internal baset* iterStartSmallJson(smallJsont *self) { 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 @@ -18703,14 +20091,10 @@ internal const char* iterStartKeySmallJson(smallJsont *self) { // get first element range(i, self->top->count) - if (&((self->top)->elements) + i) { + if ((&((self->top)->elements) + i)->key) { 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); @@ -18748,10 +20132,6 @@ internal baset* iterStartLastSmallJson(smallJsont *self) { 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 @@ -18889,11 +20269,11 @@ internal baset* iterNextSmallJson(smallJsont *self) { finishO(self->iterElement); } rangeFrom(i, self->iterIndex, self->top->count) { - if (&((self->top)->elements) + i) { + if ((&((self->top)->elements) + i)->key) { self->iterIndex = i; break; } - if (!(&((self->top)->elements) + i) && (i == (self->top->count -1))) { + if (!(&((self->top)->elements) + i)->key && (i == (self->top->count -1))) { // last element is null, finish iteration self->iterIndex = self->top->count; } @@ -18918,6 +20298,9 @@ internal baset* iterNextSmallJson(smallJsont *self) { if (self->iterElementDataType != SH_DT_BASET) { finishO(self->iterElement); } + else { + self->iterElement = NULL; + } // get next element until we get a valid item or the end is reached while (!self->iterElement) { // get next element @@ -18961,11 +20344,11 @@ internal const char* iterNextKeySmallJson(smallJsont *self) { finishO(self->iterElement); } rangeFrom(i, self->iterIndex, self->top->count) { - if (&((self->top)->elements) + i) { + if ((&((self->top)->elements) + i)->key) { self->iterIndex = i; break; } - if (!(&((self->top)->elements) + i) && (i == (self->top->count -1))) { + if (!(&((self->top)->elements) + i)->key && (i == (self->top->count -1))) { // last element is null, finish iteration self->iterIndex = self->top->count; } @@ -19016,7 +20399,7 @@ internal intmax_t iterStepSmallJson(smallJsont *self) { return(-1); } - return(self->iterIndex); + return(self->iterStep); } internal baset* getSmallJson(smallJsont *self, const char *key) { @@ -19646,7 +21029,7 @@ internal void* getNDupVoidSmallJson(smallJsont *self, const char *key) { e = getNDupSmallContainerSmallJson(self, key); if (e) { r = e->f->get(e); - finishO(e); + terminateO(e); } return(r); } @@ -20157,6 +21540,11 @@ baset* getAtNDupSmallJson(smallJsont *self, intmax_t index) { if (!o) { return(NULL); } + + if (o->type == UNDEFINED) { + return((baset*) allocUndefined()); + } + if (o->type == CONTAINER && (((sContainert*)o)->dataType == SH_DT_BASET)) { baset *b = ((sContainert*)o)->data; return(duplicateO(b)); @@ -20498,7 +21886,7 @@ internal void* getAtNDupVoidSmallJson(smallJsont *self, intmax_t index) { e = getAtNDupSmallContainerSmallJson(self, index); if (e) { r = e->f->get(e); - finishO(e); + smashO(e); } return(r); } @@ -20701,9 +22089,8 @@ internal smallJsont* delSmallJson(smallJsont *self, intmax_t start, intmax_t end sArrayDelRangeTiny(self->topA, start, end); return(self); default: - return(self); + return(NULL); } - return(self); } internal smallJsont* delElemIndexSmallJson(smallJsont *self, intmax_t index) { @@ -20755,9 +22142,8 @@ internal smallJsont* delElemIndexSmallJson(smallJsont *self, intmax_t index) { sArrayDelTiny(self->topA, index); return(self); default: - return(self); + return(NULL); } - return(self); } internal smallJsont* removeElemSmallJson(smallJsont *self, const char *key) { @@ -20863,9 +22249,8 @@ internal smallJsont* removeSmallJson(smallJsont *self, intmax_t start, intmax_t } return(self); default: - return(self); + return(NULL); } - return(self); } internal smallJsont* removeElemIndexSmallJson(smallJsont *self, intmax_t index) { @@ -20917,9 +22302,8 @@ internal smallJsont* removeElemIndexSmallJson(smallJsont *self, intmax_t index) sArraySetShortTiny(self->topA, index, NULL); return(self); default: - return(self); + return(NULL); } - return(self); } @@ -21183,7 +22567,12 @@ internal char** sDictToYMLForeach(sDictElemt *e, char *indentS2, int indent, int char **r = NULL; smallt *obj = NULL; - line = catS(indentS2, e->key);; + if (isBlankS(e->key)) { + line = catS(indentS2, "\"", e->key, "\"");; + } + else { + line = catS(indentS2, e->key);; + } obj = e->data; @@ -21504,6 +22893,10 @@ internal bool parseSmallStringSmallJson(smallJsont *self, smallStringt *input) { if (!input) { return(false); } + if (checkObjectTypes && !isOSmallString(input)) { + return(false); + } + return(parseSmallJson(self, ssGet(input))); } @@ -21901,6 +23294,10 @@ internal bool parseYMLSmallStringSmallJson(smallJsont *self, smallStringt *input if (!input) { return(false); } + if (checkObjectTypes && !isOSmallString(input)) { + return(false); + } + return(parseYMLSmallJson(self, ssGet(input))); } @@ -22285,7 +23682,7 @@ internal smallJsont* readStreamSmallJson(smallJsont *self, FILE *fp) { internal int writeFileSmallJson(smallJsont *self, const char *filePath) { int r; - if (!filePath) { + if (!filePath || isBlankS(filePath)) { return(0); } @@ -22367,11 +23764,14 @@ internal int writeStreamSmallJson(smallJsont *self, FILE *fp) { internal int appendFileSmallJson(smallJsont *self, const char *filePath) { int r; - if (!filePath) { + if (!filePath || isBlankS(filePath)) { return(0); } char *tmp = strdup(filePath); + if (!tmp) { + return(0); + } iLowerS(&tmp); char *s = NULL; @@ -22394,7 +23794,8 @@ internal int appendFileSmallJson(smallJsont *self, const char *filePath) { } r = appendFileS(filePath, s); done: - freeManyS(s, tmp); + // s can be null when filePath doesn't end with json or yml and self is not string or array + freeManyS(tmp, s); if (r) { return(1); } @@ -22589,7 +23990,7 @@ internal bool writeTextJsonSmallJson(smallJsont *self, smallJsont *filePath) { internal bool writeTextStreamSmallJson(smallJsont *self, FILE *fp) { // sanity checks - if (self->topIsA != SMALLJSON_IS_EMPTY && self->topIsA != TOP_IS_ARRAY) { + if (self->topIsA != TOP_IS_ARRAY) { return(false); } @@ -22669,6 +24070,7 @@ internal const char* typeStringSmallJson(smallJsont *self, const char *key) { typeStringJsonPath; if (!o) { + // not reachable, but make sure this function is robust return(NULL); } @@ -22677,7 +24079,11 @@ internal const char* typeStringSmallJson(smallJsont *self, const char *key) { internal smallStringt* typeSmallStringSmallJson(smallJsont *self, const char *key) { - return(allocSmallString(typeStringSmallJson(self, key))); + const char *r = typeStringSmallJson(self, key); + if (!r) { + return(NULL); + } + return(allocSmallString(r)); } internal const char* typeAtStringSmallJson(smallJsont *self, intmax_t index) { @@ -22708,6 +24114,7 @@ internal char typeSmallJson(smallJsont *self, const char *key) { typeJsonPath; if (!o) { + // not reachable, but make sure this function is robust return(0); } @@ -22778,15 +24185,17 @@ internal smallJsont* typeStringsSmallJson(smallJsont *self) { return(r); case TOP_IS_ARRAY: ;char** a = sArrayTypeStrings(self->topA); + // the strings in a are const char*, just free the array, not the strings if (!a) { return(NULL); } createAllocateSmallJson(ra); if (!ra) { - listFreeS(a); + free(a); return(NULL); } - setTopNFreeArraycSmallJson(ra, a); + setTopArraycSmallJson(ra, a); + free(a); return(ra); } return(NULL); @@ -23069,8 +24478,10 @@ internal bool areAllETypeSmallJson(smallJsont *self, const char *type) { return(false); } + bool foundAnElement = false; forEachSDict(self->top, e) { if (e->key) { + foundAnElement = true; smallt *o = sDictGetTiny(self->top, e->key); if (!o) { @@ -23084,6 +24495,10 @@ internal bool areAllETypeSmallJson(smallJsont *self, const char *type) { } } + if (!foundAnElement) { + // detect when elements are deleted, len > 0 and e->key is null + return(false); + } return(r); case TOP_IS_ARRAY: if (!type) { @@ -23490,11 +24905,9 @@ smallJsont* pushVoidSmallJsonG (smallJsont *self, void *value) { if (!c) { return(NULL); } - self->f->pushNFreeSmallContainer(self, c); - return(self); + return(self->f->pushNFreeSmallContainer(self, c)); } - self->f->pushUndefined(self); - return(self); + return(self->f->pushUndefined(self)); } smallJsont* pushSmallBoolSmallJsonG (smallJsont *self, smallBoolt *value) { @@ -23700,13 +25113,9 @@ smallJsont* setSmallJsonG (smallJsont *self, const char *key, baset *val return(self->f->set(self, key, value)); } -smallJsont* setUndefinedSmallJsonG(smallJsont *self, const char *key, void *value) { +smallJsont* setUndefinedSmallJsonG(smallJsont *self, const char *key, void *value UNUSED) { - if (value) { - free(value); - } - self->f->setUndefined(self,key); - return(self); + return(self->f->setUndefined(self,key)); } smallJsont* setBoolSmallJsonG (smallJsont *self, const char *key, bool value) { @@ -23755,17 +25164,18 @@ smallJsont* setCArraycSmallJsonG (smallJsont *self, const char *key, const char } smallJsont* setVoidSmallJsonG (smallJsont *self, const char *key, void *value) { + smallJsont *r = NULL; if (key && value) { smallContainert *c = allocSmallContainer(value); if (!c) { return(NULL); } - self->f->setNFreeSmallContainer(self, key, c); - return(self); + r = self->f->setNFreeSmallContainer(self, key, c);; + return(r); } - self->f->setUndefined(self,key); - return(self); + r = self->f->setUndefined(self,key);; + return(r); } smallJsont* setSmallBoolSmallJsonG (smallJsont *self, const char *key, smallBoolt *value) { @@ -23977,11 +25387,9 @@ smallJsont* setAtVoidSmallJsonG (smallJsont *self, intmax_t index, void *val if (!c) { return(NULL); } - self->f->setAtNFreeSmallContainer(self, index, c); - return(self); + return(self->f->setAtNFreeSmallContainer(self, index, c)); } - self->f->setAtUndefined(self,index); - return(self); + return(self->f->setAtUndefined(self,index)); } smallJsont* setAtSmallBoolSmallJsonG (smallJsont *self, intmax_t index, smallBoolt *value) { @@ -24660,11 +26068,9 @@ smallJsont* prependVoidSmallJsonG (smallJsont *self, void *value) { if (!c) { return(NULL); } - self->f->prependNFreeSmallContainer(self, c); - return(self); + return(self->f->prependNFreeSmallContainer(self, c)); } - self->f->prependUndefined(self); - return(self); + return(self->f->prependUndefined(self)); } smallJsont* prependSmallBoolSmallJsonG (smallJsont *self, smallBoolt *value) { @@ -24943,9 +26349,9 @@ smallJsont* shiftSmallJsonSmallJsonG(smallJsont *self, smallJsont *array) { } } -smallJsont* shiftSmallJsonNSmashSmallJsonG(smallJsont *self, smallJsont *array) { +smallJsont* shiftNSmashSmallJsonSmallJsonG(smallJsont *self, smallJsont *array) { - return(self->f->shiftSmallJsonNSmash(self,array)); { + return(self->f->shiftNSmashSmallJson(self,array)); { // cg c bug if } } @@ -25190,14 +26596,14 @@ smallJsont* insertNSmashSmallJsonG(smallJsont *self, intmax_t index, sm return(self->f->insertNSmash(self, index, toInsert)); } -smallJsont* insertJsonSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert) { +smallJsont* insertSmallJsonSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert) { - return(self->f->insertJson(self, index, toInsert)); + return(self->f->insertSmallJson(self, index, toInsert)); } -smallJsont* insertJsonNSmashSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert) { +smallJsont* insertNSmashSmallJsonSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert) { - return(self->f->insertJsonNSmash(self, index, toInsert)); + return(self->f->insertNSmashSmallJson(self, index, toInsert)); } smallJsont* insertStringSmallJsonG (smallJsont *self, intmax_t index, smallStringt *toInsert) { @@ -25210,12 +26616,12 @@ smallJsont* insertSSmallJsonG (smallJsont *self, intmax_t index, const cha return(self->f->insertS(self, index, toInsert)); } -smallJsont* insertNFreeSmallJsonG(smallJsont *self, intmax_t index, smallStringt *toInsert) { +smallJsont* insertNFreeStringSmallJsonG(smallJsont *self, intmax_t index, smallStringt *toInsert) { return(self->f->insertNFreeString(self, index, toInsert)); } -smallJsont* insertSNFreeStringSmallJsonG(smallJsont *self, intmax_t index, char *toInsert) { +smallJsont* insertNFreeSSmallJsonG(smallJsont *self, intmax_t index, char *toInsert) { return(self->f->insertSNFree(self, index, toInsert)); } @@ -25226,13 +26632,9 @@ smallJsont* injectSmallJsonG (smallJsont *self, intmax_t index, baset *v return(self->f->inject(self, index, value)); } -smallJsont* injectUndefinedSmallJsonG(smallJsont *self, intmax_t index, void *value) { +smallJsont* injectUndefinedSmallJsonG(smallJsont *self, intmax_t index, void *value UNUSED) { - if (value) { - free(value); - } - self->f->injectUndefined(self,index); - return(self); + return(self->f->injectUndefined(self,index)); } smallJsont* injectBoolSmallJsonG (smallJsont *self, intmax_t index, bool value) { @@ -26217,6 +27619,11 @@ bool equalSmallJsonSmallBoolG(smallJsont* self, smallBoolt* p2) { return(self->f->equalSmallBool(self, p2)); } +bool equalSmallJsonSmallBytesG(smallJsont* self, smallBytest* p2) { + + return(self->f->equalSmallBytes(self, p2)); +} + bool equalSmallJsonSmallDoubleG(smallJsont* self, smallDoublet* p2) { return(self->f->equalSmallDouble(self, p2)); @@ -26799,7 +28206,7 @@ smallJsont* zipArraySmallJsonG(smallJsont *self, char** array1, smallArrayt *arr smallJsont* zipCArraySmallJsonG(smallJsont *self, const char** array1, smallArrayt *array2) { - return(self->f->zipArray(self,(char**)array1,array2)); + return(self->f->zipCArray(self,array1,array2)); } smallJsont* zipCharSmallJsonG(smallJsont *self, smallArrayt *array1, char** array2) { @@ -26809,7 +28216,7 @@ smallJsont* zipCharSmallJsonG(smallJsont *self, smallArrayt *array1, char** arra smallJsont* zipCCharSmallJsonG(smallJsont *self, smallArrayt *array1, const char** array2) { - return(self->f->zipChar(self,array1,(char**)array2)); + return(self->f->zipCChar(self,array1,array2)); } smallJsont* zipArrayCharSmallJsonG(smallJsont *self, char** array1, char** array2) { @@ -26819,17 +28226,17 @@ smallJsont* zipArrayCharSmallJsonG(smallJsont *self, char** array1, char** array smallJsont* zipArrayCCharSmallJsonG(smallJsont *self, char** array1, const char** array2) { - return(self->f->zipArrayChar(self,array1,(char**)array2)); + return(self->f->zipArrayCChar(self,array1,array2)); } smallJsont* zipCArrayCharSmallJsonG(smallJsont *self, const char** array1, char** array2) { - return(self->f->zipArrayChar(self,(char**)array1,array2)); + return(self->f->zipCArrayChar(self,array1,array2)); } smallJsont* zipCArrayCCharSmallJsonG(smallJsont *self, const char** array1, const char** array2) { - return(self->f->zipArrayChar(self,(char**)array1,(char**)array2)); + return(self->f->zipCArrayCChar(self,array1,array2)); } smallJsont* zipJsonSmallJsonG (smallJsont *self, smallJsont *array1, smallJsont *array2) { @@ -26849,7 +28256,7 @@ smallJsont* zipJsonArraySmallJsonG (smallJsont *self, smallJsont *array1, ch smallJsont* zipJsonCArraySmallJsonG (smallJsont *self, smallJsont *array1, const char** array2) { - return(self->f->zipJsonArray(self,array1,(char**)array2)); + return(self->f->zipJsonCArray(self,array1,array2)); } smallJsont* zipSmallArrayJsonSmallJsonG(smallJsont *self, smallArrayt *array1, smallJsont *array2) { @@ -26864,7 +28271,7 @@ smallJsont* zipArrayJsonSmallJsonG (smallJsont *self, char** array1, smallJs smallJsont* zipCArrayJsonSmallJsonG (smallJsont *self, const char** array1, smallJsont *array2) { - return(self->f->zipArrayJson(self,(char**)array1,array2)); + return(self->f->zipCArrayJson(self,array1,array2)); } @@ -26987,7 +28394,7 @@ smallJsont* readTextJsonSmallJsonG(smallJsont *self, smallJsont *filePath) { smallJsont* readTextStreamSmallJsonG (smallJsont *self, FILE *fp) { - return(self->f->readStream(self, fp)); + return(self->f->readTextStream(self, fp)); } bool writeTextSmallJsonG (smallJsont *self, const char *filePath) { @@ -27007,7 +28414,7 @@ bool writeTextJsonSmallJsonG(smallJsont *self, smallJsont *filePath) { bool writeTextStreamSmallJsonG (smallJsont *self, FILE *fp) { - return(self->f->writeStream(self, fp)); + return(self->f->writeTextStream(self, fp)); } bool appendTextSmallStringSmallJsonG(smallJsont *self, smallStringt *filePath) { diff --git a/src/json/libsheepyCSmallJson.h b/src/json/libsheepyCSmallJson.h @@ -1064,7 +1064,7 @@ typedef smallJsont* (*appendNSmashArraySmallJsonFt)(smallJsont *self, c typedef smallJsont* (*shiftSmallJsonFt) (smallJsont *self, smallArrayt *array); typedef smallJsont* (*shiftNSmashSmallJsonFt) (smallJsont *self, smallArrayt *array); typedef smallJsont* (*shiftSmallJsonSmallJsonFt) (smallJsont *self, smallJsont *array); -typedef smallJsont* (*shiftSmallJsonNSmashSmallJsonFt)(smallJsont *self, smallJsont *array); +typedef smallJsont* (*shiftNSmashSmallJsonSmallJsonFt)(smallJsont *self, smallJsont *array); /** * add self and list in a new list @@ -1222,8 +1222,8 @@ typedef smallJsont* (*copySmallJsonFt) (smallJsont *self, intmax_t star */ typedef smallJsont* (*insertSmallJsonFt) (smallJsont *self, intmax_t index, smallArrayt *toInsert); typedef smallJsont* (*insertNSmashSmallJsonFt) (smallJsont *self, intmax_t index, smallArrayt *toInsert); -typedef smallJsont* (*insertJsonSmallJsonFt) (smallJsont *self, intmax_t index, smallJsont *toInsert); -typedef smallJsont* (*insertJsonNSmashSmallJsonFt)(smallJsont *self, intmax_t index, smallJsont *toInsert); +typedef smallJsont* (*insertSmallJsonSmallJsonFt) (smallJsont *self, intmax_t index, smallJsont *toInsert); +typedef smallJsont* (*insertNSmashSmallJsonSmallJsonFt)(smallJsont *self, intmax_t index, smallJsont *toInsert); /** * insert smallString in self at index @@ -2254,13 +2254,20 @@ typedef char* (*colordSmallJsonFt) (smallJsont *self, const char *color); */ typedef smallJsont* (*zipSmallJsonFt) (smallJsont *self, smallArrayt *array1, smallArrayt *array2); typedef smallJsont* (*zipArraySmallJsonFt) (smallJsont *self, char** array1, smallArrayt *array2); +typedef smallJsont* (*zipCArraySmallJsonFt) (smallJsont *self, const char** array1, smallArrayt *array2); typedef smallJsont* (*zipCharSmallJsonFt) (smallJsont *self, smallArrayt *array1, char** array2); +typedef smallJsont* (*zipCCharSmallJsonFt) (smallJsont *self, smallArrayt *array1, const char** array2); typedef smallJsont* (*zipArrayCharSmallJsonFt) (smallJsont *self, char** array1, char** array2); +typedef smallJsont* (*zipCArrayCharSmallJsonFt) (smallJsont *self, const char** array1, char** array2); +typedef smallJsont* (*zipArrayCCharSmallJsonFt) (smallJsont *self, char** array1, const char** array2); +typedef smallJsont* (*zipCArrayCCharSmallJsonFt)(smallJsont *self, const char** array1, const char** array2); typedef smallJsont* (*zipJsonSmallJsonFt) (smallJsont *self, smallJsont *array1, smallJsont *array2); typedef smallJsont* (*zipJsonSmallArraySmallJsonFt)(smallJsont *self, smallJsont *array1, smallArrayt *array2); typedef smallJsont* (*zipJsonArraySmallJsonFt) (smallJsont *self, smallJsont *array1, char** array2); +typedef smallJsont* (*zipJsonCArraySmallJsonFt) (smallJsont *self, smallJsont *array1, const char** array2); typedef smallJsont* (*zipSmallArrayJsonSmallJsonFt)(smallJsont *self, smallArrayt *array1, smallJsont *array2); typedef smallJsont* (*zipArrayJsonSmallJsonFt) (smallJsont *self, char** array1, smallJsont *array2); +typedef smallJsont* (*zipCArrayJsonSmallJsonFt) (smallJsont *self, const char** array1, smallJsont *array2); /** * iterator @@ -2950,7 +2957,7 @@ typedef bool (*areAllEBytesSmallJsonFt) (smallJsont *self); shiftSmallJsonFt shift;\ shiftNSmashSmallJsonFt shiftNSmash;\ shiftSmallJsonSmallJsonFt shiftSmallJson;\ - shiftSmallJsonNSmashSmallJsonFt shiftSmallJsonNSmash;\ + shiftNSmashSmallJsonSmallJsonFt shiftNSmashSmallJson;\ addSmallJsonFt add;\ addJsonSmallJsonFt addJson;\ sliceSmallJsonFt slice;\ @@ -2999,8 +3006,8 @@ typedef bool (*areAllEBytesSmallJsonFt) (smallJsont *self); copySmallJsonFt copy;\ insertSmallJsonFt insert;\ insertNSmashSmallJsonFt insertNSmash;\ - insertJsonSmallJsonFt insertJson;\ - insertJsonNSmashSmallJsonFt insertJsonNSmash;\ + insertSmallJsonSmallJsonFt insertSmallJson;\ + insertNSmashSmallJsonSmallJsonFt insertNSmashSmallJson;\ insertStringSmallJsonFt insertString;\ insertSSmallJsonFt insertS;\ insertNFreeStringSmallJsonFt insertNFreeString;\ @@ -3316,13 +3323,20 @@ typedef bool (*areAllEBytesSmallJsonFt) (smallJsont *self); colordSmallJsonFt colord;\ zipSmallJsonFt zip;\ zipArraySmallJsonFt zipArray;\ + zipCArraySmallJsonFt zipCArray;\ zipCharSmallJsonFt zipChar;\ + zipCCharSmallJsonFt zipCChar;\ zipArrayCharSmallJsonFt zipArrayChar;\ + zipCArrayCharSmallJsonFt zipCArrayChar;\ + zipArrayCCharSmallJsonFt zipArrayCChar;\ + zipCArrayCCharSmallJsonFt zipCArrayCChar;\ zipJsonSmallJsonFt zipJson;\ zipJsonSmallArraySmallJsonFt zipJsonSmallArray;\ zipJsonArraySmallJsonFt zipJsonArray;\ + zipJsonCArraySmallJsonFt zipJsonCArray;\ zipSmallArrayJsonSmallJsonFt zipSmallArrayJson;\ zipArrayJsonSmallJsonFt zipArrayJson;\ + zipCArrayJsonSmallJsonFt zipCArrayJson;\ iterStartSmallJsonFt iterStart;\ iterStartKeySmallJsonFt iterStartKey;\ iterStartLastSmallJsonFt iterStartLast;\ @@ -3917,7 +3931,7 @@ smallJsont* appendCArraySmallJsonG (smallJsont *self, const char **array); smallJsont* shiftSmallJsonG (smallJsont *self, smallArrayt *array); smallJsont* shiftNSmashSmallJsonG (smallJsont *self, smallArrayt *array); smallJsont* shiftSmallJsonSmallJsonG (smallJsont *self, smallJsont *array); -smallJsont* shiftSmallJsonNSmashSmallJsonG(smallJsont *self, smallJsont *array); +smallJsont* shiftNSmashSmallJsonSmallJsonG(smallJsont *self, smallJsont *array); smallJsont* addSmallJsonG (smallJsont *self, smallArrayt *array); smallJsont* addJsonSmallJsonG(smallJsont *self, smallJsont *array); @@ -3967,12 +3981,12 @@ smallContainert* cropElemKeySmallContainerSmallJsonG(smallJsont *self, const cha smallJsont* copySmallJsonG (smallJsont *self, intmax_t start, intmax_t end); smallJsont* insertSmallJsonG(smallJsont *self, intmax_t index, smallArrayt *toInsert); smallJsont* insertNSmashSmallJsonG(smallJsont *self, intmax_t index, smallArrayt *toInsert); -smallJsont* insertJsonSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert); -smallJsont* insertJsonNSmashSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert); +smallJsont* insertSmallJsonSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert); +smallJsont* insertNSmashSmallJsonSmallJsonG(smallJsont *self, intmax_t index, smallJsont *toInsert); smallJsont* insertStringSmallJsonG (smallJsont *self, intmax_t index, smallStringt *toInsert); smallJsont* insertSSmallJsonG (smallJsont *self, intmax_t index, const char *toInsert); -smallJsont* insertNFreeStringSmallJsonG(smallJsont *self, intmax_t index, smallStringt *toInsert); -smallJsont* insertSNFreeSmallJsonG (smallJsont *self, intmax_t index, char *toInsert); +smallJsont* insertNFreeStringSmallJsonG(smallJsont *self, intmax_t index, smallStringt *toInsert); // not used in generics +smallJsont* insertNFreeSSmallJsonG (smallJsont *self, intmax_t index, char *toInsert); // not used in generics smallJsont* injectSmallJsonG (smallJsont *self, intmax_t index, baset *value); smallJsont* injectUndefinedSmallJsonG(smallJsont *self, intmax_t index, void *value); diff --git a/src/json/libsheepyCSmallJsonInternal.h b/src/json/libsheepyCSmallJsonInternal.h @@ -123,10 +123,23 @@ declareRecycle(smallJsont); keyIdx = i+1;\ }\ if (key[i] == '\\' && status == 1) escape = true;\ - } + }\ + UNIQVAR(walkEnd): UNUSED +// when the path is wrong: +// - free the sContainer if baset is not smallContainer +// (it means baset was not a smallObject, it was a user class instead) +// - free the sObjects that not stored in a smallObject (newly allocated) #define setJsonPath\ - walkJsonPath(if (!oo) {free(keyInPath);return NULL;}/*getSmallObj*/, sArraySetTiny(*a, idx, o)/*arrayCode*/, sDictSetTiny(d, keyInPath, o)/*dictCode*/, /*returnFail*/, sDictSetTiny(&(self->top), key, o); return self/*directHit*/, return NULL/*keyErrorReturn*/) + walkJsonPath(if (!oo) {free(keyInPath); if(isNew){free(o);} return NULL;}/*getSmallObj*/, sArraySetTiny(*a, idx, o)/*arrayCode*/, sDictSetTiny(d, keyInPath, o)/*dictCode*/, /*returnFail*/, sDictSetTiny(&(self->top), key, o); return self/*directHit*/, if(isNew){free(o);} return NULL/*keyErrorReturn*/) + +// for setArrayc, the strings are newly allocated so sFree has to used instead +// of free to avoid memory leaks +#define setJsonPathsFree\ + walkJsonPath(if (!oo) {free(keyInPath); if(isNew){sFree(o);} return NULL;}/*getSmallObj*/, sArraySetTiny(*a, idx, o)/*arrayCode*/, sDictSetTiny(d, keyInPath, o)/*dictCode*/, /*returnFail*/, sDictSetTiny(&(self->top), key, o); return self/*directHit*/, if(isNew){sFree(o);} return NULL/*keyErrorReturn*/) + +#define setJsonPathWithoutReturn\ + walkJsonPath(if (!oo) {free(keyInPath);if(isNew){free(o);}return NULL;}/*getSmallObj*/, sArraySetTiny(*a, idx, o)/*arrayCode*/, sDictSetTiny(d, keyInPath, o)/*dictCode*/, /*returnFail*/, sDictSetTiny(&(self->top), key, o); goto UNIQVAR(walkEnd)/*directHit*/, if(isNew){free(o);}return NULL/*keyErrorReturn*/) #define setPJsonPath(objP)\ walkJsonPath(if (!oo) {free(keyInPath);return NULL;}/*getSmallObj*/, sArraySetP(*a, idx, objP)/*arrayCode*/, sDictSetP(d, keyInPath, objP)/*dictCode*/, /*returnFail*/, sDictSetP(&(self->top), key, objP); return self/*directHit*/, return NULL/*keyErrorReturn*/) @@ -143,7 +156,7 @@ declareRecycle(smallJsont); walkJsonPath(if (!oo) {free(keyInPath);return NULL;}/*getSmallObj*/, if (!*a) {free(keyInPath);return NULL;} sArrayDelTiny(*a, idx)/*arrayCode*/, if (!*d) {free(keyInPath);return NULL;} sDictDelTiny(*d, keyInPath)/*dictCode*/, if (!self->top) return NULL/*returnFail*/, sDictDelTiny(self->top, key) ; return self/*directHit*/, return NULL/*keyErrorReturn*/) #define removeJsonPath\ - walkJsonPath(if (!oo) {free(keyInPath);return NULL;}/*getSmallObj*/, if (!*a) {free(keyInPath);return NULL;} sArraySetShortTiny(*a, idx, NULL)/*arrayCode*/, if (!*d) {free(keyInPath);return NULL;} forEachSDict(*d, e) {if (e->key && eqS(keyInPath, e->key)) { e->key = NULL; break;} }/*dictCode*/, if (!self->top) return NULL/*returnFail*/, forEachSDict(self->top, e) {if (e->key && eqS(key, e->key)) { e->key = NULL; break;} } ; return self/*directHit*/, return NULL/*keyErrorReturn*/) + walkJsonPath(if (!oo) {free(keyInPath);return NULL;}/*getSmallObj*/, if (!*a) {free(keyInPath);return NULL;} sArraySetShortTiny(*a, idx, NULL)/*arrayCode*/, if (!*d) {free(keyInPath);return NULL;} forEachSDict(*d, e) {if (e->key && eqS(keyInPath, e->key)) { free(e->key); e->key = NULL; break;} }/*dictCode*/, if (!self->top) return NULL/*returnFail*/, forEachSDict(self->top, e) {if (e->key && eqS(key, e->key)) { free(e->key); e->key = NULL; break;} } ; return self/*directHit*/, return NULL/*keyErrorReturn*/) #define typeStringJsonPath\ walkJsonPath(if (!oo) {free(keyInPath);return NULL;}/*getSmallObj*/, if (!*a) {free(keyInPath);return NULL;} o = sArrayGetTiny(*a, idx)/*arrayCode*/, if (!*d) {free(keyInPath);return NULL;} o = sDictGetTiny(*d, keyInPath)/*dictCode*/, if (!self->top) return NULL/*returnFail*/, o = sDictGet(self->top, key) ; if (!o) return NULL; return SMALL_TYPE_NAMES[(size_t)o->type]/*directHit*/, return NULL/*keyErrorReturn*/) @@ -154,12 +167,12 @@ declareRecycle(smallJsont); #define isEJsonPath(test)\ walkJsonPath(if (!oo) {free(keyInPath);return 0;}/*getSmallObj*/, if (!*a) {free(keyInPath);return 0;} o = sArrayGetTiny(*a, idx)/*arrayCode*/, if (!*d) {free(keyInPath);return 0;} o = sDictGetTiny(*d, keyInPath)/*dictCode*/, if (!self->top) return 0/*returnFail*/, o = sDictGet(self->top, key) ; if (!o) return false; return test/*directHit*/, return 0/*keyErrorReturn*/) -#define mainSetJsonPath(initValue, allocValue, subSetJsonPath) \ +#define mainSetJsonPathWithoutReturn(initValue, allocValue, subSetJsonPath) \ smallt *o = NULL;\ if (!key)\ return NULL;\ jsonPathRest keyType = keyIsSmallJson(self, key);\ - if (self->topIsA != SMALLJSON_IS_EMPTY && ((keyType == ARRAY_PATH && (self->topIsA != TOP_IS_ARRAY && self->topIsA != SMALLJSON_IS_EMPTY)) || ((keyType == NOT_A_PATH || keyType == DICT_PATH) && (self->topIsA != TOP_IS_DICT && self->topIsA != SMALLJSON_IS_EMPTY))))\ + if (self->topIsA != SMALLJSON_IS_EMPTY && ((keyType == ARRAY_PATH && self->topIsA != TOP_IS_ARRAY) || ((keyType == NOT_A_PATH || keyType == DICT_PATH) && self->topIsA != TOP_IS_DICT)))\ return NULL;\ initValue;\ switch(self->topIsA) {\ @@ -172,9 +185,14 @@ declareRecycle(smallJsont); case TOP_IS_ARRAY:\ ;FALLTHRU;\ case TOP_IS_DICT:;\ + bool isNew UNUSED = false;\ allocValue;\ subSetJsonPath;\ break;\ - }\ + } + + +#define mainSetJsonPath(initValue, allocValue, subSetJsonPath) \ + mainSetJsonPathWithoutReturn(initValue, allocValue, subSetJsonPath)\ return self diff --git a/src/json/libsheepyCSmallString.c b/src/json/libsheepyCSmallString.c @@ -900,6 +900,9 @@ internal smallStringt* duplicateSmallString(smallStringt *self) { if (!dup) { return(NULL); } + if (!self->data) { + return(dup); + } isError(dup->data, (sStringt *)strdup((char *)self->data)) { terminateO(dup); return(NULL); @@ -1026,6 +1029,11 @@ internal smallStringt* setSmallArraySmallString(smallStringt* self, smallArrayt* if (!p2) { return(NULL); } + + if (checkObjectTypes && !isOSmallArray(p2)) { + return(NULL); + } + char *s = toStringO(p2); smallStringt *r = setNFreeSmallString(self, s); if (!r) { @@ -1039,6 +1047,10 @@ internal smallStringt* setFromSmallDictSmallString(smallStringt* self, smallDict if (!p2) { return(NULL); } + if (checkObjectTypes && !isOSmallDict(p2)) { + return(NULL); + } + char *s = toStringO(p2); smallStringt *r = setNFreeSmallString(self, s); if (!r) { @@ -1052,6 +1064,10 @@ internal smallStringt* setFromSmallJsonSmallString(smallStringt* self, smallJson if (!p2) { return(NULL); } + if (checkObjectTypes && !isOSmallJson(p2)) { + return(NULL); + } + char *s = toStringO(p2); smallStringt *r = setNFreeSmallString(self, s); if (!r) { @@ -1065,6 +1081,10 @@ internal smallStringt* setSmallBoolSmallString(smallStringt* self, smallBoolt* p if (!p2) { return(NULL); } + if (checkObjectTypes && !isOSmallBool(p2)) { + return(NULL); + } + char *s = toStringO(p2); smallStringt *r = setNFreeSmallString(self, s); if (!r) { @@ -1078,6 +1098,10 @@ internal smallStringt* setSmallDoubleSmallString(smallStringt* self, smallDouble if (!p2) { return(NULL); } + if (checkObjectTypes && !isOSmallDouble(p2)) { + return(NULL); + } + char *s = toStringO(p2); smallStringt *r = setNFreeSmallString(self, s); if (!r) { @@ -1091,6 +1115,10 @@ internal smallStringt* setSmallIntSmallString(smallStringt* self, smallIntt* p2) if (!p2) { return(NULL); } + if (checkObjectTypes && !isOSmallInt(p2)) { + return(NULL); + } + char *s = toStringO(p2); smallStringt *r = setNFreeSmallString(self, s); if (!r) { @@ -1104,6 +1132,10 @@ internal smallStringt* setSmallJsonSmallString(smallStringt* self, smallJsont* p if (!p2) { return(NULL); } + if (checkObjectTypes && !isOSmallJson(p2)) { + return(NULL); + } + const char *type = getTopTypeO(p2); @@ -1144,6 +1176,10 @@ internal smallStringt* setSmallStringSmallString(smallStringt* self, smallString if (!p2) { return(NULL); } + if (checkObjectTypes && !isOSmallString(p2)) { + return(NULL); + } + setSmallString(self, ssGet(p2)); return(self); } @@ -1157,7 +1193,7 @@ internal smallStringt* setNFreeSmallString(smallStringt *self, char *string) { if (r) { free(string); } - return(self); + return(r); } internal smallStringt* appendSmallString(smallStringt *self, smallStringt *string) { @@ -1166,6 +1202,10 @@ internal smallStringt* appendSmallString(smallStringt *self, smallStringt *strin if (!string) { return(NULL); } + if (checkObjectTypes && !isOSmallString(string)) { + return(NULL); + } + if (!string->data) { return(self); } @@ -1185,7 +1225,7 @@ internal smallStringt* appendSmallString(smallStringt *self, smallStringt *strin // +1 for 0 at the end +1 for sType at the start tmp = realloc(self->data, self->_len + string->_len + 2); if (!tmp) { - return(self); + return(NULL); } self->data = (sStringt *)tmp; @@ -1199,11 +1239,15 @@ internal smallStringt* appendSmallJsonSmallString(smallStringt *self, smallJsont if (!string) { return(NULL); } + if (checkObjectTypes && !isOSmallJson(string)) { + return(NULL); + } + const char *type = getTopTypeO(string); if (!type || !eqS(type, "string")) { - return(self); + return(NULL); } return(appendSSmallString(self, getTopSO(string))); @@ -1215,7 +1259,12 @@ internal smallStringt* appendNSmashSmallString(smallStringt *self, smallStringt if (!string) { return(NULL); } + if (checkObjectTypes && !isOSmallString(string)) { + return(NULL); + } + if (!string->data) { + terminateO(string); return(self); } @@ -1228,6 +1277,7 @@ internal smallStringt* appendNSmashSmallString(smallStringt *self, smallStringt if (!string->_len) { // empty string + terminateO(string); return(self); } @@ -1235,13 +1285,13 @@ internal smallStringt* appendNSmashSmallString(smallStringt *self, smallStringt // +1 for 0 at the end +1 for sType at the start tmp = realloc(self->data, self->_len + string->_len + 2); if (!tmp) { - return(self); + return(NULL); } self->data = (sStringt *)tmp; strcat((char *)self->data, sStringGetTiny(string->data)); self->_len += string->_len; - smashO(string); + terminateO(string); return(self); } @@ -1263,10 +1313,6 @@ internal smallStringt* appendSSmallString(smallStringt *self, char *string) { return(self); } - if (!len) { - return(self); - } - char *tmp; // +1 for 0 at the end +1 for sType at the start @@ -1292,20 +1338,24 @@ internal smallStringt* appendNSmashSmallJsonSmallString(smallStringt *self, smal if (!string) { return(NULL); } + if (checkObjectTypes && !isOSmallJson(string)) { + return(NULL); + } + const char *type = getTopTypeO(string); if (!type || !eqS(type, "string")) { - return(self); + return(NULL); } char *s = getTopSO(string); - if (!s) { - return(self); + smallStringt *r = appendSSmallString(self, s); + if (r) { + terminateO(string); } - - return(appendSSmallString(self, s)); + return(r); } @@ -1324,6 +1374,10 @@ internal smallStringt* prependSmallString(smallStringt *self, smallStringt *stri if (!string) { return(NULL); } + if (checkObjectTypes && !isOSmallString(string)) { + return(NULL); + } + if (!string->data) { return(self); } @@ -1349,11 +1403,15 @@ internal smallStringt* prependSmallJsonSmallString(smallStringt *self, smallJson if (!json) { return(NULL); } + if (checkObjectTypes && !isOSmallJson(json)) { + return(NULL); + } + const char *type = getTopTypeO(json); if (!type || !eqS(type, "string")) { - return(self); + return(NULL); } return(prependSSmallString(self, getTopSO(json))); @@ -1365,23 +1423,30 @@ internal smallStringt* prependNSmashSmallString(smallStringt *self, smallStringt if (!string) { return(NULL); } + if (checkObjectTypes && !isOSmallString(string)) { + return(NULL); + } + if (!string->data) { + terminateO(string); return(self); } if (!self->data) { self->data = (sStringt *)strdup((char *)string->data); self->_len = string->_len; + terminateO(string); return(self); } if (!string->_len) { // empty string + terminateO(string); return(self); } insertSmallString(self, 0, string); - smashO(string); + terminateO(string); return(self); } @@ -1391,12 +1456,15 @@ internal smallStringt* prependNSmashSmallJsonSmallString(smallStringt *self, sma if (!json) { return(NULL); } + if (checkObjectTypes && !isOSmallJson(json)) { + return(NULL); + } + const char *type = getTopTypeO(json); if (!type || !eqS(type, "string")) { - terminateO(json); - return(self); + return(NULL); } smallStringt* r = prependSSmallString(self, getTopSO(json)); @@ -1455,6 +1523,11 @@ internal smallStringt* catSmallString(smallStringt *self, ...) { va_start(pl, self); paramType = va_arg(pl, smallStringt*); while (paramType) { + if (checkObjectTypes && !isOSmallString(paramType)) { + disposeO(&l); + va_end(pl); + return(NULL); + } l.f->pushSmallString(&l, paramType); paramType = va_arg(pl, smallStringt*); } @@ -1489,6 +1562,7 @@ internal smallStringt* catSSmallString(smallStringt *self, ...) { va_end(pl); // join list with empty delimiter smallStringt *r = l.f->join(&l, ""); + free(self->data); self->data = r->data; self->_len = r->_len; finishO(r); @@ -1506,21 +1580,26 @@ internal smallStringt* pushNFreeManySmallString(smallStringt *self, ...) { l.f->pushSmallString(&l, self); } + // TODO free parameters after all have been added to the l array without error // add arguments to a list va_start(pl, self); paramType = va_arg(pl, smallStringt*); while (paramType) { + if (checkObjectTypes && !isOSmallString(paramType)) { + delO(&l, 1, 0); + disposeO(&l); + return(NULL); + } l.f->pushNFreeSmallString(&l, paramType); paramType = va_arg(pl, smallStringt*); } va_end(pl); // join list with empty delimiter smallStringt *r = l.f->join(&l, ""); - free(self->data); + freeO(&l); self->data = r->data; self->_len = r->_len; finishO(r); - disposeO(&l); return(self); } @@ -1531,7 +1610,7 @@ internal smallStringt* pushNFreeManySSmallString(smallStringt *self, ...) { createSmallArray(l); if (self->data) { // add self to result - l.f->pushS(&l, sStringGetTiny(self->data)); + l.f->pushSmallString(&l, self); } // add arguments to a list @@ -1575,7 +1654,7 @@ internal smallStringt* replaceSmallString(smallStringt *self, const char *olds, } if (!self->_len) { // s is empty, do nothing - return(NULL); + return(self); } if (!olds) { return(NULL); @@ -1668,7 +1747,15 @@ internal smallStringt* replaceCharCharSmallString(smallStringt *self, char olds, internal smallStringt* replaceSmallJsonSmallJsonSmallString(smallStringt *self, smallJsont *olds, smallJsont *news, size_t max) { - if (!olds || !news) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!olds) { return(NULL); } @@ -1676,6 +1763,10 @@ internal smallStringt* replaceSmallJsonSmallJsonSmallString(smallStringt *self, return(NULL); } + if (!news) { + return(replaceSmallJsonSSmallString(self, olds, "", max)); + } + if (checkObjectTypes && !isOSmallJson(news)) { return(NULL); } @@ -1697,7 +1788,15 @@ internal smallStringt* replaceSmallJsonSmallJsonSmallString(smallStringt *self, internal smallStringt* replaceSmallJsonSmallStringSmallString(smallStringt *self, smallJsont *olds, smallStringt *news, size_t max) { - if (!olds || !news) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!olds) { return(NULL); } @@ -1705,6 +1804,10 @@ internal smallStringt* replaceSmallJsonSmallStringSmallString(smallStringt *self return(NULL); } + if (!news) { + return(replaceSmallJsonSSmallString(self, olds, "", max)); + } + if (checkObjectTypes && !isOSmallString(news)) { return(NULL); } @@ -1720,7 +1823,15 @@ internal smallStringt* replaceSmallJsonSmallStringSmallString(smallStringt *self internal smallStringt* replaceSmallJsonSSmallString(smallStringt *self, smallJsont *olds, const char *news, size_t max) { - if (!olds || !news) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!olds) { return(NULL); } @@ -1739,6 +1850,14 @@ internal smallStringt* replaceSmallJsonSSmallString(smallStringt *self, smallJso internal smallStringt* replaceSmallJsonCharSmallString(smallStringt *self, smallJsont *olds, char news, size_t max) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + if (!olds) { return(NULL); } @@ -1759,7 +1878,15 @@ internal smallStringt* replaceSmallJsonCharSmallString(smallStringt *self, small internal smallStringt* replaceSmallStringSmallJsonSmallString(smallStringt *self, smallStringt *olds, smallJsont *news, size_t max) { - if (!olds || !news) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!olds) { return(NULL); } @@ -1767,6 +1894,10 @@ internal smallStringt* replaceSmallStringSmallJsonSmallString(smallStringt *self return(NULL); } + if (!news) { + return(replaceSmallStringSSmallString(self, olds, "", max)); + } + if (checkObjectTypes && !isOSmallJson(news)) { return(NULL); } @@ -1782,35 +1913,92 @@ internal smallStringt* replaceSmallStringSmallJsonSmallString(smallStringt *self internal smallStringt* replaceSmallStringSmallStringSmallString(smallStringt *self, smallStringt *olds, smallStringt *news, size_t max) { - if (!olds || !news) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!olds) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallString(olds)) { return(NULL); } + + if (!news) { + return(replaceSmallStringSSmallString(self, olds, "", max)); + } + + if (checkObjectTypes && !isOSmallString(news)) { + return(NULL); + } + return(replaceSmallString(self, ssGet(olds), ssGet(news), max)); } internal smallStringt* replaceSmallStringSSmallString(smallStringt *self, smallStringt *olds, const char *news, size_t max) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + if (!olds) { return(NULL); } + if (checkObjectTypes && !isOSmallString(olds)) { + return(NULL); + } + return(replaceSmallString(self, ssGet(olds), news, max)); } internal smallStringt* replaceSmallStringCharSmallString(smallStringt *self, smallStringt *olds, char news, size_t max) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + if (!olds) { return(NULL); } + if (checkObjectTypes && !isOSmallString(olds)) { + return(NULL); + } + charToS(s, news); return(replaceSmallString(self, ssGet(olds), s, max)); } internal smallStringt* replaceSSmallJsonSmallString(smallStringt *self, const char *olds, smallJsont *news, size_t max) { - if (!olds || !news) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!olds) { return(NULL); } + if (!news) { + return(replaceSmallString(self, olds, "", max)); + } + if (checkObjectTypes && !isOSmallJson(news)) { return(NULL); } @@ -1826,17 +2014,39 @@ internal smallStringt* replaceSSmallJsonSmallString(smallStringt *self, const ch internal smallStringt* replaceSSmallStringSmallString(smallStringt *self, const char *olds, smallStringt *news, size_t max) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + if (!news) { + return(replaceSmallString(self, olds, "", max)); + } + + if (checkObjectTypes && !isOSmallString(news)) { return(NULL); } + return(replaceSmallString(self, olds, ssGet(news), max)); } internal smallStringt* replaceCharSmallJsonSmallString(smallStringt *self, char olds, smallJsont *news, size_t max) { - if (!news) { + if (!self->data) { return(NULL); } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!news) { + charToS(s, olds); + return(replaceSmallString(self, s, "", max)); + } if (checkObjectTypes && !isOSmallJson(news)) { return(NULL); @@ -1854,9 +2064,23 @@ internal smallStringt* replaceCharSmallJsonSmallString(smallStringt *self, char internal smallStringt* replaceCharSmallStringSmallString(smallStringt *self, char olds, smallStringt *news, size_t max) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + if (!news) { + charToS(s, olds); + return(replaceSmallString(self, s, "", max)); + } + + if (checkObjectTypes && !isOSmallString(news)) { return(NULL); } + charToS(s, olds); return(replaceSmallString(self, s, ssGet(news), max)); } @@ -1867,22 +2091,36 @@ internal smallStringt* replaceManySmallString(smallStringt *self, const char *pa char *news = NULL; // sanity checks - if (!self->_len) { + if (!self->data) { return(NULL); } + if (!self->_len) { + // s is empty, do nothing + return(self); + } olds = (char *) paramType;; // add arguments to a list va_start(pl, paramType); paramType = va_arg(pl, char *); + if (!paramType) { + // missing news + va_end(pl); + return(NULL); + } while (paramType) { if (!olds) { olds = (char *) paramType; } else if (!news) { news = (char *) paramType; - replaceSmallString(self, olds, news, 0); + smallStringt *r = replaceSmallString(self, olds, news, 0); + if (!r) { + // replace error + va_end(pl); + return(NULL); + } olds = NULL; news = NULL; } @@ -1916,7 +2154,7 @@ internal smallStringt* icReplaceSmallString(smallStringt *self, const char *olds } if (!self->_len) { // s is empty, do nothing - return(NULL); + return(self); } if (!olds) { return(NULL); @@ -2009,7 +2247,15 @@ internal smallStringt* icReplaceCharCharSmallString(smallStringt *self, char old internal smallStringt* icReplaceSmallJsonSmallJsonSmallString(smallStringt *self, smallJsont *olds, smallJsont *news, size_t max) { - if (!olds || !news) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!olds) { return(NULL); } @@ -2017,6 +2263,10 @@ internal smallStringt* icReplaceSmallJsonSmallJsonSmallString(smallStringt *self return(NULL); } + if (!news) { + return(icReplaceSmallJsonSSmallString(self, olds, "", max)); + } + if (checkObjectTypes && !isOSmallJson(news)) { return(NULL); } @@ -2038,7 +2288,15 @@ internal smallStringt* icReplaceSmallJsonSmallJsonSmallString(smallStringt *self internal smallStringt* icReplaceSmallJsonSmallStringSmallString(smallStringt *self, smallJsont *olds, smallStringt *news, size_t max) { - if (!olds || !news) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!olds) { return(NULL); } @@ -2046,6 +2304,10 @@ internal smallStringt* icReplaceSmallJsonSmallStringSmallString(smallStringt *se return(NULL); } + if (!news) { + return(icReplaceSmallJsonSSmallString(self, olds, "", max)); + } + if (checkObjectTypes && !isOSmallString(news)) { return(NULL); } @@ -2061,7 +2323,15 @@ internal smallStringt* icReplaceSmallJsonSmallStringSmallString(smallStringt *se internal smallStringt* icReplaceSmallJsonSSmallString(smallStringt *self, smallJsont *olds, const char *news, size_t max) { - if (!olds || !news) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!olds) { return(NULL); } @@ -2080,6 +2350,14 @@ internal smallStringt* icReplaceSmallJsonSSmallString(smallStringt *self, smallJ internal smallStringt* icReplaceSmallJsonCharSmallString(smallStringt *self, smallJsont *olds, char news, size_t max) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + if (!olds) { return(NULL); } @@ -2100,7 +2378,15 @@ internal smallStringt* icReplaceSmallJsonCharSmallString(smallStringt *self, sma internal smallStringt* icReplaceSmallStringSmallJsonSmallString(smallStringt *self, smallStringt *olds, smallJsont *news, size_t max) { - if (!olds || !news) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!olds) { return(NULL); } @@ -2108,6 +2394,10 @@ internal smallStringt* icReplaceSmallStringSmallJsonSmallString(smallStringt *se return(NULL); } + if (!news) { + return(icReplaceSmallStringSSmallString(self, olds, "", max)); + } + if (checkObjectTypes && !isOSmallJson(news)) { return(NULL); } @@ -2123,34 +2413,91 @@ internal smallStringt* icReplaceSmallStringSmallJsonSmallString(smallStringt *se internal smallStringt* icReplaceSmallStringSmallStringSmallString(smallStringt *self, smallStringt *olds, smallStringt *news, size_t max) { - if (!olds || !news) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!olds) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallString(olds)) { + return(NULL); + } + + if (!news) { + return(icReplaceSmallStringSSmallString(self, olds, "", max)); + } + + if (checkObjectTypes && !isOSmallString(news)) { return(NULL); } + return(icReplaceSmallString(self, ssGet(olds), ssGet(news), max)); } internal smallStringt* icReplaceSmallStringSSmallString(smallStringt *self, smallStringt *olds, const char *news, size_t max) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + if (!olds) { return(NULL); } + if (checkObjectTypes && !isOSmallString(olds)) { + return(NULL); + } + return(icReplaceSmallString(self, ssGet(olds), news, max)); } internal smallStringt* icReplaceSmallStringCharSmallString(smallStringt *self, smallStringt *olds, char news, size_t max) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + if (!olds) { return(NULL); } + if (checkObjectTypes && !isOSmallString(olds)) { + return(NULL); + } + charToS(s, news); return(icReplaceSmallString(self, ssGet(olds), s, max)); } internal smallStringt* icReplaceSSmallJsonSmallString(smallStringt *self, const char *olds, smallJsont *news, size_t max) { - if (!olds || !news) { + if (!self->data) { return(NULL); } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!olds) { + return(NULL); + } + + if (!news) { + return(icReplaceSmallString(self, olds, "", max)); + } if (checkObjectTypes && !isOSmallJson(news)) { return(NULL); @@ -2167,17 +2514,39 @@ internal smallStringt* icReplaceSSmallJsonSmallString(smallStringt *self, const internal smallStringt* icReplaceSSmallStringSmallString(smallStringt *self, const char *olds, smallStringt *news, size_t max) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + if (!news) { + return(icReplaceSmallString(self, olds, "", max)); + } + + if (checkObjectTypes && !isOSmallString(news)) { return(NULL); } + return(icReplaceSmallString(self, olds, ssGet(news), max)); } internal smallStringt* icReplaceCharSmallJsonSmallString(smallStringt *self, char olds, smallJsont *news, size_t max) { - if (!news) { + if (!self->data) { return(NULL); } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!news) { + charToS(s, olds); + return(icReplaceSmallString(self, s, "", max)); + } if (checkObjectTypes && !isOSmallJson(news)) { return(NULL); @@ -2195,6 +2564,23 @@ internal smallStringt* icReplaceCharSmallJsonSmallString(smallStringt *self, cha internal smallStringt* icReplaceCharSmallStringSmallString(smallStringt *self, char olds, smallStringt *news, size_t max) { + if (!self->data) { + return(NULL); + } + if (!self->_len) { + // s is empty, do nothing + return(self); + } + + if (!news) { + charToS(s, olds); + return(icReplaceSmallString(self, s, "", max)); + } + + if (checkObjectTypes && !isOSmallString(news)) { + return(NULL); + } + charToS(s, olds); return(icReplaceSmallString(self, s, ssGet(news), max)); } @@ -2205,22 +2591,36 @@ internal smallStringt* icReplaceManySmallString(smallStringt *self, const char * char *news = NULL; // sanity checks - if (!self->_len) { + if (!self->data) { return(NULL); } + if (!self->_len) { + // s is empty, do nothing + return(self); + } olds = (char *) paramType;; // add arguments to a list va_start(pl, paramType); paramType = va_arg(pl, char *); + if (!paramType) { + // missing news + va_end(pl); + return(NULL); + } while (paramType) { if (!olds) { olds = (char *) paramType; } else if (!news) { news = (char *) paramType; - icReplaceSmallString(self, olds, news, 0); + smallStringt *r = icReplaceSmallString(self, olds, news, 0);; + if (!r) { + // replace error + va_end(pl); + return(NULL); + } olds = NULL; news = NULL; } @@ -2233,7 +2633,19 @@ internal smallStringt* icReplaceManySmallString(smallStringt *self, const char * internal bool equalSmallString(smallStringt *self, smallStringt *string) { - if (!self->data || !string || !string->data) { + if (!self->data) { + return(false); + } + + if (!string) { + return(false); + } + + if (checkObjectTypes && !isOSmallString(string)) { + return(false); + } + + if (!string->data) { return(false); } @@ -2380,10 +2792,20 @@ internal bool equalSmallStringUint64(smallStringt* self, uint64_t p2) { internal bool equalSmallStringSmallBool(smallStringt* self, smallBoolt* p2) { - if (!self->data || !p2 || !p2->value) { + if (!self->data) { + return(false); + } + if (!p2) { + return(false); + } + if (checkObjectTypes && !isOSmallBool(p2)) { + return(false); + } + if (!p2->value) { return(false); } + char *s = sStringGetTiny(self->data); if (strEq(s, "true") || strEq(s, "TRUE")) { return(p2->value->value); @@ -2396,7 +2818,16 @@ internal bool equalSmallStringSmallBool(smallStringt* self, smallBoolt* p2) { internal bool equalSmallStringSmallBytes(smallStringt* self, smallBytest* p2) { - if (!self->data || !p2 || !p2->B) { + if (!self->data) { + return(false); + } + if (!p2) { + return(false); + } + if (checkObjectTypes && !isOSmallBytes(p2)) { + return(false); + } + if (!p2->B) { return(false); } @@ -2410,7 +2841,16 @@ internal bool equalSmallStringSmallBytes(smallStringt* self, smallBytest* p2) { internal bool equalSmallStringSmallDouble(smallStringt* self, smallDoublet* p2) { - if (!self->data || !p2 || !p2->value) { + if (!self->data) { + return(false); + } + if (!p2) { + return(false); + } + if (checkObjectTypes && !isOSmallDouble(p2)) { + return(false); + } + if (!p2->value) { return(false); } @@ -2427,7 +2867,16 @@ internal bool equalSmallStringSmallDouble(smallStringt* self, smallDoublet* p2) internal bool equalSmallStringSmallInt(smallStringt* self, smallIntt* p2) { - if (!self->data || !p2 || !p2->value) { + if (!self->data) { + return(false); + } + if (!p2) { + return(false); + } + if (checkObjectTypes && !isOSmallInt(p2)) { + return(false); + } + if (!p2->value) { return(false); } @@ -2458,7 +2907,15 @@ internal bool equalSmallStringSmallJson(smallStringt* self, smallJsont* p2) { internal bool icEqualSmallString(smallStringt *self, smallStringt *string) { - if (!self->data || !string || !string->data) { + if (!self->data || !string) { + return(false); + } + + if (checkObjectTypes && !isOSmallString(string)) { + return(false); + } + + if (!string->data) { return(false); } @@ -2522,7 +2979,7 @@ internal bool equalISSmallString(smallStringt *self, const char *string, intmax_ return(false); } - if (index >= (intmax_t)self->_len) { + if (index >= (intmax_t)self->_len && self->_len) { return(false); } if (index < -(intmax_t)self->_len) { @@ -2536,6 +2993,14 @@ internal bool equalISSmallString(smallStringt *self, const char *string, intmax_ return(false); } + if (!string[0] && !self->_len) { + return(true); + } + + if (!string[0]) { + return(false); + } + return(strncmp(sStringGetTiny(self->data) + index, string, strlen(string)) == 0);; } @@ -2570,6 +3035,10 @@ internal bool equalISmallStringSmallString(smallStringt *self, smallStringt *str if (!string) { return(false); } + if (checkObjectTypes && !isOSmallString(string)) { + return(false); + } + return(equalISSmallString(self, ssGet(string), index)); } @@ -2584,8 +3053,11 @@ internal bool startsWithSSmallString(smallStringt *self, const char *string) { internal bool startsWithCharSmallString(smallStringt *self, char c) { - charToS(s, c); - return(startsWithSSmallString(self, s)); + if (!self->data) { + return(false); + } + + return(self->data->data == c);; } internal bool startsWithSmallJsonSmallString(smallStringt *self, smallJsont *string) { @@ -2613,6 +3085,10 @@ internal bool startsWithSmallStringSmallString(smallStringt *self, smallStringt if (!string) { return(false); } + if (checkObjectTypes && !isOSmallString(string)) { + return(false); + } + return(startsWithSSmallString(self, ssGet(string))); } @@ -2630,8 +3106,18 @@ internal bool endsWithSSmallString(smallStringt *self, const char *string) { internal bool endsWithCharSmallString(smallStringt *self, char c) { - charToS(s, c); - return(endsWithSSmallString(self, s)); + if (!self->data) { + return(false); + } + + if (!self->_len && !c) { + // self is empty and c is 0 + return(true); + } + if (!self->_len) { + return(false); + } + return(*(sStringGetTiny(self->data) + self->_len-1) == c);; } internal bool endsWithSmallJsonSmallString(smallStringt *self, smallJsont *string) { @@ -2659,12 +3145,16 @@ internal bool endsWithSmallStringSmallString(smallStringt *self, smallStringt *s if (!string) { return(false); } + if (checkObjectTypes && !isOSmallString(string)) { + return(false); + } + return(endsWithSSmallString(self, ssGet(string))); } internal ssize_t countSSmallString(smallStringt *self, const char *string) { - if (!self->data || !string) { + if (!self->data || !string || !string[0]) { return(-1); } @@ -2696,17 +3186,17 @@ internal ssize_t countSmallJsonSmallString(smallStringt *self, smallJsont *strin // sanity checks if (!string) { - return(false); + return(-1); } if (checkObjectTypes && !isOSmallJson(string)) { - return(false); + return(-1); } const char *type = getTopTypeO(string); if (!eqS(type,"string")) { - return(false); + return(-1); } return(countSSmallString(self, getTopSO(string))); @@ -2717,6 +3207,10 @@ internal ssize_t countSmallStringSmallString(smallStringt *self, smallStringt *s if (!string) { return(-1); } + if (checkObjectTypes && !isOSmallString(string)) { + return(-1); + } + return(countSSmallString(self, ssGet(string))); } @@ -2731,8 +3225,10 @@ internal bool icStartsWithSSmallString(smallStringt *self, const char *string) { internal bool icStartsWithCharSmallString(smallStringt *self, char c) { - charToS(s, c); - return(icStartsWithSSmallString(self, s)); + if (!self->data) { + return(false); + } + return(toupper(self->data->data) == toupper(c));; } internal bool icStartsWithSmallJsonSmallString(smallStringt *self, smallJsont *string) { @@ -2760,6 +3256,10 @@ internal bool icStartsWithSmallStringSmallString(smallStringt *self, smallString if (!string) { return(false); } + if (checkObjectTypes && !isOSmallString(string)) { + return(false); + } + return(icStartsWithSSmallString(self, ssGet(string))); } @@ -2777,8 +3277,18 @@ internal bool icEndsWithSSmallString(smallStringt *self, const char *string) { internal bool icEndsWithCharSmallString(smallStringt *self, char c) { - charToS(s, c); - return(icEndsWithSSmallString(self, s)); + if (!self->data) { + return(false); + } + + if (!self->_len && !c) { + // self is empty and c is 0 + return(true); + } + if (!self->_len) { + return(false); + } + return(toupper(*(sStringGetTiny(self->data) + self->_len-1)) == toupper(c));; } internal bool icEndsWithSmallJsonSmallString(smallStringt *self, smallJsont *string) { @@ -2806,12 +3316,16 @@ internal bool icEndsWithSmallStringSmallString(smallStringt *self, smallStringt if (!string) { return(false); } + if (checkObjectTypes && !isOSmallString(string)) { + return(false); + } + return(icEndsWithSSmallString(self, ssGet(string))); } internal ssize_t icCountSSmallString(smallStringt *self, const char *string) { - if (!self->data || !string) { + if (!self->data || !string || !string[0]) { return(-1); } @@ -2843,17 +3357,17 @@ internal ssize_t icCountSmallJsonSmallString(smallStringt *self, smallJsont *str // sanity checks if (!string) { - return(false); + return(-1); } if (checkObjectTypes && !isOSmallJson(string)) { - return(false); + return(-1); } const char *type = getTopTypeO(string); if (!eqS(type,"string")) { - return(false); + return(-1); } return(icCountSSmallString(self, getTopSO(string))); @@ -2864,6 +3378,10 @@ internal ssize_t icCountSmallStringSmallString(smallStringt *self, smallStringt if (!string) { return(-1); } + if (checkObjectTypes && !isOSmallString(string)) { + return(-1); + } + return(icCountSSmallString(self, ssGet(string))); } @@ -2971,9 +3489,6 @@ internal double parseDoubleSmallString(smallStringt *self) { } char *string = sStringGetTiny(self->data); - if (!isNumber(string)) { - return(0); - } while (!isdigit(*string) && *string != '-' && *string != 0) { string++; } @@ -3168,10 +3683,13 @@ internal smallStringt* icUniqSmallString(smallStringt *self, char c) { } char *string = sStringGetTiny(self->data); + + c = tolower(c); + // index in r size_t j = 1;; rangeFrom(i, 1, self->_len) - if (string[i-1] == c && string[i] == c) { + if (tolower(string[i-1]) == c && tolower(string[i]) == c) { // skip continue; } @@ -3340,6 +3858,10 @@ internal char* cropSSmallString(smallStringt *self, intmax_t start, intmax_t end strLCpy(string+start, len-end+1, string+end); self->_len -= n; } + else { + // start = end, return empty string + l = strdup(""); + } return(l); } @@ -3393,7 +3915,7 @@ internal char cropElemSmallString(smallStringt *self, intmax_t index) { string[len-1] = 0; } else { - strLCpy(string+index, len-1, string+index+1); + strLCpy(string+index, len - index, string+index+1); } self->_len--; @@ -3464,6 +3986,10 @@ internal smallStringt* insertSmallString(smallStringt *self, intmax_t index, sma if (!toInsert) { return(NULL); } + if (checkObjectTypes && !isOSmallString(toInsert)) { + return(NULL); + } + if (!self->data) { if (index) { return(NULL); @@ -3531,6 +4057,12 @@ internal smallStringt* insertSmallJsonSmallString(smallStringt *self, intmax_t i const char *type = getTopTypeO(toInsert); + if (!type) { + // this provides a behavior similar to insertSmallString and insertS + // when toInsert is empty, self is unchanged and no error is returned + return(self); + } + if (!eqS(type,"string")) { return(NULL); } @@ -3543,7 +4075,7 @@ internal smallStringt* insertSSmallString(smallStringt *self, intmax_t index, co // sanity checks if (!toInsert) { - return(NULL); + return(self); } if (!self->data) { if (index) { @@ -3785,8 +4317,11 @@ internal char* hasSmallString(smallStringt *self, const char *needle) { internal char* hasCharSmallString(smallStringt *self, char c) { - charToS(s, c); - return(hasSmallString(self, s)); + // sanity checks + if (!self->data || !self->_len) { + return(NULL); + } + return(strchr(sStringGetTiny(self->data), c)); } internal char* hasSmallJsonSmallString(smallStringt *self, smallJsont *needle) { @@ -3813,6 +4348,10 @@ internal char* hasSmallStringSmallString(smallStringt *self, smallStringt *needl if (!needle) { return(NULL); } + if (checkObjectTypes && !isOSmallString(needle)) { + return(NULL); + } + return(hasSmallString(self, ssGet(needle))); } @@ -3837,8 +4376,21 @@ internal smallStringt* findSmallString(smallStringt *self, const char *needle) { internal smallStringt* findCharSmallString(smallStringt *self, char c) { - charToS(s, c); - return(findSmallString(self, s)); + // sanity checks + if (!self->data || !self->_len) { + return(NULL); + } + + char *s = strchr(sStringGetTiny(self->data), c); + if (!s) { + return(NULL); + } + createAllocateSmallString(r); + if (!r) { + return(NULL); + } + r->f->set(r, s); + return(r); } internal smallStringt* findSmallJsonSmallString(smallStringt *self, smallJsont *needle) { @@ -3865,6 +4417,10 @@ internal smallStringt* findSmallStringSmallString(smallStringt *self, smallStrin if (!needle) { return(NULL); } + if (checkObjectTypes && !isOSmallString(needle)) { + return(NULL); + } + return(findSmallString(self, ssGet(needle))); } @@ -3925,6 +4481,15 @@ internal char* icHasSmallString(smallStringt *self, const char *needle) { internal char* icHasCharSmallString(smallStringt *self, char c) { + if (c == 0) { + // include terminating '\0' in comparison + // like findCharS + // sanity checks + if (!self->data || !self->_len) { + return(NULL); + } + return(strchr(sStringGetTiny(self->data), c)); + } charToS(s, c); return(icHasSmallString(self, s)); } @@ -3953,6 +4518,10 @@ internal char* icHasSmallStringSmallString(smallStringt *self, smallStringt *nee if (!needle) { return(NULL); } + if (checkObjectTypes && !isOSmallString(needle)) { + return(NULL); + } + return(icHasSmallString(self, ssGet(needle))); } @@ -3977,6 +4546,24 @@ internal smallStringt* icFindSmallString(smallStringt *self, const char *needle) internal smallStringt* icFindCharSmallString(smallStringt *self, char c) { + if (c == 0) { + // include terminating '\0' in comparison + // like findCharS + // sanity checks + if (!self->data || !self->_len) { + return(NULL); + } + char *s = strchr(sStringGetTiny(self->data), c); + if (!s) { + return(NULL); + } + createAllocateSmallString(r); + if (!r) { + return(NULL); + } + r->f->set(r, s); + return(r); + } charToS(s, c); return(icFindSmallString(self, s)); } @@ -4005,6 +4592,10 @@ internal smallStringt* icFindSmallStringSmallString(smallStringt *self, smallStr if (!needle) { return(NULL); } + if (checkObjectTypes && !isOSmallString(needle)) { + return(NULL); + } + return(icFindSmallString(self, ssGet(needle))); } @@ -4132,6 +4723,10 @@ internal smallArrayt* splitSmallStringSmallString(smallStringt *self, smallStrin if (!delim) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim)) { + return(NULL); + } + return(splitSmallString(self, ssGet(delim))); } @@ -4149,7 +4744,7 @@ internal char** splitSSmallString(smallStringt *self, const char *delim) { if (!*delim || !*sStringGetTiny(self->data)) { // empty delimiter or empty string - listPushS(&r, ""); + listPushS(&r, sStringGetTiny(self->data)); return(r); } @@ -4211,6 +4806,10 @@ internal char** splitSmallStringSSmallString(smallStringt *self, smallStringt *d if (!delim) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim)) { + return(NULL); + } + return(splitSSmallString(self, ssGet(delim))); } @@ -4227,11 +4826,7 @@ internal smallArrayt* extractSmallString(smallStringt *self, const char* delim1, return(NULL); } - tmp = splitSSmallString(self, delim1);; - - if (!tmp) { - return(NULL); - } + tmp = splitSSmallString(self, delim1); if (listLengthS(tmp) < 2) { // delim1 not found @@ -4252,6 +4847,10 @@ internal smallArrayt* extractSmallString(smallStringt *self, const char* delim1, listFreeS(tmp); + if (!r) { + return(NULL); + } + createAllocateSmallArray(a); if (!a) { listFreeS(r); @@ -4319,6 +4918,10 @@ internal smallArrayt* extractSmallJsonSmallStringSmallString(smallStringt *self, return(NULL); } + if (checkObjectTypes && !isOSmallString(delim2)) { + return(NULL); + } + const char *type = getTopTypeO(delim1); if (!eqS(type,"string")) { @@ -4373,6 +4976,10 @@ internal smallArrayt* extractSmallStringSmallJsonSmallString(smallStringt *self, return(NULL); } + if (checkObjectTypes && !isOSmallString(delim1)) { + return(NULL); + } + if (checkObjectTypes && !isOSmallJson(delim2)) { return(NULL); } @@ -4391,6 +4998,14 @@ internal smallArrayt* extractSmallStringSmallStringSmallString(smallStringt *sel if (!delim1 || !delim2) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim1)) { + return(NULL); + } + if (checkObjectTypes && !isOSmallString(delim2)) { + return(NULL); + } + + return(extractSmallString(self, ssGet(delim1), ssGet(delim2))); } @@ -4399,6 +5014,10 @@ internal smallArrayt* extractSmallStringSSmallString(smallStringt *self, smallSt if (!delim1) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim1)) { + return(NULL); + } + return(extractSmallString(self, ssGet(delim1), delim2)); } @@ -4407,6 +5026,10 @@ internal smallArrayt* extractSmallStringCharSmallString(smallStringt *self, smal if (!delim1) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim1)) { + return(NULL); + } + charToS(d2, delim2); return(extractSmallString(self, ssGet(delim1), d2)); } @@ -4435,6 +5058,10 @@ internal smallArrayt* extractSSmallStringSmallString(smallStringt *self, const c if (!delim2) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim2)) { + return(NULL); + } + return(extractSmallString(self, delim1, ssGet(delim2))); } @@ -4463,6 +5090,10 @@ internal smallArrayt* extractCharSmallStringSmallString(smallStringt *self, char if (!delim2) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim2)) { + return(NULL); + } + charToS(d1, delim1); return(extractSmallString(self, d1, ssGet(delim2))); } @@ -4515,6 +5146,10 @@ internal smallArrayt* icSplitSmallStringSmallString(smallStringt *self, smallStr if (!delim) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim)) { + return(NULL); + } + return(icSplitSmallString(self, ssGet(delim))); } @@ -4532,7 +5167,7 @@ internal char** icSplitSSmallString(smallStringt *self, const char *delim) { if (!*delim || !*sStringGetTiny(self->data)) { // empty delimiter or empty string - listPushS(&r, ""); + listPushS(&r, sStringGetTiny(self->data)); return(r); } @@ -4594,6 +5229,10 @@ internal char** icSplitSmallStringSSmallString(smallStringt *self, smallStringt if (!delim) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim)) { + return(NULL); + } + return(icSplitSSmallString(self, ssGet(delim))); } @@ -4612,10 +5251,6 @@ internal smallArrayt* icExtractSmallString(smallStringt *self, const char* delim tmp = icSplitSSmallString(self, delim1);; - if (!tmp) { - return(NULL); - } - if (listLengthS(tmp) < 2) { // delim1 not found listFreeS(tmp); @@ -4635,6 +5270,10 @@ internal smallArrayt* icExtractSmallString(smallStringt *self, const char* delim listFreeS(tmp); + if (!r) { + return(NULL); + } + createAllocateSmallArray(a); if (!a) { listFreeS(r); @@ -4702,6 +5341,10 @@ internal smallArrayt* icExtractSmallJsonSmallStringSmallString(smallStringt *sel return(NULL); } + if (checkObjectTypes && !isOSmallString(delim2)) { + return(NULL); + } + const char *type = getTopTypeO(delim1); if (!eqS(type,"string")) { @@ -4756,6 +5399,10 @@ internal smallArrayt* icExtractSmallStringSmallJsonSmallString(smallStringt *sel return(NULL); } + if (checkObjectTypes && !isOSmallString(delim1)) { + return(NULL); + } + if (checkObjectTypes && !isOSmallJson(delim2)) { return(NULL); } @@ -4774,6 +5421,15 @@ internal smallArrayt* icExtractSmallStringSmallStringSmallString(smallStringt *s if (!delim1 || !delim2) { return(NULL); } + + if (checkObjectTypes && !isOSmallString(delim1)) { + return(NULL); + } + + if (checkObjectTypes && !isOSmallString(delim2)) { + return(NULL); + } + return(icExtractSmallString(self, ssGet(delim1), ssGet(delim2))); } @@ -4782,6 +5438,10 @@ internal smallArrayt* icExtractSmallStringSSmallString(smallStringt *self, small if (!delim1) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim1)) { + return(NULL); + } + return(icExtractSmallString(self, ssGet(delim1), delim2)); } @@ -4790,6 +5450,10 @@ internal smallArrayt* icExtractSmallStringCharSmallString(smallStringt *self, sm if (!delim1) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim1)) { + return(NULL); + } + charToS(d2, delim2); return(icExtractSmallString(self, ssGet(delim1), d2)); } @@ -4818,6 +5482,10 @@ internal smallArrayt* icExtractSSmallStringSmallString(smallStringt *self, const if (!delim2) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim2)) { + return(NULL); + } + return(icExtractSmallString(self, delim1, ssGet(delim2))); } @@ -4846,14 +5514,18 @@ internal smallArrayt* icExtractCharSmallStringSmallString(smallStringt *self, ch if (!delim2) { return(NULL); } + if (checkObjectTypes && !isOSmallString(delim2)) { + return(NULL); + } + charToS(d1, delim1); return(icExtractSmallString(self, d1, ssGet(delim2))); } internal smallStringt* colorSmallString(smallStringt *self, const char *colr) { - if (!self->data) { - return(self); + if (!self->data || !colr) { + return(NULL); } char *s = catS(colr, sStringGetTiny(self->data), RST); @@ -4864,13 +5536,13 @@ internal smallStringt* colorSmallString(smallStringt *self, const char *colr) { internal char* colordSmallString(smallStringt *self, const char *color) { - if (!self->data) { + if (!self->data || !color) { return(NULL); } if (isEmptySmallString(self)) { return(emptySF()); } - return(catS(color, self->data, RST)); + return(catS(color, sStringGetTiny(self->data), RST)); } internal smallStringt* readFileSmallString(smallStringt *self, const char *filePath) { @@ -4973,7 +5645,7 @@ internal smallStringt* readStreamSmallString(smallStringt *self, FILE *fp) { return(NULL); } - readStatus = fread(((char *)self->data)+1, 1, len , fp); + readStatus = fread(&self->data->data, 1, len , fp); self->data->type = STRING; @@ -4982,8 +5654,8 @@ internal smallStringt* readStreamSmallString(smallStringt *self, FILE *fp) { return(NULL); } - *((char*)self->data+len+1) = 0; - self->_len = len; + *(&self->data->data+len) = 0; + self->_len = len; return(self); } @@ -5106,6 +5778,10 @@ internal int appendFileSmallStringSmallString(smallStringt *self, smallStringt * if (!filePath) { return(0); } + if (checkObjectTypes && !isOSmallString(filePath)) { + return(0); + } + return(appendFileSmallString(self, ssGet(filePath))); } diff --git a/src/json/libsheepyObject.c b/src/json/libsheepyObject.c @@ -923,7 +923,9 @@ void putsDictGF(smallDictt* object) { return; } s = toStringO((baset *)object); - puts(s); + if (s) { + puts(s); + } free(s); } @@ -934,7 +936,9 @@ void putsJsonGF(smallJsont* object) { return; } s = toStringO((baset *)object); - puts(s); + if (s) { + puts(s); + } free(s); } @@ -945,7 +949,9 @@ void putsArrayGF(smallArrayt* object) { return; } s = toStringO((baset *)object); - puts(s); + if (s) { + puts(s); + } free(s); } @@ -956,7 +962,9 @@ void putsSmallBoolGF(smallBoolt* object) { return; } s = toStringO((baset *)object); - puts(s); + if (s) { + puts(s); + } free(s); } @@ -967,7 +975,9 @@ void putsSmallBytesGF(smallBytest* object) { return; } s = toStringO((baset *)object); - puts(s); + if (s) { + puts(s); + } free(s); } @@ -978,7 +988,9 @@ void putsSmallDoubleGF(smallDoublet* object) { return; } s = toStringO((baset *)object); - puts(s); + if (s) { + puts(s); + } free(s); } @@ -989,7 +1001,9 @@ void putsSmallIntGF(smallIntt* object) { return; } s = toStringO((baset *)object); - puts(s); + if (s) { + puts(s); + } free(s); } @@ -1000,7 +1014,9 @@ void putsSmallStringGF(smallStringt* object) { return; } s = toStringO((baset *)object); - puts(s); + if (s) { + puts(s); + } free(s); } @@ -1019,7 +1035,9 @@ void putsSmallContainerGF(smallContainert* object) { return; } s = toStringO((baset *)object); - puts(s); + if (s) { + puts(s); + } free(s); } @@ -1074,6 +1092,9 @@ char *toStringFloatGF(float object) { char *toStringFloatPGF(float* object) { char *s = NULL; + if (!object) { + return(NULL); + } isError(s, malloc(256*sizeof(char))) return(NULL); snprintf(s,256, "%f", *object); return(s); @@ -1208,7 +1229,13 @@ char *toStringListSGF(char** object) { if (!object) { return(NULL); } - return(join(object, ",")); + char *s = join(object, "\",\""); + if (!s) { + return(NULL); + } + char *r = catS("[\"", s, "\"]");; + free(s); + return(r); } char *toStringListCSGF(const char** object) { @@ -1216,7 +1243,13 @@ char *toStringListCSGF(const char** object) { if (!object) { return(NULL); } - return(join((char**)object, ",")); + char *s = join((char**)object, "\",\""); + if (!s) { + return(NULL); + } + char *r = catS("[\"", s, "\"]");; + free(s); + return(r); } char *toStringDictGF(smallDictt* object) { @@ -1314,6 +1347,7 @@ smallStringt *formatO(const char *fmt, ...) { } va_start(pl, fmt); if (vasprintf(&r, fmt, pl) == -1) { + va_end(pl); return(NULL); } va_end(pl); @@ -1547,7 +1581,7 @@ smallArrayt *readDirO(const char *dirPath) { if (!d) { pFuncError shEPrintfS("The path was: \"%s\"\n", dirPath); - return(NULL); + return(list); } dp = readdir(d); @@ -1614,7 +1648,7 @@ smallArrayt *readDirDirO(const char *dirPath) { if (!d) { pFuncError shEPrintfS("The path was: \"%s\"\n", dirPath); - return(NULL); + return(list); } dp = readdir(d); @@ -1757,7 +1791,7 @@ smallArrayt *readDirAllO(const char *dirPath) { if (!d) { pFuncError shEPrintfS("The path was: \"%s\"\n", dirPath); - return(NULL); + return(list); } dp = readdir(d); @@ -2078,6 +2112,7 @@ smallJsont *expandHomeJO(smallJsont *path) { } char *r = expandHome(getTopSO(path)); + freeO(path); setTopNFreeStringO(path, r); return(path); } @@ -2157,6 +2192,7 @@ smallJsont *normalizePathJO(smallJsont *path) { } char *r = normalizePath(getTopSO(path)); + freeO(path); setTopNFreeStringO(path, r); return(path); } @@ -2169,7 +2205,6 @@ smallStringt *normalizePathO(smallStringt *path) { } if (isEmptyO(path)) { - emptyO(path); return(path); } @@ -2440,13 +2475,13 @@ bool fileChmodO(smallStringt *filePath, mode_t mode) { ssize_t fileSizeJO(smallJsont *filePath) { if (!filePath) { - return(false); + return(-1); } const char *type = getTopTypeO(filePath); if (!eqS(type,"string")) { - return(false); + return(-1); } return(fileSize(getTopSO(filePath))); @@ -3236,8 +3271,10 @@ smallJsont *readSmallJsonO(void) { return(r); } +#define startMax 20 + smallStringt *readO(void) { - int max = 20;; + int max = startMax;; char *s = malloc(max); if (!s) { @@ -3246,7 +3283,17 @@ smallStringt *readO(void) { int i = 0; while (1) { + #ifndef unitTest int c = getchar(); + #else + int c; + if (i < startMax) { + c = 'a'; + } + else { + c = '\n'; + } + #endif if (c == '\n') { s[i] = 0; break; @@ -4997,6 +5044,9 @@ bool icEqualChaSmallJsonG(char c, smallJsont* value) { bool icEqualChaSmallStringG(char c, smallStringt* value) { + if (!value) { + return(false); + } charToS(s, c); return(icEqualSO(value, s)); } @@ -5030,6 +5080,9 @@ bool icEqualCharPSmallJsonG(const char *s, smallJsont* value) { bool icEqualCharPSmallStringG(const char *s, smallStringt* value) { + if (!value) { + return(false); + } return(icEqualSO(value, s)); } @@ -5094,6 +5147,11 @@ bool icEqualArraySmallArrayG(char ** p1, smallArrayt* p2) { if (!p2) { return(false); } + + if (checkObjectTypes && !isOSmallArray(p2)) { + return(false); + } + return(p2->f->icEqualArray(p2, p1)); } @@ -5151,6 +5209,11 @@ bool icEqualOSmallArrayG(baset* p1, smallArrayt* p2) { if (!p2) { return(false); } + + if (checkObjectTypes && !isOSmallArray(p2)) { + return(false); + } + return(p2->f->icEqualBase(p2, p1)); } @@ -5159,6 +5222,11 @@ bool icEqualOSmallDictG(baset* p1, smallDictt* p2) { if (!p2) { return(false); } + + if (checkObjectTypes && !isOSmallDict(p2)) { + return(false); + } + return(p2->f->icEqualBase(p2, p1)); } @@ -5182,6 +5250,10 @@ bool icEqualOSmallStringG(baset* p1, smallStringt* p2) { return(false); } + if (checkObjectTypes && !isOSmallString(p2)) { + return(false); + } + char *s = toStringO(p1); bool r = icEqualSO(p2, s); @@ -5200,7 +5272,7 @@ void freeBasetInContainer(void *baseo) { terminateO(o); } -// TODO duplicate strings? cannot be shared between objects + smallt *toSmallt(baset *obj) { smallt *r = NULL; @@ -5209,56 +5281,56 @@ smallt *toSmallt(baset *obj) { } if (isOType(obj, "undefined")) { - r = (smallt *)allocSUndefined(); + r = (smallt *)allocSUndefined(); } else if (isOType(obj, "smallArray")) { + if (!((smallArrayt *)obj)->a) { + ((smallArrayt *)obj)->a = allocSArray(); + } r = (smallt *)((smallArrayt *)obj)->a; - if (!r) { - r = (smallt *) allocSArray(); } - } else if (isOType(obj, "smallDict")) { + if (!((smallDictt *)obj)->d) { + ((smallDictt *)obj)->d = allocSDict(); + } r = (smallt *)((smallDictt *)obj)->d; - if (!r) { - r = (smallt *) allocSDict(); } - } else if (isOType(obj, "smallBool")) { + if (!((smallBoolt *)obj)->value) { + ((smallBoolt *)obj)->value = allocSBool(false); + } r = (smallt *)((smallBoolt *)obj)->value; - if (!r) { - r = (smallt *) allocSBool(false); } - } else if (isOType(obj, "smallBytes")) { + if (!((smallBytest *)obj)->B) { + ((smallBytest *)obj)->B = allocSBytes(); + } r = (smallt *)((smallBytest *)obj)->B; - if (!r) { - r = (smallt *) allocSBytes(); } - } else if (isOType(obj, "smallContainer")) { + if (!((smallContainert *)obj)->data) { + ((smallContainert *)obj)->data = allocSContainer(NULL); + } r = (smallt *)((smallContainert *)obj)->data; - if (!r) { - r = (smallt *) allocSContainer(NULL); } - } else if (isOType(obj, "smallDouble")) { + if (!((smallDoublet *)obj)->value) { + ((smallDoublet *)obj)->value = allocSDouble(0); + } r = (smallt *)((smallDoublet *)obj)->value; - if (!r) { - r = (smallt *) allocSDouble(0); } - } else if (isOType(obj, "smallInt")) { + if (!((smallIntt *)obj)->value) { + ((smallIntt *)obj)->value = allocSInt(0); + } r = (smallt *)((smallIntt *)obj)->value; - if (!r) { - r = (smallt *) allocSInt(0); } - } else if (isOType(obj, "smallString")) { + if (!((smallStringt *)obj)->data) { + ((smallStringt *)obj)->data = allocSStringTiny(""); + } r = (smallt *)((smallStringt *)obj)->data; - if (!r) { - r = (smallt *) allocSStringTiny(""); } - } else { // store other baset object in sContainers sContainert *c = allocSContainer(obj); @@ -5273,6 +5345,7 @@ smallt *toSmallt(baset *obj) { return(r); } + baset *toBaset(smallt *obj) { undefinedt *u = NULL; smallBoolt *b = NULL; diff --git a/src/json/libsheepyObjectCuTest.c b/src/json/libsheepyObjectCuTest.c @@ -2345,6 +2345,7 @@ void toBasetT(CuTest *tc UNUSED) { + int main(int n UNUSED, char**v UNUSED) { // disable btrace to make the test run faster btraceDisable(); diff --git a/src/json/libsheepyObjectTest.c b/src/json/libsheepyObjectTest.c @@ -2333,6 +2333,7 @@ END_TEST + Suite * libsheepySuite(void) { Suite *s; TCase *tc_core; diff --git a/src/libsheepy.c b/src/libsheepy.c @@ -235,7 +235,7 @@ char *randomAlphaNumS(uint64_t length); char *bRandomAlphaNumS(char *dst, size_t dstSize); char *readS(void); char *bLReadS(char *dst, size_t dstSize); -local char *shGetpass(void); +UNUSED local char *shGetpass(void); char *readPasswordS(void); bool zeroS(char *string); bool zeroBuf(void *buf, size_t len); @@ -6250,7 +6250,7 @@ char *bLReadS(char *dst, size_t dstSize) { * * currently used in Termux because getpass is missging */ -local char *shGetpass(void) { +UNUSED local char *shGetpass(void) { char *r = NULL; #ifndef unitTest @@ -9190,7 +9190,7 @@ bool endsWithCharS(const char *string1, char c) { ssize_t countS(const char *s, const char *needle) { // sanity checks - if (!s || !needle) { + if (!s || !needle || !needle[0]) { return(-1); } @@ -9406,7 +9406,7 @@ bool icEndsWithCharS(const char *string1, char c) { ssize_t icCountS(const char *s, const char *needle) { // sanity checks - if (!s || !needle) { + if (!s || !needle || !needle[0]) { return(-1); } @@ -13298,7 +13298,7 @@ char cropElemS(char *string, intmax_t index) { string[len-1] = 0; } else { - strLCpy(string+index, len, string+index+1); + strLCpy(string+index, len - index, string+index+1); } return(r); @@ -46452,9 +46452,11 @@ internal size_t runeFullyDecompose(rune ch, rune *result, size_t resultLen) { } internal int output_marks(const char **p_inout, char *dst, bool remove_dot) { - const char *p = *p_inout = NULL;; + const char *p = NULL; int len = 0;; + p = *p_inout; + while (*p) { rune c = code2RuneUTF8(p);; @@ -52609,6 +52611,7 @@ int systemf(const char *fmt, ...) { } va_start(pl, fmt); if (vasprintf(&cmd, fmt, pl) == -1) { + va_end(pl); return(-1); } va_end(pl); diff --git a/src/libsheepy.h b/src/libsheepy.h @@ -2872,7 +2872,7 @@ int listPrintS(char **list); */ #define forEachS(list, element) \ ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ - for (char *element = list[0]; list[UNIQVAR(libsheepyInternalIndex)]!= NULL ; UNIQVAR(libsheepyInternalIndex)++, element = list[UNIQVAR(libsheepyInternalIndex)]) + for (char *element = (list)[0]; (list)[UNIQVAR(libsheepyInternalIndex)]!= NULL ; UNIQVAR(libsheepyInternalIndex)++, element = (list)[UNIQVAR(libsheepyInternalIndex)]) /** * forEach - loop macro on list indexes @@ -2898,7 +2898,7 @@ int listPrintS(char **list); */ #define enumerateS(list, element, index) \ ;size_t index = 0; \ - for (char *element=list[0]; element != NULL ; index++, element = list[index]) + for (char *element=(list)[0]; element != NULL ; index++, element = (list)[index]) /** * enumerateCharP list diff --git a/src/libsheepyCuTest.c b/src/libsheepyCuTest.c @@ -5071,6 +5071,8 @@ void countST(CuTest *tc UNUSED) { ck_assert_int_eq(countS("shee", "SH"), 0); ck_assert_int_eq(countS("shee", "sheepy"), 0); ck_assert_int_eq(countS("aaa aaa", "ab"), 0); + // empty string + ck_assert_int_eq(countS("shee", ""), -1); // NULL string ck_assert_int_eq(countS(NULL,"a"), -1); ck_assert_int_eq(countS("a", NULL), -1); @@ -5284,6 +5286,8 @@ void icCountST(CuTest *tc UNUSED) { ck_assert_int_eq(icCountS("shee", "eSH"), 0); ck_assert_int_eq(icCountS("shee", "sheepy"), 0); ck_assert_int_eq(icCountS("aaa aaa", "ab"), 0); + // empty needle + ck_assert_int_eq(icCountS("aaa aaa", ""), -1); // NULL string ck_assert_int_eq(icCountS(NULL,"a"), -1); ck_assert_int_eq(icCountS("a", NULL), -1); diff --git a/src/libsheepyObject.h b/src/libsheepyObject.h @@ -2838,7 +2838,7 @@ void finishManyOF(void *paramType, ...); smallJsont*: _Generic(obj, \ smallArrayt *: shiftNSmashSmallJsonG, \ smallDictt *: mergeDictNSmashSmallJsonG, \ - smallJsont *: shiftSmallJsonNSmashSmallJsonG, \ + smallJsont *: shiftNSmashSmallJsonSmallJsonG, \ default: mergeNSmashSmallJsonG), \ smallStringt*: _Generic((obj), \ smallStringt*: prependNSmashSmallStringG, \ @@ -5172,7 +5172,7 @@ void finishManyOF(void *paramType, ...); smallStringt *: rTrimSmallStringG \ )(self) -#define uniqO(self) (self)->f->uniq(self) +#define uniqO(self, c) (self)->f->uniq(self, c) #define uniqG(self, c) _Generic((self), \ char *: uniqS, \ const char *: uniqS, \ @@ -5183,7 +5183,7 @@ void finishManyOF(void *paramType, ...); smallStringt *: uniqSmallStringG \ )(self, c) -#define icUniqO(self) (self)->f->icUniq(self) +#define icUniqO(self, c) (self)->f->icUniq(self, c) #define icUniqG(self, c) _Generic((self), \ char *: icUniqS, \ const char *: icUniqS, \ @@ -5591,7 +5591,7 @@ void finishManyOF(void *paramType, ...); smallBytest*: injectSmallBytesSmallJsonG, \ smallDoublet*: injectSmallDoubleSmallJsonG, \ smallIntt*: injectSmallIntSmallJsonG, \ - smallJsont*: insertJsonSmallJsonG, \ + smallJsont*: insertSmallJsonSmallJsonG, \ smallStringt*: injectSmallStringSmallJsonG, \ smallContainert*: injectSmallContainerSmallJsonG, \ undefinedt*: injectUndefinedSmallJsonG, \ @@ -5677,7 +5677,7 @@ void finishManyOF(void *paramType, ...); smallBytest*: injectNFreeSmallBytesSmallJsonG, \ smallDoublet*: injectNFreeSmallDoubleSmallJsonG, \ smallIntt*: injectNFreeSmallIntSmallJsonG, \ - smallJsont*: insertJsonNSmashSmallJsonG, \ + smallJsont*: insertNSmashSmallJsonSmallJsonG, \ smallStringt*: injectNFreeSmallStringSmallJsonG, \ smallContainert*: injectNFreeSmallContainerSmallJsonG, \ undefinedt*: injectNFreeUndefinedSmallJsonG, \ @@ -7403,6 +7403,8 @@ void finishManyOF(void *paramType, ...); #define pushBufferG pushBufferO +#define setTopSO setTopStringO +#define setTopSG setTopStringO // generated with utils/oGMacros @@ -7970,15 +7972,33 @@ void finishManyOF(void *paramType, ...); #define zipSmallJsonCharO(self, array1, array2) (self)->f->zipSmallJsonChar(self, array1, array2) #define zipSmallJsonCharG zipSmallJsonCharO +#define zipSmallJsonCCharO(self, array1, array2) (self)->f->zipSmallJsonCChar(self, array1, array2) +#define zipSmallJsonCCharG zipSmallJsonCCharO + #define zipArrayO(self, array1, array2) (self)->f->zipArray(self, array1, array2) #define zipArrayG zipArrayO +#define zipCArrayO(self, a1, array2) (self)->f->zipCArray(self, a1, array2) +#define zipCArrayG zipCArrayO + #define zipCharO(self, array1, array2) (self)->f->zipChar(self, array1, array2) #define zipCharG zipCharO +#define zipCCharO(self, array1, a2) (self)->f->zipCChar(self, array1, a2) +#define zipCCharG zipCCharO + #define zipArrayCharO(self, array1, array2) (self)->f->zipArrayChar(self, array1, array2) #define zipArrayCharG zipArrayCharO +#define zipCArrayCharO(self, a1, array2) (self)->f->zipCArrayChar(self, a1, array2) +#define zipCArrayCharG zipCArrayCharO + +#define zipArrayCCharO(self, array1, a2) (self)->f->zipArrayCChar(self, array1, a2) +#define zipArrayCCharG zipArrayCCharO + +#define zipCArrayCCharO(self, a1, a2) (self)->f->zipCArrayCChar(self, a1, a2) +#define zipCArrayCCharG zipCArrayCCharO + #define readTextSmallStringO(self, filePath) (self)->f->readTextSmallString(self, filePath) #define readTextSmallStringG readTextSmallStringO @@ -8840,12 +8860,6 @@ void finishManyOF(void *paramType, ...); #define cropElemKeySmallContainerO(self, key) (self)->f->cropElemKeySmallContainer(self, key) #define cropElemKeySmallContainerG cropElemKeySmallContainerO -#define insertJsonO(self, index, toInsert) (self)->f->insertJson(self, index, toInsert) -#define insertJsonG insertJsonO - -#define insertJsonNSmashO(self, index, toInsert) (self)->f->insertJsonNSmash(self, index, toInsert) -#define insertJsonNSmashG insertJsonNSmashO - #define insertStringO(self, index, toInsert) (self)->f->insertString(self, index, toInsert) #define insertStringG insertStringO diff --git a/src/libsheepyTest.c b/src/libsheepyTest.c @@ -5060,6 +5060,8 @@ START_TEST(countST) ck_assert_int_eq(countS("shee", "SH"), 0); ck_assert_int_eq(countS("shee", "sheepy"), 0); ck_assert_int_eq(countS("aaa aaa", "ab"), 0); + // empty string + ck_assert_int_eq(countS("shee", ""), -1); // NULL string ck_assert_int_eq(countS(NULL,"a"), -1); ck_assert_int_eq(countS("a", NULL), -1); @@ -5273,6 +5275,8 @@ START_TEST(icCountST) ck_assert_int_eq(icCountS("shee", "eSH"), 0); ck_assert_int_eq(icCountS("shee", "sheepy"), 0); ck_assert_int_eq(icCountS("aaa aaa", "ab"), 0); + // empty needle + ck_assert_int_eq(icCountS("aaa aaa", ""), -1); // NULL string ck_assert_int_eq(icCountS(NULL,"a"), -1); ck_assert_int_eq(icCountS("a", NULL), -1);