Author: kotkov
Date: Thu Aug 24 13:16:38 2017
New Revision: 1806041

URL: http://svn.apache.org/viewvc?rev=1806041&view=rev
Log:
Introduce the svn_cstring_join2() API that allows selecting if the
trailing separator is required in the joined string.

Previously, svn_cstring_join() has been always appending the trailing
separator, and that required us to sometimes manually strip that final
separator from the result.

* subversion/include/svn_string.h
  (svn_cstring_join2): New, revved from ...
  (svn_cstring_join): ...this function, which is now deprecated.

* subversion/libsvn_subr/string.c
  (svn_cstring_join2): New, supports the new 'trailing_separator' argument.
  (svn_cstring_join): Move ...

* subversion/libsvn_subr/deprecated.c
  (svn_cstring_join): ...here and call svn_cstring_join2() with the
   'trailing_separator' set to true, to match the original behavior.

* subversion/tests/libsvn_subr/string-test.c
  (test_cstring_join): New test.
  (test_funcs): Add new test.

* subversion/libsvn_repos/hooks.c
  (svn_repos__hooks_start_commit): Use svn_cstring_join2() without
   the trailing separator, instead of manually stripping it from the
   resulting string.
  (svn_repos__hooks_post_lock, svn_repos__hooks_post_unlock):
   Switch to svn_cstring_join2(), request the trailing separator as
   before.

* tools/dev/svnmover/svnmover.c
  (execute): Switch to svn_cstring_join2(), request the trailing separator
   as before.

Modified:
    subversion/trunk/subversion/include/svn_string.h
    subversion/trunk/subversion/libsvn_repos/hooks.c
    subversion/trunk/subversion/libsvn_subr/deprecated.c
    subversion/trunk/subversion/libsvn_subr/string.c
    subversion/trunk/subversion/tests/libsvn_subr/string-test.c
    subversion/trunk/tools/dev/svnmover/svnmover.c

Modified: subversion/trunk/subversion/include/svn_string.h
URL: 
http://svn.apache.org/viewvc/subversion/trunk/subversion/include/svn_string.h?rev=1806041&r1=1806040&r2=1806041&view=diff
==============================================================================
--- subversion/trunk/subversion/include/svn_string.h (original)
+++ subversion/trunk/subversion/include/svn_string.h Thu Aug 24 13:16:38 2017
@@ -533,9 +533,25 @@ svn_cstring_count_newlines(const char *m
  * of char *) each followed by @a separator (that is, @a separator
  * will also end the resulting string).  Allocate the result in @a pool.
  * If @a strings is empty, then return the empty string.
+ * If @a trailing_separator is non-zero, also append the separator
+ * after the last joined element.
+ *
+ * @since New in 1.10.
+ */
+char *
+svn_cstring_join2(const apr_array_header_t *strings,
+                  const char *separator,
+                  svn_boolean_t trailing_separator,
+                  apr_pool_t *pool);
+
+/**
+ * Similar to svn_cstring_join2(), but always includes the trailing
+ * separator.
  *
  * @since New in 1.2.
+ * @deprecated Provided for backwards compatibility with the 1.9 API.
  */
