Added more string utils and line stuff

This commit is contained in:
Maix0 2024-07-08 22:27:26 +02:00
parent 13ba83d6af
commit 6b5bc68de9
9 changed files with 229 additions and 151 deletions

View file

@ -6,54 +6,53 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 17:54:28 by maiboyer #+# #+# */
/* Updated: 2024/04/30 14:14:42 by maiboyer ### ########.fr */
/* Updated: 2024/07/08 21:58:11 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef STRING_H
# define STRING_H
# include "me/types.h"
#define STRING_H
#include "me/types.h"
typedef struct s_string
{
t_str buf;
t_usize capacity;
t_usize len;
} t_string;
t_str buf;
t_usize capacity;
t_usize len;
} t_string;
/// @brief Push a string to a buffer
/// @param buf the string to be pushed to
/// @param to_push the string to push
/// @return true if it failed, false otherwise
t_error string_push(t_string *buf, t_const_str to_push);
/// @return true if it failed, false otherwise
t_error string_push(t_string *buf, t_const_str to_push);
/// @brief Push a character to a buffer
/// @param buf the string to be pushed to
/// @param to_push the character to push
/// @return true if it failed, false otherwise
t_error string_push_char(t_string *buf, char to_push);
t_error string_push_char(t_string *buf, char to_push);
/// @brief Clear a string
/// @param buf the string to clear
void string_clear(t_string *buf);
void string_clear(t_string *buf);
/// @brief Create a new string
/// @param capacity the initial capacity of the string
/// @return the created string
t_string string_new(t_usize capacity);
t_string string_new(t_usize capacity);
/// @brief Make the string able to hold at least size characters if not already
/// @param buf the string to operate on
/// @param size the minimum size of the string wanted
/// @return true if it failed, false otherwise
t_error string_reserve(t_string *buf, t_usize size);
t_error string_reserve(t_string *buf, t_usize size);
/// @brief free a string
/// @param buf the string to free
static inline void string_free(t_string buf)
static inline void string_free(t_string buf)
{
void mem_free(void *);
void mem_free(void *);
mem_free(buf.buf);
}
@ -61,9 +60,9 @@ static inline void string_free(t_string buf)
/// @brief Pop a character from a string
/// @param buf the string to pop from
/// @return the popped character, or '\0' if the string is empty
static inline char string_pop(t_string *buf)
static inline char string_pop(t_string *buf)
{
char c;
char c;
c = '\0';
if (buf->buf && buf->len)
@ -74,4 +73,33 @@ static inline char string_pop(t_string *buf)
return (c);
}
/// @brief Insert a string into self
/// @param self the string to insert into
/// @param pos the index to start inserting at
/// @param str the string to insert
/// @return Error in case pos was over the string length, self was equal to NULL
/// or str was equal to NULL
t_error string_insert(t_string *self, t_usize pos, t_str str);
/// @brief Insert a char into self
/// @param self the string to insert into
/// @param pos the index to start inserting at
/// @param str the character to insert
/// @return Error in case pos was over the string length, self was equal to NULL
/// or chr was '\0'
t_error string_insert_char(t_string *self, t_usize pos, char chr);
/// @brief Clear the string, keeping everything before `pos`
/// @param self the string to operate on
/// @param pos the position to start remove from
/// @return returns an Error if self is null or pos is after the string length
t_error string_clear_after(t_string *self, t_usize pos);
/// @brief Remove a single character from the string, putting it in `out`
/// @param self the string to operate on
/// @param pos the position to start remove
/// @param out[out] the place to put the removed character, if any. Can be NULL
/// @return returns an Error if self is null or pos is after the string length
t_error string_remove(t_string *self, t_usize pos, char *out);
#endif

View file

@ -106,3 +106,6 @@ str/str_split
str/str_substring
str/str_trim
string/mod
string/string_insert
string/string_remove
string/string_reserve

View 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));
}

View 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);
}

View 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);
}