From 6b5bc68de94bfe83a2a8cb67bc3f3f077b821642 Mon Sep 17 00:00:00 2001 From: Maix0 Date: Mon, 8 Jul 2024 22:27:26 +0200 Subject: [PATCH] Added more string utils and line stuff --- line/include/line/line.h | 7 +- line/src/line.c | 181 ++++++++++-------------------- sources/main.c | 6 +- sources/signal_handler.c | 9 +- stdme/include/me/string/string.h | 64 ++++++++--- stdme/src.list | 3 + stdme/src/string/string_insert.c | 44 ++++++++ stdme/src/string/string_remove.c | 41 +++++++ stdme/src/string/string_reserve.c | 25 +++++ 9 files changed, 229 insertions(+), 151 deletions(-) create mode 100644 stdme/src/string/string_insert.c create mode 100644 stdme/src/string/string_remove.c create mode 100644 stdme/src/string/string_reserve.c diff --git a/line/include/line/line.h b/line/include/line/line.h index 583adb3f..703f5144 100644 --- a/line/include/line/line.h +++ b/line/include/line/line.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/05 18:07:02 by maiboyer #+# #+# */ -/* Updated: 2024/07/05 19:59:47 by maiboyer ### ########.fr */ +/* Updated: 2024/07/08 22:21:38 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ #define LINE_H #include "me/fs/fs.h" +#include "me/string/string.h" #include "me/types.h" #include @@ -31,13 +32,11 @@ struct s_line_state { t_fd *input_fd; /* Terminal stdin file descriptor. */ t_fd *output_fd; /* Terminal stdout file descriptor. */ - t_str buf; /* Edited line buffer. */ - t_usize buflen; /* Edited line buffer size. */ + t_string buf; /* Edited line buffer. */ t_const_str prompt; /* Prompt to display. */ t_usize prompt_len; /* Prompt length. */ t_usize pos; /* Current cursor position. */ t_usize old_pos; /* Previous refresh cursor position. */ - t_usize len; /* Current edited line length. */ t_usize columns; /* Number of columns in terminal. */ t_usize old_rows; /* Rows used by last refrehsed line (multiline mode) */ t_i32 history_index; /* The history index we are currently editing. */ diff --git a/line/src/line.c b/line/src/line.c index 221b75a9..e1eaf406 100644 --- a/line/src/line.c +++ b/line/src/line.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 16:53:27 by maiboyer #+# #+# */ -/* Updated: 2024/07/08 19:42:52 by maiboyer ### ########.fr */ +/* Updated: 2024/07/08 22:23:24 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -142,7 +142,7 @@ t_error linenoise_get_cursor_position(t_fd *input, t_fd *output, t_u32 *column_o buf[i] = '\0'; /* Parse it. */ - if (buf[0] != K_ESC || buf[1] != '[') + if (buf[0] != K_ESC && buf[1] != '[') return (ERROR); if (sscanf(buf + 2, "%d;%d", &rows, &cols) != 2) return (ERROR); @@ -165,14 +165,12 @@ t_u32 linenoise_get_columns(t_fd *input, t_fd *output) return (80); /* Go to right margin and get position. */ - me_eprintf("going to the right;\n"); me_printf_fd(output, "\x1b[999C"); if (linenoise_get_cursor_position(input, output, &cols)) return (80); if (cols > start) { - me_eprintf("reseting col;\n"); me_printf_fd(output, "\x1b[%dD", cols - start); } return (cols); @@ -184,7 +182,6 @@ t_u32 linenoise_get_columns(t_fd *input, t_fd *output) /* Clear the screen. Used to handle ctrl+l */ void linenoise_clear_screen(t_fd *output) { - me_eprintf("clear screen;\n"); me_printf_fd(output, "\x1b[H\x1b[2J"); } @@ -293,23 +290,13 @@ t_usize linenoise_get_prompt_len(t_const_str s) * prompt, just write it, or both. */ void linenoise_refresh_single_line(t_line_state *l, t_line_flags flags) { - size_t prompt_len = linenoise_get_prompt_len(l->prompt); - t_str input_buf = l->buf; - size_t cursor_pos = l->pos; t_string str; - str = string_new(16); + str = string_new(64); string_push(&str, "\r\x1b[2K"); - if (flags & REFRESH_WRITE) - { - /* Write the prompt and the current buffer content */ - string_push(&str, l->prompt); - string_push(&str, input_buf); // , len); - me_printf_str(&str, "\x1b[0G\x1b[%uC", cursor_pos + prompt_len); - } + me_printf_str(&str, "%s%s\x1b[0G\x1b[%uC", l->prompt, l->buf.buf, l->pos + linenoise_get_prompt_len(l->prompt)); me_printf_fd(l->output_fd, "%s", str.buf); - string_free(str); } @@ -343,26 +330,15 @@ void linenoise_show(t_line_state *l) * On error writing to the terminal -1 is returned, otherwise 0. */ t_error linenoise_edit_insert(t_line_state *l, char c) { - if (l->len < l->buflen) + if (l->pos == l->buf.len) { - if (l->len == l->pos) - { - l->buf[l->pos] = c; - l->pos++; - l->len++; - l->buf[l->len] = '\0'; - linenoise_refresh_line(l); - } - else - { - memmove(l->buf + l->pos + 1, l->buf + l->pos, l->len - l->pos); - l->buf[l->pos] = c; - l->len++; - l->pos++; - l->buf[l->len] = '\0'; - linenoise_refresh_line(l); - } + if (string_push_char(&l->buf, c)) + return (ERROR); } + else if (string_insert_char(&l->buf, l->pos, c)) + return (ERROR); + l->pos++; + linenoise_refresh_line(l); return (NO_ERROR); } @@ -379,7 +355,7 @@ void linenoise_edit_move_left(t_line_state *l) /* Move cursor on the right. */ void linenoise_edit_move_right(t_line_state *l) { - if (l->pos != l->len) + if (l->pos != l->buf.len) { l->pos++; linenoise_refresh_line(l); @@ -387,7 +363,7 @@ void linenoise_edit_move_right(t_line_state *l) } /* Move cursor to the start of the line. */ -void linenoiseEditMoveHome(t_line_state *l) +void linenoise_edit_move_home(t_line_state *l) { if (l->pos != 0) { @@ -399,9 +375,9 @@ void linenoiseEditMoveHome(t_line_state *l) /* Move cursor to the end of the line. */ void linenoise_edit_move_end(t_line_state *l) { - if (l->pos != l->len) + if (l->pos != l->buf.len) { - l->pos = l->len; + l->pos = l->buf.len; linenoise_refresh_line(l); } } @@ -421,7 +397,7 @@ void linenoise_edit_history_next(t_line_state *l, int dir) /* Update the current history entry before to * overwrite it with the next one. */ mem_free(history->buffer[history->len - 1 - l->history_index]); - history->buffer[history->len - 1 - l->history_index] = strdup(l->buf); + history->buffer[history->len - 1 - l->history_index] = str_clone(l->buf.buf); /* Show the new entry */ l->history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1; if (l->history_index < 0) @@ -434,9 +410,9 @@ void linenoise_edit_history_next(t_line_state *l, int dir) l->history_index = history->len - 1; return; } - strncpy(l->buf, history->buffer[history->len - 1 - l->history_index], l->buflen); - l->buf[l->buflen - 1] = '\0'; - l->len = l->pos = strlen(l->buf); + string_clear(&l->buf); + string_push(&l->buf, history->buffer[history->len - 1 - l->history_index]); + l->pos = l->buf.len; linenoise_refresh_line(l); } } @@ -445,45 +421,21 @@ void linenoise_edit_history_next(t_line_state *l, int dir) * position. Basically this is what happens with the "Delete" keyboard key. */ void linenoise_edit_delete(t_line_state *l) { - if (l->len > 0 && l->pos < l->len) - { - memmove(l->buf + l->pos, l->buf + l->pos + 1, l->len - l->pos - 1); - l->len--; - l->buf[l->len] = '\0'; - linenoise_refresh_line(l); - } + string_remove(&l->buf, l->pos, NULL); + linenoise_refresh_line(l); } /* Backspace implementation. */ void linenoise_edit_backspace(t_line_state *l) { - if (l->pos > 0 && l->len > 0) + if (l->pos > 0) { - memmove(l->buf + l->pos - 1, l->buf + l->pos, l->len - l->pos); + string_remove(&l->buf, l->pos - 1, NULL); l->pos--; - l->len--; - l->buf[l->len] = '\0'; linenoise_refresh_line(l); } } -/* Delete the previosu word, maintaining the cursor at the start of the - * current word. */ -void linenoise_edit_delete_prev_word(t_line_state *l) -{ - size_t old_pos = l->pos; - size_t diff; - - while (l->pos > 0 && l->buf[l->pos - 1] == ' ') - l->pos--; - while (l->pos > 0 && l->buf[l->pos - 1] != ' ') - l->pos--; - diff = old_pos - l->pos; - memmove(l->buf + l->pos, l->buf + old_pos, l->len - old_pos + 1); - l->len -= diff; - linenoise_refresh_line(l); -} - /* This function is part of the multiplexed API of Linenoise, that is used * in order to implement the blocking variant of the API but can also be * called by the user directly in an event driven program. It will: @@ -508,18 +460,22 @@ void linenoise_edit_delete_prev_word(t_line_state *l) * fails. If stdin_fd or stdout_fd are set to -1, the default is to use * STDIN_FILENO and STDOUT_FILENO. */ -t_error linenoise_edit_start(t_line_state *l, t_fd *stdin_fd, t_fd *stdout_fd, char *buf, size_t buflen, t_const_str prompt) +t_error linenoise_edit_start(t_line_state *l, t_fd *stdin_fd, t_fd *stdout_fd, t_string buf, t_const_str prompt) { + if (stdin_fd == NULL) + stdin_fd = get_stdin(); + if (stdout_fd == NULL) + stdout_fd = get_stdout(); + /* Populate the linenoise state that we pass to functions implementing * specific editing functionalities. */ l->input_fd = stdin_fd; l->output_fd = stdout_fd; l->buf = buf; - l->buflen = buflen; l->prompt = prompt; l->prompt_len = str_len(l->prompt); - l->old_pos = l->pos = 0; - l->len = 0; + l->old_pos = 0; + l->pos = 0; // /* Enter raw mode. */ // if (enableRawMode(l->ifd) == -1) @@ -529,15 +485,11 @@ t_error linenoise_edit_start(t_line_state *l, t_fd *stdin_fd, t_fd *stdout_fd, c l->old_rows = 0; l->history_index = 0; - /* Buffer starts empty. */ - l->buf[0] = '\0'; - l->buflen--; /* Make sure there is always space for the nulterm */ - /* If stdin is not a tty, stop here with the initialization. We * will actually just read a line from standard input in blocking * mode later, in linenoiseEditFeed(). */ if (!isatty(l->input_fd->fd)) - return 0; + return (NO_ERROR); /* The latest history entry is always our current buffer, that * initially is just an empty string. */ @@ -582,7 +534,7 @@ t_str linenoise_edit_feed(t_line_state *l) char seq[3]; t_vec_str *history = get_history(); if (read_fd(l->input_fd, (t_u8 *)&c, 1, &nread)) - return (printf("null1\n"), NULL); + return (NULL); switch (c) { @@ -590,7 +542,7 @@ t_str linenoise_edit_feed(t_line_state *l) case K_ENTER: /* enter */ history->len--; mem_free(history->buffer[history->len]); - return strdup(l->buf); + return str_clone(l->buf.buf); case K_CTRL_C: /* ctrl-c */ errno = EAGAIN; return NULL; @@ -600,7 +552,7 @@ t_str linenoise_edit_feed(t_line_state *l) break; case K_CTRL_D: /* ctrl-d, remove char at right of cursor, or if the line is empty, act as end-of-file. */ - if (l->len > 0) + if (l->buf.len > 0) linenoise_edit_delete(l); else { @@ -627,15 +579,9 @@ t_str linenoise_edit_feed(t_line_state *l) * Use two calls to handle slow terminals returning the two * chars at different times. */ if (read_fd(l->input_fd, (t_u8 *)seq, 1, NULL)) - { - printf("Failed read\n"); break; - } if (read_fd(l->input_fd, (t_u8 *)(seq + 1), 1, NULL)) - { - printf("Failed read2\n"); break; - } /* ESC [ sequences. */ if (seq[0] == '[') @@ -647,6 +593,8 @@ t_str linenoise_edit_feed(t_line_state *l) break; if (seq[1] == '3' && seq[2] == '~') linenoise_edit_delete(l); + if (seq[1] == '1' && seq[2] == ';') + me_printf("Something"); } else { @@ -665,7 +613,7 @@ t_str linenoise_edit_feed(t_line_state *l) linenoise_edit_move_left(l); break; case 'H': /* Home */ - linenoiseEditMoveHome(l); + linenoise_edit_move_home(l); break; case 'F': /* End*/ linenoise_edit_move_end(l); @@ -680,7 +628,7 @@ t_str linenoise_edit_feed(t_line_state *l) switch (seq[1]) { case 'H': /* Home */ - linenoiseEditMoveHome(l); + linenoise_edit_move_home(l); break; case 'F': /* End*/ linenoise_edit_move_end(l); @@ -689,17 +637,16 @@ t_str linenoise_edit_feed(t_line_state *l) } break; case K_CTRL_U: /* Ctrl+u, delete the whole line. */ - l->buf[0] = '\0'; - l->pos = l->len = 0; + string_clear(&l->buf); + l->pos = 0; linenoise_refresh_line(l); break; case K_CTRL_K: /* Ctrl+k, delete from current to end of line. */ - l->buf[l->pos] = '\0'; - l->len = l->pos; + string_clear_after(&l->buf, l->pos); linenoise_refresh_line(l); break; case K_CTRL_A: /* Ctrl+a, go to the start of the line */ - linenoiseEditMoveHome(l); + linenoise_edit_move_home(l); break; case K_CTRL_E: /* ctrl+e, go to the end of the line */ linenoise_edit_move_end(l); @@ -708,12 +655,9 @@ t_str linenoise_edit_feed(t_line_state *l) linenoise_clear_screen(l->output_fd); linenoise_refresh_line(l); break; - case K_CTRL_W: /* ctrl+w, delete previous word */ - linenoise_edit_delete_prev_word(l); - break; default: if (linenoise_edit_insert(l, c)) - return NULL; + return (printf("lool\n"), NULL); break; } return linenoiseEditMore; @@ -723,38 +667,35 @@ t_str linenoise_edit_feed(t_line_state *l) * for more information. This function is called when linenoiseEditFeed() * returns something different than NULL. At this point the user input * is in the buffer, and we can restore the terminal in normal mode. */ -void linenoiseEditStop(t_line_state *l) +void linenoise_edit_stop(t_line_state *l) { if (!isatty(l->input_fd->fd)) return; linenoise_disable_raw_mode(l->input_fd); me_printf_fd(l->output_fd, "\n"); + string_free(l->buf); } /* This just implements a blocking loop for the multiplexed API. * In many applications that are not event-drivern, we can just call * the blocking linenoise API, wait for the user to complete the editing * and return the buffer. */ -char *linenoise_blocking_edit(t_fd *stdin_fd, t_fd *stdout_fd, char *buf, size_t buflen, t_const_str prompt) +char *linenoise_blocking_edit(t_fd *stdin_fd, t_fd *stdout_fd, t_string buf, t_const_str prompt) { t_line_state l; - /* Editing without a buffer is invalid. */ - if (buflen == 0) - return (errno = EINVAL, NULL); - - linenoise_edit_start(&l, stdin_fd, stdout_fd, buf, buflen, prompt); + linenoise_edit_start(&l, stdin_fd, stdout_fd, buf, prompt); char *res; while ((res = linenoise_edit_feed(&l)) == linenoiseEditMore) ; // printf("edit\n"); - linenoiseEditStop(&l); + linenoise_edit_stop(&l); return res; } /* This special mode is used by linenoise in order to print scan codes * on screen for debugging / development purposes. It is implemented * by the linenoise_example program using the --keycodes option. */ -void linenoisePrintKeyCodes(void) +void linenoise_print_key_codes(void) { char quit[4]; @@ -762,18 +703,19 @@ void linenoisePrintKeyCodes(void) "Press keys to see scan codes. Type 'quit' at any time to exit.\n"); // if (enableRawMode(STDIN_FILENO) == -1) // return; - memset(quit, ' ', 4); + mem_set(quit, ' ', 4); while (1) { - char c; - int nread; + char c; + t_isize nread; - nread = read(STDIN_FILENO, &c, 1); + if (read_fd(get_stdin(), (void *)&c, 1, &nread)) + continue; if (nread <= 0) continue; - memmove(quit, quit + 1, sizeof(quit) - 1); /* shift string to left. */ - quit[sizeof(quit) - 1] = c; /* Insert current char on the right. */ - if (memcmp(quit, "quit", sizeof(quit)) == 0) + mem_move(quit, quit + 1, sizeof(quit) - 1); /* shift string to left. */ + quit[sizeof(quit) - 1] = c; /* Insert current char on the right. */ + if (mem_compare(quit, "quit", sizeof(quit))) break; printf("'%c' %02x (%d) (type quit to exit)\n", isprint(c) ? c : '?', (int)c, (int)c); @@ -812,20 +754,17 @@ t_str linenoise_no_tty_impl(void) * for a blacklist of stupid terminals, and later either calls the line * editing function or uses dummy fgets() so that you will be able to type * something even in the most desperate of the conditions. */ -t_str linenoise(const char *prompt) +t_str linenoise(t_const_str prompt) { - char buf[4096]; t_str retval; if (!isatty(get_stdin()->fd)) { - printf("not_tty\n"); /* Not a tty: read from file / pipe. In this mode we don't want any * limit to the line size, so we call a function to handle that. */ return linenoise_no_tty_impl(); } - printf("is_tty\n"); - retval = linenoise_blocking_edit(get_stdin(), get_stdout(), buf, 4096, prompt); + retval = linenoise_blocking_edit(get_stdin(), get_stdout(), string_new(1024), prompt); return (retval); } diff --git a/sources/main.c b/sources/main.c index dced5764..8ba88432 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */ -/* Updated: 2024/07/08 17:02:15 by maiboyer ### ########.fr */ +/* Updated: 2024/07/08 21:11:43 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -132,8 +132,8 @@ void ft_take_args(t_utils *shcat) cmd = linenoise((t_const_str)shcat->name_shell); if (cmd == NULL) ft_exit(shcat, 0); - shcat->str_input = str_clone(cmd); - free(cmd); + shcat->str_input = cmd; + linenoise_history_add(shcat->str_input); shcat->current_node = parse_str(&shcat->parser, shcat->str_input); exec_shcat(shcat); mem_free(shcat->str_input); diff --git a/sources/signal_handler.c b/sources/signal_handler.c index fdb8e4fe..1fdea570 100644 --- a/sources/signal_handler.c +++ b/sources/signal_handler.c @@ -6,14 +6,14 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 13:22:14 by maiboyer #+# #+# */ -/* Updated: 2024/07/08 15:54:21 by maiboyer ### ########.fr */ +/* Updated: 2024/07/08 21:19:35 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "app/signal_handler.h" +#include "me/fs/fs.h" #include "me/printf/printf.h" #include "me/types.h" -#include "readline/readline.h" #include #include @@ -22,7 +22,7 @@ void sigint_handle(int sig, siginfo_t *info, void *ucontext) (void)(sig); (void)(info); (void)(ucontext); - printf("\n"); + write_fd(get_stdout(), (void *)"\n", 1, NULL); // TODO: change this to the linenoise verison // rl_replace_line("", 0); // rl_on_new_line(); @@ -34,8 +34,7 @@ void sigquit_handle(int sig, siginfo_t *info, void *ucontext) (void)(sig); (void)(info); (void)(ucontext); - printf("\n"); - // + write_fd(get_stdout(), (void *)"\n", 1, NULL); // rl_replace_line("", 0); // rl_on_new_line(); // rl_redisplay(); diff --git a/stdme/include/me/string/string.h b/stdme/include/me/string/string.h index ce59fab9..e4b8748e 100644 --- a/stdme/include/me/string/string.h +++ b/stdme/include/me/string/string.h @@ -6,54 +6,53 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/stdme/src.list b/stdme/src.list index f1f7cb6d..d0bc717f 100644 --- a/stdme/src.list +++ b/stdme/src.list @@ -106,3 +106,6 @@ str/str_split str/str_substring str/str_trim string/mod +string/string_insert +string/string_remove +string/string_reserve diff --git a/stdme/src/string/string_insert.c b/stdme/src/string/string_insert.c new file mode 100644 index 00000000..cb36d233 --- /dev/null +++ b/stdme/src/string/string_insert.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* string_insert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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)); +} diff --git a/stdme/src/string/string_remove.c b/stdme/src/string/string_remove.c new file mode 100644 index 00000000..442f45b8 --- /dev/null +++ b/stdme/src/string/string_remove.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* string_remove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/stdme/src/string/string_reserve.c b/stdme/src/string/string_reserve.c new file mode 100644 index 00000000..63c1d5b6 --- /dev/null +++ b/stdme/src/string/string_reserve.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* string_reserve.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +}