1
0
mirror of https://github.com/systemd/systemd synced 2024-07-08 20:15:55 +00:00

strbuf: make length for strbuf_add_string() optional

This commit is contained in:
Yu Watanabe 2024-06-12 03:25:57 +09:00
parent 34d4e6f32a
commit 3bc70f104e
5 changed files with 22 additions and 20 deletions

View File

@ -105,13 +105,16 @@ static void bubbleinsert(struct strbuf_node *node,
}
/* add string, return the index/offset into the buffer */
ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
ssize_t strbuf_add_string_full(struct strbuf *str, const char *s, size_t len) {
uint8_t c;
ssize_t off;
assert(str);
assert(s || len == 0);
if (len == SIZE_MAX)
len = strlen(s);
if (!str->root)
return -EINVAL;

View File

@ -33,7 +33,10 @@ struct strbuf_child_entry {
};
struct strbuf* strbuf_new(void);
ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len);
ssize_t strbuf_add_string_full(struct strbuf *str, const char *s, size_t len);
static inline ssize_t strbuf_add_string(struct strbuf *str, const char *s) {
return strbuf_add_string_full(str, s, SIZE_MAX);
}
void strbuf_complete(struct strbuf *str);
struct strbuf* strbuf_free(struct strbuf *str);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct strbuf*, strbuf_free);

View File

@ -461,7 +461,7 @@ int catalog_update(const char* database, const char* root, const char* const* di
SD_ID128_FORMAT_VAL(i->id),
isempty(i->language) ? "C" : i->language);
offset = strbuf_add_string(sb, payload, strlen(payload));
offset = strbuf_add_string(sb, payload);
if (offset < 0)
return log_oom();

View File

@ -138,15 +138,15 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
ssize_t k, v, fn = 0;
struct trie_value_entry *val;
k = strbuf_add_string(trie->strings, key, strlen(key));
k = strbuf_add_string(trie->strings, key);
if (k < 0)
return k;
v = strbuf_add_string(trie->strings, value, strlen(value));
v = strbuf_add_string(trie->strings, value);
if (v < 0)
return v;
if (!compat) {
fn = strbuf_add_string(trie->strings, filename, strlen(filename));
fn = strbuf_add_string(trie->strings, filename);
if (fn < 0)
return fn;
}
@ -224,7 +224,7 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
if (!s)
return -ENOMEM;
off = strbuf_add_string(trie->strings, s, p);
off = strbuf_add_string_full(trie->strings, s, p);
if (off < 0)
return off;
@ -254,7 +254,7 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
if (!new_child)
return -ENOMEM;
off = strbuf_add_string(trie->strings, search + i+1, strlen(search + i+1));
off = strbuf_add_string(trie->strings, search + i+1);
if (off < 0)
return off;

View File

@ -8,10 +8,6 @@
#include "strv.h"
#include "tests.h"
static ssize_t add_string(struct strbuf *sb, const char *s) {
return strbuf_add_string(sb, s, strlen(s));
}
TEST(strbuf) {
_cleanup_(strbuf_freep) struct strbuf *sb = NULL;
_cleanup_strv_free_ char **l = NULL;
@ -19,14 +15,14 @@ TEST(strbuf) {
sb = strbuf_new();
a = add_string(sb, "waldo");
b = add_string(sb, "foo");
c = add_string(sb, "bar");
d = add_string(sb, "waldo"); /* duplicate */
e = add_string(sb, "aldo"); /* duplicate */
f = add_string(sb, "do"); /* duplicate */
g = add_string(sb, "waldorf"); /* not a duplicate: matches from tail */
h = add_string(sb, "");
a = strbuf_add_string(sb, "waldo");
b = strbuf_add_string(sb, "foo");
c = strbuf_add_string(sb, "bar");
d = strbuf_add_string(sb, "waldo"); /* duplicate */
e = strbuf_add_string(sb, "aldo"); /* duplicate */
f = strbuf_add_string(sb, "do"); /* duplicate */
g = strbuf_add_string(sb, "waldorf"); /* not a duplicate: matches from tail */
h = strbuf_add_string(sb, "");
/* check the content of the buffer directly */
l = strv_parse_nulstr(sb->buf, sb->len);