Add dstr_t type
This commit is contained in:
parent
0095d8fe61
commit
57fe5ab1b7
4
compile
4
compile
@ -5,6 +5,10 @@ CFLAGS="-g -Wall -Werror -pedantic -Iinclude"
|
|||||||
SRC="src/main.c"
|
SRC="src/main.c"
|
||||||
OUT=main
|
OUT=main
|
||||||
|
|
||||||
|
# DSTRING
|
||||||
|
CFLAGS+=" -Iinclude/dstring"
|
||||||
|
SRC+=" src/dstring/*.c"
|
||||||
|
|
||||||
# LEXER
|
# LEXER
|
||||||
CFLAGS+=" -Iinclude/lexer"
|
CFLAGS+=" -Iinclude/lexer"
|
||||||
SRC+=" src/lexer/*.c"
|
SRC+=" src/lexer/*.c"
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
"-Werror",
|
"-Werror",
|
||||||
"-pedantic",
|
"-pedantic",
|
||||||
"-Iinclude",
|
"-Iinclude",
|
||||||
|
"-Iinclude/dstring",
|
||||||
"-Iinclude/lexer",
|
"-Iinclude/lexer",
|
||||||
"-o",
|
"-o",
|
||||||
"main",
|
"main",
|
||||||
@ -26,13 +27,33 @@
|
|||||||
"-Werror",
|
"-Werror",
|
||||||
"-pedantic",
|
"-pedantic",
|
||||||
"-Iinclude",
|
"-Iinclude",
|
||||||
|
"-Iinclude/dstring",
|
||||||
"-Iinclude/lexer",
|
"-Iinclude/lexer",
|
||||||
"-o",
|
"-o",
|
||||||
"main",
|
"main",
|
||||||
"src/lexer/lexer_states.c"
|
"src/dstring/dstring.c"
|
||||||
],
|
],
|
||||||
"directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json",
|
"directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json",
|
||||||
"file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/lexer/lexer_states.c",
|
"file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/dstring/dstring.c",
|
||||||
|
"output": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/main"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"arguments": [
|
||||||
|
"/usr/bin/clang",
|
||||||
|
"-c",
|
||||||
|
"-g",
|
||||||
|
"-Wall",
|
||||||
|
"-Werror",
|
||||||
|
"-pedantic",
|
||||||
|
"-Iinclude",
|
||||||
|
"-Iinclude/dstring",
|
||||||
|
"-Iinclude/lexer",
|
||||||
|
"-o",
|
||||||
|
"main",
|
||||||
|
"src/lexer/lexer.c"
|
||||||
|
],
|
||||||
|
"directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json",
|
||||||
|
"file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/lexer/lexer.c",
|
||||||
"output": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/main"
|
"output": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/main"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -71,6 +92,8 @@
|
|||||||
"-I",
|
"-I",
|
||||||
"include",
|
"include",
|
||||||
"-I",
|
"-I",
|
||||||
|
"include/dstring",
|
||||||
|
"-I",
|
||||||
"include/lexer",
|
"include/lexer",
|
||||||
"-internal-isystem",
|
"-internal-isystem",
|
||||||
"/usr/lib64/clang/16/include",
|
"/usr/lib64/clang/16/include",
|
||||||
@ -95,12 +118,12 @@
|
|||||||
"-x",
|
"-x",
|
||||||
"c",
|
"c",
|
||||||
"-o",
|
"-o",
|
||||||
"/tmp/main-dc9945.o",
|
"/tmp/main-c4d09c.o",
|
||||||
"src/main.c"
|
"src/main.c"
|
||||||
],
|
],
|
||||||
"directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json",
|
"directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json",
|
||||||
"file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/main.c",
|
"file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/main.c",
|
||||||
"output": "/tmp/main-dc9945.o"
|
"output": "/tmp/main-c4d09c.o"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"arguments": [
|
"arguments": [
|
||||||
@ -138,6 +161,8 @@
|
|||||||
"-I",
|
"-I",
|
||||||
"include",
|
"include",
|
||||||
"-I",
|
"-I",
|
||||||
|
"include/dstring",
|
||||||
|
"-I",
|
||||||
"include/lexer",
|
"include/lexer",
|
||||||
"-internal-isystem",
|
"-internal-isystem",
|
||||||
"/usr/lib64/clang/16/include",
|
"/usr/lib64/clang/16/include",
|
||||||
@ -162,11 +187,80 @@
|
|||||||
"-x",
|
"-x",
|
||||||
"c",
|
"c",
|
||||||
"-o",
|
"-o",
|
||||||
"/tmp/lexer_states-e000bc.o",
|
"/tmp/dstring-9f956a.o",
|
||||||
"src/lexer/lexer_states.c"
|
"src/dstring/dstring.c"
|
||||||
],
|
],
|
||||||
"directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json",
|
"directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json",
|
||||||
"file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/lexer/lexer_states.c",
|
"file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/dstring/dstring.c",
|
||||||
"output": "/tmp/lexer_states-e000bc.o"
|
"output": "/tmp/dstring-9f956a.o"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"arguments": [
|
||||||
|
"/usr/bin/clang-16",
|
||||||
|
"-cc1",
|
||||||
|
"-triple",
|
||||||
|
"x86_64-redhat-linux-gnu",
|
||||||
|
"-emit-obj",
|
||||||
|
"-mrelax-all",
|
||||||
|
"-disable-free",
|
||||||
|
"-clear-ast-before-backend",
|
||||||
|
"-disable-llvm-verifier",
|
||||||
|
"-discard-value-names",
|
||||||
|
"-main-file-name",
|
||||||
|
"-mrelocation-model",
|
||||||
|
"static",
|
||||||
|
"-mframe-pointer=all",
|
||||||
|
"-fmath-errno",
|
||||||
|
"-ffp-contract=on",
|
||||||
|
"-fno-rounding-math",
|
||||||
|
"-mconstructor-aliases",
|
||||||
|
"-funwind-tables=2",
|
||||||
|
"-target-cpu",
|
||||||
|
"x86-64",
|
||||||
|
"-tune-cpu",
|
||||||
|
"generic",
|
||||||
|
"-mllvm",
|
||||||
|
"-treat-scalable-fixed-error-as-warning",
|
||||||
|
"-debug-info-kind=constructor",
|
||||||
|
"-dwarf-version=4",
|
||||||
|
"-debugger-tuning=gdb",
|
||||||
|
"-fcoverage-compilation-dir=/home/abdelrahman/dev_work/say_it_in_json",
|
||||||
|
"-resource-dir",
|
||||||
|
"/usr/lib64/clang/16",
|
||||||
|
"-I",
|
||||||
|
"include",
|
||||||
|
"-I",
|
||||||
|
"include/dstring",
|
||||||
|
"-I",
|
||||||
|
"include/lexer",
|
||||||
|
"-internal-isystem",
|
||||||
|
"/usr/lib64/clang/16/include",
|
||||||
|
"-internal-isystem",
|
||||||
|
"/usr/local/include",
|
||||||
|
"-internal-isystem",
|
||||||
|
"/usr/bin/../lib/gcc/x86_64-redhat-linux/13/../../../../x86_64-redhat-linux/include",
|
||||||
|
"-internal-externc-isystem",
|
||||||
|
"/include",
|
||||||
|
"-internal-externc-isystem",
|
||||||
|
"/usr/include",
|
||||||
|
"-Wall",
|
||||||
|
"-Werror",
|
||||||
|
"-pedantic",
|
||||||
|
"-fdebug-compilation-dir=/home/abdelrahman/dev_work/say_it_in_json",
|
||||||
|
"-ferror-limit",
|
||||||
|
"19",
|
||||||
|
"-fgnuc-version=4.2.1",
|
||||||
|
"-fcolor-diagnostics",
|
||||||
|
"-faddrsig",
|
||||||
|
"-D__GCC_HAVE_DWARF2_CFI_ASM=1",
|
||||||
|
"-x",
|
||||||
|
"c",
|
||||||
|
"-o",
|
||||||
|
"/tmp/lexer-7622c3.o",
|
||||||
|
"src/lexer/lexer.c"
|
||||||
|
],
|
||||||
|
"directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json",
|
||||||
|
"file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/lexer/lexer.c",
|
||||||
|
"output": "/tmp/lexer-7622c3.o"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
19
include/dstring/dstring.h
Normal file
19
include/dstring/dstring.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#ifndef DSTRING_H
|
||||||
|
#define DSTRING_H
|
||||||
|
|
||||||
|
#include "aliases.h"
|
||||||
|
|
||||||
|
typedef struct dstr dstr_t;
|
||||||
|
|
||||||
|
dstr_t *dstr_with_capacity(u64 capacity);
|
||||||
|
dstr_t *dstr_from_string(const char *str);
|
||||||
|
void update_dstr(dstr_t **dst, const char *src);
|
||||||
|
void delete_dstr(dstr_t **str);
|
||||||
|
void concat_dstr(dstr_t **dst, const char *src);
|
||||||
|
void append_to_dstr(dstr_t **dst, char c);
|
||||||
|
void empty_dstr(dstr_t *str);
|
||||||
|
void print_dstr(const dstr_t *str);
|
||||||
|
u64 dstr_length(const dstr_t *str);
|
||||||
|
u64 dstr_capacity(const dstr_t *str);
|
||||||
|
|
||||||
|
#endif // !DSTRING_H
|
158
src/dstring/dstring.c
Normal file
158
src/dstring/dstring.c
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
#include "dstring.h"
|
||||||
|
#include "aliases.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// Use this scalar to allocate extra memory in order to avoid having to
|
||||||
|
// constantly reallocate
|
||||||
|
#define CAPACITY_SCALAR 8
|
||||||
|
|
||||||
|
struct dstr {
|
||||||
|
u64 capacity;
|
||||||
|
u64 size;
|
||||||
|
char buf[];
|
||||||
|
};
|
||||||
|
|
||||||
|
dstr_t *dstr_with_capacity(u64 capacity) {
|
||||||
|
dstr_t *out = (dstr_t *)malloc(sizeof(dstr_t) + capacity + 1);
|
||||||
|
|
||||||
|
if (!out) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
out->capacity = capacity;
|
||||||
|
out->size = 0;
|
||||||
|
memset(out->buf, 0, capacity + 1);
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
dstr_t *dstr_from_string(const char *str) {
|
||||||
|
u64 length = strlen(str);
|
||||||
|
|
||||||
|
u64 capacity = length * CAPACITY_SCALAR;
|
||||||
|
|
||||||
|
dstr_t *out = dstr_with_capacity(capacity);
|
||||||
|
|
||||||
|
if (!out) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
out->size = length;
|
||||||
|
strncpy(out->buf, str, length);
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_dstr(dstr_t **dst, const char *src) {
|
||||||
|
if (!(*dst)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 length = strlen(src);
|
||||||
|
|
||||||
|
dstr_t *str = *dst;
|
||||||
|
|
||||||
|
if (length <= str->capacity) {
|
||||||
|
memset(str->buf, 0, str->capacity);
|
||||||
|
|
||||||
|
str->size = length;
|
||||||
|
|
||||||
|
strncpy(str->buf, src, length);
|
||||||
|
} else {
|
||||||
|
u64 capacity = length * CAPACITY_SCALAR;
|
||||||
|
|
||||||
|
dstr_t *tmp = (dstr_t *)realloc(*dst, sizeof(dstr_t) + capacity + 1);
|
||||||
|
|
||||||
|
if (!tmp) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp->capacity = capacity;
|
||||||
|
tmp->size = length;
|
||||||
|
strncpy(tmp->buf, src, length);
|
||||||
|
|
||||||
|
*dst = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete_dstr(dstr_t **str) {
|
||||||
|
if (!(*str)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(*str);
|
||||||
|
*str = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void concat_dstr(dstr_t **dst, const char *src) {
|
||||||
|
if (!(*dst)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 src_length = strlen(src);
|
||||||
|
|
||||||
|
if (src_length == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 new_length = (*dst)->size + src_length;
|
||||||
|
|
||||||
|
char str[new_length + 1];
|
||||||
|
memset(str, 0, new_length + 1);
|
||||||
|
|
||||||
|
strncpy(str, (*dst)->buf, (*dst)->size);
|
||||||
|
strncat(str, src, src_length);
|
||||||
|
|
||||||
|
update_dstr(dst, str);
|
||||||
|
}
|
||||||
|
|
||||||
|
void append_to_dstr(dstr_t **dst, char c) {
|
||||||
|
if (!(*dst)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 new_length = (*dst)->size + 1;
|
||||||
|
|
||||||
|
char str[new_length + 1];
|
||||||
|
memset(str, 0, new_length + 1);
|
||||||
|
|
||||||
|
strncpy(str, (*dst)->buf, (*dst)->size);
|
||||||
|
str[(*dst)->size] = c;
|
||||||
|
|
||||||
|
update_dstr(dst, str);
|
||||||
|
}
|
||||||
|
|
||||||
|
void empty_dstr(dstr_t *str) {
|
||||||
|
if (!str || str->size == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(str->buf, 0, str->capacity);
|
||||||
|
str->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_dstr(const dstr_t *str) {
|
||||||
|
if (!str) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s\n", str->buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 dstr_length(const dstr_t *str) {
|
||||||
|
if (!str) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return str->size;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 dstr_capacity(const dstr_t *str) {
|
||||||
|
if (!str) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return str->capacity;
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "lexer_states.h"
|
#include "lexer.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
Loading…
Reference in New Issue
Block a user