[Asterisk-code-review] vector: Additional string vector definitions. (asterisk[master])

Jenkins2 asteriskteam at digium.com
Tue Jan 16 09:26:16 CST 2018


Jenkins2 has submitted this change and it was merged. ( https://gerrit.asterisk.org/7871 )

Change subject: vector: Additional string vector definitions.
......................................................................

vector: Additional string vector definitions.

ast_vector_string_split:
This function will add items to an ast_vector_string by splitting values
of a string buffer.  Items are appended to the vector in the order they
are found.

ast_vector_const_string:
A vector of 'const char *'.

Change-Id: I1bf02a1efeb2baeea11c59c557d39dd1197494d7
---
M include/asterisk/vector.h
M main/strings.c
2 files changed, 73 insertions(+), 1 deletion(-)

Approvals:
  Richard Mudgett: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved
  Jenkins2: Approved for Submit



diff --git a/include/asterisk/vector.h b/include/asterisk/vector.h
index 8bd1cef..d1b2973 100644
--- a/include/asterisk/vector.h
+++ b/include/asterisk/vector.h
@@ -51,8 +51,38 @@
 /*! \brief Integer vector definition */
 AST_VECTOR(ast_vector_int, int);
 
-/*! \brief String vector definition */
+/*! \brief String vector definitions */
 AST_VECTOR(ast_vector_string, char *);
+AST_VECTOR(ast_vector_const_string, const char *);
+
+/*! Options to override default processing of ast_vector_string_split. */
+enum ast_vector_string_split_flags {
+	/*! Do not trim whitespace from values. */
+	AST_VECTOR_STRING_SPLIT_NO_TRIM = 0x01,
+	/*! Append empty strings to the vector. */
+	AST_VECTOR_STRING_SPLIT_ALLOW_EMPTY = 0x02,
+};
+
+/*!
+ * \brief Append a string vector by splitting a string.
+ *
+ * \param dest Pointer to an initialized vector.
+ * \param input String buffer to split.
+ * \param delim String delimeter passed to strsep.
+ * \param flags Processing options defined by \ref enum ast_vector_string_split_flags.
+ * \param excludes_cmp NULL or a function like strcmp to exclude duplicate strings.
+ *
+ * \retval 0 Success
+ * \retval -1 Failure
+ *
+ * \note All elements added to the vector are allocated.  The caller is always
+ *       responsible for calling ast_free on each element in the vector even
+ *       after failure.  It's possible for this function to successfully add
+ *       some elements before failing.
+ */
+int ast_vector_string_split(struct ast_vector_string *dest,
+	const char *input, const char *delim, int flags,
+	int (*excludes_cmp)(const char *s1, const char *s2));
 
 /*!
  * \brief Define a vector structure with a read/write lock
diff --git a/main/strings.c b/main/strings.c
index 82e315a..ad96df2 100644
--- a/main/strings.c
+++ b/main/strings.c
@@ -40,6 +40,7 @@
 #include <regex.h>
 #include "asterisk/strings.h"
 #include "asterisk/pbx.h"
+#include "asterisk/vector.h"
 
 /*!
  * core handler for dynamic strings.
@@ -389,3 +390,44 @@
 
 	return start;
 }
+
+int ast_vector_string_split(struct ast_vector_string *dest,
+	const char *input, const char *delim, int flags,
+	int (*excludes_cmp)(const char *s1, const char *s2))
+{
+	char *buf;
+	char *cur;
+	int no_trim = flags & AST_VECTOR_STRING_SPLIT_NO_TRIM;
+	int allow_empty = flags & AST_VECTOR_STRING_SPLIT_ALLOW_EMPTY;
+
+	ast_assert(dest != NULL);
+	ast_assert(!ast_strlen_zero(delim));
+
+	if (ast_strlen_zero(input)) {
+		return 0;
+	}
+
+	buf = ast_strdupa(input);
+	while ((cur = strsep(&buf, delim))) {
+		if (!no_trim) {
+			cur = ast_strip(cur);
+		}
+
+		if (!allow_empty && ast_strlen_zero(cur)) {
+			continue;
+		}
+
+		if (excludes_cmp && AST_VECTOR_GET_CMP(dest, cur, !excludes_cmp)) {
+			continue;
+		}
+
+		cur = ast_strdup(cur);
+		if (!cur || AST_VECTOR_APPEND(dest, cur)) {
+			ast_free(cur);
+
+			return -1;
+		}
+	}
+
+	return 0;
+}

-- 
To view, visit https://gerrit.asterisk.org/7871
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I1bf02a1efeb2baeea11c59c557d39dd1197494d7
Gerrit-Change-Number: 7871
Gerrit-PatchSet: 9
Gerrit-Owner: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Richard Mudgett <rmudgett at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180116/72eb62cf/attachment.html>


More information about the asterisk-code-review mailing list