[Asterisk-code-review] json.c/strings.c - Add a couple of utility functions (asterisk[13])

George Joseph asteriskteam at digium.com
Wed Feb 20 09:53:16 CST 2019


George Joseph has submitted this change and it was merged. ( https://gerrit.asterisk.org/10977 )

Change subject: json.c/strings.c - Add a couple of utility functions
......................................................................

json.c/strings.c - Add a couple of utility functions

Added 'ast_json_object_string_get' to the JSON wrapper in order to make it a
little easier to retrieve a string field from the JSON object.

Also added an 'ast_strings_equal' function that safely checks (checks for NULLs)
for equality between two strings.

Change-Id: I26f0a16d61537505eb41b4b05ef2e6d67fc2541b
---
M include/asterisk/json.h
M include/asterisk/strings.h
M main/strings.c
3 files changed, 35 insertions(+), 0 deletions(-)

Approvals:
  George Joseph: Looks good to me, but someone else must approve; Approved for Submit
  Joshua C. Colp: Looks good to me, but someone else must approve
  Matthew Fredrickson: Looks good to me, approved



diff --git a/include/asterisk/json.h b/include/asterisk/json.h
index 011359b..02b617e 100644
--- a/include/asterisk/json.h
+++ b/include/asterisk/json.h
@@ -591,6 +591,17 @@
 struct ast_json *ast_json_object_get(struct ast_json *object, const char *key);
 
 /*!
+ * \brief Get a string field from a JSON object.
+ * \since 13.26.0
+ *
+ * \param object JSON object.
+ * \param key Key of string field to look up.
+ * \return String value of given \a key.
+ * \return \c NULL on error, or key value is not a string.
+ */
+#define ast_json_object_string_get(object, key) ast_json_string_get(ast_json_object_get(object, key))
+
+/*!
  * \brief Set a field in a JSON object.
  * \since 12.0.0
  *
diff --git a/include/asterisk/strings.h b/include/asterisk/strings.h
index 85393b4..0f0e891 100644
--- a/include/asterisk/strings.h
+++ b/include/asterisk/strings.h
@@ -1355,6 +1355,21 @@
 char *ast_generate_random_string(char *buf, size_t size);
 
 /*!
+ * \brief Compare strings for equality checking for NULL.
+ * \since 13.26.0
+ *
+ * This function considers NULL values as non-strings, thus a false condition.
+ * This means that it will return false if one, or both of the given values are
+ * NULL (i.e. two NULLs are not equal strings).
+ *
+ * \param str1 The string to compare to str2
+ * \param str2 The string to compare to str1
+ *
+ * \return true if valid strings and equal, false otherwise.
+ */
+int ast_strings_equal(const char *str1, const char *str2);
+
+/*!
  * \brief Compares 2 strings using realtime-style operators
  * \since 13.9.0
  *
diff --git a/main/strings.c b/main/strings.c
index 8102c3e..cdb1984 100644
--- a/main/strings.c
+++ b/main/strings.c
@@ -247,6 +247,15 @@
 	return buf;
 }
 
+int ast_strings_equal(const char *str1, const char *str2)
+{
+	if (!str1 || !str2) {
+		return 0;
+	}
+
+	return str1 == str2 || !strcmp(str1, str2);
+}
+
 int ast_strings_match(const char *left, const char *op, const char *right)
 {
 	char *internal_op = (char *)op;

-- 
To view, visit https://gerrit.asterisk.org/10977
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-MessageType: merged
Gerrit-Change-Id: I26f0a16d61537505eb41b4b05ef2e6d67fc2541b
Gerrit-Change-Number: 10977
Gerrit-PatchSet: 4
Gerrit-Owner: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: Friendly Automation (1000185)
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua C. Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Matthew Fredrickson <creslin at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190220/59be41d7/attachment.html>


More information about the asterisk-code-review mailing list