mirror of
https://github.com/samsonjs/lake.git
synced 2026-03-25 08:55:49 +00:00
70 lines
1.1 KiB
C
70 lines
1.1 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "int.h"
|
|
#include "lake.h"
|
|
#include "string.h"
|
|
|
|
static LakeStr *str_alloc(void)
|
|
{
|
|
LakeStr *str = malloc(sizeof(LakeStr));
|
|
str->base.type = TYPE_STR;
|
|
str->base.size = sizeof(LakeStr);
|
|
return str;
|
|
}
|
|
|
|
void str_free(LakeStr *str)
|
|
{
|
|
free(str->s);
|
|
free(str);
|
|
}
|
|
|
|
LakeStr *str_make(void)
|
|
{
|
|
return str_from_c("");
|
|
}
|
|
|
|
LakeInt *str_len(LakeStr *str)
|
|
{
|
|
return int_from_c(str->n);
|
|
}
|
|
|
|
LakeStr *str_copy(LakeStr *str)
|
|
{
|
|
return str_from_c(str->s);
|
|
}
|
|
|
|
LakeStr *str_from_c(char *s)
|
|
{
|
|
LakeStr *str = str_alloc();
|
|
str_set(str, s);
|
|
return str;
|
|
}
|
|
|
|
char *str_val(LakeStr *str)
|
|
{
|
|
return strdup(str->s);
|
|
}
|
|
|
|
NILP str_set(LakeStr *str, char *s)
|
|
{
|
|
str->n = strlen(s);
|
|
str->s = strdup(s);
|
|
return NIL;
|
|
}
|
|
|
|
#define MIN(a, b) (a < b ? a : b)
|
|
|
|
LakeInt *str_cmp(LakeStr *a, LakeStr *b)
|
|
{
|
|
return int_from_c(strncmp(a->s, b->s, MIN(a->n, b->n)));
|
|
}
|
|
|
|
LakeSym *str_eq(LakeStr *a, LakeStr *b)
|
|
{
|
|
return bool_from_int(strncmp(a->s, b->s, MIN(a->n, b->n)) == 0);
|
|
}
|
|
|
|
LakeStr *str_to_str(LakeStr *str)
|
|
{
|
|
return str_copy(str);
|
|
}
|