[epiphany/wip/sync] sync-crypto: Move _find_and_replace() to ephy-string



commit 09cdb1102068768281ee1186b3c64f57e05a765d
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Mon Jun 12 18:11:52 2017 +0300

    sync-crypto: Move _find_and_replace() to ephy-string

 lib/ephy-string.c           |   37 ++++++++++++++++++++++++++++++++++
 lib/ephy-string.h           |    4 +++
 lib/sync/ephy-sync-crypto.c |   46 ++++--------------------------------------
 3 files changed, 46 insertions(+), 41 deletions(-)
---
diff --git a/lib/ephy-string.c b/lib/ephy-string.c
index de44d4e..e605d49 100644
--- a/lib/ephy-string.c
+++ b/lib/ephy-string.c
@@ -243,6 +243,43 @@ ephy_string_commandline_args_to_uris (char **arguments, GError **error)
   return args;
 }
 
+char *
+ephy_string_find_and_replace (const char *string,
+                              const char *to_find,
+                              const char *to_repl)
+{
+  const char *haystack = string;
+  const char *needle = NULL;
+  char *out;
+  gsize haystack_len;
+  gsize to_find_len;
+  gsize to_repl_len;
+  gsize new_len = 0;
+  gsize skip_len = 0;
+
+  g_return_val_if_fail (string, NULL);
+  g_return_val_if_fail (to_find, NULL);
+  g_return_val_if_fail (to_repl, NULL);
+
+  haystack_len = strlen (string);
+  to_find_len = strlen (to_find);
+  to_repl_len = strlen (to_repl);
+  out = g_malloc (haystack_len + 1);
+
+  while ((needle = g_strstr_len (haystack, -1, to_find)) != NULL) {
+    haystack_len += to_find_len - to_repl_len;
+    out = g_realloc (out, haystack_len + 1);
+    skip_len = needle - haystack;
+    memcpy (out + new_len, haystack, skip_len);
+    memcpy (out + new_len + skip_len, to_repl, to_repl_len);
+    new_len += skip_len + to_repl_len;
+    haystack = needle + to_find_len;
+  }
+  strcpy (out + new_len, haystack);
+
+  return out;
+}
+
 char **
 ephy_strv_append (const char * const *strv,
                   const char         *str)
diff --git a/lib/ephy-string.h b/lib/ephy-string.h
index af9edc5..44913f3 100644
--- a/lib/ephy-string.h
+++ b/lib/ephy-string.h
@@ -40,6 +40,10 @@ char     *ephy_string_get_host_name            (const char *url);
 
 char    **ephy_string_commandline_args_to_uris (char **arguments, GError **error);
 
+char     *ephy_string_find_and_replace         (const char *string,
+                                                const char *to_find,
+                                                const char *to_repl);
+
 char    **ephy_strv_append                     (const char * const *strv,
                                                 const char *str);
 char    **ephy_strv_remove                     (const char * const *strv,
diff --git a/lib/sync/ephy-sync-crypto.c b/lib/sync/ephy-sync-crypto.c
index e18784c..fad1084 100644
--- a/lib/sync/ephy-sync-crypto.c
+++ b/lib/sync/ephy-sync-crypto.c
@@ -21,6 +21,7 @@
 #include "config.h"
 #include "ephy-sync-crypto.h"
 
+#include "ephy-string.h"
 #include "ephy-sync-utils.h"
 
 #include <glib/gstdio.h>
@@ -128,43 +129,6 @@ ephy_sync_crypto_hawk_artifacts_free (SyncCryptoHawkArtifacts *artifacts)
 }
 
 static char *
-ephy_sync_crypto_find_and_replace (const char *where,
-                                   const char *to_find,
-                                   const char *to_repl)
-{
-  const char *haystack = where;
-  const char *needle = NULL;
-  char *out;
-  gsize haystack_len;
-  gsize to_find_len;
-  gsize to_repl_len;
-  gsize new_len = 0;
-  gsize skip_len = 0;
-
-  g_assert (where);
-  g_assert (to_find);
-  g_assert (to_repl);
-
-  haystack_len = strlen (where);
-  to_find_len = strlen (to_find);
-  to_repl_len = strlen (to_repl);
-  out = g_malloc (haystack_len + 1);
-
-  while ((needle = g_strstr_len (haystack, -1, to_find)) != NULL) {
-    haystack_len += to_find_len - to_repl_len;
-    out = g_realloc (out, haystack_len + 1);
-    skip_len = needle - haystack;
-    memcpy (out + new_len, haystack, skip_len);
-    memcpy (out + new_len + skip_len, to_repl, to_repl_len);
-    new_len += skip_len + to_repl_len;
-    haystack = needle + to_find_len;
-  }
-  strcpy (out + new_len, haystack);
-
-  return out;
-}
-
-static char *
 hawk_parse_content_type (const char *content_type)
 {
   char **tokens;
@@ -252,8 +216,8 @@ hawk_normalize_string (const char              *type,
                           NULL);
 
   if (artifacts->ext && strlen (artifacts->ext) > 0) {
-    tmp = ephy_sync_crypto_find_and_replace (artifacts->ext, "\\", "\\\\");
-    n_ext = ephy_sync_crypto_find_and_replace (tmp, "\n", "\\n");
+    tmp = ephy_string_find_and_replace (artifacts->ext, "\\", "\\\\");
+    n_ext = ephy_string_find_and_replace (tmp, "\n", "\\n");
     g_free (tmp);
   }
 
@@ -393,8 +357,8 @@ ephy_sync_crypto_hawk_header_new (const char            *url,
     char *h_ext;
     char *tmp_ext;
 
-    tmp_ext = ephy_sync_crypto_find_and_replace (artifacts->ext, "\\", "\\\\");
-    h_ext = ephy_sync_crypto_find_and_replace (tmp_ext, "\n", "\\n");
+    tmp_ext = ephy_string_find_and_replace (artifacts->ext, "\\", "\\\\");
+    h_ext = ephy_string_find_and_replace (tmp_ext, "\n", "\\n");
     header = hawk_append_to_header (header, "ext", h_ext);
 
     g_free (h_ext);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]