2024-06-11 23:40:34 -04:00
|
|
|
/* Copyright (c) 2024, Peter McGoron
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1) Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2) Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "uns.h"
|
|
|
|
#include "uns_string.h"
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ALLEN_IND = 0,
|
|
|
|
LEN_IND = 1,
|
|
|
|
BYTES_IND = 2,
|
|
|
|
NUMBER_OF_IND = 3
|
|
|
|
};
|
|
|
|
|
2024-06-12 21:41:35 -04:00
|
|
|
static size_t get_size_t(struct uns_gc *gc, struct uns_ctr *root, size_t i)
|
2024-06-11 23:40:34 -04:00
|
|
|
{
|
|
|
|
size_t r;
|
|
|
|
|
|
|
|
void *p = gc->record_get_ptr(root->p, i);
|
|
|
|
memcpy(&r, p, sizeof(r));
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2024-06-12 21:41:35 -04:00
|
|
|
static void set_size_t(struct uns_gc *gc, struct uns_ctr *root, size_t i, size_t val)
|
2024-06-11 23:40:34 -04:00
|
|
|
{
|
|
|
|
void *p = gc->record_get_ptr(root->p, i);
|
|
|
|
memcpy(p, &val, sizeof(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define set_allen(gc,root,val) set_size_t(gc, root, ALLEN_IND, val)
|
|
|
|
#define set_len(gc,root,val) set_size_t(gc, root, LEN_IND, val)
|
|
|
|
|
2024-06-12 21:41:35 -04:00
|
|
|
static size_t uns_string_allen(struct uns_gc *gc, struct uns_ctr *root)
|
2024-06-11 23:40:34 -04:00
|
|
|
{
|
|
|
|
return get_size_t(gc, root, ALLEN_IND);
|
|
|
|
}
|
|
|
|
|
2024-06-12 21:41:35 -04:00
|
|
|
size_t uns_string_len(struct uns_gc *gc, struct uns_ctr *root)
|
2024-06-11 23:40:34 -04:00
|
|
|
{
|
|
|
|
return get_size_t(gc, root, LEN_IND);
|
|
|
|
}
|
|
|
|
|
2024-06-12 21:41:35 -04:00
|
|
|
char *uns_string_ptr(struct uns_gc *gc, struct uns_ctr *root)
|
2024-06-11 23:40:34 -04:00
|
|
|
{
|
|
|
|
return gc->record_get_ptr(root->p, BYTES_IND);
|
|
|
|
}
|
|
|
|
|
2024-06-12 21:41:35 -04:00
|
|
|
void uns_string_alloc(struct uns_gc *gc, struct uns_ctr *root, size_t start_len)
|
2024-06-11 23:40:34 -04:00
|
|
|
{
|
2024-06-12 08:56:49 -04:00
|
|
|
unsigned char *p;
|
2024-06-11 23:40:34 -04:00
|
|
|
root->p = gc->alloc_record(gc, NUMBER_OF_IND);
|
|
|
|
|
2024-06-12 08:56:49 -04:00
|
|
|
/* The following is incorrect:
|
|
|
|
* gc->record_set_ptr(root->p, ALLEN_IND, gc->alloc(gc, sizeof(size_t)));
|
|
|
|
* This is because gc->alloc() can cause a collection, invalidating root->p,
|
|
|
|
* but root->p may have already been put on the stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
p = gc->alloc(gc, sizeof(size_t));
|
|
|
|
gc->record_set_ptr(root->p, ALLEN_IND, p);
|
2024-06-11 23:40:34 -04:00
|
|
|
set_allen(gc, root, start_len);
|
|
|
|
|
2024-06-12 08:56:49 -04:00
|
|
|
p = gc->alloc(gc, sizeof(size_t));
|
|
|
|
gc->record_set_ptr(root->p, LEN_IND, p);
|
2024-06-11 23:40:34 -04:00
|
|
|
set_len(gc, root, 0);
|
|
|
|
|
2024-06-12 08:56:49 -04:00
|
|
|
p = gc->alloc(gc, start_len);
|
|
|
|
gc->record_set_ptr(root->p, BYTES_IND, p);
|
2024-06-11 23:40:34 -04:00
|
|
|
}
|
|
|
|
|
2024-06-12 21:41:35 -04:00
|
|
|
void uns_string_resize(struct uns_gc *gc, struct uns_ctr *root, size_t newlen)
|
2024-06-11 23:40:34 -04:00
|
|
|
{
|
2024-06-12 21:41:35 -04:00
|
|
|
struct uns_ctr tmp_new = {0};
|
2024-06-12 08:56:49 -04:00
|
|
|
size_t old_len = uns_string_len(gc, root);
|
|
|
|
|
2024-06-12 21:41:35 -04:00
|
|
|
/* The temporary string needs to be added to the roots list because
|
|
|
|
* there are multiple allocations that happen in the body of the string.
|
|
|
|
*/
|
2024-06-12 08:56:49 -04:00
|
|
|
uns_root_add(gc, &tmp_new);
|
|
|
|
uns_string_alloc(gc, &tmp_new, newlen);
|
2024-06-11 23:40:34 -04:00
|
|
|
|
2024-06-12 08:56:49 -04:00
|
|
|
if (newlen <= old_len)
|
|
|
|
set_len(gc, &tmp_new, newlen);
|
|
|
|
else
|
|
|
|
set_len(gc, &tmp_new, old_len);
|
2024-06-11 23:40:34 -04:00
|
|
|
|
2024-06-12 08:56:49 -04:00
|
|
|
memcpy(uns_string_ptr(gc, &tmp_new), uns_string_ptr(gc, root), uns_string_len(gc, root));
|
|
|
|
root->p = tmp_new.p;
|
|
|
|
uns_root_remove(gc, &tmp_new);
|
2024-06-11 23:40:34 -04:00
|
|
|
}
|
|
|
|
|
2024-06-12 21:41:35 -04:00
|
|
|
void uns_string_ensure(struct uns_gc *gc, struct uns_ctr *root, size_t extent)
|
2024-06-11 23:40:34 -04:00
|
|
|
{
|
|
|
|
size_t used = uns_string_len(gc, root);
|
|
|
|
size_t allen = uns_string_allen(gc, root);
|
|
|
|
|
|
|
|
if (used + extent > allen)
|
|
|
|
uns_string_resize(gc, root, used + extent);
|
|
|
|
}
|
|
|
|
|
2024-06-12 21:41:35 -04:00
|
|
|
void uns_string_append_bytes(struct uns_gc *gc, struct uns_ctr *to,
|
2024-06-11 23:40:34 -04:00
|
|
|
const void *from, size_t bytes)
|
|
|
|
{
|
|
|
|
size_t len = uns_string_len(gc, to);
|
|
|
|
uns_string_ensure(gc, to, bytes);
|
|
|
|
memcpy(uns_string_ptr(gc, to) + len, from, bytes);
|
|
|
|
set_len(gc, to, len + bytes);
|
|
|
|
}
|
|
|
|
|
2024-06-12 21:41:35 -04:00
|
|
|
void uns_string_append_char(struct uns_gc *gc, struct uns_ctr *root, char c)
|
2024-06-11 23:40:34 -04:00
|
|
|
{
|
|
|
|
uns_string_append_bytes(gc, root, &c, 1);
|
|
|
|
}
|
|
|
|
|
2024-06-12 21:41:35 -04:00
|
|
|
char *uns_string_cstring(struct uns_gc *gc, struct uns_ctr *root)
|
2024-06-11 23:40:34 -04:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
uns_string_ensure(gc, root, 1);
|
|
|
|
|
|
|
|
p = uns_string_ptr(gc, root);
|
|
|
|
p[uns_string_len(gc, root)] = 0;
|
|
|
|
return p;
|
|
|
|
}
|