cutils/lib/str.c

57 lines
1008 B
C
Raw Normal View History

2023-02-12 18:36:44 +01:00
#include "str.h"
#include <stdlib.h>
#include <string.h>
#include "common.h"
#define STR_MIN_ALLOC 64
void
str_new (str_t *str)
{
str->str = smalloc (STR_MIN_ALLOC * sizeof (char));
str->len = 0;
str->cap = STR_MIN_ALLOC;
}
void
str_new_cap (str_t *str, size_t want_cap)
{
size_t cap;
cap = max (next_pow_of_two (want_cap), STR_MIN_ALLOC);
str->str = smalloc (cap);
str->len = 0;
str->cap = cap;
}
2023-02-14 12:15:05 +01:00
inline int
2023-02-12 18:36:44 +01:00
str_new_from (str_t *str, const char *src)
{
2023-02-14 12:15:05 +01:00
if (src)
return str_new_from_len (str, src, strlen (src)), 0;
else
return errno = EINVAL, -1;
2023-02-12 18:36:44 +01:00
}
2023-02-14 12:15:05 +01:00
int
2023-02-12 18:36:44 +01:00
str_new_from_len (str_t *str, const char *src, size_t len)
{
size_t cap;
2023-02-14 12:15:05 +01:00
if (!src)
return errno = EINVAL, -1;
2023-02-12 18:36:44 +01:00
cap = max (next_pow_of_two (len + 1), STR_MIN_ALLOC);
str->str = smalloc (cap * sizeof (char));
str->len = len;
str->cap = cap;
strcpy (str->str, src);
2023-02-14 12:15:05 +01:00
return 0;
2023-02-12 18:36:44 +01:00
}
inline void
str_free (str_t *str)
{
2023-02-14 12:15:05 +01:00
if (str)
free (str->str);
2023-02-12 18:36:44 +01:00
}