[Common] string util to replace str
authorJaeyun <jy1210.jung@samsung.com>
Tue, 13 Aug 2019 04:23:42 +0000 (13:23 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Sat, 7 Sep 2019 13:52:52 +0000 (22:52 +0900)
Add util function to replace string.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
gst/nnstreamer/nnstreamer_plugin_api.h
gst/nnstreamer/tensor_common.c
tests/common/unittest_common.cpp

index 6489684..b978db4 100644 (file)
@@ -331,5 +331,18 @@ gst_tensor_get_type_string (tensor_type type);
 extern gint
 find_key_strv (const gchar ** strv, const gchar * key);
 
+/**
+ * @brief Replaces string.
+ * This function deallocates the input source string.
+ * @param[in] source The input string. This will be freed when returning the replaced string.
+ * @param[in] what The string to search for.
+ * @param[in] to The string to be replaced.
+ * @param[in] delimiters The characters which specify the place to split the string. Set NULL to replace all matched string.
+ * @param[out] count The count of replaced. Set NULL if it is unnecessary.
+ * @return Newly allocated string. The returned string should be freed with g_free().
+ */
+extern gchar *
+replace_string (gchar * source, const gchar * what, const gchar * to, const gchar * delimiters, guint * count);
+
 G_END_DECLS
 #endif /* __NNS_PLUGIN_API_H__ */
index e00c13a..dd5ba36 100644 (file)
@@ -1055,6 +1055,85 @@ find_key_strv (const gchar ** strv, const gchar * key)
   return -1;                    /* Not Found */
 }
 
+/**
+ * @brief Replaces string.
+ * This function deallocates the input source string.
+ * @param[in] source The input string. This will be freed when returning the replaced string.
+ * @param[in] what The string to search for.
+ * @param[in] to The string to be replaced.
+ * @param[in] delimiters The characters which specify the place to split the string. Set NULL to replace all matched string.
+ * @param[out] count The count of replaced. Set NULL if it is unnecessary.
+ * @return Newly allocated string. The returned string should be freed with g_free().
+ */
+gchar *
+replace_string (gchar * source, const gchar * what, const gchar * to,
+    const gchar * delimiters, guint * count)
+{
+  GString *builder;
+  gchar *start, *pos, *result;
+  guint changed = 0;
+  gsize len;
+
+  g_return_val_if_fail (source, NULL);
+  g_return_val_if_fail (what && to, source);
+
+  len = strlen (what);
+  start = source;
+
+  builder = g_string_new (NULL);
+  while ((pos = g_strstr_len (start, -1, what)) != NULL) {
+    gboolean skip = FALSE;
+
+    if (delimiters) {
+      const gchar *s;
+      gchar *prev, *next;
+      gboolean prev_split, next_split;
+
+      prev = next = NULL;
+      prev_split = next_split = FALSE;
+
+      if (pos != source)
+        prev = pos - 1;
+      if (*(pos + len) != '\0')
+        next = pos + len;
+
+      for (s = delimiters; *s != '\0'; ++s) {
+        if (!prev || *s == *prev)
+          prev_split = TRUE;
+        if (!next || *s == *next)
+          next_split = TRUE;
+        if (prev_split && next_split)
+          break;
+      }
+
+      if (!prev_split || !next_split)
+        skip = TRUE;
+    }
+
+    builder = g_string_append_len (builder, start, pos - start);
+
+    /* replace string if found */
+    if (skip)
+      builder = g_string_append_len (builder, pos, len);
+    else
+      builder = g_string_append (builder, to);
+
+    start = pos + len;
+    if (!skip)
+      changed++;
+  }
+
+  /* append remains */
+  builder = g_string_append (builder, start);
+  result = g_string_free (builder, FALSE);
+
+  if (count)
+    *count = changed;
+
+  g_free (source);
+  return result;
+}
+
 static const gchar *gst_tensor_time_sync_mode_string[] = {
   [SYNC_NOSYNC] = "nosync",
   [SYNC_SLOWEST] = "slowest",
index 9d8330e..5d00638 100644 (file)
@@ -531,6 +531,64 @@ TEST (common_tensors_info_string, names)
 }
 
 /**
+ * @brief Test to replace string.
+ */
+TEST (common_string_util, replace_str_01)
+{
+  gchar *result;
+  guint changed;
+
+  result = g_strdup ("sourceelement ! parser ! converter ! format ! converter ! format ! converter ! sink");
+
+  result = replace_string (result, "sourceelement", "src", NULL, &changed);
+  EXPECT_EQ (changed, 1U);
+  EXPECT_TRUE (g_str_equal (result, "src ! parser ! converter ! format ! converter ! format ! converter ! sink"));
+
+  result = replace_string (result, "format", "fmt", NULL, &changed);
+  EXPECT_EQ (changed, 2U);
+  EXPECT_TRUE (g_str_equal (result, "src ! parser ! converter ! fmt ! converter ! fmt ! converter ! sink"));
+
+  result = replace_string (result, "converter", "conv", NULL, &changed);
+  EXPECT_EQ (changed, 3U);
+  EXPECT_TRUE (g_str_equal (result, "src ! parser ! conv ! fmt ! conv ! fmt ! conv ! sink"));
+
+  result = replace_string (result, "invalidname", "invalid", NULL, &changed);
+  EXPECT_EQ (changed, 0U);
+  EXPECT_TRUE (g_str_equal (result, "src ! parser ! conv ! fmt ! conv ! fmt ! conv ! sink"));
+
+  g_free (result);
+}
+
+/**
+ * @brief Test to replace string.
+ */
+TEST (common_string_util, replace_str_02)
+{
+  gchar *result;
+  guint changed;
+
+  result = g_strdup ("source! parser ! sources ! mysource ! source ! format !source! conv source");
+
+  result = replace_string (result, "source", "src", " !", &changed);
+  EXPECT_EQ (changed, 4U);
+  EXPECT_TRUE (g_str_equal (result, "src! parser ! sources ! mysource ! src ! format !src! conv src"));
+
+  result = replace_string (result, "src", "mysource", "! ", &changed);
+  EXPECT_EQ (changed, 4U);
+  EXPECT_TRUE (g_str_equal (result, "mysource! parser ! sources ! mysource ! mysource ! format !mysource! conv mysource"));
+
+  result = replace_string (result, "source", "src", NULL, &changed);
+  EXPECT_EQ (changed, 6U);
+  EXPECT_TRUE (g_str_equal (result, "mysrc! parser ! srcs ! mysrc ! mysrc ! format !mysrc! conv mysrc"));
+
+  result = replace_string (result, "mysrc", "src", ";", &changed);
+  EXPECT_EQ (changed, 0U);
+  EXPECT_TRUE (g_str_equal (result, "mysrc! parser ! srcs ! mysrc ! mysrc ! format !mysrc! conv mysrc"));
+
+  g_free (result);
+}
+
+/**
  * @brief Create null files
  */
 static gchar *create_null_file (const gchar *dir, const gchar *file)