*: Split off headers into their own folder
Main reason is so we don't have that stupid arl prefix directory in our source code. Now our source code is flat, and we can still reference headers by linking from root.
This commit is contained in:
63
src/lib/sv.c
Normal file
63
src/lib/sv.c
Normal file
@@ -0,0 +1,63 @@
|
||||
/* sv.c: String View implementations
|
||||
* Created: 2026-01-22
|
||||
* Author: Aryadev Chavali
|
||||
* License: See end of file
|
||||
* Commentary: See /include/sv.h
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <arl/lib/sv.h>
|
||||
|
||||
sv_t sv_chop_left(sv_t sv, u64 len)
|
||||
{
|
||||
if (sv.size < len)
|
||||
{
|
||||
return SV(NULL, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return SV(sv.data + len, sv.size - len);
|
||||
}
|
||||
}
|
||||
|
||||
sv_t sv_chop_right(sv_t sv, u64 len)
|
||||
{
|
||||
if (sv.size < len)
|
||||
{
|
||||
return SV(NULL, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return SV(sv.data, sv.size - len);
|
||||
}
|
||||
}
|
||||
|
||||
u64 sv_while(const sv_t sv, const char *expected)
|
||||
{
|
||||
u64 i;
|
||||
for (i = 0; i < sv.size && strchr(expected, sv.data[i]) != NULL; ++i)
|
||||
continue;
|
||||
return i;
|
||||
}
|
||||
|
||||
u64 sv_till(const sv_t sv, const char *expected)
|
||||
{
|
||||
u64 i;
|
||||
for (i = 0; i < sv.size && strchr(expected, sv.data[i]) == NULL; ++i)
|
||||
continue;
|
||||
return i;
|
||||
}
|
||||
|
||||
/* Copyright (C) 2026 Aryadev Chavali
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MIT License
|
||||
* for details.
|
||||
|
||||
* You may distribute and modify this code under the terms of the
|
||||
* MIT License, which you should have received a copy of along with this
|
||||
* program. If not, please go to <https://opensource.org/license/MIT>.
|
||||
|
||||
*/
|
||||
111
src/lib/vec.c
Normal file
111
src/lib/vec.c
Normal file
@@ -0,0 +1,111 @@
|
||||
/* vec.c: Vector implementation
|
||||
* Created: 2026-01-22
|
||||
* Author: Aryadev Chavali
|
||||
* License: See end of file
|
||||
* Commentary:
|
||||
|
||||
Taken from prick_vec.h: see https://github.com/oreodave/prick.
|
||||
*/
|
||||
|
||||
#include <arl/lib/base.h>
|
||||
#include <arl/lib/vec.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void vec_append(vec_t *vec, const void *const ptr, u64 size)
|
||||
{
|
||||
if (!vec || !ptr || !size)
|
||||
return;
|
||||
vec_ensure_free(vec, size);
|
||||
memcpy(&VEC_GET(vec, vec->size, u8), ptr, size);
|
||||
vec->size += size;
|
||||
}
|
||||
|
||||
void vec_append_byte(vec_t *vec, u8 byte)
|
||||
{
|
||||
if (!vec)
|
||||
return;
|
||||
vec_ensure_free(vec, 1);
|
||||
VEC_GET(vec, vec->size, u8) = byte;
|
||||
++vec->size;
|
||||
}
|
||||
|
||||
void *vec_data(vec_t *vec)
|
||||
{
|
||||
if (!vec)
|
||||
return NULL;
|
||||
|
||||
if (vec->not_inlined)
|
||||
{
|
||||
return vec->ptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
return vec->inlined;
|
||||
}
|
||||
}
|
||||
|
||||
void vec_ensure_capacity(vec_t *vec, u64 capacity)
|
||||
{
|
||||
if (!vec)
|
||||
return;
|
||||
if (vec->capacity == 0)
|
||||
vec->capacity = VEC_INLINE_CAPACITY;
|
||||
if (vec->capacity < capacity)
|
||||
{
|
||||
vec->capacity = MAX(vec->capacity * VEC_MULT, capacity);
|
||||
if (!vec->not_inlined)
|
||||
{
|
||||
// We were a small buffer, and now we cannot be i.e. we need to allocate
|
||||
// on the heap.
|
||||
vec->not_inlined = 1;
|
||||
void *buffer = calloc(1, vec->capacity);
|
||||
memcpy(buffer, vec->inlined, vec->size);
|
||||
memset(vec->inlined, 0, sizeof(vec->inlined));
|
||||
vec->ptr = buffer;
|
||||
}
|
||||
else
|
||||
{
|
||||
// We're already on the heap, just reallocate.
|
||||
vec->ptr = realloc(vec->ptr, vec->capacity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void vec_ensure_free(vec_t *vec, u64 size)
|
||||
{
|
||||
if (!vec)
|
||||
return;
|
||||
vec_ensure_capacity(vec, vec->size + size);
|
||||
}
|
||||
|
||||
void vec_free(vec_t *vec)
|
||||
{
|
||||
if (!vec)
|
||||
return;
|
||||
if (vec->not_inlined)
|
||||
free(vec->ptr);
|
||||
memset(vec, 1, sizeof(*vec));
|
||||
}
|
||||
|
||||
void vec_clone(vec_t *v2, vec_t *v1)
|
||||
{
|
||||
if (!v1 || !v2)
|
||||
return;
|
||||
vec_append(v2, vec_data(v1), v1->size);
|
||||
}
|
||||
|
||||
#undef MAX
|
||||
|
||||
/* Copyright (C) 2026 Aryadev Chavali
|
||||
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the MIT License for details.
|
||||
|
||||
* You may distribute and modify this code under the terms of the MIT License,
|
||||
* which you should have received a copy of along with this program. If not,
|
||||
* please go to <https://opensource.org/license/MIT>.
|
||||
|
||||
*/
|
||||
Reference in New Issue
Block a user