From 521d01e8ad87e931af3e9a763cc84a6cf7fe5ee3 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Sun, 5 Dec 2021 09:47:21 -0800 Subject: Feat: basic string and memory functions Continue filling out the basic standard lib functions. Included prototypes of the str* and mem* families. Plan to add e(str|mem) and n(str|mem) variants as well. --- include/base/fmt.h | 4 +-- include/base/io.h | 8 ------ include/base/mem.h | 65 ------------------------------------------ include/base/memory.h | 66 ++++++++++++++++++++++++++++++++++++++++++ include/base/string.h | 79 ++++++++++++++++++++++++++++++++------------------- 5 files changed, 117 insertions(+), 105 deletions(-) delete mode 100644 include/base/mem.h create mode 100644 include/base/memory.h (limited to 'include/base') diff --git a/include/base/fmt.h b/include/base/fmt.h index 4c187fd..92e3261 100644 --- a/include/base/fmt.h +++ b/include/base/fmt.h @@ -54,12 +54,12 @@ int fmt·fprint(int fd, char *fmt, ...); void fmt·panic(char *fmt, ...); int fmt·sprint(char *buf, char *fmt, ...); -int fmt·nsprint(int len, char *buf, char *fmt, ...); +int fmt·nsprint(char *buf, int len, char *fmt, ...); char *fmt·esprint(char *buf, char *end, char *fmt, ...); int fmt·vprint(char *fmt, va_list args); int fmt·vfprint(int fd, char *fmt, va_list args); -int fmt·vnsprint(int len, char *buf, char *fmt, va_list args); +int fmt·vnsprint(char *buf, int len, char *fmt, va_list args); char *fmt·vesprint(char *buf, char *end, char *fmt, va_list args); /* low-level interface: used for custom printing verbs */ diff --git a/include/base/io.h b/include/base/io.h index dae57ef..04b596f 100644 --- a/include/base/io.h +++ b/include/base/io.h @@ -61,14 +61,6 @@ typedef struct io·ReadWriter } io·ReadWriter; extern io·ReadWriter sys·ReadWriter; -/* XXX: change casing */ -enum -{ - ReadOK = R_OK, - WriteOK = W_OK, - ExecOK = X_OK, -}; - /* buffered i/o */ typedef struct io·Header io·Header; typedef struct io·Buffer io·Buffer; diff --git a/include/base/mem.h b/include/base/mem.h deleted file mode 100644 index 6f14c8e..0000000 --- a/include/base/mem.h +++ /dev/null @@ -1,65 +0,0 @@ -#pragma once - -// ---------------------------------------------------------------------------- -// dynamic array - -typedef struct BufHdr -{ - vlong len; - vlong cap; - byte buf[]; -} BufHdr; - -#define bufhdr(b) ((BufHdr*)((uint8*)(b)-offsetof(BufHdr, buf))) -#define buflen(b) ((b) ? (bufhdr(b)->len) : 0) -#define bufcap(b) ((b) ? (bufhdr(b)->cap) : 0) -#define bufend(b) ((b) + buflen(b)) -#define bufsize(b) ((b) ? (buflen(b) * sizeof((b)[0])) : 0) - -#define buffree(b) ((b) ? (free(bufhdr(b)), (b) = nil) : 0) -#define buffit(b, n) ((n) <= bufcap(b) ? 0 : ((b) = ·bufgrow((b), (n), sizeof(*(b))))) - -#define bufpush(b, ...) (buffit((b), 1 + buflen(b)), (b)[bufhdr(b)->len++] = (__VA_ARGS__)) -#define bufaddn(b, n) (buffit(b, buflen(b)+n), bufhdr(b)->len += n, b+bufhdr(b)->len-n) - -#define bufpop(b) ((b)[--bufhdr(b)->len]) -#define bufdel(b, i) bufdeln((b), (i), 1) -#define bufdeln(b, i, n) (memmove((b)+(i), (b)+(i)+(n), sizeof(*(b))*(bufhdr(b)->len-(n)-(i)), bufhdr(b)->len -= (n)) -#define bufdelswap(b, i) ((b)[i] = bufend(b)[-1], bufhdr(b)->len-=1) - -void* ·bufgrow(void*, vlong, vlong); - -// ----------------------------------------------------------------------------- -// memory allocation - -/* allocator interface */ -typedef struct mem·Allocator { - void *(*alloc)(void *heap, uint n, ulong size); - void (*free)(void *heap, void *ptr); -} mem·Allocator; - -extern mem·Allocator sys·Memory; - -typedef struct mem·Reallocator { - void *(*alloc)(void *iface, uint n, ulong size); - void *(*realloc)(void *iface, void *ptr, uint n, ulong size); - void (*free)(void *iface, void *ptr); -} mem·Reallocator; - -extern mem·Reallocator sys·FullMemory; - -/* simple memory arena */ -typedef struct mem·Arena mem·Arena; - -mem·Arena *mem·makearena(mem·Allocator from, void*); -void *mem·arenaalloc(mem·Arena *A, uint n, ulong size); -void mem·freearena(mem·Arena *A); - -extern mem·Allocator mem·ArenaAllocator; - -/* mem functions */ -int mem·move(void *dst, void *src, uintptr size); -void *mem·findc(void *dst, uintptr len, int c); -int mem·set(void *dst, int val, uintptr size); -int mem·set64(void *dst, uint64 val, uintptr size); - diff --git a/include/base/memory.h b/include/base/memory.h new file mode 100644 index 0000000..d80b34c --- /dev/null +++ b/include/base/memory.h @@ -0,0 +1,66 @@ +#pragma once + +// ---------------------------------------------------------------------------- +// dynamic array + +typedef struct BufHdr +{ + vlong len; + vlong cap; + byte buf[]; +} BufHdr; + +#define bufhdr(b) ((BufHdr*)((uint8*)(b)-offsetof(BufHdr, buf))) +#define buflen(b) ((b) ? (bufhdr(b)->len) : 0) +#define bufcap(b) ((b) ? (bufhdr(b)->cap) : 0) +#define bufend(b) ((b) + buflen(b)) +#define bufsize(b) ((b) ? (buflen(b) * sizeof((b)[0])) : 0) + +#define buffree(b) ((b) ? (free(bufhdr(b)), (b) = nil) : 0) +#define buffit(b, n) ((n) <= bufcap(b) ? 0 : ((b) = ·bufgrow((b), (n), sizeof(*(b))))) + +#define bufpush(b, ...) (buffit((b), 1 + buflen(b)), (b)[bufhdr(b)->len++] = (__VA_ARGS__)) +#define bufaddn(b, n) (buffit(b, buflen(b)+n), bufhdr(b)->len += n, b+bufhdr(b)->len-n) + +#define bufpop(b) ((b)[--bufhdr(b)->len]) +#define bufdel(b, i) bufdeln((b), (i), 1) +#define bufdeln(b, i, n) (memmove((b)+(i), (b)+(i)+(n), sizeof(*(b))*(bufhdr(b)->len-(n)-(i)), bufhdr(b)->len -= (n)) +#define bufdelswap(b, i) ((b)[i] = bufend(b)[-1], bufhdr(b)->len-=1) + +void* ·bufgrow(void*, vlong, vlong); + +// ----------------------------------------------------------------------------- +// memory allocation + +/* allocator interface */ +typedef struct mem·Allocator { + void *(*alloc)(void *heap, uint n, ulong size); + void (*free)(void *heap, void *ptr); +} mem·Allocator; + +extern mem·Allocator sys·Memory; + +typedef struct mem·Reallocator { + void *(*alloc)(void *iface, uint n, ulong size); + void *(*realloc)(void *iface, void *ptr, uint n, ulong size); + void (*free)(void *iface, void *ptr); +} mem·Reallocator; + +extern mem·Reallocator sys·FullMemory; + +/* simple memory arena */ +typedef struct mem·Arena mem·Arena; + +mem·Arena *mem·makearena(mem·Allocator from, void*); +void *mem·arenaalloc(mem·Arena *A, uint n, ulong size); +void mem·freearena(mem·Arena *A); + +extern mem·Allocator mem·ArenaAllocator; + +/* mem functions */ +int mem·move(void *dst, uintptr size, void *src); +int mem·copy(void *dst, uintptr size, void *src); +int mem·compare(void *, uintptr size, void *); +void *mem·findc(void *dst, uintptr len, int c); +int mem·set(void *dst, uintptr size, int val); +int mem·set64(void *dst, uintptr size, uint64 val); diff --git a/include/base/string.h b/include/base/string.h index 0800f60..a6d6612 100644 --- a/include/base/string.h +++ b/include/base/string.h @@ -1,34 +1,53 @@ #pragma once -typedef byte* string; +typedef char* string; + +/* basic c string (nul terminated) functions */ +intptr str·len(char *s); + +char *str·findc(char *s, int c); +char *str·nfindc(char *s, intptr n, int c); +char *str·efindc(char *s, char *e, int c); + +char *str·find(char *s, char *sub); +char *str·nfind(char *s, intptr n, char *sub); +char *str·efind(char *s, char *e, char *sub); + +char *str·copy(char *dst, char *src); +char *str·ncopy(char *dst, intptr n, char *src); +char *str·ecopy(char *dst, char *end, char *src); + +char *str·append(char *dst, char *src); +char *str·nappend(char *dst, intptr n, char *src); +char *str·eappend(char *dst, char *end, char *src); + +int str·compare(char *, char *); +int str·ncompare(char *, intptr len, char *); +int str·ecompare(char *, char *end, char *); + +int str·atoi(char *s); +char *str·itoa(char *s, int x); /* augmented string functions */ -string str·makecap(const char *s, vlong len, vlong cap); -string str·makelen(const char *s, vlong len); -string str·make(const char *s); -string str·makef(const char *fmt, ...); -void str·free(string s); -int str·len(const string s); -int str·cap(const string s); -void str·clear(string *s); -void str·grow(string *s, vlong delta); -void str·fit(string *s); -int str·appendlen(string *s, vlong len, const char *b); -int str·append(string *s, const char *b); -int str·appendf(string *s, const char *fmt, ...); -int str·appendbyte(string *s, const char b); -bool str·equals(const string s, const string t); -int str·find(string s, const char* substr); -void str·lower(string s); -void str·upper(string s); -int str·read(string s, int size, int n, void *buf); -void str·replace(string s, const byte* from, const byte* to); -string* str·split(string s, const byte* tok); -string str·join(vlong len, byte** fields, const byte* sep); - -/* raw C string functions */ -char *str·copyn(char *dst, char *src, int n); - -/* string parsing */ -int str·atoi(char *s); -char *str·itoa(char *s, int x); +string string·makecap(char *s, vlong len, vlong cap); +string string·makelen(char *s, vlong len); +string string·make(char *s); +string string·makef(char *fmt, ...); +void string·free(string s); +int string·len(string s); +int string·cap(string s); +void string·clear(string *s); +void string·grow(string *s, vlong delta); +void string·fit(string *s); +int string·appendlen(string *s, vlong len, char *b); +int string·append(string *s, char *b); +int string·appendf(string *s, char *fmt, ...); +int string·appendbyte(string *s, char b); +bool string·equals(string s, string t); +int string·find(string s, char *substr); +void string·lower(string s); +void string·upper(string s); +int string·read(string s, int size, int n, void *buf); +void string·replace(string s, byte* from, byte* to); +string* string·split(string s, byte* tok); +string string·join(vlong len, byte** fields, byte* sep); -- cgit v1.2.1