+SVN_DEPRECATED
 char *
 svn_cstring_join(const apr_array_header_t *strings,
                  const char *separator,

Modified: subversion/trunk/subversion/libsvn_repos/hooks.c
URL: 
http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_repos/hooks.c?rev=1806041&r1=1806040&r2=1806041&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_repos/hooks.c (original)
+++ subversion/trunk/subversion/libsvn_repos/hooks.c Thu Aug 24 13:16:38 2017
@@ -476,11 +476,8 @@ svn_repos__hooks_start_commit(svn_repos_
 
       if (capabilities)
         {
-          capabilities_string = svn_cstring_join(capabilities, ":", pool);
-
-          /* Get rid of that annoying final colon. */
-          if (capabilities_string[0])
-            capabilities_string[strlen(capabilities_string) - 1] = '\0';
+          capabilities_string = svn_cstring_join2(capabilities, ":",
+                                                  FALSE, pool);
         }
       else
         {
@@ -799,8 +796,8 @@ svn_repos__hooks_post_lock(svn_repos_t *
     {
       const char *args[5];
       apr_file_t *stdin_handle = NULL;
-      svn_string_t *paths_str = svn_string_create(svn_cstring_join
-                                                  (paths, "\n", pool),
+      svn_string_t *paths_str = svn_string_create(svn_cstring_join2
+                                                  (paths, "\n", TRUE, pool),
                                                   pool);
 
       SVN_ERR(create_temp_file(&stdin_handle, paths_str, pool));
@@ -875,8 +872,8 @@ svn_repos__hooks_post_unlock(svn_repos_t
     {
       const char *args[5];
       apr_file_t *stdin_handle = NULL;
-      svn_string_t *paths_str = svn_string_create(svn_cstring_join
-                                                  (paths, "\n", pool),
+      svn_string_t *paths_str = svn_string_create(svn_cstring_join2
+                                                  (paths, "\n", TRUE, pool),
                                                   pool);
 
       SVN_ERR(create_temp_file(&stdin_handle, paths_str, pool));

Modified: subversion/trunk/subversion/libsvn_subr/deprecated.c
URL: 
http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_subr/deprecated.c?rev=1806041&r1=1806040&r2=1806041&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_subr/deprecated.c (original)
+++ subversion/trunk/subversion/libsvn_subr/deprecated.c Thu Aug 24 13:16:38 
2017
@@ -1592,3 +1592,12 @@ svn_base64_encode(svn_stream_t *output,
 {
   return svn_base64_encode2(output, TRUE, pool);
 }
+
+/*** From string.c ***/
+char *
+svn_cstring_join(const apr_array_header_t *strings,
+                 const char *separator,
+                 apr_pool_t *pool)
+{
+  return svn_cstring_join2(strings, separator, TRUE, pool);
+}

Modified: subversion/trunk/subversion/libsvn_subr/string.c
URL: 
http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_subr/string.c?rev=1806041&r1=1806040&r2=1806041&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_subr/string.c (original)
+++ subversion/trunk/subversion/libsvn_subr/string.c Thu Aug 24 13:16:38 2017
@@ -1021,9 +1021,10 @@ int svn_cstring_count_newlines(const cha
 }
 
 char *
-svn_cstring_join(const apr_array_header_t *strings,
-                 const char *separator,
-                 apr_pool_t *pool)
+svn_cstring_join2(const apr_array_header_t *strings,
+                  const char *separator,
+                  svn_boolean_t trailing_separator,
+                  apr_pool_t *pool)
 {
   svn_stringbuf_t *new_str = svn_stringbuf_create_empty(pool);
   size_t sep_len = strlen(separator);
@@ -1032,9 +1033,14 @@ svn_cstring_join(const apr_array_header_
   for (i = 0; i < strings->nelts; i++)
     {
       const char *string = APR_ARRAY_IDX(strings, i, const char *);
+      if (i > 0)
+        svn_stringbuf_appendbytes(new_str, separator, sep_len);
       svn_stringbuf_appendbytes(new_str, string, strlen(string));
-      svn_stringbuf_appendbytes(new_str, separator, sep_len);
     }
+
+  if (strings->nelts > 0 && trailing_separator)
+    svn_stringbuf_appendbytes(new_str, separator, sep_len);
+
   return new_str->data;
 }
 

Modified: subversion/trunk/subversion/tests/libsvn_subr/string-test.c
URL: 
http://svn.apache.org/viewvc/subversion/trunk/subversion/tests/libsvn_subr/string-test.c?rev=1806041&r1=1806040&r2=1806041&view=diff
==============================================================================
--- subversion/trunk/subversion/tests/libsvn_subr/string-test.c (original)
+++ subversion/trunk/subversion/tests/libsvn_subr/string-test.c Thu Aug 24 
13:16:38 2017
@@ -1013,6 +1013,60 @@ test_stringbuf_set(apr_pool_t *pool)
   return SVN_NO_ERROR;
 }
 
+static svn_error_t *
+test_cstring_join(apr_pool_t *pool)
+{
+  apr_array_header_t *arr;
+
+  {
+    arr = apr_array_make(pool, 0, sizeof(const char *));
+
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, "", FALSE, pool), "");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, "", TRUE, pool), "");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, ";", FALSE, pool), "");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, ";", TRUE, pool), "");
+  }
+
+  {
+    arr = apr_array_make(pool, 0, sizeof(const char *));
+    APR_ARRAY_PUSH(arr, const char *) = "";
+
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, "", FALSE, pool), "");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, "", TRUE, pool), "");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, ";", FALSE, pool), "");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, ";", TRUE, pool), ";");
+  }
+
+  {
+    arr = apr_array_make(pool, 0, sizeof(const char *));
+    APR_ARRAY_PUSH(arr, const char *) = "ab";
+    APR_ARRAY_PUSH(arr, const char *) = "cd";
+
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, "", FALSE, pool), "abcd");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, "", TRUE, pool), "abcd");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, ";", FALSE, pool), "ab;cd");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, ";", TRUE, pool), "ab;cd;");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, "//", FALSE, pool), 
"ab//cd");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, "//", TRUE, pool), 
"ab//cd//");
+  }
+
+  {
+    arr = apr_array_make(pool, 0, sizeof(const char *));
+    APR_ARRAY_PUSH(arr, const char *) = "";
+    APR_ARRAY_PUSH(arr, const char *) = "ab";
+    APR_ARRAY_PUSH(arr, const char *) = "";
+
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, "", FALSE, pool), "ab");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, "", TRUE, pool), "ab");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, ";", FALSE, pool), ";ab;");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, ";", TRUE, pool), ";ab;;");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, "//", FALSE, pool), 
"//ab//");
+    SVN_TEST_STRING_ASSERT(svn_cstring_join2(arr, "//", TRUE, pool), 
"//ab////");
+  }
+
+  return SVN_NO_ERROR;
+}
+
 /*
    ====================================================================
    If you add a new test to this file, update this array.
@@ -1095,6 +1149,8 @@ static struct svn_test_descriptor_t test
                    "test svn_stringbuf_leftchop"),
     SVN_TEST_PASS2(test_stringbuf_set,
                    "test svn_stringbuf_set()"),
+    SVN_TEST_PASS2(test_cstring_join,
+                   "test svn_cstring_join2()"),
     SVN_TEST_NULL
   };
 

Modified: subversion/trunk/tools/dev/svnmover/svnmover.c
URL: 
http://svn.apache.org/viewvc/subversion/trunk/tools/dev/svnmover/svnmover.c?rev=1806041&r1=1806040&r2=1806041&view=diff
==============================================================================
--- subversion/trunk/tools/dev/svnmover/svnmover.c (original)
+++ subversion/trunk/tools/dev/svnmover/svnmover.c Thu Aug 24 13:16:38 2017
@@ -3921,7 +3921,8 @@ execute(svnmover_wc_t *wc,
           wc->list_of_commands
             = apr_psprintf(pool, "%s%s\n",
                            wc->list_of_commands ? wc->list_of_commands : "",
-                           svn_cstring_join(action->action_args, " ", pool));
+                           svn_cstring_join2(action->action_args, " ",
+                                             TRUE, pool));
         }
     }
   svn_pool_destroy(iterpool);


Reply via email to