summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Weiner <hannes@cmpxchg.org>2010-05-11 00:23:13 +0200
committerJohannes Weiner <hannes@cmpxchg.org>2010-05-11 00:33:42 +0200
commitba7d5097da435938de6f391dda623572ef1a21ed (patch)
tree6e6290e68cbcfdcfaf41b1ccb3ea24d3174dd656
parentb245c3c5972cdc2db0f650836ab6a9a0a23f29a3 (diff)
object: use strbuf for formatting
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
-rw-r--r--include/sheep/object_types.h3
-rw-r--r--include/sheep/string.h3
-rw-r--r--include/sheep/util.h7
-rw-r--r--lib/io.c4
-rw-r--r--sheep/alien.c6
-rw-r--r--sheep/bool.c6
-rw-r--r--sheep/function.c8
-rw-r--r--sheep/list.c10
-rw-r--r--sheep/module.c4
-rw-r--r--sheep/name.c6
-rw-r--r--sheep/number.c4
-rw-r--r--sheep/object.c4
-rw-r--r--sheep/string.c24
-rw-r--r--sheep/type.c8
-rw-r--r--sheep/util.c25
15 files changed, 61 insertions, 61 deletions
diff --git a/include/sheep/object_types.h b/include/sheep/object_types.h
index 4cf2646..027ccde 100644
--- a/include/sheep/object_types.h
+++ b/include/sheep/object_types.h
@@ -13,6 +13,7 @@ typedef struct sheep_object * sheep_t;
struct sheep_function;
struct sheep_compile;
struct sheep_context;
+struct sheep_strbuf;
struct sheep_vm;
struct sheep_sequence {
@@ -44,7 +45,7 @@ struct sheep_type {
int (*test)(sheep_t);
int (*equal)(sheep_t, sheep_t);
- void (*format)(sheep_t, char **, size_t *, int);
+ void (*format)(sheep_t, struct sheep_strbuf *, int);
const struct sheep_sequence *sequence;
};
diff --git a/include/sheep/string.h b/include/sheep/string.h
index f8e64c6..34ee427 100644
--- a/include/sheep/string.h
+++ b/include/sheep/string.h
@@ -7,6 +7,7 @@
#define _SHEEP_STRING_H
#include <sheep/object.h>
+#include <sheep/util.h>
#include <stdlib.h>
struct sheep_vm;
@@ -31,7 +32,7 @@ static inline const char *sheep_rawstring(sheep_t sheep)
return sheep_string(sheep)->bytes;
}
-void __sheep_format(sheep_t, char **, size_t *, int);
+void __sheep_format(sheep_t, struct sheep_strbuf *, int);
char *sheep_format(sheep_t);
char *sheep_repr(sheep_t);
diff --git a/include/sheep/util.h b/include/sheep/util.h
index ba8023c..2e156af 100644
--- a/include/sheep/util.h
+++ b/include/sheep/util.h
@@ -20,7 +20,12 @@ void *sheep_zalloc(size_t);
void *sheep_realloc(void *, size_t);
char *sheep_strdup(const char *);
void sheep_free(const void *);
-int sheep_addprintf(char **, size_t *, const char *, ...);
+
+struct sheep_strbuf {
+ char *bytes;
+ size_t nr_bytes;
+};
+void sheep_strbuf_addf(struct sheep_strbuf *, const char *, ...);
void __noreturn sheep_bug(const char *, ...);
diff --git a/lib/io.c b/lib/io.c
index 7d0a6e1..41a5f82 100644
--- a/lib/io.c
+++ b/lib/io.c
@@ -36,9 +36,9 @@ static void file_free(struct sheep_vm *vm, sheep_t sheep)
sheep_free(file);
}
-static void file_format(sheep_t sheep, char **bufp, size_t *posp, int repr)
+static void file_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
- sheep_addprintf(bufp, posp, "#<file '%p'>", sheep);
+ sheep_strbuf_addf(sb, "#<file '%p'>", sheep);
}
static const struct sheep_type file_type = {
diff --git a/sheep/alien.c b/sheep/alien.c
index b644f6b..18ab48b 100644
--- a/sheep/alien.c
+++ b/sheep/alien.c
@@ -27,15 +27,15 @@ static enum sheep_call alien_call(struct sheep_vm *vm, sheep_t callable,
return SHEEP_CALL_DONE;
}
-static void alien_format(sheep_t sheep, char **bufp, size_t *posp, int repr)
+static void alien_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
struct sheep_alien *alien;
alien = sheep_data(sheep);
if (alien->name)
- sheep_addprintf(bufp, posp, "#<alien '%s'>", alien->name);
+ sheep_strbuf_addf(sb, "#<alien '%s'>", alien->name);
else
- sheep_addprintf(bufp, posp, "#<alien '%p'>", alien);
+ sheep_strbuf_addf(sb, "#<alien '%p'>", alien);
}
const struct sheep_type sheep_alien_type = {
diff --git a/sheep/bool.c b/sheep/bool.c
index 24f33aa..d818ce2 100644
--- a/sheep/bool.c
+++ b/sheep/bool.c
@@ -17,12 +17,12 @@ static int bool_test(sheep_t sheep)
return sheep != &sheep_false;
}
-static void bool_format(sheep_t sheep, char **bufp, size_t *posp, int repr)
+static void bool_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
if (sheep == &sheep_false)
- sheep_addprintf(bufp, posp, "false");
+ sheep_strbuf_addf(sb, "false");
else
- sheep_addprintf(bufp, posp, "true");
+ sheep_strbuf_addf(sb, "true");
}
const struct sheep_type sheep_bool_type = {
diff --git a/sheep/function.c b/sheep/function.c
index 5ada498..b840ec6 100644
--- a/sheep/function.c
+++ b/sheep/function.c
@@ -52,15 +52,15 @@ static enum sheep_call function_call(struct sheep_vm *vm, sheep_t callable,
return SHEEP_CALL_EVAL;
}
-static void function_format(sheep_t sheep, char **bufp, size_t *posp, int repr)
+static void function_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
struct sheep_function *function;
- function = sheep_data(sheep);
+ function = sheep_function(sheep);
if (function->name)
- sheep_addprintf(bufp, posp, "#<function '%s'>", function->name);
+ sheep_strbuf_addf(sb, "#<function '%s'>", function->name);
else
- sheep_addprintf(bufp, posp, "#<function '%p'>", function);
+ sheep_strbuf_addf(sb, "#<function '%p'>", function);
}
const struct sheep_type sheep_function_type = {
diff --git a/sheep/list.c b/sheep/list.c
index 38b572b..5d5a7f8 100644
--- a/sheep/list.c
+++ b/sheep/list.c
@@ -59,21 +59,21 @@ static int list_equal(sheep_t a, sheep_t b)
return !(la->head || lb->head);
}
-static void list_format(sheep_t sheep, char **bufp, size_t *posp, int repr)
+static void list_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
struct sheep_list *list;
list = sheep_list(sheep);
- sheep_addprintf(bufp, posp, "(");
+ sheep_strbuf_addf(sb, "(");
while (list->head) {
sheep = list->head;
- __sheep_format(sheep, bufp, posp, 1);
+ __sheep_format(sheep, sb, 1);
list = sheep_list(list->tail);
if (!list->head)
break;
- sheep_addprintf(bufp, posp, " ");
+ sheep_strbuf_addf(sb, " ");
}
- sheep_addprintf(bufp, posp, ")");
+ sheep_strbuf_addf(sb, ")");
}
static size_t list_length(sheep_t sheep)
diff --git a/sheep/module.c b/sheep/module.c
index a6a901e..f1aa23f 100644
--- a/sheep/module.c
+++ b/sheep/module.c
@@ -39,11 +39,11 @@ static void module_free(struct sheep_vm *vm, sheep_t sheep)
free_module(sheep_data(sheep));
}
-static void module_format(sheep_t sheep, char **bufp, size_t *posp, int repr)
+static void module_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
struct sheep_module *mod = sheep_data(sheep);
- sheep_addprintf(bufp, posp, "#<module '%s'>", mod->name);
+ sheep_strbuf_addf(sb, "#<module '%s'>", mod->name);
}
const struct sheep_type sheep_module_type = {
diff --git a/sheep/name.c b/sheep/name.c
index e79c6f7..006e231 100644
--- a/sheep/name.c
+++ b/sheep/name.c
@@ -39,15 +39,15 @@ static int name_equal(sheep_t a, sheep_t b)
return 1;
}
-static void name_format(sheep_t sheep, char **bufp, size_t *posp, int repr)
+static void name_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
struct sheep_name *name;
unsigned int i;
name = sheep_name(sheep);
- sheep_addprintf(bufp, posp, "%s", name->parts[0]);
+ sheep_strbuf_addf(sb, "%s", name->parts[0]);
for (i = 1; i < name->nr_parts; i++)
- sheep_addprintf(bufp, posp, ":%s", name->parts[i]);
+ sheep_strbuf_addf(sb, ":%s", name->parts[i]);
}
const struct sheep_type sheep_name_type = {
diff --git a/sheep/number.c b/sheep/number.c
index 72ef74c..f61037a 100644
--- a/sheep/number.c
+++ b/sheep/number.c
@@ -21,9 +21,9 @@ static int number_test(sheep_t sheep)
return !!sheep_fixnum(sheep);
}
-static void number_format(sheep_t sheep, char **bufp, size_t *posp, int repr)
+static void number_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
- sheep_addprintf(bufp, posp, "%ld", sheep_fixnum(sheep));
+ sheep_strbuf_addf(sb, "%ld", sheep_fixnum(sheep));
}
const struct sheep_type sheep_number_type = {
diff --git a/sheep/object.c b/sheep/object.c
index 6a48fc0..fc728a1 100644
--- a/sheep/object.c
+++ b/sheep/object.c
@@ -48,9 +48,9 @@ static int test_nil(sheep_t sheep)
return 0;
}
-static void format_nil(sheep_t sheep, char **bufp, size_t *posp, int repr)
+static void format_nil(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
- sheep_addprintf(bufp, posp, "nil");
+ sheep_strbuf_addf(sb, "nil");
}
const struct sheep_type sheep_nil_type = {
diff --git a/sheep/string.c b/sheep/string.c
index c2a5d15..4f6c3ec 100644
--- a/sheep/string.c
+++ b/sheep/string.c
@@ -42,11 +42,11 @@ static int string_equal(sheep_t a, sheep_t b)
return !memcmp(sa->bytes, sb->bytes, sa->nr_bytes);
}
-static void string_format(sheep_t sheep, char **bufp, size_t *posp, int repr)
+static void string_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
const char *fmt = repr ? "\"%s\"" : "%s";
- sheep_addprintf(bufp, posp, fmt, sheep_rawstring(sheep));
+ sheep_strbuf_addf(sb, fmt, sheep_rawstring(sheep));
}
static size_t string_length(sheep_t sheep)
@@ -149,27 +149,27 @@ sheep_t sheep_make_string(struct sheep_vm *vm, const char *str)
return __sheep_make_string(vm, sheep_strdup(str), strlen(str));
}
-void __sheep_format(sheep_t sheep, char **bufp, size_t *posp, int repr)
+void __sheep_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
- sheep_type(sheep)->format(sheep, bufp, posp, repr);
+ sheep_type(sheep)->format(sheep, sb, repr);
}
char *sheep_format(sheep_t sheep)
{
- char *buf = NULL;
- size_t pos = 0;
+ struct sheep_strbuf sb;
- __sheep_format(sheep, &buf, &pos, 0);
- return buf;
+ memset(&sb, 0, sizeof(struct sheep_strbuf));
+ __sheep_format(sheep, &sb, 0);
+ return sb.bytes;
}
char *sheep_repr(sheep_t sheep)
{
- char *buf = NULL;
- size_t pos = 0;
+ struct sheep_strbuf sb;
- __sheep_format(sheep, &buf, &pos, 1);
- return buf;
+ memset(&sb, 0, sizeof(struct sheep_strbuf));
+ __sheep_format(sheep, &sb, 1);
+ return sb.bytes;
}
/* (string expression) */
diff --git a/sheep/type.c b/sheep/type.c
index 4b87d36..27f74f2 100644
--- a/sheep/type.c
+++ b/sheep/type.c
@@ -36,12 +36,12 @@ static void typeobject_free(struct sheep_vm *vm, sheep_t sheep)
sheep_free(object);
}
-static void typeobject_format(sheep_t sheep, char **bufp, size_t *posp, int repr)
+static void typeobject_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
struct sheep_typeobject *object;
object = sheep_data(sheep);
- sheep_addprintf(bufp, posp, "#<object '%p'>", object);
+ sheep_strbuf_addf(sb, "#<object '%p'>", object);
}
const struct sheep_type sheep_typeobject_type = {
@@ -88,12 +88,12 @@ static enum sheep_call typeclass_call(struct sheep_vm *vm, sheep_t callable,
return SHEEP_CALL_DONE;
}
-static void typeclass_format(sheep_t sheep, char **bufp, size_t *posp, int repr)
+static void typeclass_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
struct sheep_typeclass *class;
class = sheep_data(sheep);
- sheep_addprintf(bufp, posp, "#<type '%s'>", class->name);
+ sheep_strbuf_addf(sb, "#<type '%s'>", class->name);
}
const struct sheep_type sheep_typeclass_type = {
diff --git a/sheep/util.c b/sheep/util.c
index 395586c..26b146b 100644
--- a/sheep/util.c
+++ b/sheep/util.c
@@ -44,35 +44,28 @@ void sheep_free(const void *mem)
free((void *)mem);
}
-#define DEFAULT_BUF 64
+#define DEFAULT_BUF 64
-int sheep_addprintf(char **bufp, size_t *pos, const char *fmt, ...)
+void sheep_strbuf_addf(struct sheep_strbuf *sb, const char *fmt, ...)
{
va_list ap;
- int len;
+ size_t len;
- *bufp = sheep_realloc(*bufp, *pos + DEFAULT_BUF);
+ sb->bytes = sheep_realloc(sb->bytes, sb->nr_bytes + DEFAULT_BUF);
va_start(ap, fmt);
- len = vsnprintf(*bufp + *pos, DEFAULT_BUF, fmt, ap);
+ len = vsnprintf(sb->bytes + sb->nr_bytes, DEFAULT_BUF, fmt, ap);
va_end(ap);
- sheep_bug_on(len < 0);
-
if (len >= DEFAULT_BUF) {
- int bytes = len + 1;
-
- *bufp = sheep_realloc(*bufp, *pos + bytes);
+ sb->bytes = sheep_realloc(sb->bytes, sb->nr_bytes + len + 1);
va_start(ap, fmt);
- len = vsnprintf(*bufp + *pos, bytes, fmt, ap);
+ len = vsnprintf(sb->bytes + sb->nr_bytes, len + 1, fmt, ap);
va_end(ap);
-
- sheep_bug_on(len < 0);
- sheep_bug_on(len > bytes - 1);
}
- *pos += len;
- return len;
+
+ sb->nr_bytes += len;
}
void sheep_bug(const char *fmt, ...)