[or-cvs] Add a slightly trickier string-join interface for making NU...

Nick Mathewson nickm at seul.org
Wed Nov 3 18:28:02 UTC 2004


Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv12701/src/common

Modified Files:
	container.c container.h 
Log Message:
Add a slightly trickier string-join interface for making NUL-terminated string messages

Index: container.c
===================================================================
RCS file: /home/or/cvsroot/src/common/container.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- container.c	3 Nov 2004 07:29:03 -0000	1.2
+++ container.c	3 Nov 2004 18:28:00 -0000	1.3
@@ -301,32 +301,49 @@
 /** Allocate and return a new string containing the concatenation of
  * the elements of <b>sl</b>, in order, separated by <b>join</b>.  If
  * <b>terminate</b> is true, also terminate the string with <b>join</b>.
- * Requires that every element of <b>sl</b> is NUL-terminated string.
+ * If <b>len_out</b> is not NULL, set <b>len_out</b> to the length of
+ * the returned string. Requires that every element of <b>sl</b> is
+ * NUL-terminated string.
  */
-char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate)
+char *smartlist_join_strings(smartlist_t *sl, const char *join,
+                             int terminate, size_t *len_out)
+{
+  return smartlist_join_strings2(sl,join,strlen(join),terminate,len_out);
+}
+
+/** As smartlist_join_strings2, but instead of separating/terminated with a
+ * NUL-terminated string <b>join</b>, uses the <b>join_len</b>-byte sequence
+ * at <b>join</b>.  (Useful for generating a sequenct of NUL-terminated
+ * strings.)
+ */
+char *smartlist_join_strings2(smartlist_t *sl, const char *join,
+                              size_t join_len, int terminate, size_t *len_out)
 {
   int i;
-  size_t n = 0, jlen;
+  size_t n = 0;
   char *r = NULL, *dst, *src;
 
   tor_assert(sl);
   tor_assert(join);
-  jlen = strlen(join);
+  join_len = strlen(join);
   for (i = 0; i < sl->num_used; ++i) {
     n += strlen(sl->list[i]);
-    n += jlen;
+    n += join_len;
   }
-  if (!terminate) n -= jlen;
+  if (!terminate) n -= join_len;
   dst = r = tor_malloc(n+1);
   for (i = 0; i < sl->num_used; ) {
     for (src = sl->list[i]; *src; )
       *dst++ = *src++;
     if (++i < sl->num_used || terminate) {
-      memcpy(dst, join, jlen);
-      dst += jlen;
+      memcpy(dst, join, join_len);
+      dst += join_len;
     }
   }
   *dst = '\0';
+
+  if (len_out)
+    *len_out = dst-r;
   return r;
 }
 

Index: container.h
===================================================================
RCS file: /home/or/cvsroot/src/common/container.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- container.h	2 Nov 2004 02:28:42 -0000	1.2
+++ container.h	3 Nov 2004 18:28:00 -0000	1.3
@@ -32,7 +32,10 @@
 #define SPLIT_IGNORE_BLANK 0x02
 int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
                            int flags, int max);
-char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate);
+char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
+                             size_t *len_out);
+char *smartlist_join_strings2(smartlist_t *sl, const char *join,
+                              size_t join_len, int terminate, size_t *len_out);
 
 #define SMARTLIST_FOREACH(sl, type, var, cmd)                   \
   do {                                                          \



More information about the tor-commits mailing list