New functions ds_put_uninit(), ds_put_char_multiple().
authorBen Pfaff <blp@nicira.com>
Wed, 15 Oct 2008 21:34:14 +0000 (14:34 -0700)
committerBen Pfaff <blp@nicira.com>
Thu, 23 Oct 2008 17:59:22 +0000 (10:59 -0700)
include/dynamic-string.h
lib/dynamic-string.c

index 7a25b07..7bd58cb 100644 (file)
@@ -54,7 +54,9 @@ void ds_init(struct ds *);
 void ds_clear(struct ds *);
 void ds_truncate(struct ds *, size_t new_length);
 void ds_reserve(struct ds *, size_t min_length);
+char *ds_put_uninit(struct ds *, size_t n);
 void ds_put_char(struct ds *, char);
+void ds_put_char_multiple(struct ds *, char, size_t n);
 void ds_put_cstr(struct ds *, const char *);
 void ds_put_format(struct ds *, const char *, ...) PRINTF_FORMAT(2, 3);
 void ds_put_format_valist(struct ds *, const char *, va_list)
index 3193d43..f67a63f 100644 (file)
@@ -37,6 +37,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
+#include "timeval.h"
 #include "util.h"
 
 void
@@ -72,21 +73,32 @@ ds_reserve(struct ds *ds, size_t min_length)
     }
 }
 
+char *
+ds_put_uninit(struct ds *ds, size_t n)
+{
+    ds_reserve(ds, ds->length + n);
+    ds->length += n;
+    ds->string[ds->length] = '\0';
+    return &ds->string[ds->length - n];
+}
+
 void
 ds_put_char(struct ds *ds, char c)
 {
-    ds_reserve(ds, ds->length + 1);
-    ds->string[ds->length++] = c;
-    ds->string[ds->length] = '\0';
+    *ds_put_uninit(ds, 1) = c;
+}
+
+void
+ds_put_char_multiple(struct ds *ds, char c, size_t n)
+{
+    memset(ds_put_uninit(ds, n), ' ', n);
 }
 
 void
 ds_put_cstr(struct ds *ds, const char *s)
 {
     size_t s_len = strlen(s);
-    ds_reserve(ds, ds->length + s_len);
-    memcpy(&ds->string[ds->length], s, s_len + 1);
-    ds->length += s_len;
+    memcpy(ds_put_uninit(ds, s_len), s, s_len);
 }
 
 void
@@ -145,6 +157,10 @@ ds_put_printable(struct ds *ds, const char *s, size_t n)
 void
 ds_put_strftime(struct ds *ds, const char *template, const struct tm *tm)
 {
+    if (!tm) {
+        time_t now = time_now();
+        tm = localtime(&now);
+    }
     for (;;) {
         size_t avail = ds->string ? ds->allocated - ds->length + 1 : 0;
         size_t used = strftime(&ds->string[ds->length], avail, template, tm);