From 17c0ec4f337d5423e9d0622c3430f72ddd78865c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Gomez?= Date: Sun, 4 Feb 2024 22:44:27 +0900 Subject: [PATCH] Add method to stringify a line struct, skipping first N elements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - adds an internal method _irc_line_to_string with current irc_line_to_string code, adding capability to skip the first N elements - call this internal method from irc_line_to_string with N=0 - call this internal method from new irc_line_to_string_skip method Signed-off-by: Loïc Gomez --- src/line.c | 19 +++++++++++++++++-- src/line.h | 1 + 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/line.c b/src/line.c index 34cc19d..6fe7d89 100644 --- a/src/line.c +++ b/src/line.c @@ -16,6 +16,8 @@ #include "line.h" #include "util.h" +char *_irc_line_to_string(struct line *l, int skip_first); + // TODO resolve assuming signed overflow does not occur when changing X +- C1 // cmp C2 to X cmp C2 -+ C1 #pragma GCC diagnostic ignored "-Wstrict-overflow" @@ -76,6 +78,19 @@ void irc_line_append(struct line *l, const char *s) } char *irc_line_to_string(struct line *l) +{ + return _irc_line_to_string(l, 0); +} + +char *irc_line_to_string_skip(struct line *l, int skip_first) +{ + if (skip_first >= irc_line_count(l)) { + return NULL; + } + return _irc_line_to_string(l, skip_first); +} + +char *_irc_line_to_string(struct line *l, int skip_first) { size_t len = 0; int i; @@ -83,7 +98,7 @@ char *irc_line_to_string(struct line *l) if (l->origin) len = strlen(l->origin) + 2; - for (i = 0; i < array_count(&l->words); i++) + for (i = skip_first; i < array_count(&l->words); i++) len += strlen(array_get(&l->words, i)) + 1; len += 1; /* remove one trailing space and add \r\n */ len++; /* last args ":" */ @@ -95,7 +110,7 @@ char *irc_line_to_string(struct line *l) strcat(ret, l->origin); strcat(ret, " "); } - for (i = 0; i < array_count(&l->words) - 1; i++) { + for (i = skip_first; i < array_count(&l->words) - 1; i++) { strcat(ret, array_get(&l->words, i)); strcat(ret, " "); } diff --git a/src/line.h b/src/line.h index 2965766..daef7f1 100644 --- a/src/line.h +++ b/src/line.h @@ -90,6 +90,7 @@ void irc_line_write(struct line *l, connection_t *c); void irc_line_append(struct line *l, const char *s); struct line *irc_line_new_from_string(char *str); char *irc_line_to_string(struct line *l); +char *irc_line_to_string_skip(struct line *l, int skip_first); char *irc_line_to_string_to(struct line *line, char *nick); void irc_line_free(struct line *l); struct line *irc_line_dup(struct line *line);