no more stb_ds

This commit is contained in:
Nishi 2025-12-23 00:32:53 +09:00
commit 76f23b93ed
Signed by: nishi
GPG key ID: 27EF69B208EB9343
7 changed files with 89 additions and 1919 deletions

View file

@ -3,7 +3,7 @@ CFLAGS = -I include -fPIC -g
LDFLAGS = -shared
LIBS =
OBJS = src/core.o src/file.o src/unicode.o src/util.o src/stb_ds.o
OBJS = src/core.o src/file.o src/unicode.o src/util.o src/array.o
SO = .so

1895
external/stb_ds.h vendored

File diff suppressed because it is too large Load diff

View file

@ -83,6 +83,15 @@ XLDECL int xl_unicode_8_to_32(const char* input, int* output);
XLDECL int xl_unicode_32_to_8(const int input, char* output);
/* array.c */
XLDECL void xl_array_push(int** array, int value);
XLDECL int xl_array_length(int** array);
XLDECL void xl_array_pop(int** array);
XLDECL void xl_array_free(int** array);
#ifdef __cplusplus
}
#endif

62
src/array.c Normal file
View file

@ -0,0 +1,62 @@
#include <xmllib.h>
void xl_array_push(int** array, int value) {
if((*array) == NULL) {
int* raw = malloc(sizeof(*raw) * 2);
*array = &raw[1];
raw[0] = 1;
} else {
int* old = &(*array)[-1];
int* raw = malloc(sizeof(*raw) * (old[0] + 1 + 1));
int i;
for(i = 0; i < old[0] + 1; i++) {
raw[i] = old[i];
}
raw[0]++;
*array = &raw[1];
free(old);
}
(*array)[xl_array_length(array) - 1] = value;
}
int xl_array_length(int** array) {
if((*array) == NULL) return 0;
return (*array)[-1];
}
void xl_array_pop(int** array) {
int* old;
int* raw;
int i;
if((*array) == NULL) return;
old = &(*array)[-1];
(*array)[-1]--;
raw = malloc(sizeof(*raw) * (old[0] + 1));
for(i = 0; i < old[0] + 1; i++) {
raw[i] = old[i];
}
free(old);
*array = &raw[1];
}
void xl_array_free(int** array) {
if((*array) == NULL) return;
free(&(*array)[-1]);
*array = NULL;
}

View file

@ -1,7 +1,5 @@
#include <xmllib.h>
#include "../external/stb_ds.h"
xmllib_t* xl_open(xl_driver_t* driver, void* arg) {
xmllib_t* handle = malloc(sizeof(*handle));
memset(handle, 0, sizeof(*handle));
@ -45,7 +43,7 @@ enum STATES {
#define CLEANUP \
{ \
if(node != NULL) free(node); \
arrfree(state); \
xl_array_free(&state); \
}
#define ERROR \
@ -55,7 +53,7 @@ enum STATES {
longjmp(err, 1); \
}
#define STATE state[arrlen(state) - 1]
#define STATE state[xl_array_length(&state) - 1]
static xl_attribute_t* xl_parse_attribute(const char* str) {
xl_attribute_t* first = NULL;
@ -146,7 +144,7 @@ int xl_parse(xmllib_t* handle) {
return 0;
}
arrput(state, STATE_INITIAL);
xl_array_push(&state, STATE_INITIAL);
while(1) {
char in[4];
@ -162,7 +160,7 @@ int xl_parse(xmllib_t* handle) {
if(cp == '<' && STATE != STATE_COMMENT) {
if(STATE == STATE_INITIAL) {
arrput(state, STATE_TAG);
xl_array_push(&state, STATE_TAG);
node = malloc(1);
node[0] = 0;
@ -170,14 +168,14 @@ int xl_parse(xmllib_t* handle) {
} else if(STATE == STATE_STRING) {
TAKE_AS_NODE(cp);
} else if(STATE == STATE_MISC) {
arrput(state, STATE_IGNORE_TAG);
xl_array_push(&state, STATE_IGNORE_TAG);
} else {
ERROR;
}
} else if(cp == '>') {
if(STATE == STATE_TAG || STATE == STATE_COMMENT || STATE == STATE_MISC || STATE == STATE_IGNORE_TAG) {
if((STATE != STATE_COMMENT || node[strlen(node) - 1] == '-')) {
arrpop(state);
xl_array_pop(&state);
if(STATE == STATE_INITIAL) {
/* parsed tag */
@ -348,21 +346,21 @@ int xl_parse(xmllib_t* handle) {
}
} else if(cp == '"' && STATE != STATE_COMMENT && STATE != STATE_MISC) {
if(STATE == STATE_TAG) {
arrput(state, STATE_STRING);
xl_array_push(&state, STATE_STRING);
} else if(STATE == STATE_STRING) {
arrpop(state);
xl_array_pop(&state);
}
TAKE_AS_NODE(cp);
} else if(STATE == STATE_TAG || STATE == STATE_STRING || STATE == STATE_SPECIAL || STATE == STATE_COMMENT || STATE == STATE_MISC || STATE == STATE_IGNORE_TAG) {
if(node_count == 0 && cp == '!') {
arrpop(state);
arrput(state, STATE_SPECIAL);
xl_array_pop(&state);
xl_array_push(&state, STATE_SPECIAL);
} else if(node_count == 1 && STATE == STATE_SPECIAL && cp == '-') {
arrpop(state);
arrput(state, STATE_COMMENT);
xl_array_pop(&state);
xl_array_push(&state, STATE_COMMENT);
} else if(node_count == 1 && STATE == STATE_SPECIAL) {
arrpop(state);
arrput(state, STATE_MISC);
xl_array_pop(&state);
xl_array_push(&state, STATE_MISC);
}
TAKE_AS_NODE(cp);
} else if(STATE == STATE_INITIAL) {

View file

@ -1,2 +0,0 @@
#define STB_DS_IMPLEMENTATION
#include "../external/stb_ds.h"

View file

@ -1,7 +1,5 @@
#include <xmllib.h>
#include "../external/stb_ds.h"
char* xl_util_trim(const char* str) {
char* s = malloc(strlen(str) + 1);
int nl = 1;
@ -47,13 +45,13 @@ char* xl_util_trim(const char* str) {
if(cp == 0) break;
arrput(l, new);
xl_array_push(&l, new);
i += new;
}
i = strlen(s);
inc = arrlen(l) - 1;
inc = xl_array_length(&l) - 1;
if(inc > 0) {
while(1) {
int cp, new;
@ -72,7 +70,7 @@ char* xl_util_trim(const char* str) {
}
}
arrfree(l);
xl_array_free(&l);
return s;
}