Skip to content

Commit fe5751d

Browse files
author
skywind3000
committed
fix minor issues and improve stability
1 parent 0a60778 commit fe5751d

6 files changed

Lines changed: 215 additions & 118 deletions

File tree

system/imemdata.c

Lines changed: 134 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ ib_object *ib_object_new_map(struct IALLOCATOR *alloc, int capacity)
298298
// ib_object_delete - recursive delete
299299
// Asserts DYNAMIC (shell was malloc'd). If OWNED, frees STR/BIN buffer
300300
// or recursively deletes ARRAY/MAP children + frees element array.
301-
// Without OWNED, data/children are external references not freed.
301+
// Without OWNED, data/children are external references -- not freed.
302302
//---------------------------------------------------------------------
303303
void ib_object_delete(struct IALLOCATOR *alloc, ib_object *obj)
304304
{
@@ -778,7 +778,7 @@ int ib_object_map_erase(struct IALLOCATOR *alloc,
778778
}
779779

780780

781-
// detach pair by key deletes key, returns value object.
781+
// detach pair by key -- deletes key, returns value object.
782782
ib_object *ib_object_map_detach(struct IALLOCATOR *alloc,
783783
ib_object *map, const ib_object *key)
784784
{
@@ -805,7 +805,7 @@ ib_object *ib_object_map_detach(struct IALLOCATOR *alloc,
805805
// upsert: if key exists, keep old key, delete new key and old value,
806806
// store new value. If key not found, append new pair.
807807
// On success key/val ownership transfers. On failure (grow fails when
808-
// key not found) key/val are NOT consumed caller still owns them.
808+
// key not found) key/val are NOT consumed -- caller still owns them.
809809
int ib_object_map_set(struct IALLOCATOR *alloc,
810810
ib_object *map, ib_object *key, ib_object *val)
811811
{
@@ -1343,7 +1343,7 @@ int ib_object_path_set(struct IALLOCATOR *alloc,
13431343
int next_type = ib_object_path_segment(path, &p_next,
13441344
&key, &key_len, &idx);
13451345
if (next_type < 0) return -1;
1346-
// current segment is not the last one navigate or create
1346+
// current segment is not the last one -- navigate or create
13471347
if (last_seg_type == 1) {
13481348
// array index step: must exist, cannot auto-create
13491349
if (current->type != IB_OBJECT_ARRAY) return -1;
@@ -1912,35 +1912,38 @@ ilong ims_move(struct IMSTREAM *dst, struct IMSTREAM *src, ilong size)
19121912

19131913

19141914
//=====================================================================
1915-
// Common string operation (not be defined in some compiler)
1915+
// C-string enhancement (because some may not always be available)
19161916
//=====================================================================
19171917

19181918
// strcasestr
1919-
const char* istrcasestr(const char* s1, const char* s2)
1920-
{
1921-
const char* ptr = s1;
1922-
if (!s1 || !s2 || !*s2) return s1;
1923-
1924-
while (*ptr) {
1925-
if (ITOUPPER(*ptr) == ITOUPPER(*s2)) {
1926-
const char* cur1 = ptr + 1;
1927-
const char* cur2 = s2 + 1;
1928-
while (*cur1 && *cur2 && ITOUPPER(*cur1) == ITOUPPER(*cur2)) {
1929-
cur1++;
1930-
cur2++;
1931-
}
1932-
if (!*cur2) return ptr;
1933-
}
1934-
ptr++;
1935-
}
1936-
return NULL;
1919+
const char* istrcasestr(const char* s1, const char* s2)
1920+
{
1921+
const char* ptr;
1922+
if (s1 == NULL || s2 == NULL) return NULL;
1923+
if (!*s2) return s1;
1924+
ptr = s1;
1925+
while (*ptr) {
1926+
if (ITOUPPER(*ptr) == ITOUPPER(*s2)) {
1927+
const char* cur1 = ptr + 1;
1928+
const char* cur2 = s2 + 1;
1929+
while (*cur1 && *cur2 && ITOUPPER(*cur1) == ITOUPPER(*cur2)) {
1930+
cur1++;
1931+
cur2++;
1932+
}
1933+
if (!*cur2) return ptr;
1934+
}
1935+
ptr++;
1936+
}
1937+
return NULL;
19371938
}
19381939

19391940
// strncasecmp
19401941
int istrncasecmp(const char* s1, const char* s2, size_t num)
19411942
{
19421943
char c1, c2;
1943-
if (!s1 || !s2 || num == 0) return 0;
1944+
if (num == 0) return 0;
1945+
if (s1 == NULL) return (s2 == NULL) ? 0 : -1;
1946+
if (s2 == NULL) return 1;
19441947
while(num > 0){
19451948
c1 = ITOUPPER(*s1);
19461949
c2 = ITOUPPER(*s2);
@@ -1977,6 +1980,90 @@ char *istrsep(char **stringp, const char *delim)
19771980
}
19781981
}
19791982

1983+
// strspn implementation
1984+
ilong istrspn(const char *s, const char *accept)
1985+
{
1986+
const char *p;
1987+
char c;
1988+
ilong count = 0;
1989+
1990+
if (accept == NULL || *accept == '\0') return 0;
1991+
1992+
while ((c = *s++) != '\0') {
1993+
p = accept;
1994+
while (*p != '\0' && *p != c) {
1995+
p++;
1996+
}
1997+
if (*p == '\0') {
1998+
return count;
1999+
}
2000+
count++;
2001+
}
2002+
return count;
2003+
}
2004+
2005+
// strcspn implementation
2006+
ilong istrcspn(const char *s, const char *reject)
2007+
{
2008+
const char *p;
2009+
char c;
2010+
ilong count = 0;
2011+
2012+
if (reject == NULL || *reject == '\0') {
2013+
while (*s++ != '\0') count++;
2014+
return count;
2015+
}
2016+
2017+
while ((c = *s++) != '\0') {
2018+
p = reject;
2019+
while (*p != '\0' && *p != c) {
2020+
p++;
2021+
}
2022+
if (*p != '\0') {
2023+
return count;
2024+
}
2025+
count++;
2026+
}
2027+
return count;
2028+
}
2029+
2030+
// strtok_r implementation
2031+
char *istrtok_r(char *str, const char *delim, char **saveptr)
2032+
{
2033+
char *endup;
2034+
2035+
if (str == NULL) {
2036+
str = *saveptr;
2037+
if (str == NULL) {
2038+
return NULL;
2039+
}
2040+
}
2041+
2042+
if (*str == '\0') {
2043+
*saveptr = str;
2044+
return NULL;
2045+
}
2046+
2047+
str += istrspn(str, delim);
2048+
if (*str == '\0') {
2049+
*saveptr = str;
2050+
return NULL;
2051+
}
2052+
2053+
endup = str + istrcspn(str, delim);
2054+
2055+
if (*endup == '\0') {
2056+
*saveptr = endup;
2057+
return str;
2058+
}
2059+
2060+
*endup = '\0';
2061+
*saveptr = endup + 1;
2062+
2063+
return str;
2064+
}
2065+
2066+
19802067
// istrtoxl macro
19812068
#define IFL_NEG 1
19822069
#define IFL_READDIGIT 2
@@ -2003,14 +2090,11 @@ static unsigned long istrtoxl(const char *nptr, const char **endptr,
20032090
c = *p++;
20042091
while (isspace((int)(IUINT8)c)) c = *p++;
20052092

2006-
if (c == '+') c = *p++;
2007-
if (c == '-') {
2008-
flags |= IFL_NEG;
2093+
if (c == '+' || c == '-') {
2094+
if (c == '-') flags |= IFL_NEG;
20092095
c = *p++;
20102096
}
20112097

2012-
if (c == '+') c = *p++;
2013-
20142098
if (ibase < 0 || ibase == 1 || ibase > 36) {
20152099
if (endptr) *endptr = nptr;
20162100
return 0;
@@ -2081,7 +2165,7 @@ static unsigned long istrtoxl(const char *nptr, const char **endptr,
20812165
else number = (unsigned long)ILONG_MAX;
20822166
}
20832167

2084-
if (endptr) *endptr = p;
2168+
if ((flags & IFL_READDIGIT) && endptr) *endptr = p;
20852169

20862170
if (flags & IFL_NEG)
20872171
number = (unsigned long)(-(long)number);
@@ -2109,14 +2193,11 @@ static IUINT64 istrtoxll(const char *nptr, const char **endptr,
21092193
c = *p++;
21102194
while (isspace((int)(IUINT8)c)) c = *p++;
21112195

2112-
if (c == '+') c = *p++;
2113-
if (c == '-') {
2114-
flags |= IFL_NEG;
2196+
if (c == '+' || c == '-') {
2197+
if (c == '-') flags |= IFL_NEG;
21152198
c = *p++;
21162199
}
21172200

2118-
if (c == '+') c = *p++;
2119-
21202201
if (ibase < 0 || ibase == 1 || ibase > 36) {
21212202
if (endptr) *endptr = nptr;
21222203
return 0;
@@ -2177,7 +2258,7 @@ static IUINT64 istrtoxll(const char *nptr, const char **endptr,
21772258
else number = (IUINT64)IINT64_MAX;
21782259
}
21792260

2180-
if (endptr) *endptr = p;
2261+
if ((flags & IFL_READDIGIT) && endptr) *endptr = p;
21812262

21822263
if (flags & IFL_NEG)
21832264
number = (IUINT64)(-(IINT64)number);
@@ -2385,18 +2466,25 @@ ilong istrload(const char *src, ilong size, char *out)
23852466
case '\"': *output++ = '\"'; i += 2; break;
23862467
case '\\': *output++ = '\\'; i += 2; break;
23872468
case '0': *output++ = '\0'; i += 2; break;
2388-
case 'x':
2469+
case 'x':
23892470
case 'X':
23902471
if (i < size - 3) {
2391-
IUINT8 a = ptr[i + 2], b = ptr[i + 3], c = 0, d = 0;
2392-
if (a >= '0' && a <= '9') c = a - '0';
2393-
else if (a >= 'a' && a <= 'f') c = a - 'a' + 10;
2394-
else if (a >= 'A' && a <= 'F') c = a - 'A' + 10;
2395-
if (b >= '0' && b <= '9') d = b - '0';
2396-
else if (b >= 'a' && b <= 'f') d = b - 'a' + 10;
2397-
else if (b >= 'A' && b <= 'F') d = b - 'A' + 10;
2398-
*output++ = (c << 4) | d;
2399-
i += 4;
2472+
IUINT8 a = ptr[i + 2], b = ptr[i + 3];
2473+
IUINT8 c = 0, d = 0;
2474+
int a_valid = 0, b_valid = 0;
2475+
if (a >= '0' && a <= '9') { c = a - '0'; a_valid = 1; }
2476+
else if (a >= 'a' && a <= 'f') { c = a - 'a' + 10; a_valid = 1; }
2477+
else if (a >= 'A' && a <= 'F') { c = a - 'A' + 10; a_valid = 1; }
2478+
if (b >= '0' && b <= '9') { d = b - '0'; b_valid = 1; }
2479+
else if (b >= 'a' && b <= 'f') { d = b - 'a' + 10; b_valid = 1; }
2480+
else if (b >= 'A' && b <= 'F') { d = b - 'A' + 10; b_valid = 1; }
2481+
if (a_valid && b_valid) {
2482+
*output++ = (c << 4) | d;
2483+
i += 4;
2484+
} else {
2485+
*output++ = '\\';
2486+
i++;
2487+
}
24002488
} else {
24012489
*output++ = '\\';
24022490
i++;

0 commit comments

Comments
 (0)