Added more string utils and line stuff
This commit is contained in:
parent
13ba83d6af
commit
6b5bc68de9
9 changed files with 229 additions and 151 deletions
44
stdme/src/string/string_insert.c
Normal file
44
stdme/src/string/string_insert.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* string_insert.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/08 21:32:17 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/08 22:25:52 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/str/str.h"
|
||||
#include "me/string/string.h"
|
||||
#include "me/types.h"
|
||||
|
||||
t_error string_insert(t_string *self, t_usize pos, t_str str)
|
||||
{
|
||||
t_usize len;
|
||||
if (self == NULL || str == NULL || pos > self->len)
|
||||
return (ERROR);
|
||||
if (pos == self->len)
|
||||
return (string_push(self, str));
|
||||
len = str_len(str);
|
||||
if (string_reserve(self, self->len + len))
|
||||
return (ERROR);
|
||||
mem_move(&self->buf[pos + len], &self->buf[pos], self->len - pos);
|
||||
mem_copy(&self->buf[pos], str, len);
|
||||
self->len += len;
|
||||
self->buf[self->len] = '\0';
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error string_insert_char(t_string *self, t_usize pos, char chr)
|
||||
{
|
||||
char tmp[2];
|
||||
|
||||
if (chr == '\0')
|
||||
return (ERROR);
|
||||
tmp[0] = chr;
|
||||
tmp[1] = '\0';
|
||||
return (string_insert(self, pos, tmp));
|
||||
}
|
||||
41
stdme/src/string/string_remove.c
Normal file
41
stdme/src/string/string_remove.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* string_remove.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/08 21:50:27 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/08 21:54:45 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/str/str.h"
|
||||
#include "me/string/string.h"
|
||||
#include "me/types.h"
|
||||
|
||||
t_error string_remove(t_string *self, t_usize pos, char *out)
|
||||
{
|
||||
char fake_out;
|
||||
|
||||
if (out == NULL)
|
||||
out = &fake_out;
|
||||
if (self == NULL || pos >= self->len)
|
||||
return (ERROR);
|
||||
*out = self->buf[pos];
|
||||
mem_move(&self->buf[pos], &self->buf[pos] + 1, self->len - pos);
|
||||
self->len--;
|
||||
self->buf[self->len] = '\0';
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error string_clear_after(t_string *self, t_usize pos)
|
||||
{
|
||||
if (self == NULL || pos >= self->len)
|
||||
return (ERROR);
|
||||
mem_set_zero(&self->buf[pos], self->len - pos);
|
||||
self->len = pos;
|
||||
self->buf[self->len] = '\0';
|
||||
return (NO_ERROR);
|
||||
}
|
||||
25
stdme/src/string/string_reserve.c
Normal file
25
stdme/src/string/string_reserve.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* string_reserve.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/08 22:02:49 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/08 22:04:49 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/string/string.h"
|
||||
#include "me/types.h"
|
||||
|
||||
t_error string_reserve(t_string *self, t_usize capacity)
|
||||
{
|
||||
if (self == NULL)
|
||||
return (ERROR);
|
||||
if (self->capacity >= capacity)
|
||||
return (NO_ERROR);
|
||||
self->buf = mem_realloc(self->buf, capacity);
|
||||
return (NO_ERROR);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue