summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Weiner <hannes@cmpxchg.org>2010-05-11 00:31:17 +0200
committerJohannes Weiner <hannes@cmpxchg.org>2010-05-11 00:37:20 +0200
commit38fb0e580a7fe6912eb6cfaa7d293d492828ec46 (patch)
tree31580825968a028e1103e67f1c50bd25aa0b4832
parentba7d5097da435938de6f391dda623572ef1a21ed (diff)
util: non-formatting strbuf appending
Use this in places where simple strings are appended and the power of printf() style formatting is unneeded overhead. Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
-rw-r--r--include/sheep/util.h1
-rw-r--r--sheep/bool.c4
-rw-r--r--sheep/list.c6
-rw-r--r--sheep/name.c2
-rw-r--r--sheep/object.c2
-rw-r--r--sheep/string.c7
-rw-r--r--sheep/util.c9
7 files changed, 21 insertions, 10 deletions
diff --git a/include/sheep/util.h b/include/sheep/util.h
index 2e156af..80bd187 100644
--- a/include/sheep/util.h
+++ b/include/sheep/util.h
@@ -25,6 +25,7 @@ struct sheep_strbuf {
char *bytes;
size_t nr_bytes;
};
+void sheep_strbuf_add(struct sheep_strbuf *, const char *);
void sheep_strbuf_addf(struct sheep_strbuf *, const char *, ...);
void __noreturn sheep_bug(const char *, ...);
diff --git a/sheep/bool.c b/sheep/bool.c
index d818ce2..0d73cfc 100644
--- a/sheep/bool.c
+++ b/sheep/bool.c
@@ -20,9 +20,9 @@ static int bool_test(sheep_t sheep)
static void bool_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
if (sheep == &sheep_false)
- sheep_strbuf_addf(sb, "false");
+ sheep_strbuf_add(sb, "false");
else
- sheep_strbuf_addf(sb, "true");
+ sheep_strbuf_add(sb, "true");
}
const struct sheep_type sheep_bool_type = {
diff --git a/sheep/list.c b/sheep/list.c
index 5d5a7f8..86fccb9 100644
--- a/sheep/list.c
+++ b/sheep/list.c
@@ -64,16 +64,16 @@ static void list_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
struct sheep_list *list;
list = sheep_list(sheep);
- sheep_strbuf_addf(sb, "(");
+ sheep_strbuf_add(sb, "(");
while (list->head) {
sheep = list->head;
__sheep_format(sheep, sb, 1);
list = sheep_list(list->tail);
if (!list->head)
break;
- sheep_strbuf_addf(sb, " ");
+ sheep_strbuf_add(sb, " ");
}
- sheep_strbuf_addf(sb, ")");
+ sheep_strbuf_add(sb, ")");
}
static size_t list_length(sheep_t sheep)
diff --git a/sheep/name.c b/sheep/name.c
index 006e231..829d868 100644
--- a/sheep/name.c
+++ b/sheep/name.c
@@ -45,7 +45,7 @@ static void name_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
unsigned int i;
name = sheep_name(sheep);
- sheep_strbuf_addf(sb, "%s", name->parts[0]);
+ sheep_strbuf_add(sb, name->parts[0]);
for (i = 1; i < name->nr_parts; i++)
sheep_strbuf_addf(sb, ":%s", name->parts[i]);
}
diff --git a/sheep/object.c b/sheep/object.c
index fc728a1..59cf8e6 100644
--- a/sheep/object.c
+++ b/sheep/object.c
@@ -50,7 +50,7 @@ static int test_nil(sheep_t sheep)
static void format_nil(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
- sheep_strbuf_addf(sb, "nil");
+ sheep_strbuf_add(sb, "nil");
}
const struct sheep_type sheep_nil_type = {
diff --git a/sheep/string.c b/sheep/string.c
index 4f6c3ec..3063d41 100644
--- a/sheep/string.c
+++ b/sheep/string.c
@@ -44,9 +44,10 @@ static int string_equal(sheep_t a, sheep_t b)
static void string_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
- const char *fmt = repr ? "\"%s\"" : "%s";
-
- sheep_strbuf_addf(sb, fmt, sheep_rawstring(sheep));
+ if (repr)
+ sheep_strbuf_addf(sb, "\"%s\"", sheep_rawstring(sheep));
+ else
+ sheep_strbuf_add(sb, sheep_rawstring(sheep));
}
static size_t string_length(sheep_t sheep)
diff --git a/sheep/util.c b/sheep/util.c
index 26b146b..c870926 100644
--- a/sheep/util.c
+++ b/sheep/util.c
@@ -44,6 +44,15 @@ void sheep_free(const void *mem)
free((void *)mem);
}
+void sheep_strbuf_add(struct sheep_strbuf *sb, const char *str)
+{
+ size_t len;
+
+ len = strlen(str);
+ sb->bytes = sheep_realloc(sb->bytes, sb->nr_bytes + len + 1);
+ memcpy(sb->bytes + sb->nr_bytes, str, len + 1);
+}
+
#define DEFAULT_BUF 64
void sheep_strbuf_addf(struct sheep_strbuf *sb, const char *fmt, ...)