added:
* file: small wrap over winapi for windows and stdio for posix * fs: small wrapper over stat * slice: slice macro type modified: * str and strview are now slices
This commit is contained in:
parent
bb5cce33f0
commit
c4d1ffe539
15 changed files with 451 additions and 88 deletions
100
strview.c
100
strview.c
|
|
@ -20,7 +20,7 @@ strview_t strvInit(const char *cstr) {
|
|||
strview_t strvInitLen(const char *buf, size_t size) {
|
||||
strview_t view;
|
||||
view.buf = buf;
|
||||
view.size = size;
|
||||
view.len = size;
|
||||
return view;
|
||||
}
|
||||
|
||||
|
|
@ -29,7 +29,7 @@ char strvFront(strview_t ctx) {
|
|||
}
|
||||
|
||||
char strvBack(strview_t ctx) {
|
||||
return ctx.buf[ctx.size - 1];
|
||||
return ctx.buf[ctx.len - 1];
|
||||
}
|
||||
|
||||
const char *strvBegin(strview_t *ctx) {
|
||||
|
|
@ -37,31 +37,31 @@ const char *strvBegin(strview_t *ctx) {
|
|||
}
|
||||
|
||||
const char *strvEnd(strview_t *ctx) {
|
||||
return ctx->buf + ctx->size;
|
||||
return ctx->buf + ctx->len;
|
||||
}
|
||||
|
||||
bool strvIsEmpty(strview_t ctx) {
|
||||
return ctx.size == 0;
|
||||
return ctx.len == 0;
|
||||
}
|
||||
|
||||
void strvRemovePrefix(strview_t *ctx, size_t n) {
|
||||
ctx->buf += n;
|
||||
ctx->size -= n;
|
||||
ctx->len -= n;
|
||||
}
|
||||
|
||||
void strvRemoveSuffix(strview_t *ctx, size_t n) {
|
||||
ctx->size -= n;
|
||||
ctx->len -= n;
|
||||
}
|
||||
|
||||
size_t strvCopy(strview_t ctx, char **buf) {
|
||||
*buf = malloc(ctx.size + 1);
|
||||
memcpy(*buf, ctx.buf, ctx.size);
|
||||
(*buf)[ctx.size] = '\0';
|
||||
return ctx.size;
|
||||
*buf = malloc(ctx.len + 1);
|
||||
memcpy(*buf, ctx.buf, ctx.len);
|
||||
(*buf)[ctx.len] = '\0';
|
||||
return ctx.len;
|
||||
}
|
||||
|
||||
size_t strvCopyN(strview_t ctx, char **buf, size_t count, size_t from) {
|
||||
size_t sz = ctx.size + 1 - from;
|
||||
size_t sz = ctx.len + 1 - from;
|
||||
count = min(count, sz);
|
||||
*buf = malloc(count + 1);
|
||||
memcpy(*buf, ctx.buf + from, count);
|
||||
|
|
@ -70,7 +70,7 @@ size_t strvCopyN(strview_t ctx, char **buf, size_t count, size_t from) {
|
|||
}
|
||||
|
||||
size_t strvCopyBuf(strview_t ctx, char *buf, size_t len, size_t from) {
|
||||
size_t sz = ctx.size + 1 - from;
|
||||
size_t sz = ctx.len + 1 - from;
|
||||
len = min(len, sz);
|
||||
memcpy(buf, ctx.buf + from, len);
|
||||
buf[len - 1] = '\0';
|
||||
|
|
@ -78,21 +78,21 @@ size_t strvCopyBuf(strview_t ctx, char *buf, size_t len, size_t from) {
|
|||
}
|
||||
|
||||
strview_t strvSubstr(strview_t ctx, size_t from, size_t len) {
|
||||
if(from > ctx.size) from = ctx.size - len;
|
||||
size_t sz = ctx.size - from;
|
||||
if(from > ctx.len) from = ctx.len - len;
|
||||
size_t sz = ctx.len - from;
|
||||
return strvInitLen(ctx.buf + from, min(len, sz));
|
||||
}
|
||||
|
||||
int strvCompare(strview_t ctx, strview_t other) {
|
||||
if(ctx.size < other.size) return -1;
|
||||
if(ctx.size > other.size) return 1;
|
||||
return memcmp(ctx.buf, other.buf, ctx.size);
|
||||
if(ctx.len < other.len) return -1;
|
||||
if(ctx.len > other.len) return 1;
|
||||
return memcmp(ctx.buf, other.buf, ctx.len);
|
||||
}
|
||||
|
||||
int strvICompare(strview_t ctx, strview_t other) {
|
||||
if(ctx.size < other.size) return -1;
|
||||
if(ctx.size > other.size) return 1;
|
||||
for(size_t i = 0; i < ctx.size; ++i) {
|
||||
if(ctx.len < other.len) return -1;
|
||||
if(ctx.len > other.len) return 1;
|
||||
for(size_t i = 0; i < ctx.len; ++i) {
|
||||
char a = tolower(ctx.buf[i]);
|
||||
char b = tolower(other.buf[i]);
|
||||
if(a != b) return a - b;
|
||||
|
|
@ -105,8 +105,8 @@ bool strvStartsWith(strview_t ctx, char c) {
|
|||
}
|
||||
|
||||
bool strvStartsWithView(strview_t ctx, strview_t view) {
|
||||
if(ctx.size < view.size) return false;
|
||||
return memcmp(ctx.buf, view.buf, view.size) == 0;
|
||||
if(ctx.len < view.len) return false;
|
||||
return memcmp(ctx.buf, view.buf, view.len) == 0;
|
||||
}
|
||||
|
||||
bool strvEndsWith(strview_t ctx, char c) {
|
||||
|
|
@ -114,45 +114,45 @@ bool strvEndsWith(strview_t ctx, char c) {
|
|||
}
|
||||
|
||||
bool strvEndsWithView(strview_t ctx, strview_t view) {
|
||||
if(ctx.size < view.size) return false;
|
||||
return memcmp(ctx.buf + ctx.size - view.size, view.buf, view.size) == 0;
|
||||
if(ctx.len < view.len) return false;
|
||||
return memcmp(ctx.buf + ctx.len - view.len, view.buf, view.len) == 0;
|
||||
}
|
||||
|
||||
bool strvContains(strview_t ctx, char c) {
|
||||
for(size_t i = 0; i < ctx.size; ++i) {
|
||||
for(size_t i = 0; i < ctx.len; ++i) {
|
||||
if(ctx.buf[i] == c) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool strvContainsView(strview_t ctx, strview_t view) {
|
||||
if(ctx.size < view.size) return false;
|
||||
size_t end = ctx.size - view.size;
|
||||
if(ctx.len < view.len) return false;
|
||||
size_t end = ctx.len - view.len;
|
||||
for(size_t i = 0; i < end; ++i) {
|
||||
if(memcmp(ctx.buf + i, view.buf, view.size) == 0) return true;
|
||||
if(memcmp(ctx.buf + i, view.buf, view.len) == 0) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t strvFind(strview_t ctx, char c, size_t from) {
|
||||
for(size_t i = from; i < ctx.size; ++i) {
|
||||
for(size_t i = from; i < ctx.len; ++i) {
|
||||
if(ctx.buf[i] == c) return i;
|
||||
}
|
||||
return SIZE_MAX;
|
||||
}
|
||||
|
||||
size_t strvFindView(strview_t ctx, strview_t view, size_t from) {
|
||||
if(ctx.size < view.size) return SIZE_MAX;
|
||||
size_t end = ctx.size - view.size;
|
||||
if(ctx.len < view.len) return SIZE_MAX;
|
||||
size_t end = ctx.len - view.len;
|
||||
for(size_t i = from; i < end; ++i) {
|
||||
if(memcmp(ctx.buf + i, view.buf, view.size) == 0) return i;
|
||||
if(memcmp(ctx.buf + i, view.buf, view.len) == 0) return i;
|
||||
}
|
||||
return SIZE_MAX;
|
||||
}
|
||||
|
||||
size_t strvRFind(strview_t ctx, char c, size_t from) {
|
||||
if(from >= ctx.size) {
|
||||
from = ctx.size - 1;
|
||||
if(from >= ctx.len) {
|
||||
from = ctx.len - 1;
|
||||
}
|
||||
|
||||
const char *buf = ctx.buf + from;
|
||||
|
|
@ -164,23 +164,23 @@ size_t strvRFind(strview_t ctx, char c, size_t from) {
|
|||
}
|
||||
|
||||
size_t strvRFindView(strview_t ctx, strview_t view, size_t from) {
|
||||
from = min(from, ctx.size);
|
||||
from = min(from, ctx.len);
|
||||
|
||||
if(view.size > ctx.size) {
|
||||
from -= view.size;
|
||||
if(view.len > ctx.len) {
|
||||
from -= view.len;
|
||||
}
|
||||
|
||||
const char *buf = ctx.buf + from;
|
||||
for(; buf >= ctx.buf; --buf) {
|
||||
if(memcmp(buf, view.buf, view.size) == 0) return (buf - ctx.buf);
|
||||
if(memcmp(buf, view.buf, view.len) == 0) return (buf - ctx.buf);
|
||||
}
|
||||
return SIZE_MAX;
|
||||
}
|
||||
|
||||
size_t strvFindFirstOf(strview_t ctx, strview_t view, size_t from) {
|
||||
if(ctx.size < view.size) return SIZE_MAX;
|
||||
for(size_t i = from; i < ctx.size; ++i) {
|
||||
for(size_t j = 0; j < view.size; ++j) {
|
||||
if(ctx.len < view.len) return SIZE_MAX;
|
||||
for(size_t i = from; i < ctx.len; ++i) {
|
||||
for(size_t j = 0; j < view.len; ++j) {
|
||||
if(ctx.buf[i] == view.buf[j]) return i;
|
||||
}
|
||||
}
|
||||
|
|
@ -188,13 +188,13 @@ size_t strvFindFirstOf(strview_t ctx, strview_t view, size_t from) {
|
|||
}
|
||||
|
||||
size_t strvFindLastOf(strview_t ctx, strview_t view, size_t from) {
|
||||
if(from >= ctx.size) {
|
||||
from = ctx.size - 1;
|
||||
if(from >= ctx.len) {
|
||||
from = ctx.len - 1;
|
||||
}
|
||||
|
||||
const char *buf = ctx.buf + from;
|
||||
for(; buf >= ctx.buf; --buf) {
|
||||
for(size_t j = 0; j < view.size; ++j) {
|
||||
for(size_t j = 0; j < view.len; ++j) {
|
||||
if(*buf == view.buf[j]) return (buf - ctx.buf);
|
||||
}
|
||||
}
|
||||
|
|
@ -203,7 +203,7 @@ size_t strvFindLastOf(strview_t ctx, strview_t view, size_t from) {
|
|||
}
|
||||
|
||||
size_t strvFindFirstNot(strview_t ctx, char c, size_t from) {
|
||||
size_t end = ctx.size - 1;
|
||||
size_t end = ctx.len - 1;
|
||||
for(size_t i = from; i < end; ++i) {
|
||||
if(ctx.buf[i] != c) return i;
|
||||
}
|
||||
|
|
@ -211,7 +211,7 @@ size_t strvFindFirstNot(strview_t ctx, char c, size_t from) {
|
|||
}
|
||||
|
||||
size_t strvFindFirstNotOf(strview_t ctx, strview_t view, size_t from) {
|
||||
for(size_t i = from; i < ctx.size; ++i) {
|
||||
for(size_t i = from; i < ctx.len; ++i) {
|
||||
if(!strvContains(view, ctx.buf[i])) {
|
||||
return i;
|
||||
}
|
||||
|
|
@ -220,8 +220,8 @@ size_t strvFindFirstNotOf(strview_t ctx, strview_t view, size_t from) {
|
|||
}
|
||||
|
||||
size_t strvFindLastNot(strview_t ctx, char c, size_t from) {
|
||||
if(from >= ctx.size) {
|
||||
from = ctx.size - 1;
|
||||
if(from >= ctx.len) {
|
||||
from = ctx.len - 1;
|
||||
}
|
||||
|
||||
const char *buf = ctx.buf + from;
|
||||
|
|
@ -235,8 +235,8 @@ size_t strvFindLastNot(strview_t ctx, char c, size_t from) {
|
|||
}
|
||||
|
||||
size_t strvFindLastNotOf(strview_t ctx, strview_t view, size_t from) {
|
||||
if(from >= ctx.size) {
|
||||
from = ctx.size - 1;
|
||||
if(from >= ctx.len) {
|
||||
from = ctx.len - 1;
|
||||
}
|
||||
|
||||
const char *buf = ctx.buf + from;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue