1
0
forked from bip/bip

Add method to stringify a line struct, skipping first N elements

- 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 <bip@animanova.fr>
This commit is contained in:
Loïc Gomez 2024-02-04 22:44:27 +09:00
parent d19099eb3c
commit b810ba0f77
Signed by: Kyoshiro
GPG Key ID: F80C2F71E89B990A
2 changed files with 18 additions and 2 deletions

View File

@ -16,6 +16,8 @@
#include "line.h" #include "line.h"
#include "util.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 // TODO resolve assuming signed overflow does not occur when changing X +- C1
// cmp C2 to X cmp C2 -+ C1 // cmp C2 to X cmp C2 -+ C1
#pragma GCC diagnostic ignored "-Wstrict-overflow" #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) 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; size_t len = 0;
int i; int i;
@ -83,7 +98,7 @@ char *irc_line_to_string(struct line *l)
if (l->origin) if (l->origin)
len = strlen(l->origin) + 2; 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 += strlen(array_get(&l->words, i)) + 1;
len += 1; /* remove one trailing space and add \r\n */ len += 1; /* remove one trailing space and add \r\n */
len++; /* last args ":" */ len++; /* last args ":" */
@ -95,7 +110,7 @@ char *irc_line_to_string(struct line *l)
strcat(ret, l->origin); strcat(ret, l->origin);
strcat(ret, " "); 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, array_get(&l->words, i));
strcat(ret, " "); strcat(ret, " ");
} }

View File

@ -90,6 +90,7 @@ void irc_line_write(struct line *l, connection_t *c);
void irc_line_append(struct line *l, const char *s); void irc_line_append(struct line *l, const char *s);
struct line *irc_line_new_from_string(char *str); struct line *irc_line_new_from_string(char *str);
char *irc_line_to_string(struct line *l); 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); char *irc_line_to_string_to(struct line *line, char *nick);
void irc_line_free(struct line *l); void irc_line_free(struct line *l);
struct line *irc_line_dup(struct line *line); struct line *irc_line_dup(struct line *line);